forked from webex/react-widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeps.js
115 lines (93 loc) · 2.8 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
const {readFileSync, writeFileSync} = require('fs');
const path = require('path');
const glob = require('glob');
const detective = require('detective-es6');
const builtinModules = require('builtin-modules');
const {uniq} = require('lodash');
const {getAllPackages, getAllPackagePaths} = require('./package');
const flatten = (arr) => arr.reduce(
(acc, val) => acc.concat(
Array.isArray(val) ? flatten(val) : val
),
[]
);
function updatePackageJson(pkgPath, packages, topPkgJson) {
let outputPackages = packages;
if (!outputPackages) {
outputPackages = getAllPackages();
}
let outputTopPkgJson = topPkgJson;
if (!outputTopPkgJson) {
outputTopPkgJson = JSON.parse(readFileSync('./package.json', 'utf8'));
}
const pkgJsonPath = path.join(pkgPath, 'package.json');
const pkgJson = JSON.parse(readFileSync(pkgJsonPath, 'utf8'));
// for the dependencies, find all require() calls
const srcFiles = glob.sync(path.join(pkgPath, 'src/**/*.js'), {
ignore: [
'**/*.test.js',
'**/__mocks__/*.js',
'**/__fixtures__/*.js',
'**/react-test-utils/**/*.js'
]
});
const uniqDeps = uniq(
flatten(
srcFiles.map(
(srcFile) => {
const code = readFileSync(srcFile, 'utf8');
try {
return detective(code);
}
catch (e) {
return [];
}
}
)
)
)
.filter((dep) =>
// built in modules
builtinModules.indexOf(dep) === -1
// react-intl locale imports
&& !dep.includes('react-intl/locale-data')
// local references
&& dep[0] !== '.')
.sort();
pkgJson.dependencies = pkgJson.dependencies || {};
const deps = pkgJson.dependencies;
uniqDeps.forEach((dep) => {
const depArray = dep.split('/');
let cleanDep = depArray[0];
if (depArray[0].startsWith('@')) {
cleanDep = depArray.slice(0, 2).join('/');
}
if (outputTopPkgJson.dependencies[cleanDep]) {
deps[cleanDep] = outputTopPkgJson.dependencies[cleanDep];
}
else if (outputPackages.indexOf(cleanDep) !== -1) {
deps[cleanDep] = outputTopPkgJson.version;
}
else {
throw new Error(`Unknown dependency ${cleanDep}`);
}
});
pkgJson.version = outputTopPkgJson.version;
const jsonString = `${JSON.stringify(pkgJson, null, 2)}\n`;
writeFileSync(pkgJsonPath, jsonString, 'utf8');
}
/**
* Updates all package.json files with depedencies
* @returns {undefined}
*/
function updateAllPackageJson() {
const packages = getAllPackages();
const pkgPaths = getAllPackagePaths();
const topPkgJson = JSON.parse(readFileSync('./package.json', 'utf8'));
console.log(packages);
pkgPaths.forEach((pkgPath) => updatePackageJson(pkgPath, packages, topPkgJson));
}
module.exports = {
updateAllPackageJson,
updatePackageJson
};