2020-10-20 23:52:29 +03:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2022-10-26 20:37:16 +03:00
|
|
|
const { spawn } = require('@malept/cross-spawn-promise');
|
2021-09-12 20:44:30 +03:00
|
|
|
const glob = require('fast-glob');
|
|
|
|
const { satisfies } = require('semver');
|
2020-10-20 23:52:29 +03:00
|
|
|
|
|
|
|
const DO_NOT_UPGRADE = [
|
2021-09-08 17:36:07 +03:00
|
|
|
'@types/node-fetch', // No longer needed when node-fetch is upgraded to >= 3.0.0
|
2020-10-20 23:52:29 +03:00
|
|
|
'@typescript-eslint/eslint-plugin', // special case
|
2022-01-09 20:44:42 +03:00
|
|
|
'chalk', // Requires ESM
|
2020-11-09 07:16:39 +03:00
|
|
|
'commander', // TODO: convert to yargs
|
2022-01-09 21:42:29 +03:00
|
|
|
'eslint-plugin-mocha', // Requires Node 14
|
2021-08-25 18:40:31 +03:00
|
|
|
'find-up', // Requires ESM
|
2021-07-23 20:03:35 +03:00
|
|
|
'log-symbols', // Requires ESM
|
2021-09-08 17:36:07 +03:00
|
|
|
'node-fetch', // Requires ESM
|
2021-08-25 18:37:12 +03:00
|
|
|
'ora', // Requires ESM
|
2021-09-12 20:44:30 +03:00
|
|
|
'username', // Requires ESM
|
|
|
|
];
|
2020-10-20 23:52:29 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Spawn, but pass through stdio by default.
|
|
|
|
*/
|
|
|
|
async function spawnPassthrough(cmd, args, options) {
|
2021-09-12 20:44:30 +03:00
|
|
|
await spawn(cmd, args, { stdio: 'inherit', ...options });
|
2020-10-20 23:52:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async function git(...args) {
|
2021-09-12 20:44:30 +03:00
|
|
|
await spawnPassthrough('git', args);
|
2020-10-20 23:52:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async function yarn(...args) {
|
2021-09-12 20:44:30 +03:00
|
|
|
await spawnPassthrough('yarn', args);
|
2020-10-20 23:52:29 +03:00
|
|
|
}
|
|
|
|
|
2021-09-12 20:44:30 +03:00
|
|
|
const packageJSON = require(__dirname + '/../package.json');
|
2020-10-20 23:52:29 +03:00
|
|
|
|
|
|
|
class Package {
|
|
|
|
constructor(name, currentVersion, wantedVersion, latestVersion, type) {
|
2021-09-12 20:44:30 +03:00
|
|
|
this.name = name;
|
|
|
|
this.currentVersion = currentVersion;
|
|
|
|
this.wantedVersion = wantedVersion;
|
|
|
|
this.latestVersion = latestVersion;
|
|
|
|
this.type = type;
|
2020-10-20 23:52:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get commitType() {
|
|
|
|
switch (this.type) {
|
|
|
|
case 'dependencies':
|
|
|
|
case 'optionalDependencies':
|
2021-09-12 20:44:30 +03:00
|
|
|
return 'deps';
|
2020-10-20 23:52:29 +03:00
|
|
|
case 'devDependencies':
|
2021-09-12 20:44:30 +03:00
|
|
|
return 'deps-dev';
|
2020-10-20 23:52:29 +03:00
|
|
|
default:
|
2021-09-12 20:44:30 +03:00
|
|
|
return 'deps-unknown';
|
2020-10-20 23:52:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get commitVersion() {
|
|
|
|
if (this.isMajorVersionBump()) {
|
2021-09-12 20:44:30 +03:00
|
|
|
return `^${this.latestVersion}`;
|
2021-07-11 19:58:31 +03:00
|
|
|
} else if (this.isMinorVersionBump()) {
|
2021-09-12 20:44:30 +03:00
|
|
|
return `~${this.latestVersion}`;
|
2020-10-20 23:52:29 +03:00
|
|
|
} else {
|
2021-09-12 20:44:30 +03:00
|
|
|
return this.latestVersion;
|
2020-10-20 23:52:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-11 19:58:31 +03:00
|
|
|
get minorVersionLocked() {
|
2021-09-12 20:44:30 +03:00
|
|
|
return packageJSON[this.type][this.name].startsWith('~');
|
2021-07-11 19:58:31 +03:00
|
|
|
}
|
|
|
|
|
2020-10-20 23:52:29 +03:00
|
|
|
isMajorVersionBump() {
|
2021-09-12 20:44:30 +03:00
|
|
|
return !satisfies(this.latestVersion, `^${this.wantedVersion}`);
|
2020-10-20 23:52:29 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 19:58:31 +03:00
|
|
|
isMinorVersionBump() {
|
2021-09-12 20:44:30 +03:00
|
|
|
return this.minorVersionLocked && !satisfies(this.latestVersion, `~${this.wantedVersion}`);
|
2021-07-11 19:58:31 +03:00
|
|
|
}
|
|
|
|
|
2020-10-20 23:52:29 +03:00
|
|
|
async smoketestAndCommit(packageName = null) {
|
2021-09-12 20:44:30 +03:00
|
|
|
const packageJSONs = await glob('packages/*/*/package.json');
|
|
|
|
await yarn('lint');
|
2022-11-14 22:12:29 +03:00
|
|
|
await yarn('build');
|
2021-09-12 20:44:30 +03:00
|
|
|
await git('add', 'package.json', 'yarn.lock', ...packageJSONs);
|
|
|
|
await git('commit', '-m', `build(${this.commitType}): upgrade ${packageName || this.name} to ${this.commitVersion}`);
|
2020-10-20 23:52:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async upgrade() {
|
2021-07-11 19:58:31 +03:00
|
|
|
if (this.isMajorVersionBump() || this.isMinorVersionBump()) {
|
2022-11-14 22:12:29 +03:00
|
|
|
await this.yarn_upgrade_and_update_packageJSON();
|
2020-10-20 23:52:29 +03:00
|
|
|
} else {
|
2022-11-14 22:12:29 +03:00
|
|
|
await this.yarn_upgrade_in_yarn_lock();
|
2020-10-20 23:52:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 22:12:29 +03:00
|
|
|
async yarn_upgrade_and_update_packageJSON() {
|
2021-09-12 20:44:30 +03:00
|
|
|
console.log(`Upgrading ${this.name} from ${this.wantedVersion} to ^${this.latestVersion} (and updating package.json)...`);
|
2022-11-14 22:12:29 +03:00
|
|
|
await yarn('upgrade', `${this.name}@^${this.latestVersion}`);
|
2020-10-20 23:52:29 +03:00
|
|
|
}
|
|
|
|
|
2022-11-14 22:12:29 +03:00
|
|
|
async yarn_upgrade_in_yarn_lock() {
|
2021-09-12 20:44:30 +03:00
|
|
|
console.log(`Upgrading ${this.name} from ${this.currentVersion} to ${this.latestVersion} in yarn.lock...`);
|
|
|
|
await yarn('upgrade', this.name);
|
2020-10-20 23:52:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
2021-10-18 09:47:29 +03:00
|
|
|
const onlyModules = [];
|
|
|
|
if (process.argv.length > 2) {
|
|
|
|
onlyModules.push(...process.argv.slice(2));
|
|
|
|
}
|
2020-10-20 23:52:29 +03:00
|
|
|
try {
|
2021-09-12 20:44:30 +03:00
|
|
|
await spawn('yarn', ['outdated', '--json']);
|
|
|
|
console.log('No packages to update.');
|
2020-10-20 23:52:29 +03:00
|
|
|
} catch (error) {
|
2021-09-12 20:44:30 +03:00
|
|
|
const table = JSON.parse(error.stdout.split('\n')[1]);
|
|
|
|
for (const [packageName, currentVersion, wantedVersion, latestVersion, packageType /*, _url */] of table.data.body) {
|
2020-10-20 23:52:29 +03:00
|
|
|
if (DO_NOT_UPGRADE.includes(packageName)) {
|
2021-10-18 09:47:29 +03:00
|
|
|
console.log(`Skipping "${packageName} from update as it is in the denylist`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (onlyModules.length > 0 && !onlyModules.includes(packageName)) {
|
|
|
|
console.log(`Skipping "${packageName}" from update as it was not specified on the command line`);
|
2021-09-12 20:44:30 +03:00
|
|
|
continue;
|
2020-10-20 23:52:29 +03:00
|
|
|
}
|
2021-09-12 20:44:30 +03:00
|
|
|
let commitPackageName = null;
|
|
|
|
const nodePackage = new Package(packageName, currentVersion, wantedVersion, latestVersion, packageType);
|
|
|
|
await nodePackage.upgrade();
|
2020-10-20 23:52:29 +03:00
|
|
|
|
|
|
|
if (packageName === '@typescript-eslint/parser') {
|
2021-09-12 20:44:30 +03:00
|
|
|
const eslintPlugin = new Package('@typescript-eslint/eslint-plugin', currentVersion, wantedVersion, latestVersion, packageType);
|
|
|
|
await eslintPlugin.upgrade();
|
|
|
|
commitPackageName = '@typescript-eslint/{parser,eslint-plugin}';
|
2020-10-20 23:52:29 +03:00
|
|
|
}
|
|
|
|
|
2021-09-12 20:44:30 +03:00
|
|
|
await nodePackage.smoketestAndCommit(commitPackageName);
|
2020-10-20 23:52:29 +03:00
|
|
|
}
|
|
|
|
}
|
2021-08-05 06:46:17 +03:00
|
|
|
|
2021-10-18 09:47:29 +03:00
|
|
|
if (onlyModules.length == 0) {
|
|
|
|
console.log(`Upgrading transitive dependencies in yarn.lock...`);
|
|
|
|
await yarn('upgrade');
|
|
|
|
await git('add', 'yarn.lock');
|
|
|
|
await git('commit', '-m', `build(deps): upgrade transitive dependencies`);
|
|
|
|
}
|
2020-10-20 23:52:29 +03:00
|
|
|
}
|
|
|
|
|
2021-09-12 20:44:30 +03:00
|
|
|
main().catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
});
|