UNPKG

@goldstack/utils-typescript-references

Version:

Utility for keeping TypeScript references in sync in a Yarn monorepo

74 lines 2.59 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPackages = getPackages; exports.getTsConfigPath = getTsConfigPath; exports.makeReferences = makeReferences; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); function getPackages(cmdRes) { return cmdRes .split('\n') .map((line) => { if (line.trim() === '') { return { path: undefined, name: undefined, }; } const packageData = JSON.parse(line); if (packageData.location === '.') { return { path: undefined, name: undefined, }; } // ignore packages without TypeScript configuration if (!fs_1.default.existsSync(`${packageData.location}/tsconfig.json`)) { return { path: undefined, name: undefined, }; } return { path: packageData.location, name: packageData.name, }; }) .filter((packageData) => packageData.path && packageData.name); } /** * Search a folder for the preferred tsconfig.json file based * on the options in tsConfigNames. * * @param workspacePath Directory to search in * @param tsConfigNames List of tsconfig.json files to search for in order * of preference */ function getTsConfigPath(workspacePath, tsConfigNames) { return tsConfigNames .map((tsConfigName) => path_1.default.posix.join(workspacePath, tsConfigName)) .find((tsConfigPath) => fs_1.default.existsSync(tsConfigPath)); } /** * Calculate the references array for the package * * @param packagePath Path to the package we are updating references for * @param packages Packages the package wants to reference * @param tsConfigNames Configured tsconfig file names */ function makeReferences(packagePath, packages, tsConfigNames) { return (packages .filter((p) => !!p) .map((dependencyData) => getTsConfigPath(dependencyData.path, tsConfigNames)) .filter((p) => !!p) // for each add a path .map((tsConfigPath) => ({ path: path_1.default.posix.relative(packagePath, path_1.default.basename(tsConfigPath) === 'tsconfig.json' ? path_1.default.dirname(tsConfigPath) : tsConfigPath), }))); } //# sourceMappingURL=sharedUtils.js.map