2020-10-20 23:52:29 +03:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2021-07-23 21:12:20 +03:00
|
|
|
const glob = require('fast-glob')
|
2020-10-20 23:52:29 +03:00
|
|
|
const { satisfies } = require('semver')
|
|
|
|
const { spawn } = require('@malept/cross-spawn-promise')
|
|
|
|
|
|
|
|
const DO_NOT_UPGRADE = [
|
|
|
|
'@typescript-eslint/eslint-plugin', // special case
|
2020-11-09 07:16:39 +03:00
|
|
|
'commander', // TODO: convert to yargs
|
2021-07-23 20:03:35 +03:00
|
|
|
'log-symbols', // Requires ESM
|
2021-05-23 00:31:22 +03:00
|
|
|
'typescript' // Promisify issues, see https://github.com/DefinitelyTyped/DefinitelyTyped/pull/49699
|
2020-10-20 23:52:29 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Spawn, but pass through stdio by default.
|
|
|
|
*/
|
|
|
|
async function spawnPassthrough(cmd, args, options) {
|
|
|
|
await spawn(cmd, args, { stdio: 'inherit', ...options })
|
|
|
|
}
|
|
|
|
|
|
|
|
async function bolt(...args) {
|
|
|
|
await spawnPassthrough('bolt', args)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function git(...args) {
|
|
|
|
await spawnPassthrough('git', args)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function yarn(...args) {
|
|
|
|
await spawnPassthrough('yarn', args)
|
|
|
|
}
|
|
|
|
|
2021-07-11 19:58:31 +03:00
|
|
|
const packageJSON = require(__dirname + '/../package.json')
|
2020-10-20 23:52:29 +03:00
|
|
|
|
|
|
|
class Package {
|
|
|
|
constructor(name, currentVersion, wantedVersion, latestVersion, type) {
|
|
|
|
this.name = name
|
|
|
|
this.currentVersion = currentVersion
|
|
|
|
this.wantedVersion = wantedVersion
|
|
|
|
this.latestVersion = latestVersion
|
|
|
|
this.type = type
|
|
|
|
}
|
|
|
|
|
|
|
|
get commitType() {
|
|
|
|
switch (this.type) {
|
|
|
|
case 'dependencies':
|
|
|
|
case 'optionalDependencies':
|
|
|
|
return 'deps'
|
|
|
|
case 'devDependencies':
|
|
|
|
return 'deps-dev'
|
|
|
|
default:
|
|
|
|
return 'deps-unknown'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get commitVersion() {
|
|
|
|
if (this.isMajorVersionBump()) {
|
|
|
|
return `^${this.latestVersion}`
|
2021-07-11 19:58:31 +03:00
|
|
|
} else if (this.isMinorVersionBump()) {
|
|
|
|
return `~${this.latestVersion}`
|
2020-10-20 23:52:29 +03:00
|
|
|
} else {
|
|
|
|
return this.latestVersion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-11 19:58:31 +03:00
|
|
|
get minorVersionLocked() {
|
|
|
|
return packageJSON[this.type][this.name].startsWith('~')
|
|
|
|
}
|
|
|
|
|
2020-10-20 23:52:29 +03:00
|
|
|
isMajorVersionBump() {
|
|
|
|
return !satisfies(this.latestVersion, `^${this.wantedVersion}`)
|
|
|
|
}
|
|
|
|
|
2021-07-11 19:58:31 +03:00
|
|
|
isMinorVersionBump() {
|
|
|
|
return this.minorVersionLocked && !satisfies(this.latestVersion, `~${this.wantedVersion}`)
|
|
|
|
}
|
|
|
|
|
2020-10-20 23:52:29 +03:00
|
|
|
async smoketestAndCommit(packageName = null) {
|
2021-07-23 21:12:20 +03:00
|
|
|
const packageJSONs = await glob('packages/*/*/package.json')
|
2020-10-20 23:52:29 +03:00
|
|
|
await yarn('lint')
|
|
|
|
await bolt('build')
|
|
|
|
await git('add', 'package.json', 'yarn.lock', ...packageJSONs)
|
|
|
|
await git('commit', '-m', `build(${this.commitType}): upgrade ${packageName || this.name} to ${this.commitVersion}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
async upgrade() {
|
2021-07-11 19:58:31 +03:00
|
|
|
if (this.isMajorVersionBump() || this.isMinorVersionBump()) {
|
2020-10-20 23:52:29 +03:00
|
|
|
await this.bolt_upgrade()
|
|
|
|
} else {
|
|
|
|
await this.yarn_upgrade()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async bolt_upgrade() {
|
|
|
|
console.log(`Upgrading ${this.name} from ${this.wantedVersion} to ^${this.latestVersion} (and updating package.json)...`)
|
|
|
|
await bolt('upgrade', `${this.name}@^${this.latestVersion}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
async yarn_upgrade() {
|
|
|
|
console.log(`Upgrading ${this.name} from ${this.currentVersion} to ${this.latestVersion} in yarn.lock...`)
|
|
|
|
await yarn('upgrade', this.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
try {
|
|
|
|
await spawn('yarn', ['outdated', '--json'])
|
|
|
|
console.log('No packages to update.')
|
|
|
|
} catch (error) {
|
|
|
|
const table = JSON.parse(error.stdout.split('\n')[1])
|
|
|
|
for (const [packageName, currentVersion, wantedVersion, latestVersion, packageType, _url] of table.data.body) {
|
|
|
|
if (DO_NOT_UPGRADE.includes(packageName)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
let commitPackageName = null
|
|
|
|
const package = new Package(packageName, currentVersion, wantedVersion, latestVersion, packageType)
|
|
|
|
await package.upgrade()
|
|
|
|
|
|
|
|
if (packageName === '@typescript-eslint/parser') {
|
|
|
|
const eslintPlugin = new Package('@typescript-eslint/eslint-plugin', currentVersion, wantedVersion, latestVersion, packageType)
|
|
|
|
await eslintPlugin.upgrade()
|
|
|
|
commitPackageName = '@typescript-eslint/{parser,eslint-plugin}'
|
|
|
|
}
|
|
|
|
|
|
|
|
await package.smoketestAndCommit(commitPackageName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main().catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|