UNPKG

@goldstack/utils-typescript-references

Version:

Utility for keeping TypeScript references in sync in a Yarn monorepo

47 lines 2.21 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateRootProjectReferences = void 0; const child_process_1 = require("child_process"); const fs_1 = __importDefault(require("fs")); const sharedUtils_1 = require("./sharedUtils"); const updateRootProjectReferences = ({ tsConfigNames, excludeProjects, }) => { const cmdRes = (0, child_process_1.execSync)('yarn workspaces list --json').toString(); const allPackages = (0, sharedUtils_1.getPackages)(cmdRes); const tsConfigPath = (0, sharedUtils_1.getTsConfigPath)('.', tsConfigNames); if (!tsConfigPath) { console.error('No root-level tsconfig.json found'); return; } try { const tsConfig = fs_1.default.readFileSync(tsConfigPath).toString(); const tsConfigData = JSON.parse(tsConfig); const oldReferences = tsConfigData.references; const newReferences = (0, sharedUtils_1.makeReferences)('.', allPackages.filter((packageData) => excludeProjects.indexOf(packageData.name) === -1), tsConfigNames); // Don't continue if references are unchanged if (JSON.stringify(newReferences) === JSON.stringify(oldReferences)) { return; } const newContent = JSON.stringify({ ...tsConfigData, // Override references; or omit them if empty references: newReferences.length ? newReferences : undefined, }, null, 2); if (newReferences.length) { console.log(`Updating project references in ${tsConfigPath} to:` + newReferences.map((refData) => `\n ${refData.path}`).join('')); } else { console.log(`Removing project references in ${tsConfigPath}`); } fs_1.default.writeFileSync(tsConfigPath, newContent); } catch (e) { console.error(`Error while processing top level config file ${tsConfigPath} \n${e}`); throw e; } }; exports.updateRootProjectReferences = updateRootProjectReferences; //# sourceMappingURL=updateRootProjectReferences.js.map