@adinsure-ops/ops-cli
Version:
Operations CLI for working with AdInsure
126 lines (125 loc) • 6.01 kB
JavaScript
import { __awaiter } from "tslib";
import { Args, Flags, ux } from '@oclif/core';
import chalk from 'chalk';
import fs from 'fs-extra';
import { minimatch } from 'minimatch';
import semver from 'semver';
import CommandBase from '../../command.base.js';
import ChildProcessCommand from '../../lib/command.js';
import NpmContext from '../../lib/npm.js';
class NpmDependencyUpdate extends CommandBase {
/**
* Update dependencies in package.json files for @adinsure packages
* @param {string} newVersion New version to update to
* @param {NpmPackage[]} packages Packages to update
* @param {boolean} [force=false] Force the update
* @param {string[]} filter Regular expressions filter for packages
* @returns {Prmoise<void>} Void Promise
*/
updateDependencies(newVersion_1, packages_1) {
return __awaiter(this, arguments, void 0, function* (newVersion, packages, force = false, filter = ['**/*']) {
// Find all package.json files that are not in node modules
for (const path of packages) {
try {
const data = yield fs.readJSON(path.jsonPath);
const updatedKeys = [];
if (Object.prototype.hasOwnProperty.call(data, 'dependencies')) {
for (const [key, value] of Object.entries(data.dependencies)) {
const currentVersion = semver.valid(String(value));
if (filter.some(pkg => minimatch(key, pkg))) {
if (currentVersion && semver.gt(currentVersion, newVersion) && !force) {
this.warn(`Lower of same version for dependency ${key} updated in file ${path.jsonPath}: ${newVersion} vs. ${currentVersion} is ${semver.gt(newVersion, currentVersion)}`);
}
else {
data.dependencies[key] = newVersion;
updatedKeys.push(key);
}
}
}
fs.writeFileSync(path.jsonPath, JSON.stringify(data, null, 4));
if (updatedKeys.length > 0) {
this.log(`Dependencies ${updatedKeys} updated in file ${path.jsonPath}`);
}
}
if (Object.prototype.hasOwnProperty.call(data, 'devDependencies')) {
for (const [key, value] of Object.entries(data.devDependencies)) {
const currentVersion = semver.valid(String(value));
if (filter.some(pkg => minimatch(key, pkg))) {
if (currentVersion && semver.gt(currentVersion, newVersion) && !force) {
this.warn(`Lower of same version for devDependency ${key} updated in file ${path.jsonPath}: ${newVersion} vs. ${currentVersion} is ${semver.gt(newVersion, currentVersion)}`);
}
else {
data.devDependencies[key] = newVersion;
updatedKeys.push(key);
}
}
}
fs.writeFileSync(path.jsonPath, JSON.stringify(data, null, 4));
if (updatedKeys.length > 0) {
this.log(`devDependencies ${updatedKeys} updated in file ${path.jsonPath}`);
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}
catch (error) {
this.warn(error.message);
break;
}
}
});
}
run() {
const _super = Object.create(null, {
run: { get: () => super.run }
});
return __awaiter(this, void 0, void 0, function* () {
_super.run.call(this);
const { args, flags } = yield this.parse(NpmDependencyUpdate);
const newVersion = semver.valid(args.version);
const npmContext = new NpmContext();
const command = new ChildProcessCommand();
// Stop if version invalid
if (!newVersion) {
return this.error(`${chalk.red('Version in incorrect format')}`);
}
const packages = yield npmContext.packageList(this);
try {
yield this.updateDependencies(newVersion, packages, flags.force, flags.filter);
ux.action.start(`running 'yarn install'`, undefined, { stdout: true });
yield command.runCommand(['yarn', 'install']);
ux.action.stop(`${chalk.green('done')}`);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}
catch (error) {
this.error(error.message);
}
});
}
}
NpmDependencyUpdate.description = 'update npm dependencies per package filter';
NpmDependencyUpdate.usage = 'npm:dependency-update <type> [flags]';
NpmDependencyUpdate.examples = [
`$ ops npm:dependency-update 3.4.3 --filter @adinsure/*`,
];
NpmDependencyUpdate.args = {
version: Args.string({
required: true,
description: 'Type of release',
}),
};
NpmDependencyUpdate.flags = {
filter: Flags.string({
description: 'regular expression for which NPM packages to update',
required: true,
multiple: true,
}),
exclude: Flags.string({
description: 'exclude directories where to search for package.json',
multiple: true,
}),
force: Flags.boolean({
description: 'force update of version regardless of lower vs higher version check',
default: false,
}),
};
export default NpmDependencyUpdate;