-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater.js
213 lines (192 loc) · 5.92 KB
/
updater.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const fs = require('fs');
const path = require('path');
const { spawn } = require('child_process');
let usingYarn = fs.existsSync(path.join(__dirname, 'yarn.lock'));
const argv = process.argv.slice('2');
if (argv.includes('--yarn') || argv.includes('-yarn')) {
usingYarn = true;
}
async function main(cwd) {
if (!cwd) cwd = __dirname;
const pjson = parsePkgJson(cwd);
await updatePkgJSON(pjson, cwd);
if (pjson['workspaces']) {
let workspaces = Array.isArray(pjson['workspaces']) ? pjson['workspaces'] : null;
if (!workspaces && Array.isArray(pjson['workspaces']['packages'])) {
workspaces = pjson['workspaces']['packages'];
}
workspaces = [...new Set(workspaces)];
for (let i = 0; i < workspaces.length; i++) {
const workspace = path.join(cwd, workspaces[i]);
await main(workspace);
}
}
}
/**
* get and parse package.json from folder
* @param {string} cwd
*/
function parsePkgJson(cwd) {
return JSON.parse(fs.readFileSync(path.join(cwd, 'package.json')).toString());
}
main();
/**
* update package.json
* @param {{ dependencies?: Record<string, string>, devDependencies?: Record<string, string>, peerDependencies?: Record<string, string>, optionalDependencies?: Record<string, string> }} pkgJSON
*/
async function updatePkgJSON(pkgJSON, cwd = null) {
if (!cwd) cwd = __dirname;
if (pkgJSON.dependencies) await doUpdate(pkgJSON.dependencies, 'production', cwd);
if (pkgJSON.devDependencies) await doUpdate(pkgJSON.devDependencies, 'development', cwd);
}
/**
*
* @param {Record<string,string>} packages
* @param {'production'|'development'|'optional'|'peer'} mode
*/
async function doUpdate(packages, mode, cwd = null) {
if (!cwd) cwd = __dirname;
if (!packages) packages = {};
const pkgnames = Object.keys(packages);
if (pkgnames.length === 0) {
return;
}
console.log('cwd', cwd);
const pkg2update = [];
for (let i = 0; i < pkgnames.length; i++) {
const pkgname = pkgnames[i];
const version = packages[pkgname];
/**
* is remote url package
*/
let isTarballPkg = /^(https?)|.(tgz|zip|tar|tar.gz)$|\/tarball\//i.test(version);
/**
* is github package
*/
let isGitPkg = /^(git+|github:|https?:\/\/github.com\/)/i.test(version);
/**
* is local package
*/
const isLocalPkg = /^(file):/i.test(version);
/**
* is local tarball package
*/
const isLocalTarballpkg = isLocalPkg && /.(tgz|zip|tar|tar.gz)$/i.test(version);
if (isLocalPkg && fs.existsSync(path.join(cwd, 'node_modules', pkgname))) {
fs.rmSync(path.join(cwd, 'node_modules', pkgname), { recursive: true, force: true });
}
if (isLocalTarballpkg || isGitPkg || isTarballPkg || isLocalPkg) {
/*if (!usingYarn) {
pkg2update.push(pkgname);
} else {
pkg2update.push(`${pkgname}@${version}`);
}*/
pkg2update.push(`${pkgname}@${version}`);
}
}
const pkgm = usingYarn ? 'yarn' : 'npm';
const installArg = usingYarn ? 'add' : 'i';
let updateArg = 'update';
if (usingYarn) {
const yarnVersion = await summon('yarn', ['-v'], { cwd, shell: true });
if (yarnVersion instanceof Error === false) {
if (yarnVersion.output.startsWith('3')) {
updateArg = 'up';
} else {
updateArg = 'upgrade';
}
}
}
// const updateArg = usingYarn ? 'up' : 'update';
let saveAs = '';
switch (mode) {
case 'development':
if (usingYarn) {
saveAs = '--dev';
} else {
saveAs = '-D';
}
break;
case 'optional':
if (usingYarn) {
saveAs = '--optional';
} else {
saveAs = '-O';
}
break;
case 'peer':
if (usingYarn) {
saveAs = '--peer';
}
break;
}
const argsInstall = [installArg, saveAs, ...pkg2update];
const argsUpdate = [updateArg, ...pkg2update];
// do update
pkg2update.forEach((pkg) => {
// console.log(pkgm, 'updating', saveAs, pkg);
if (pkg.includes('highli')) console.log(pkgm, 'updating', saveAs, pkg, cwd);
});
const method = 'update';
if (method === 'update') {
// update method
await new Promise((resolve) => {
spawn(pkgm, argsUpdate, { cwd, stdio: 'ignore', shell: true }).once('exit', function () {
resolve(null);
});
});
} else {
// install method
await new Promise((resolve) => {
spawn(pkgm, argsInstall, { cwd, stdio: 'ignore', shell: true }).once('exit', function () {
resolve(null);
});
});
}
}
/**
* spawn command prompt
* @param {string} cmd
* @param {string[]} args
* @param {Parameters<typeof spawn>[2]} opt
* @returns {Promise<{ stdout:string, stderr:string, output:string, error?: Error }>}
*/
function summon(cmd, args = [], opt = {}) {
if (!opt) opt = {};
if (!opt.cwd) opt.cwd = __dirname;
const spawnopt = Object.assign(opt);
// *** Return the promise
return new Promise(function (resolve) {
if (typeof cmd !== 'string' || cmd.trim().length === 0) return resolve(new Error('cmd empty'));
let stdout = '';
let stderr = '';
let output = '';
const child = spawn(cmd, args, spawnopt);
// if (spawnopt.stdio === 'ignore') child.unref();
if (child.stdout && 'on' in child.stdout) {
child.stdout.setEncoding('utf8');
child.stdout.on('data', (data) => {
stdout += data;
output += data;
});
}
if (child.stderr && 'on' in child.stdout) {
child.stderr.setEncoding('utf8');
child.stderr.on('data', (data) => {
stderr += data;
output += data;
});
}
child.on('close', function (code) {
// Should probably be 'exit', not 'close'
if (code !== 0) console.log('[ERROR]', cmd, ...args, 'dies with code', code);
// *** Process completed
resolve({ stdout, stderr, output });
});
child.on('error', function (error) {
// *** Process creation failed
console.log('got error', error);
resolve({ stdout, stderr, output, error });
});
});
}