-
Notifications
You must be signed in to change notification settings - Fork 6
/
merge.js
51 lines (48 loc) · 1.53 KB
/
merge.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
/*
// find the package.json from the current folder
// find package.json from all descendent folders
node merge [path]
./package.json
./genes
/[Address1]
package.json
/[Address2]
package.json
merge Address1/package.json and Address2/package.json into ./package.json
*/
const fs = require('fs')
const path = require('path')
if (process.argv.length === 3) {
let currentPath = process.argv[2]
let childPath = currentPath + "/genes"
let parentFilename = currentPath + "/package.json"
let files = fs.readdirSync(childPath)
if (files && files.length > 0) {
let _parent = require(parentFilename)
console.log("parent BEFORE override...")
let before_parent = JSON.stringify(_parent, null, 2)
console.log(before_parent)
files.forEach(function(file) {
let fullPath = path.join(childPath, file);
if (fs.lstatSync(fullPath).isDirectory()) {
console.log(fullPath);
if (fs.existsSync(fullPath)) {
let p = fullPath + "/package.json"
console.log("opening", p)
let _child = require(p)
if (_child.dependencies) {
console.log("merging dependencies", _child.dependencies)
// overwrite dependencies
Object.keys(_child.dependencies).forEach(function(key) {
_parent.dependencies[key] = _child.dependencies[key]
})
}
}
}
});
console.log("parent AFTER override...")
let res = JSON.stringify(_parent, null, 2)
console.log(res)
fs.writeFileSync(parentFilename, res)
}
}