This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpre-build.js
49 lines (42 loc) · 1.61 KB
/
pre-build.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
const fs = require('fs')
const path = require('path')
const walkPath = './'
const topLevelDirectories = fs.readdirSync('./')
.filter(file => !file.startsWith('.'))
.filter(file => fs.lstatSync(path.join(walkPath, file)).isDirectory())
.filter(file => file !== 'node_modules')
.map(dir => path.join(walkPath, dir))
const collectDependencies = (file) => {
const packageFile = fs.readFileSync(path.join(file, 'package.json'))
return {
location: file,
dependencies: JSON.parse(packageFile).dependencies
}
}
const dependencies = topLevelDirectories.map(collectDependencies)
.map(each => {
const dependenciesBlock = each.dependencies
const location = each.location
return Object.keys(dependenciesBlock).map(key => {
const version = dependenciesBlock[key]
return {
name: key,
version: version,
location
}
})
}).reduce((accumulator, current) => {
return accumulator.concat(current)
}, [])
.reduce((accumulator, current) => {
const cached = accumulator[current.name]
if (cached && cached !== current.version) {
console.log('conflict in', current.location, 'with', current.name, 'using:', cached, 'instead of', current.version)
console.log('\n')
return accumulator
}
return accumulator[current.name] = current.version, accumulator
}, {})
const packageJson = require('./package.json')
packageJson.dependencies = dependencies
fs.writeFileSync('./package.json', JSON.stringify(packageJson, null, 4))