-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
76 lines (68 loc) · 2.31 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
const {loadPackages, iter, fs} = require('lerna-script'),
_ = require('lodash'),
deepKeys = require('deep-keys')
function syncModulesTask({packages, transformDependencies, transformPeerDependencies} = {}) {
return async log => {
const {loadedPackages, transformDeps, transformPeerDeps} = await providedOrDefaults({
packages,
transformDependencies,
transformPeerDependencies
})
log.info('modules', `syncing module versions for ${loadedPackages.length} packages`)
const modulesAndVersions = toModulesAndVersion(loadedPackages, transformDeps)
const modulesAndPeerVersions = toModulesAndVersion(loadedPackages, transformPeerDeps)
return iter.parallel(loadedPackages, {log})((lernaPackage, log) => {
const logMerged = input =>
log.info(
'modules',
`${lernaPackage.name}: ${input.key} (${input.currentValue} -> ${input.newValue})`
)
return fs
.readFile(lernaPackage)('package.json', JSON.parse)
.then(packageJson =>
merge(
packageJson,
{
dependencies: modulesAndVersions,
devDependencies: modulesAndVersions,
peerDependencies: modulesAndPeerVersions
},
logMerged
)
)
.then(packageJson => fs.writeFile(lernaPackage)('package.json', packageJson))
})
}
}
async function providedOrDefaults({
packages,
transformDependencies,
transformPeerDependencies
} = {}) {
return {
loadedPackages: await (packages || loadPackages()),
transformDeps: transformDependencies || (version => `~${version}`),
transformPeerDeps: transformPeerDependencies || (version => `>=${version}`)
}
}
function toModulesAndVersion(modules, mutateVersion) {
return modules.reduce((acc, val) => {
acc[val.name] = mutateVersion(val.version)
return acc
}, {})
}
function merge(dest, source, onMerged = _.noop) {
const destKeys = deepKeys(dest)
const sourceKeys = deepKeys(source)
const sharedKeys = _.intersection(destKeys, sourceKeys)
sharedKeys.forEach(key => {
const currentValue = _.get(dest, key)
const newValue = _.get(source, key)
if (currentValue !== newValue) {
_.set(dest, key, newValue)
onMerged({key, currentValue, newValue})
}
})
return dest
}
module.exports = syncModulesTask