-
Notifications
You must be signed in to change notification settings - Fork 2
/
lerna-semver-sync.js
170 lines (144 loc) · 5.19 KB
/
lerna-semver-sync.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
const cwd = process.cwd();
const forEach = require('lodash/forEach');
const fs = require('fs');
const glob = require('glob');
const intersect = require('semver-intersect').intersect;
const reduce = require('lodash/reduce');
const semver = require('semver');
function applyCommonRange (dependencies, name, commonRange) {
const range = dependencies[name];
const newRange = range
.split('||')
.map(range => range.trim())
.map(range => {
if (isExactRange(range)) {
return range;
}
const majorVersion = getMajorVersion(range);
return commonRange[majorVersion];
})
.join(' || ');
if (range !== newRange) {
dependencies[name] = newRange;
return true;
}
}
function applyCommonRanges (packagePaths, commonRanges) {
return packagePaths.reduce((modifiedPackages, packagePath) => {
let modified = false;
let pkg = getPackage(packagePath);
let dependencies = pkg.dependencies || {};
let devDependencies = pkg.devDependencies || {};
Object.keys(dependencies || {}).forEach(name => {
const commonRange = commonRanges[name];
const didModify = applyCommonRange(dependencies, name, commonRange);
modified = didModify || modified;
});
Object.keys(devDependencies || {}).forEach(name => {
const commonRange = commonRanges[name];
const didModify = applyCommonRange(devDependencies, name, commonRange);
modified = didModify || modified;
});
if (modified) {
modifiedPackages.push({ packagePath, pkg });
}
return modifiedPackages;
}, []);
}
function getAllDependencies (packagePaths) {
return packagePaths.reduce((result, packagePath) => {
const { dependencies, devDependencies } = getPackage(packagePath);
forEach(dependencies, (range, name) => {
if (semver.validRange(range)) {
upsert(result, name, range);
}
});
forEach(devDependencies, (range, name) => {
if (semver.validRange(range)) {
upsert(result, name, range);
}
});
return result;
}, {});
}
function getCommonRange (compoundRanges) {
const majorVersions = compoundRanges.reduce((result, compoundRange) => {
compoundRange.split('||').map(range => range.trim()).forEach(range => {
if (isExactRange(range)) {
return;
}
const majorVersion = getMajorVersion(range);
upsert(result, majorVersion, range);
});
return result;
}, {});
forEach(majorVersions, (ranges, majorVersion) => {
try {
majorVersions[majorVersion] = intersect(...ranges);
} catch(e) {
// bypass the error happened in intersect function
// let the `duplicates` object return by Sync funtion handle the warning
}
});
return majorVersions;
}
function getCommonRanges (allDependencies) {
return reduce(allDependencies, (result, ranges, name) => {
result[name] = getCommonRange(ranges);
return result;
}, {});
}
function getMajorVersion (range) {
return /(\d+)/.exec(range)[0];
}
function getPackage (packagePath) {
const packageText = fs.readFileSync(packagePath, 'utf-8');
const packageJson = JSON.parse(packageText);
return packageJson;
}
function isExactRange (range) {
return /^[\d]+\.[\d]+\.[\d]+/.test(range);
}
function sync(opts = {}) {
const { pattern = 'packages/*/package.json' } = opts;
const packagePaths = glob.sync(pattern, { cwd }).concat('package.json');
const allDependencies = getAllDependencies(packagePaths);
const commonRanges = getCommonRanges(allDependencies);
const modifiedPackages = applyCommonRanges(packagePaths, commonRanges);
modifiedPackages.forEach(({ packagePath, pkg }) => writePackage(packagePath, pkg));
const duplicates = reduce(allDependencies, (result, versions, name) => {
if (versions.length > 1) {
result[name] = versions;
}
return result;
}, {});
return duplicates;
}
function upsert (obj, key, value) {
if (!obj[key]) {
obj[key] = [];
}
if (!obj[key].includes(value)) {
obj[key].push(value);
}
return obj;
}
function writePackage (packagePath, pkg) {
const packageText = fs.readFileSync(packagePath, 'utf-8');
const eof = /\}(?=[^}]*$)([\s]*)/.exec(packageText)[1];
const indentation = /{\n(\s+)/.exec(packageText)[1];
const pkgJson = JSON.stringify(pkg, null, indentation) + eof;
fs.writeFileSync(packagePath, pkgJson, 'utf-8');
}
module.exports.default = sync;
module.exports.applyCommonRange = applyCommonRange;
module.exports.applyCommonRanges = applyCommonRanges;
module.exports.getAllDependencies = getAllDependencies;
module.exports.getCommonRange = getCommonRange;
module.exports.getCommonRanges = getCommonRanges;
module.exports.getMajorVersion = getMajorVersion;
module.exports.getPackage = getPackage;
module.exports.isExactRange = isExactRange;
module.exports.sync = sync;
module.exports.upsert = upsert;
module.exports.writePackage = writePackage;