UNPKG

@rushstack/heft

Version:

Build all your JavaScript projects the same way: A way that works.

173 lines 7.28 kB
"use strict"; // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. // See LICENSE in the project root for license information. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.makePathRelative = void 0; exports.serializeBuildInfo = serializeBuildInfo; exports.deserializeBuildInfo = deserializeBuildInfo; exports.writeBuildInfoAsync = writeBuildInfoAsync; exports.tryReadBuildInfoAsync = tryReadBuildInfoAsync; const path = __importStar(require("node:path")); const node_core_library_1 = require("@rushstack/node-core-library"); /** * Converts an absolute path to a path relative to a base path. */ exports.makePathRelative = process.platform === 'win32' ? (absolutePath, basePath) => { // On Windows, need to normalize slashes return node_core_library_1.Path.convertToSlashes(path.win32.relative(basePath, absolutePath)); } : (absolutePath, basePath) => { // On POSIX, can preserve existing slashes return path.posix.relative(basePath, absolutePath); }; /** * Serializes a build info object to a portable format that can be written to disk. * @param state - The build info to serialize * @param makePathPortable - A function that converts an absolute path to a portable path. This is a separate argument to support cross-platform tests. * @returns The serialized build info * @beta */ function serializeBuildInfo(state, makePathPortable) { const fileIndices = new Map(); const inputFileVersions = {}; for (const [absolutePath, version] of state.inputFileVersions) { const relativePath = makePathPortable(absolutePath); fileIndices.set(absolutePath, fileIndices.size); inputFileVersions[relativePath] = version; } const { fileDependencies: newFileDependencies } = state; let fileDependencies; if (newFileDependencies) { fileDependencies = {}; for (const [absolutePath, dependencies] of newFileDependencies) { const relativePath = makePathPortable(absolutePath); const indices = []; for (const dependency of dependencies) { const index = fileIndices.get(dependency); if (index === undefined) { throw new Error(`Dependency not found: ${dependency}`); } indices.push(index); } fileDependencies[relativePath] = indices; } } const serializedBuildInfo = { configHash: state.configHash, inputFileVersions, fileDependencies }; return serializedBuildInfo; } /** * Deserializes a build info object from its portable format. * @param serializedBuildInfo - The build info to deserialize * @param makePathAbsolute - A function that converts a portable path to an absolute path. This is a separate argument to support cross-platform tests. * @returns The deserialized build info */ function deserializeBuildInfo(serializedBuildInfo, makePathAbsolute) { const inputFileVersions = new Map(); const absolutePathByIndex = []; for (const [relativePath, version] of Object.entries(serializedBuildInfo.inputFileVersions)) { const absolutePath = makePathAbsolute(relativePath); absolutePathByIndex.push(absolutePath); inputFileVersions.set(absolutePath, version); } let fileDependencies; const { fileDependencies: serializedFileDependencies } = serializedBuildInfo; if (serializedFileDependencies) { fileDependencies = new Map(); for (const [relativeOutputFile, indices] of Object.entries(serializedFileDependencies)) { const absoluteOutputFile = makePathAbsolute(relativeOutputFile); const dependencies = []; for (const index of Array.isArray(indices) ? indices : [indices]) { const dependencyAbsolutePath = absolutePathByIndex[index]; if (dependencyAbsolutePath === undefined) { throw new Error(`Dependency index not found: ${index}`); } dependencies.push(dependencyAbsolutePath); } fileDependencies.set(absoluteOutputFile, dependencies); } } const buildInfo = { configHash: serializedBuildInfo.configHash, inputFileVersions, fileDependencies }; return buildInfo; } /** * Writes a build info object to disk. * @param state - The build info to write * @param filePath - The file path to write the build info to * @beta */ async function writeBuildInfoAsync(state, filePath) { const basePath = path.dirname(filePath); const serializedBuildInfo = serializeBuildInfo(state, (absolutePath) => { return (0, exports.makePathRelative)(absolutePath, basePath); }); // This file is meant only for machine reading, so don't pretty-print it. const stringified = JSON.stringify(serializedBuildInfo); await node_core_library_1.FileSystem.writeFileAsync(filePath, stringified, { ensureFolderExists: true }); } /** * Reads a build info object from disk. * @param filePath - The file path to read the build info from * @returns The build info object, or undefined if the file does not exist or cannot be parsed * @beta */ async function tryReadBuildInfoAsync(filePath) { let serializedBuildInfo; try { const fileContents = await node_core_library_1.FileSystem.readFileAsync(filePath); serializedBuildInfo = JSON.parse(fileContents); } catch (error) { if (node_core_library_1.FileSystem.isNotExistError(error)) { return; } throw error; } const basePath = path.dirname(filePath); const buildInfo = deserializeBuildInfo(serializedBuildInfo, (relativePath) => { return path.resolve(basePath, relativePath); }); return buildInfo; } //# sourceMappingURL=IncrementalBuildInfo.js.map