@o3r/schematics
Version:
Schematics module of the Otter framework
74 lines • 3.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.updatePackageGroup = updatePackageGroup;
const tslib_1 = require("tslib");
const node_child_process_1 = require("node:child_process");
const node_fs_1 = require("node:fs");
const minimist_1 = tslib_1.__importDefault(require("minimist"));
const semver = tslib_1.__importStar(require("semver"));
const loaders_1 = require("./loaders");
const package_manager_runner_1 = require("./package-manager-runner");
function getPackageGroup(packageJsonPath) {
// eslint-disable-next-line @typescript-eslint/naming-convention -- Naming from angular
const o3rPackageJson = JSON.parse((0, node_fs_1.readFileSync)(packageJsonPath, { encoding: 'utf8' }));
return o3rPackageJson['ng-update']?.packageGroup ?? [];
}
function getAllDependenciesFromPackageGroup(tree, packageJsonPath) {
const allDeps = (0, loaders_1.getAllDependencies)(tree);
const packageGroup = getPackageGroup(packageJsonPath);
allDeps.forEach((dep) => {
if (!packageGroup.includes(dep)) {
allDeps.delete(dep);
}
});
return allDeps;
}
function hasMigrationScript(dependency) {
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports -- keep it synchronous
const packageJson = require(`${dependency}/package.json`);
return !!packageJson['ng-update'];
}
catch {
return false;
}
}
function triggerPackageGroupUpdates(tree, packageJsonPath, ngUpdateOptions) {
const dependencies = getAllDependenciesFromPackageGroup(tree, packageJsonPath);
const executor = (0, package_manager_runner_1.getPackageManagerExecutor)();
for (const dependency of dependencies) {
if (hasMigrationScript(dependency)) {
// eslint-disable-next-line no-console -- Logging some stuff
console.log(`Looking for updates in ${dependency}`);
const options = Object.entries(ngUpdateOptions)
.filter(([key]) => ['migrate-only', 'from', 'to', 'next', 'force'].includes(key))
.map(([key, value]) => `--${key}=${value}`).join(' ');
const cmd = `${executor} ng update ${dependency} ${options}`;
try {
(0, node_child_process_1.execSync)(cmd, { stdio: 'inherit', env: { ...process.env, O3R_UPDATE_ONGOING: 'true' } });
}
catch (err) {
// eslint-disable-next-line no-console -- Need to show the error without throwing
console.error(`Error while executing: ${cmd}`, err);
}
}
}
}
/**
* Return a rule that will trigger the ng-update for all packages in packageGroup if present.
* @param packageJsonPath The path to the package.json file where the packageGroup is defined
* @param validRange Only execute if the target version (--to) satisfies this range
*/
function updatePackageGroup(packageJsonPath, validRange) {
return (tree) => {
const args = process.argv.slice(2);
// eslint-disable-next-line id-denylist -- Naming from minimist
const ngUpdateOptions = (0, minimist_1.default)(args, { string: ['from', 'to'] });
const isValidTarget = !ngUpdateOptions.to || !validRange
|| semver.subset(ngUpdateOptions.to, validRange, { includePrerelease: true });
if (ngUpdateOptions['migrate-only'] && ngUpdateOptions.from && isValidTarget && !process.env.O3R_UPDATE_ONGOING) {
triggerPackageGroupUpdates(tree, packageJsonPath, ngUpdateOptions);
}
};
}
//# sourceMappingURL=ng-update.js.map