forked from webex/webex-js-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deps.js
executable file
·354 lines (298 loc) · 9.19 KB
/
deps.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env node
/* eslint-disable complexity */
/* eslint-disable global-require */
/* eslint-disable no-sync */
/* eslint-disable require-jsdoc */
/* eslint-disable import/no-dynamic-require */
// Idealy, this file belongs in ./tooling, but the second iteration is a *lot*
// simpler if we don't have to deal with adding `../` to every file operation.
// Reminder: because this script uses the package.jsons in
// packages/node_modules, it should be used *after* new versions have been
// determined by babel.
// DO NOT COPY THIS FILE INTO THE react-ciscospark REPO.
// Instead, make it a package in its own right and let the react-ciscospark
// project depend on it.
const debug = require('debug')('deps');
const builtins = require('builtins')();
const detective = require('detective');
const fs = require('fs');
const _ = require('lodash');
const path = require('path');
const util = require('util');
const FILTERED_TRANSFORMS = ['babelify'];
const DEFAULT_TRANSFORMS = ['envify'];
const depsToVersions = _.curry((rootPkg, deps) => deps.reduce((acc, dep) => {
if (builtins.includes(dep)) {
return acc;
}
acc[dep] = _.get(rootPkg, `dependencies[${dep}]`) || _.get(rootPkg, `devDependencies[${dep}]`) || _.get(rootPkg, `optionalDependencies[${dep}]`);
if (!acc[dep]) {
try {
acc[dep] = require(`./packages/node_modules/${dep}/package.json`).version;
}
catch (err) {
// eslint-disable-next-line no-console
console.warn(err);
throw new Error(`Failed to determine version for ${dep}, Is it missing from package.json?`);
}
}
return acc;
}, {}));
const assignDeps = _.curry((pkg, versionedDeps) => {
pkg.dependencies = versionedDeps;
});
/**
* Finds all the entry points (pkg.main, pkg.bin[], pkg.browser{}, etc) for a
* given package
* @param {Package} pkg
* @param {string} pkgPath
* @returns {Array<string>}
*/
const findEntryPoints = _.curry((pkg, pkgPath) => {
try {
let paths = [];
if (pkg.main) {
paths.push(path.resolve(path.dirname(pkgPath), pkg.main));
}
if (paths.length === 0) {
try {
const p = path.resolve(path.dirname(pkgPath), 'index.js');
fs.statSync(p);
}
catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
}
if (pkg.bin) {
paths = paths.concat(_.values(pkg.bin));
}
if (pkg.browser) {
paths = paths.concat(_.values(pkg.browser).filter((p) => p && !p.startsWith('@')));
}
return paths;
}
catch (err) {
if (err.code === 'EISDIR') {
return findEntryPoints(pkg, path.join(pkgPath, 'package.json'));
}
throw err;
}
});
const visited = new Set();
/**
* Walks the require tree beginning with the file at filePath
* @param {string} filePath
* @returns {Array<string>}
*/
const findRequires = _.curry(function findRequires(filePath) {
if (visited.has(filePath)) {
return [];
}
try {
visited.add(filePath);
debug(`finding requires for ${filePath}`);
const requires = detective(fs.readFileSync(filePath));
debug(util.inspect(requires, {depth: null}));
return requires.reduce((acc, dep) => {
debug(`checking ${dep}`);
if (dep.startsWith('.')) {
debug(`descending into ${dep}`);
acc = acc.concat(findRequires(path.resolve(path.dirname(filePath), dep)));
}
else {
debug(`${dep} is a dependency for ${filePath}`);
acc.push(dep);
}
return acc;
}, []);
}
catch (err) {
if (err.code === 'EISDIR') {
return findRequires(path.resolve(filePath, 'index.js'));
}
if (err.code === 'ENOENT' && !filePath.endsWith('.js')) {
return findRequires(`${filePath}.js`);
}
throw err;
}
});
const entryPointsToRequires = _.curry((pkgPath, entryPoints) => entryPoints.reduce((acc, entryPoint) => acc.concat(findRequires(path.resolve(path.dirname(pkgPath), entryPoint))), []));
/**
* Turns requires into package names and filters out non-unique entries
* @param {Array<string>} requires
* @returns {Array<string>}
*/
const requiresToDeps = _.curry((requires) => _.uniq(requires.map((d) => {
// The following block makes sure the dep is a package name and not a file
// reference. Given a require of `@scope/foo/bar/baz`, the following will
// return `@scope/foo`. Given a require of `foo/bar/baz`, the folling will
// return `foo`.
d = d.split('/');
if (d[0].startsWith('@')) {
return d.slice(0, 2).join('/');
}
return d[0];
})));
const filterBrowserifyTransforms = _.curry((defaults, filtered, pkg) => {
const transforms = _.get(pkg, 'browserify.transform', [])
.reduce((acc, tx) => {
if (_.isArray(tx)) {
tx = tx[0];
}
if (!filtered.includes(tx)) {
acc.push(tx);
}
return acc;
}, []);
_.set(pkg, 'browserify.transform', _.uniq(transforms.concat(defaults)));
return pkg;
});
/**
* Reads the packages list of browserify transforms and adds all as
* dependencies, except for those in FILTERED_TRANSFORMS
* @param {Package} pkg
* @param {Array<string>} requires
* @returns {Array<string>}
*/
const appendBrowserifyTransforms = _.curry((pkg, requires) => requires.concat(pkg.browserify.transform.reduce((acc, tx) => {
if (_.isArray(tx)) {
tx = tx[0];
}
acc.push(tx);
return acc;
}, [])));
const writeFile = _.curry((pkgPath, pkg) => {
if (!pkg) {
throw new Error(`Cannot write empty pkg to ${pkgPath}`);
}
fs.writeFileSync(pkgPath, `${JSON.stringify(pkg, null, 2)}\n`);
});
function findPkgPath(pkgPath) {
if (pkgPath.endsWith('package.json')) {
return pkgPath;
}
try {
const filePath = `${pkgPath}.json`;
fs.statSync(filePath);
return filePath;
}
catch (err) {
const dirPath = path.resolve(pkgPath, 'package.json');
fs.statSync(dirPath);
return dirPath;
}
}
/**
* Modifies a single package.json
* @param {string} rootPkgPath
* @param {string} pkgPath
* @returns {Promise}
*/
const updateSinglePackage = _.curry((rootPkgPath, pkgPath) => {
debug(`\n\nFinding dependencies for package ${pkgPath}\n\n`);
rootPkgPath = findPkgPath(rootPkgPath);
pkgPath = findPkgPath(pkgPath);
const rootPkg = require(rootPkgPath);
const pkg = require(pkgPath);
return Promise.resolve(filterBrowserifyTransforms(DEFAULT_TRANSFORMS, FILTERED_TRANSFORMS, pkg))
.then(() => findEntryPoints(pkg, pkgPath))
.then(entryPointsToRequires(pkgPath))
.then(requiresToDeps)
.then(appendBrowserifyTransforms(pkg))
.then(depsToVersions(rootPkg))
.then(assignDeps(pkg))
.then(() => writeFile(pkgPath, pkg))
.catch((err) => {
debug(`failed at package ${pkgPath}`);
throw err;
});
});
/**
* Locates all packages below the specified directory
* @param {string} packagesPath
* @returns {Array<string>}
*/
function findPackages(packagesPath) {
return fs.readdirSync(packagesPath).reduce((acc, d) => {
const fullpath = path.resolve(packagesPath, d);
if (fs.statSync(fullpath).isDirectory()) {
try {
fs.statSync(path.resolve(fullpath, 'package.json'));
acc.push(fullpath);
}
catch (err) {
if (err.code === 'ENOENT') {
return acc.concat(findPackages(fullpath));
}
throw err;
}
}
return acc;
}, []);
}
/**
* Transforms all packages
* @param {string} rootPkgPath
* @param {string} packagesPath
* @returns {Promise}
*/
function updateAllPackages(rootPkgPath, packagesPath) {
const paths = findPackages(packagesPath);
return paths.reduce((promise, pkgPath) => promise.then(() => updateSinglePackage(rootPkgPath, pkgPath)), Promise.resolve());
}
if (require.main === module) {
if (process.argv[2] === '--help') {
// eslint-disable-next-line no-console
console.log();
// eslint-disable-next-line no-console
console.log('usage: node deps.js [packagepath]');
// eslint-disable-next-line no-console
console.log();
// eslint-disable-next-line no-console
console.log(`update dependency lists for all packages in ${__dirname}/packages/node_modules`);
// eslint-disable-next-line no-console
console.log('\tnode deps.js');
// eslint-disable-next-line no-console
console.log();
// eslint-disable-next-line no-console
console.log('update dependency list for single package "webex"');
// eslint-disable-next-line no-console
console.log('\tnode deps.js ./packages/node_modules/webex');
// eslint-disable-next-line no-console
console.log();
// eslint-disable-next-line no-process-exit
process.exit(0);
}
const rootPkgPath = path.resolve(process.cwd(), 'package.json');
let p;
if (process.argv[2]) {
p = updateSinglePackage(rootPkgPath, process.argv[2]);
}
else {
p = updateAllPackages(rootPkgPath, path.resolve(process.cwd(), './packages/node_modules'));
}
p.catch((err) => {
// eslint-disable-next-line no-console
console.error(err);
// eslint-disable-next-line no-console
console.error(err.stack);
// eslint-disable-next-line no-process-exit
process.exit(64);
});
}
else {
module.exports = {
depsToVersions,
assignDeps,
findEntryPoints,
findRequires,
entryPointsToRequires,
requiresToDeps,
appendBrowserifyTransforms,
writeFile,
updateSinglePackage,
updateAllPackages
};
}