-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·313 lines (300 loc) · 11.9 KB
/
index.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
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
312
313
#!/usr/bin/env node
import chalk from 'chalk';
import { execSync } from 'child_process';
import commandExists from 'command-exists';
import figures from 'figures';
import jsonfile from 'jsonfile';
import pkgUp from 'pkg-up';
import prettyMs from 'pretty-ms';
import ora from 'ora';
import registryUrl from 'registry-url';
import fetch from 'node-fetch';
import pLimit from 'p-limit';
import { existsSync } from 'fs';
import { dirname, resolve } from 'path';
import yargs from 'yargs';
import sortKeys from 'sort-keys';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
const customRules = {
webpack: ['webpack-env'],
};
const isTypes = (dep) => /^@types\//.test(dep);
// * `ora` <=> `@types/ora`
// * `@babel/core` <=> `@types/babel__core`
const depName2typeName = (name) => {
if (/@.+\/.*/.test(name)) {
return `@types/` + name.replace('@', '').replace('/', '__');
}
else {
return `@types/` + name;
}
};
const checkPkgDeps = (json) => {
const { dependencies: deps = {}, devDependencies: devDeps = {} } = json;
const sorter = (a, b) => (a < b ? -1 : 1);
// * ----------------
// * manually add `node` for `@types/node`
const allJsonDeps = ['node', ...Object.keys(deps), ...Object.keys(devDeps)];
/** search customRules with current package.json deps */
const allAddonDeps = allJsonDeps
.flatMap((e) => customRules[e])
.filter((e) => e);
const allDeps = [...allJsonDeps, ...allAddonDeps];
const allTypesToCheck = allDeps
.filter((name) => !isTypes(name))
.map(depName2typeName);
const installedTypes = allDeps.filter((name) => isTypes(name)).sort(sorter);
// * ---------------- analyzing dependencies
const skippingTypes = allTypesToCheck.filter((e) => installedTypes.includes(e));
const missedTypes = allTypesToCheck
.filter((name) => !skippingTypes.includes(name))
.sort(sorter);
const unusedTypes = installedTypes
.filter((name) => !skippingTypes.includes(name))
.sort(sorter);
// * ---------------- return
return { installedTypes, missedTypes, unusedTypes };
};
var _a;
// * ----------------------------------------------------------------
const NPM_MANAGER_LIST = ['yarn', 'tnpm', 'pnpm', 'npm'];
const DEFAULT_MANAGER = (_a = NPM_MANAGER_LIST.find((tool) => commandExists.sync(tool))) !== null && _a !== void 0 ? _a : 'npm';
const DEFAULT_PARALLEL = 10;
// * ----------------------------------------------------------------
// ! a bit tricky // Seognil LC 2021/09/18
const getTypeDonePkgVersion = () => {
let searchRoot = dirname(new URL(import.meta.url).pathname);
let pkgFile = resolve(searchRoot, './package.json');
let failedCount = 0;
while (!existsSync(pkgFile)) {
searchRoot = dirname(searchRoot);
pkgFile = resolve(searchRoot, './package.json');
failedCount++;
if (failedCount > 10)
return 'unknown';
}
return jsonfile.readFileSync(pkgFile).version;
};
// * ----------------------------------------------------------------
const argv = yargs(process.argv.slice(2))
.version(getTypeDonePkgVersion())
.options({
'tool': {
alias: 't',
describe: 'Which package manager to use',
default: DEFAULT_MANAGER,
type: 'string',
},
'skip-add': {
describe: 'Skip add missing @types',
default: false,
type: 'boolean',
},
'skip-remove': {
describe: 'Skip removing unuseful @types',
default: false,
type: 'boolean',
},
'skip-sort': {
describe: 'Skip sorting dependencies',
default: false,
type: 'boolean',
},
'skip-install': {
alias: 's',
describe: 'Skip run install after analyzed',
default: false,
type: 'boolean',
},
'dry-run': {
alias: 'd',
describe: 'Analyze only',
default: false,
type: 'boolean',
},
'parallel': {
alias: 'p',
describe: 'Set maximum number of parallel fetch tasks',
default: DEFAULT_PARALLEL,
type: 'number',
},
})
.parseSync();
// * default is: https://registry.npmjs.org/
const globalRegistry = registryUrl();
const limit = pLimit(argv.parallel);
// * ----------------
const fetchSingle = (pkgName, tick) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c;
const url = `${globalRegistry}/${pkgName}`.replace(/(?<!:)\/\//, '/');
const res = (yield fetch(url)
.then((e) => e.json())
.catch(() => {
'Not found';
tick === null || tick === void 0 ? void 0 : tick(pkgName);
}));
const lastVer = (_a = res === null || res === void 0 ? void 0 : res['dist-tags']) === null || _a === void 0 ? void 0 : _a.latest;
const isDeprecated = ((_c = (_b = res === null || res === void 0 ? void 0 : res.versions) === null || _b === void 0 ? void 0 : _b[lastVer]) === null || _c === void 0 ? void 0 : _c.deprecated) !== undefined;
const isUseful = lastVer !== undefined && !isDeprecated;
tick === null || tick === void 0 ? void 0 : tick(pkgName);
return { pkgName, lastVer, isUseful, isDeprecated };
});
// * ----------------
const fetchList = (depslist, tick) => __awaiter(void 0, void 0, void 0, function* () {
const results = yield Promise.all(depslist.map((name) => limit(() => fetchSingle(name, tick))));
const deprecatedTypes = results.filter((e) => e.isDeprecated);
const usefulTypes = results.filter((e) => e.isUseful);
return { deprecatedTypes, usefulTypes };
});
const fetchDepsInfo = ({ installedTypes, missedTypes, }) => __awaiter(void 0, void 0, void 0, function* () {
const globalRegistry = registryUrl();
console.log(`registry: "${globalRegistry}"`);
const spinner = ora('Fetching...').start();
let count = 0;
const fetchLen = installedTypes.length + missedTypes.length;
const updateSpinner = (depName) => {
spinner.prefixText = chalk.gray(`[${++count}/${fetchLen}]`);
spinner.text = `Checking ${depName} ...`;
};
const [{ deprecatedTypes }, { usefulTypes }] = yield Promise.all([
fetchList(installedTypes, updateSpinner),
fetchList(missedTypes, updateSpinner),
]);
spinner.stop();
return { deprecatedTypes, usefulTypes };
});
const b = (dep) => chalk.bold(dep);
const logAnalyzedList = ({ deprecatedTypes, unusedTypes, usefulTypes, }) => {
const deprecatedNames = deprecatedTypes.map((e) => e.pkgName);
const unusedNames = unusedTypes;
const usefulNames = usefulTypes.map((e) => e.pkgName);
// * ---------------- log uninstall list
deprecatedNames
.filter((e) => !unusedNames.includes(e))
.forEach((dep) => {
console.log(chalk.red(figures.arrowLeft, `${b(dep)} is deprecated. Needs to uninstall`));
});
unusedNames.forEach((dep) => {
console.log(chalk.red(figures.arrowLeft, `${b(dep)} is unused. Needs to uninstall`));
});
// * ---------------- log install list
usefulNames.forEach((dep) => {
console.log(chalk.green(figures.arrowRight, `${b(dep)} is missing. Waiting for install`));
});
};
const updatePackageJson = (pkgPath, pkgJson, { deprecatedTypes, unusedTypes, usefulTypes }) => __awaiter(void 0, void 0, void 0, function* () {
const hasDepsObj = pkgJson.dependencies !== undefined;
const hasDevDepsObj = pkgJson.devDependencies !== undefined;
if (!pkgJson.dependencies)
pkgJson.dependencies = {};
if (!pkgJson.devDependencies)
pkgJson.devDependencies = {};
const deps = pkgJson.dependencies;
const devDeps = pkgJson.devDependencies;
// * ---------------- skips
if (!argv['skip-add']) {
usefulTypes.forEach((e) => {
devDeps[e.pkgName] = `^${e.lastVer}`;
});
}
if (!argv['skip-remove']) {
deprecatedTypes.forEach((e) => {
delete deps[e.pkgName];
delete devDeps[e.pkgName];
});
unusedTypes.forEach((e) => {
delete deps[e];
delete devDeps[e];
});
}
if (!argv['skip-sort']) {
Object.keys(deps).forEach((e) => {
if (isTypes(e)) {
devDeps[e] = deps[e];
delete deps[e];
}
});
pkgJson.dependencies = sortKeys(deps);
pkgJson.devDependencies = sortKeys(devDeps);
}
// * ---------------- minimal change cleanup
if (!hasDepsObj && Object.keys(deps).length === 0) {
delete pkgJson.dependencies;
}
if (!hasDevDepsObj && Object.keys(devDeps).length === 0) {
delete pkgJson.devDependencies;
}
// * ---------------- update
yield jsonfile.writeFile(pkgPath, pkgJson, { spaces: 2 });
});
// * ================================================================================
const task = () => __awaiter(void 0, void 0, void 0, function* () {
const pkgPath = pkgUp.sync();
if (pkgPath === null) {
console.error('No package.json file found!');
process.exit();
}
const pkgJson = jsonfile.readFileSync(pkgPath);
// * ---------------- static package analyzing
const { installedTypes, unusedTypes, missedTypes } = checkPkgDeps(pkgJson);
// * ---------------- fetching info
const { deprecatedTypes, usefulTypes } = yield fetchDepsInfo({
installedTypes,
missedTypes,
});
// ! ---------------- all clear early quit
const allClear = !deprecatedTypes.length && !unusedTypes.length && !missedTypes.length;
if (allClear)
return console.log(chalk.white(figures.squareSmallFilled, `Nothing to do`));
// * ---------------- log result
const patchBundle = {
deprecatedTypes,
unusedTypes,
usefulTypes,
};
logAnalyzedList(patchBundle);
// ! ---------------- dry run early quit
if (argv['dry-run'])
return;
// * ---------------- update package.json
yield updatePackageJson(pkgPath, pkgJson, patchBundle);
// * ---------------- install
if (argv['skip-install'])
return;
const tool = argv.tool;
if (!commandExists.sync(tool)) {
console.error(`Command '${tool}' not found!`);
process.exit();
}
execSync(`${tool} install`, { stdio: 'inherit' });
});
// * ================================================================================
const main = () => __awaiter(void 0, void 0, void 0, function* () {
const startTime = Date.now();
yield task();
const deltaTime = prettyMs(Date.now() - startTime, {
secondsDecimalDigits: 2,
});
console.log(chalk.green(figures.tick, `All types are OK. Done in ${deltaTime}`));
});
main();