-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·184 lines (145 loc) · 4.56 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
#!/usr/bin/env node
'use strict';
import {promises as fs} from 'fs';
import {tmpdir} from 'os';
import {performance} from 'perf_hooks';
import getopts from 'getopts';
import kleur from 'kleur';
import findUp from 'find-up';
import latestVersion from 'latest-version';
import pMap from 'p-map';
import {createStore} from 'storage-async';
import logUpdate from 'log-update';
import columnify from 'columnify';
import {detectRange} from './utils/range-detector.js';
const options = getopts(process.argv.slice(2), {
alias: {
help: 'h',
version: 'v',
input: 'i',
exclude: 'e',
json: 'j'
}
});
const temporary = new Map();
const logger = new Map();
if (options.help) {
console.log(`
Usage:
$ odc <options>
Options:
-i, --input <path> Path of a package.json file (defaults to the nearest one)
-e, --exclude <pkg,...> Exclude packages
-j, --json Output a JSON object, instead of writing package.json
-nc, --no-cache Run without using cache
-v, --version Print the version
-h, --help Print this help
Examples:
$ odc
$ odc --input test/ -e chalk,lodash
`);
process.exit(0);
}
if (options.version) {
console.log(require('./package.json').version);
process.exit(0);
}
let exclude;
if (options.exclude && options.exclude !== true) {
exclude = options.exclude.split(',');
}
(async () => {
const t0 = performance.now();
const cache = await createStore({path: `${tmpdir()}/odc.json`, ttl: 900_000});
try {
const closest = await findUp(options.input || 'package.json');
if (!closest) {
console.log(kleur.red(`Unable to find package.json in ${process.cwd()} or any of its parent directories`));
process.exit(1);
}
const parsed = JSON.parse(await fs.readFile(closest));
const deps = parsed.dependencies || {};
const devDeps = parsed.devDependencies || {};
const previousDeps = {...deps};
const previousDevDeps = {...devDeps};
let updatedDeps = {};
let updatedDevDeps = {};
const mapper = async name => {
const previous = previousDeps[name];
const cached = await cache.has(name);
const latest = cached ? await cache.get(name) : `${detectRange(previous) ?? ''}${await latestVersion(name)}`;
if (previous !== latest && !/(?<range>latest|\*)/s.test(previous)) {
updatedDeps = {...updatedDeps, ...{[name]: latest}};
if (!cached) {
temporary.set(name, latest);
}
logger.set(name, {previous, latest});
}
};
const devMapper = async name => {
const previous = previousDevDeps[name];
const cached = await cache.has(name);
const latest = cached ? await cache.get(name) : `${detectRange(previous) ?? ''}${await latestVersion(name)}`;
if (previous !== latest && !/(?<range>latest|\*)/s.test(previous)) {
updatedDevDeps = {...updatedDevDeps, ...{[name]: latest}};
if (!cached) {
temporary.set(name, latest);
}
logger.set(name, {previous, latest});
}
};
const names = Object.keys(deps)
.filter(name => exclude ? !exclude.includes(name) : true);
const devNames = Object.keys(devDeps)
.filter(name => exclude ? !exclude.includes(name) : true);
await Promise.all([names, devNames]).then(async results => {
await pMap(results[0], mapper);
await pMap(results[1], devMapper);
});
if (Object.keys(updatedDeps).length === 0 && Object.keys(updatedDevDeps).length === 0) {
const t1 = performance.now();
console.log(kleur.green('Everything up-to-date'));
console.log(`\n✨ Done in ${((t1 - t0) / 1000).toFixed(2)}s`);
process.exit(0);
}
// Remove unnecessary properties
const {_id, ...rest} = parsed;
const packageJson = {
...rest,
dependencies: {
...rest.dependencies,
...updatedDeps
},
devDependencies: {
...rest.devDependencies,
...updatedDevDeps
}
};
if (options.json) {
console.log('\n' + JSON.stringify(packageJson, undefined, 4));
} else {
await fs.writeFile(closest, JSON.stringify(packageJson, undefined, 4));
}
let columns = [];
for (const [key, value] of logger) {
columns = [...columns, {
name: key,
previous: kleur.red(value.previous),
latest: kleur.green(value.latest)
}];
}
console.log(columnify(columns));
const t1 = performance.now();
console.log(`\n✨ Done in ${((t1 - t0) / 1000).toFixed(2)}s`);
if (temporary.size > 0) {
let n = 1;
for await (const [key, value] of temporary) {
await cache.set(key, value);
logUpdate(kleur.yellow(`Populating cache (${n++}/${temporary.size})`));
}
}
} catch (error) {
console.log(error);
process.exit(1);
}
})();