UNPKG

@backtrace/javascript-cli

Version:
117 lines 5.65 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.printAssetInfo = exports.uniqueBy = exports.isAssetProcessed = exports.validateUrl = exports.readSourceMapFromPathOrFromSource = exports.writeSourceAndSourceMap = exports.writeAsset = exports.readSourceAndSourceMap = exports.readSourceMap = exports.readSource = exports.pathIfExists = exports.toSourceAndSourceMapPaths = exports.toAsset = void 0; const sourcemap_tools_1 = require("@backtrace/sourcemap-tools"); const fs_1 = __importDefault(require("fs")); function toAsset(file) { return { name: file, path: file }; } exports.toAsset = toAsset; function toSourceAndSourceMapPaths(tuple) { return { source: toAsset(tuple.file1.path), sourceMap: tuple.file2 ? toAsset(tuple.file2) : undefined, }; } exports.toSourceAndSourceMapPaths = toSourceAndSourceMapPaths; async function pathIfExists(file) { try { await fs_1.default.promises.stat(file); return file; } catch (err) { return undefined; } } exports.pathIfExists = pathIfExists; function readSource(asset) { return (0, sourcemap_tools_1.pipe)(asset.path, sourcemap_tools_1.readFile, sourcemap_tools_1.R.map((content) => ({ ...asset, content }))); } exports.readSource = readSource; function readSourceMap(asset) { return (0, sourcemap_tools_1.pipe)(asset.path, sourcemap_tools_1.readFile, sourcemap_tools_1.R.map((sourcemap_tools_1.parseJSON)), sourcemap_tools_1.R.map((content) => ({ ...asset, content }))); } exports.readSourceMap = readSourceMap; function readSourceAndSourceMap(sourceProcessor) { return async function _readSourceAndSourceMap({ source, sourceMap, }) { const sourceResult = await readSource(source); if (sourceResult.isErr()) { return sourceResult; } const loadedSource = sourceResult.data; const sourceMapResult = sourceMap ? await readSourceMap(sourceMap) : await (0, sourcemap_tools_1.pipe)(loadedSource, ({ content }) => sourceProcessor.getSourceMapPathFromSource(content, source.path), (result) => result ?? pathIfExists(`${source.path}.map`), (path) => (path ? (0, sourcemap_tools_1.Ok)(toAsset(path)) : (0, sourcemap_tools_1.Err)('could not find source map for source')), sourcemap_tools_1.R.map(readSourceMap)); if (sourceMapResult.isErr()) { return sourceMapResult; } return (0, sourcemap_tools_1.Ok)({ source: loadedSource, sourceMap: sourceMapResult.data }); }; } exports.readSourceAndSourceMap = readSourceAndSourceMap; function writeAsset(asset) { return (0, sourcemap_tools_1.pipe)(asset.content, (content) => (typeof content === 'object' ? JSON.stringify(content) : String(content)), (0, sourcemap_tools_1.writeFile)(asset.path), sourcemap_tools_1.R.map(() => asset)); } exports.writeAsset = writeAsset; function writeSourceAndSourceMap(asset) { return (0, sourcemap_tools_1.pipe)(asset, () => (0, sourcemap_tools_1.pipe)(asset.source.content, (0, sourcemap_tools_1.writeFile)(asset.source.path)), sourcemap_tools_1.R.map(() => (0, sourcemap_tools_1.pipe)(JSON.stringify(asset.sourceMap.content), (0, sourcemap_tools_1.writeFile)(asset.sourceMap.path))), sourcemap_tools_1.R.map(() => asset)); } exports.writeSourceAndSourceMap = writeSourceAndSourceMap; function readSourceMapFromPathOrFromSource(sourceProcessor) { return function readSourceMapFromPathOrFromSource(asset) { return (0, sourcemap_tools_1.pipe)(asset, sourcemap_tools_1.loadSourceMap, sourcemap_tools_1.R.mapErr(() => (0, sourcemap_tools_1.pipe)(asset, resolveSourceMapPath(sourceProcessor), sourcemap_tools_1.R.map(sourcemap_tools_1.loadSourceMap)))); }; } exports.readSourceMapFromPathOrFromSource = readSourceMapFromPathOrFromSource; function resolveSourceMapPath(sourceProcessor) { return function resolveSourceMapFromSource(asset) { return (0, sourcemap_tools_1.pipe)(asset.path, (path) => sourceProcessor.getSourceMapPathFromSourceFile(path), sourcemap_tools_1.R.map((path) => (path ? (0, sourcemap_tools_1.Ok)(path) : (0, sourcemap_tools_1.Err)('could not find source map for source'))), sourcemap_tools_1.R.map((path) => ({ ...asset, name: path, path, })), sourcemap_tools_1.R.mapErr((err) => `file is not a sourcemap and sourcemap search failed: ${err}`)); }; } function validateUrl(url) { try { new URL(url); return (0, sourcemap_tools_1.Ok)(url); } catch { return (0, sourcemap_tools_1.Err)(`invalid URL: ${url}`); } } exports.validateUrl = validateUrl; function isAssetProcessed(sourceProcessor) { return function isAssetProcessed(asset) { const result = sourceProcessor.isSourceMapProcessed(asset.content); return { asset, result }; }; } exports.isAssetProcessed = isAssetProcessed; function uniqueBy(fn) { return function uniqueBy(array) { const keys = new Set(); return array.filter((t) => { const key = fn(t); if (keys.has(key)) { return false; } keys.add(key); return true; }); }; } exports.uniqueBy = uniqueBy; function printAssetInfo(logger) { return function printAssetInfo(asset) { logger.debug(`${asset.source.path}`); logger.debug(`└── ${asset.sourceMap.path}`); return asset; }; } exports.printAssetInfo = printAssetInfo; //# sourceMappingURL=common.js.map