forked from ioBroker/ioBroker.js-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreinstall.js
157 lines (149 loc) · 5.73 KB
/
reinstall.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
'use strict';
// this script reads all iobroker packages from node_modules, deletes all and installs it anew
const fs = require('fs');
const path = require('path');
const exec = require('child_process').exec;
function getNodeModulesRoot() {
// special case
// iobroker
// |-iobroker.js-controller <---
// |-node_modules |
// |-iobroker.js-controller (as soft link | )
let testPath = path.normalize(path.join(__dirname, '/../node_modules/iobroker.js-controller/'));
if (fs.existsSync(testPath)) {
return path.normalize(path.join(__dirname, '/..'));
}
// iobroker
// |-node_modules
// |-iobroker.js-controller
testPath = path.normalize(path.join(__dirname, '/../../node_modules/iobroker.js-controller/'));
if (fs.existsSync(testPath)) {
return path.normalize(path.join(__dirname, '/../..'));
}
return __dirname;
}
const defaultPack = {
name: 'iobroker.core',
version: '1.0.0',
description: 'Build automatically by reinstall.js',
private: true,
dependencies: {}
};
function savePackages(root) {
root = root || getNodeModulesRoot();
if (path.join(root, 'reinstall.js') !== path.join(__dirname, 'reinstall.js') && fs.existsSync(path.join(__dirname, 'reinstall.js'))) {
fs.writeFileSync(path.join(root, 'reinstall.js'), fs.readFileSync(path.join(__dirname, 'reinstall.js')));
}
if (!fs.existsSync(root + '/node_modules')) {
console.log('no node_modules found. Try to use actual package.json');
return;
}
let dirs = fs.readdirSync(root + '/node_modules');
let newPack = JSON.parse(JSON.stringify(defaultPack));
dirs = dirs.filter(dir => dir.match(/^iobroker\.?/i));
const deps = dirs.map(dir => {
const fileName = root + '/node_modules/' + dir + '/package.json';
try {
const pack = JSON.parse(fs.readFileSync(fileName).toString());
return {name: pack.name, version: pack.version};
} catch (e) {
console.error(`Cannot read or parse ${fileName}: ${e}`);
}
return null;
}).filter(e => e);
deps.forEach(pack => {
newPack.dependencies[pack.name] = pack.version;
});
if (fs.existsSync(root + '/package.json')) {
let actual = require(root + '/package.json');
actual.dependencies = actual.dependencies || {};
for (const pack in newPack.dependencies) {
if (newPack.dependencies.hasOwnProperty(pack) && (!actual.dependencies[pack] || !actual.dependencies[pack].match(/^file:/)) ) {
actual.dependencies[pack] = newPack.dependencies[pack];
}
}
newPack = actual;
newPack.description = `Updated by reinstall.js on ${new Date().toISOString()}`;
}
fs.writeFileSync(root + '/package.json', JSON.stringify(newPack, null, 2));
}
function deleteFolderSync(dir, level) {
level = level || 0;
if (fs.existsSync(dir)) {
const dirs = fs.readdirSync(dir);
dirs.forEach((file, i) => {
const newPath = path.join(dir, file);
if (fs.lstatSync(newPath).isDirectory()) { // recurse
deleteFolderSync(newPath, level + 1);
!level && console.log(`[${i} / ${dirs.length}] Deleted ${file}`);
} else { // delete file
fs.unlinkSync(newPath);
}
});
fs.rmdirSync(dir);
}
}
function runNpm(root) {
root = root || getNodeModulesRoot();
return new Promise((resolve, reject) => {
console.log('\n\n\nReinstall all packages cache...');
const child = exec('npm install --production', {cwd: root});
child.stderr.pipe(process.stdout);
child.stdout.pipe(process.stdout);
child.on('exit', (code /* , signal */) => {
// code 1 is strange error that cannot be explained. Everything is installed but error :(
if (code && code !== 1) {
console.error('Error ' + code);
return reject(code);
}
resolve();
});
child.on('error', error => {
console.error('Error: ' + error);
reject(error);
});
});
}
function runNpmClean() {
return new Promise((resolve, reject) => {
console.log('\n\n\nCleaning npm cache...');
const child = exec('npm cache clean -f');
child.stderr.pipe(process.stdout);
child.stdout.pipe(process.stdout);
child.on('exit', (code /* , signal */) => {
// code 1 is strange error that cannot be explained. Everything is installed but error :(
if (code && code !== 1) {
console.error('Error ' + code);
return reject(code);
}
resolve();
});
child.on('error', error => {
console.error('Error: ' + error);
reject(error);
});
});
}
let hadErrors;
function doAll() {
const root = getNodeModulesRoot();
savePackages(root);
let start = Date.now();
deleteFolderSync(path.join(root, 'node_modules'));
console.log(`Deleted in ${Math.round((Date.now() - start) / 1000)} seconds.`);
runNpmClean()
.then(() => {
start = Date.now();
return runNpm(root);
})
.then(() => {
console.log('\n\n\nEverything is done');
}).catch(e => {
console.error('Cannot reinstall all packages: ' + e);
hadErrors = 1;
}).then(() => {
console.log(`\n\n\nFinished in ${Math.round((Date.now() - start) / 1000)} seconds.`);
process.exit(hadErrors);
});
}
doAll();