forked from jupyterlab/jupyterlab
/
update-dependency.ts
executable file
·67 lines (59 loc) · 1.82 KB
/
update-dependency.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
#!/usr/bin/env node
/*-----------------------------------------------------------------------------
| 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 = require('package-json');
// Make sure we have required command line arguments.
if (process.argv.length !== 3) {
let msg = '** Must supply an update specifier\n';
process.stderr.write(msg);
process.exit(1);
}
// Extract the desired library target and specifier.
let parts = process.argv[2].slice(1).split('@');
parts[0] = process.argv[2][0] + parts[0];
// Handle the version.
if (parts.length === 1) {
parts.push('latest');
}
packageJson(parts[0], { version: parts[1] }).then(data => {
handlePackages(data.name, `~${data.version}`);
});
// Handle the packages
function handlePackages(name: string, specifier: string) {
utils.getLernaPaths().forEach(pkgPath => {
handlePackage(name, specifier, pkgPath);
});
handlePackage(name, specifier, path.resolve('.'));
utils.run('yarn');
}
/**
* Handle an individual package on the path - update the dependency.
*/
function handlePackage(
name: string,
specifier: string,
packagePath: string
): void {
// 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;
}
// Update dependencies as appropriate.
for (let dtype of ['dependencies', 'devDependencies']) {
let deps = data[dtype] || {};
if (name in deps) {
deps[name] = specifier;
}
}
// Write the file back to disk.
utils.writePackageData(packagePath, data);
}