@goldstack/utils-typescript-references
Version:
Utility for keeping TypeScript references in sync in a Yarn monorepo
80 lines • 3.79 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.updatePackageProjectReferences = void 0;
const utils_log_1 = require("@goldstack/utils-log");
const child_process_1 = require("child_process");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const sharedUtils_1 = require("./sharedUtils");
const updatePackageProjectReferences = ({ tsConfigNames, excludeInReferences, }) => {
const cmdRes = (0, child_process_1.execSync)('yarn workspaces list --json').toString();
const allPackages = (0, sharedUtils_1.getPackages)(cmdRes);
let isSuccess = true;
for (const packageData of allPackages) {
const packageDir = packageData.path;
if (fs_1.default.existsSync(path_1.default.resolve(packageDir, './tsconfig.json'))) {
isSuccess =
processPackage({
packageDir,
allPackages,
tsConfigNames,
excludeInReferences,
}) === 'success' && isSuccess;
}
else {
(0, utils_log_1.info)(`Skipping package ${packageDir}`);
}
}
if (!isSuccess) {
throw new Error('One or more packages failed to update');
}
};
exports.updatePackageProjectReferences = updatePackageProjectReferences;
function processPackage({ packageDir, allPackages, tsConfigNames, excludeInReferences, }) {
const packageJson = fs_1.default.readFileSync(path_1.default.resolve(packageDir, './package.json')).toString();
const packageJsonData = JSON.parse(packageJson);
const tsConfigPath = (0, sharedUtils_1.getTsConfigPath)(packageDir, tsConfigNames);
if (!tsConfigPath) {
return 'success';
}
try {
const tsConfig = fs_1.default.readFileSync(tsConfigPath).toString();
const tsConfigData = JSON.parse(tsConfig);
const oldReferences = tsConfigData.references || [];
const newReferences = (0, sharedUtils_1.makeReferences)(packageDir, [
...Object.keys(packageJsonData.dependencies || {}),
...Object.keys(packageJsonData.devDependencies || {}),
]
// all dependencies that are workspace dependencies and have a tsconfig.json
.map((dependencyData) => allPackages.find((packageData) => packageData.name === dependencyData))
// remove packages that should be excluded in references
.filter((packageData) => packageData && excludeInReferences.indexOf(packageData.name) === -1), tsConfigNames);
// Exit early if references are unchanged (using JSON for deep comparison)
if (JSON.stringify(oldReferences) === JSON.stringify(newReferences)) {
return 'success';
}
const newData = JSON.stringify({
...tsConfigData,
// Override references; or omit them if empty
references: newReferences.length ? newReferences : undefined,
}, null, 2);
// only update the config file when it has changed
if (newReferences.length) {
(0, utils_log_1.info)(`Updating project references in ${tsConfigPath} to:` +
newReferences.map((refData) => `\n ${refData.path}`).join(''));
}
else {
(0, utils_log_1.info)(`Removing project references in ${tsConfigPath}`);
}
fs_1.default.writeFileSync(tsConfigPath, newData);
return 'success';
}
catch (e) {
(0, utils_log_1.error)(`Error while processing ${tsConfigPath}\n${e}`);
return 'failure';
}
}
//# sourceMappingURL=updatePackageProjectReferences.js.map