/
update-dist-tag.ts
executable file
·87 lines (74 loc) · 2.64 KB
/
update-dist-tag.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*-----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/
import * as path from 'path';
import * as utils from './utils';
import packageJson from 'package-json';
import commander from 'commander';
import semver from 'semver';
/**
* Handle an individual package on the path - update the dependency.
*/
export async function handlePackage(packagePath: string): Promise<string[]> {
const cmds: string[] = [];
// Read in the package.json.
packagePath = path.join(packagePath, 'package.json');
let data: any;
try {
data = utils.readJSONFile(packagePath);
} catch (e) {
console.log('Skipping package ' + packagePath);
return cmds;
}
if (data.private) {
return cmds;
}
const pkg = data.name;
let npmData = await packageJson(pkg, { allVersions: true });
let versions = Object.keys(npmData.versions).sort(semver.rcompare);
let tags = npmData['dist-tags'];
// Go through the versions. The latest prerelease is 'next', the latest
// non-prerelease should be 'stable'.
let next = semver.prerelease(versions[0]) ? versions[0] : undefined;
let latest = versions.find(i => !semver.prerelease(i));
if (latest && latest !== tags.latest) {
cmds.push(`npm dist-tag add ${pkg}@${latest} latest`);
}
// If next is defined, but not supposed to be, remove it. If next is supposed
// to be defined, but is not the same as the current next, change it.
if (!next && tags.next) {
cmds.push(`npm dist-tag rm ${pkg} next`);
} else if (next && next !== tags.next) {
cmds.push(`npm dist-tag add ${pkg}@${next} next`);
}
return cmds;
}
function flatten(a: any[]) {
return a.reduce((acc, val) => acc.concat(val), []);
}
commander
.description(
`Print out commands to update npm 'latest' and 'next' dist-tags
so that 'latest' points to the latest stable release and 'next'
points to the latest prerelease after it.`
)
.option('--lerna', 'Update dist-tags in all lerna packages')
.option('--path [path]', 'Path to package or monorepo to update')
.action(async (args: any) => {
let basePath = path.resolve(args.path || '.');
let cmds: string[][] = [];
let paths: string[] = [];
if (args.lerna) {
paths = utils.getLernaPaths(basePath).sort();
cmds = await Promise.all(paths.map(handlePackage));
}
cmds.push(await handlePackage(basePath));
let out = flatten(cmds).join('\n');
if (out) {
console.log(out);
}
});
if (require.main === module) {
commander.parse(process.argv);
}