-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync-file-deps.js
141 lines (125 loc) · 4.37 KB
/
sync-file-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
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
const fs = require('fs');
const cp = require('child_process');
const chokidar = require('chokidar');
const {dynaNodeArguments} = require("dyna-node-arguments");
const mode =
dynaNodeArguments.args.watch === ''
? 'WATCH'
: 'ONCE';
const UPDATE_PACKAGE_DEPENDENCIES = true;
const UPDATE_PACKAGE_DEV_DEPENDENCIES = false;
// core utils
const hasPathSlash = folder => folder.split('/').reverse()[0] === '';
const loadJson = filename => JSON.parse(fs.readFileSync(filename, 'utf8'));
const saveJson = (filename, data) => fs.writeFileSync(filename, JSON.stringify(data, null, 2), 'utf8');
const package_ = loadJson('./package.json');
console.log(`${package_.name}: mode: ${mode} Scanning file dependencies...`);
const syncDeps =
[]
.concat(
Object.keys(package_.dependencies || {}).map(dep => ({dependencyName: dep, resourceFolder: package_.dependencies[dep]})),
Object.keys(package_.devDependencies || {}).map(dep => ({dependencyName: dep, resourceFolder: package_.devDependencies[dep]})),
)
.filter(dep => dep.resourceFolder.indexOf('file:') === 0)
.map(dep => {
dep.resourceFolder = dep.resourceFolder.substr(5); // remove the "file:"
if (!hasPathSlash(dep.resourceFolder)) dep.resourceFolder += '/';
return dep;
})
if (syncDeps.length) {
console.log('Scanning file dependencies');
syncDeps.forEach(d => console.log(' ', d.dependencyName));
} else {
if (mode === "WATCH") {
console.error('Error: No file dependencies found to watch');
process.exit(100);
}
console.error('Info: No file dependencies found to sync');
return;
}
if (mode === 'WATCH') {
syncDeps
.forEach(dep => {
console.log(`Watching changes for ${dep.dependencyName}`, `../${dep.dependencyName}/*`);
let bounceTimer = null;
chokidar
.watch(
`../${dep.dependencyName}`,
{
ignored: path => path.includes('node_modules'),
},
// {ignored: /node_modules|(^|[\/\\])\../},
)
.on(
'all',
(event, path) => {
// console.log('Changed dependency', dep.dependencyName, event, path);
if (bounceTimer) clearTimeout(bounceTimer);
bounceTimer = setTimeout(() => {
bounceTimer = null;
syncDependency(dep);
}, 100);
}
);
});
}
if (mode === 'ONCE') {
setTimeout(()=>{
syncDeps
.forEach(syncDependency);
})
}
const syncDependency = dep => {
const updateScript =
[
`rm -rf ./node_modules/${dep.dependencyName}`,
`mkdir ./node_modules/${dep.dependencyName}`,
`rsync -av --progress ${dep.resourceFolder} ./node_modules/${dep.dependencyName} --exclude node_modules`,
].join('; ');
execScript(`Syncing dependency ${dep.dependencyName}`, updateScript).catch(() => undefined);
updateDeps(dep.dependencyName);
};
const execScript = (description, cli) =>
new Promise((resolve, reject) => {
console.log(description, 'syncing...');
const started = Date.now();
cp.exec(
cli,
{},
(error, stdout, stderr) => {
// if (stdout.toString()) console.log('info', stdout.toString());
if (stderr.toString()) console.log('errors', stderr.toString());
if (error) {
console.error(description, 'FAILED', error);
reject({message: `execScript`, cli, error});
return;
}
console.log(description, `syncing... completed (in ${Date.now() - started}ms)`);
resolve();
});
});
const updateDeps = dependencyName => {
const dependencyPackage = loadJson(`../${dependencyName}/package.json`);
let changed = false;
if (UPDATE_PACKAGE_DEPENDENCIES) {
if (!package_.dependencies) package_.dependencies = {};
Object.keys(dependencyPackage.dependencies)
.forEach(dep => {
if (package_.dependencies[dep] === undefined) {
changed = true;
package_.dependencies[dep] = dependencyPackage.dependencies[dep];
}
});
}
if (UPDATE_PACKAGE_DEV_DEPENDENCIES) {
if (!package_.devDependencies) package_.devDependencies = {};
Object.keys(dependencyPackage.devDependencies)
.forEach(dep => {
if (package_.devDependencies[dep] === undefined) {
changed = true;
package_.devDependencies[dep] = dependencyPackage.devDependencies[dep];
}
});
}
if (changed) saveJson('./package.json', package_);
};