Use npm show <pkg> version instead of outdated
npm outdated would error out for defined local packages such as @rondo.dev/*
This commit is contained in:
parent
a39833eece
commit
11a29985eb
@ -15,74 +15,56 @@ export interface Package {
|
|||||||
devDependencies?: Record<string, string>
|
devDependencies?: Record<string, string>
|
||||||
}
|
}
|
||||||
|
|
||||||
function findOutdated(cwd: string): Record<string, Outdated> {
|
function readPackage(dir: string): Package {
|
||||||
try {
|
const pkgFile = path.join(dir, 'package.json')
|
||||||
const result = cp.execFileSync('npm', ['outdated', '--json'], {
|
return JSON.parse(fs.readFileSync(pkgFile, 'utf8'))
|
||||||
cwd,
|
|
||||||
encoding: 'utf8',
|
|
||||||
})
|
|
||||||
return result === '' ? {} : JSON.parse(result)
|
|
||||||
} catch (err) {
|
|
||||||
// npm outdated will exit with code 1 if there are outdated dependencies
|
|
||||||
return JSON.parse(err.stdout)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateDependency(
|
function writePackage(dir: string, contents: Package) {
|
||||||
pkg: Package,
|
const pkgFile = path.join(dir, 'package.json')
|
||||||
key: 'dependencies' | 'devDependencies',
|
fs.writeFileSync(pkgFile, JSON.stringify(contents, null, ' '))
|
||||||
name: string,
|
}
|
||||||
|
|
||||||
|
function findLatestVersions(
|
||||||
|
deps: Record<string, string>,
|
||||||
prefix: string,
|
prefix: string,
|
||||||
version: Outdated,
|
): Record<string, string> {
|
||||||
): Package {
|
const latestVersions = Object.keys(deps)
|
||||||
const deps = pkg[key]
|
.filter(depName => !depName.startsWith('@rondo.dev'))
|
||||||
if (!deps || !deps[name] || version.wanted === version.latest) {
|
.reduce((latestVersions, depName) => {
|
||||||
return pkg
|
const version = prefix +
|
||||||
}
|
cp.execFileSync('npm', ['info', depName, 'version']).toString().trim()
|
||||||
info(' [%s] %s %s ==> %s', key, name, version.wanted, version.latest)
|
info(' %s (current: %s, latest: %s)', depName, deps[depName], version)
|
||||||
return {
|
latestVersions[depName] = version
|
||||||
...pkg,
|
return latestVersions
|
||||||
[key]: {
|
}, deps)
|
||||||
...deps,
|
|
||||||
[name]: prefix + version.latest,
|
return latestVersions
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function update(...argv: string[]) {
|
export async function update(...argv: string[]) {
|
||||||
const {parse} = argparse({
|
const {parse} = argparse({
|
||||||
dirs: arg('string[]', {positional: true, default: ['.'], n: '+'}),
|
dirs: arg('string[]', {positional: true, default: ['.'], n: '+'}),
|
||||||
|
dryRun: arg('boolean', {alias: 'd', default: false}),
|
||||||
prefix: arg('string', {default: '^'}),
|
prefix: arg('string', {default: '^'}),
|
||||||
})
|
})
|
||||||
const {dirs, prefix} = parse(argv)
|
const {dirs, dryRun, prefix} = parse(argv)
|
||||||
|
|
||||||
let updates = 0
|
|
||||||
for (const dir of dirs) {
|
for (const dir of dirs) {
|
||||||
info(dir)
|
info(dir)
|
||||||
const outdatedByName = findOutdated(dir)
|
const pkg = readPackage(dir)
|
||||||
|
if (pkg.dependencies) {
|
||||||
const pkgFile = path.join(dir, 'package.json')
|
pkg.dependencies = findLatestVersions(pkg.dependencies, prefix)
|
||||||
const pkg: Package = JSON.parse(fs.readFileSync(pkgFile, 'utf8'))
|
|
||||||
let pkgUpdate: Package = pkg
|
|
||||||
|
|
||||||
// tslint:disable-next-line
|
|
||||||
for (const name in outdatedByName) {
|
|
||||||
const outdated = outdatedByName[name]
|
|
||||||
pkgUpdate = updateDependency(
|
|
||||||
pkgUpdate, 'dependencies', name, prefix, outdated)
|
|
||||||
pkgUpdate = updateDependency(
|
|
||||||
pkgUpdate, 'devDependencies', name, prefix, outdated)
|
|
||||||
}
|
}
|
||||||
|
if (pkg.devDependencies) {
|
||||||
if (pkgUpdate !== pkg) {
|
pkg.devDependencies = findLatestVersions(pkg.devDependencies, prefix)
|
||||||
updates += 1
|
}
|
||||||
|
if (!dryRun) {
|
||||||
info('Writing updates...')
|
info('Writing updates...')
|
||||||
fs.writeFileSync(pkgFile, JSON.stringify(pkgUpdate, null, ' '))
|
writePackage(dir , pkg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (updates) {
|
|
||||||
info('Done! Do not forget to run npm install!')
|
info('Done! Do not forget to run npm install!')
|
||||||
}
|
|
||||||
}
|
}
|
||||||
update.help = 'Update all dependencies to the latest versions'
|
update.help = 'Update all dependencies to the latest versions'
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user