-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathncu.ts
311 lines (236 loc) · 7.06 KB
/
ncu.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
* Created by user on 2019/5/19.
*/
import { basenameStrip, createCommandModuleExports } from '../../lib/cmd_dir';
import path = require('upath2');
import {
chalkByConsole,
console,
consoleDebug,
findRoot,
printRootData,
yargsProcessExit,
} from '../../lib/index';
import IPackageJson, { readPackageJson } from '@ts-type/package-dts';
import { writeJSONSync, writePackageJson } from '../../lib/pkg';
import { IUnpackMyYargsArgv } from '../../lib/cmd_dir';
import setupNcuToYargs, {
checkResolutionsUpdate,
isBadVersion,
isUpgradeable,
npmCheckUpdates, npmCheckUpdatesOptions, updateSemver,
} from '../../lib/cli/ncu';
import {
filterResolutions,
IDependencies,
IYarnLockfileParseObjectRow,
parse as parseYarnLock, removeResolutionsCore, stringify as stringifyYarnLock,
stripDepsName, writeYarnLockFile, yarnLockDiff,
} from '../../lib/yarnlock';
import fs = require('fs-extra');
import semver = require('semver');
import Bluebird = require('bluebird');
import { toDependencyTable } from '../../lib/table';
import { fsYarnLock } from '../../lib/fsYarnLock';
import { updateYarnLockTag, printReport } from '@yarn-tool/yarnlock-ncu/index';
import { writeFileSync } from 'fs';
const cmdModule = createCommandModuleExports({
command: basenameStrip(__filename) + ' [-u]',
aliases: ['update'],
describe: `Find newer versions of dependencies than what your package.json allows`,
builder(yargs)
{
return setupNcuToYargs(yargs)
.option('resolutions', {
alias: ['R'],
desc: 'do with resolutions only',
boolean: true,
})
.option('no-safe', {
boolean: true,
})
.example(`$0 ncu -u`, `check new version and update package.json`)
.example(`$0 ncu -R`, `check new version of resolutions in package.json`)
.example(`$0 ncu -u -R`, `check new version of resolutions in package.json and update package.json`)
},
async handler(argv)
{
const { cwd } = argv;
let rootData = findRoot({
...argv,
cwd,
}, true);
//console.dir(rootData);
let pkg_file_root = path.join(rootData.root, 'package.json');
let pkg_file = path.join(rootData.pkg, 'package.json');
let pkg_data = readPackageJson(pkg_file);
let resolutions = pkg_data.resolutions;
let pkg_file_ws: string;
let pkg_data_ws: IPackageJson;
let doWorkspace = !rootData.isWorkspace && rootData.hasWorkspace;
if (doWorkspace)
{
pkg_file_ws = path.join(rootData.ws, 'package.json');
pkg_data_ws = readPackageJson(pkg_file_ws);
resolutions = pkg_data_ws.resolutions;
}
if (argv.resolutions)
{
if (!resolutions || !Object.keys(resolutions).length)
{
return yargsProcessExit(`resolutions is not exists in package.json`)
}
let yl = fsYarnLock(rootData.root);
if (!yl.yarnlock_old)
{
// 防止 yarn.lock 不存在
return;
}
let ret = await checkResolutionsUpdate(resolutions, yl.yarnlock_old, argv);
//console.log(ret);
if (ret.yarnlock_changed)
{
writeYarnLockFile(yl.yarnlock_file, ret.yarnlock_new_obj);
chalkByConsole((chalk, console) =>
{
let p = chalk.cyan(path.relative(argv.cwd, yl.yarnlock_file));
console.log(`${p} is updated!`);
}, console);
let msg = yarnLockDiff(stringifyYarnLock(ret.yarnlock_old_obj), stringifyYarnLock(ret.yarnlock_new_obj));
if (msg)
{
console.log(`\n${msg}\n`);
}
}
let ls2 = Object.values(ret.deps)
.filter(data => {
let { name, version_old, version_new } = data;
return isUpgradeable(version_old, version_new)
})
;
let ncuOptions = npmCheckUpdatesOptions(argv);
let fromto = ls2
.reduce((a, data) =>
{
let { name, version_old, version_new } = data;
let new_semver = updateSemver(version_old, version_new, ncuOptions);
a.from[name] = version_old;
a.to[name] = new_semver;
resolutions[name] = new_semver;
return a;
}, {
from: {},
to: {},
} as Parameters<typeof toDependencyTable>[0])
;
let msg = toDependencyTable(fromto);
console.log(`\n${msg}\n`);
if (argv.upgrade)
{
if (doWorkspace)
{
pkg_data_ws.resolutions = resolutions;
writePackageJson(pkg_file_ws, pkg_data_ws);
chalkByConsole((chalk, console) =>
{
let p = chalk.cyan(path.relative(argv.cwd, pkg_file_ws));
console.log(`${p} is updated!`);
}, console);
}
else
{
pkg_data.resolutions = resolutions;
writePackageJson(pkg_file, pkg_data);
chalkByConsole((chalk, console) =>
{
let p = chalk.cyan(path.relative(rootData.ws || rootData.pkg, pkg_file));
console.log(`${p} is updated!`);
}, console);
}
}
return;
}
printRootData(rootData, argv);
let pkgNcu = await npmCheckUpdates({
cwd,
rootData,
// @ts-ignore
}, {
...argv,
json_old: pkg_data,
});
if (pkgNcu.json_changed && argv.upgrade)
{
writeJSONSync(pkg_file, pkgNcu.json_new);
consoleDebug.info(`package.json updated`);
}
if (argv.dedupe && resolutions && Object.keys(resolutions).length)
{
let ls = Object.entries(pkgNcu.json_new.dependencies || {})
.concat(Object.entries(pkgNcu.json_new.devDependencies || {}), Object.entries(pkgNcu.json_new.optionalDependencies || {}))
.reduce(function (a, [name, ver_new])
{
let ver_old = resolutions[name];
if (ver_old)
{
if (ver_new === 'latest')
{
ver_new = '*';
}
// @ts-ignore
a[name] = ver_new;
}
return a;
}, {} as IDependencies)
;
let yl = fsYarnLock(rootData.root);
if (!yl.yarnlock_old)
{
// 防止 yarn.lock 不存在
return;
}
let ret = await checkResolutionsUpdate(resolutions, yl.yarnlock_old, argv);
if (ret.yarnlock_changed)
{
let msg = yarnLockDiff(stringifyYarnLock(ret.yarnlock_old_obj), stringifyYarnLock(ret.yarnlock_new_obj));
if (msg)
{
console.log(`\n${msg}\n`);
}
}
if (pkgNcu.json_changed && !argv.upgrade)
{
ret.yarnlock_changed && consoleDebug.magenta.info(`your dependencies version high than resolutions`);
consoleDebug.log(`you can do `, console.bold.cyan.chalk(`yt ncu -u`), ` , for update package.json`);
}
if (ret.yarnlock_changed && argv.upgrade)
{
writeYarnLockFile(yl.yarnlock_file, ret.yarnlock_new_obj);
consoleDebug.magenta.info(`Deduplication yarn.lock`);
consoleDebug.log(`you can do `, console.bold.cyan.chalk(`yt install`), ` , for upgrade dependencies now`);
}
}
let yl = fsYarnLock(rootData.root);
if (yl.yarnlock_exists)
{
let ret = await updateYarnLockTag(yl.yarnlock_old);
if (ret.yarnlock_changed)
{
consoleDebug.magenta.info(`higher versions exists on registry`);
let s = printReport(ret.report);
s?.length > 0 && console.log(s);
if (argv.upgrade)
{
writeFileSync(yl.yarnlock_file, ret.yarnlock_new);
consoleDebug.magenta.info(`yarn.lock updated`);
consoleDebug.log(`you can do `, console.bold.cyan.chalk(`yt install`), ` , for upgrade dependencies now`);
}
else
{
consoleDebug.log(`you can do `, console.bold.cyan.chalk(`yt ncu -u`), ` , for update yarn.lock`);
}
}
}
},
});
export = cmdModule