-
Notifications
You must be signed in to change notification settings - Fork 48
/
update_dependencies.js
78 lines (69 loc) · 2.71 KB
/
update_dependencies.js
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
const fs = require('fs').promises;
const path = require('path');
const got = require('got');
const prompts = require('prompts');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
let updateCount = 0;
const dirs = ['var', 'lib', 'www'];
const pkgs = {
combined: require(path.join(__dirname, 'package.json'))
};
dirs.forEach(dir => {
pkgs[dir] = require(path.join(__dirname, 'addon_files/redmatic', dir, 'package.json'));
});
async function compareVersions(dir) {
const pkgJson = pkgs[dir];
const dependencies = pkgJson.dependencies;
for (const pkg in dependencies) {
let json;
try {
json = await got(`https://registry.npmjs.org/${pkg}`, {
responseType: 'json'
});
} catch (error) {
console.error(pkg, error.message);
}
if (json) {
const actual = dependencies[pkg];
const latest = json.body['dist-tags'].latest;
if (actual !== latest) {
const response = await prompts({
type: 'confirm',
name: 'confirmed',
message: `update ${pkg} ${actual} to ${latest}?`
});
if (response.confirmed) {
dependencies[pkg] = latest;
pkgs.combined.dependencies[pkg] = `0.0.0 - ${latest}`;
const file = path.join('addon_files/redmatic', dir, 'package.json')
await fs.writeFile(path.join(__dirname, 'package.json'), JSON.stringify(pkgs.combined, null, ' '));
await fs.writeFile(path.join(__dirname, file), JSON.stringify(pkgs[dir], null, ' '));
let changelog = '';
switch (pkg) {
case 'node-red':
changelog = ' ([Changelog](https://github.com/node-red/node-red/blob/master/CHANGELOG.md))';
break;
case 'node-red-dashboard':
changelog = ' ([Changelog](https://github.com/node-red/node-red-dashboard/blob/master/CHANGELOG.md))';
break;
default:
}
const {stdout, stderr} = await exec(`git commit package.json ${file} -m 'update ${pkg} ${actual} to ${latest}${changelog}'`);
console.log(stderr, stdout);
updateCount += 1;
}
}
}
}
}
async function checkDirs() {
for (dir of dirs) {
await compareVersions(dir);
}
if (updateCount === 0) {
console.log('all dependencies are up to date :)');
}
console.log('');
}
checkDirs();