forked from oNaiPs/npm-install-changed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·192 lines (168 loc) · 5.29 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env node
/**
* Jose Pereira <[email protected]>
*
* Run this script ensures 'npm install' is only run if there are dependency
* changes between installations. It makes a huge difference in build times
* on build servers.
*
* Use --bower argument to do the same for 'bower install'
* Use --recursive argument to verify if dependency configs changed.
* Use --prune to remove unused dependencies after running install.
*/
'use strict';
var fs = require('fs');
var spawn = require('cross-spawn');
var crypto = require('crypto');
var path = require('path');
var hashFile = '.npm-install-changed.json';
var args = process.argv.slice(2);
var filteredArgs = args.filter(function(el){
return ['--recursive', '--bower', '--prune'].indexOf(el) === -1;
});
var recursive = args.indexOf('--recursive') >= 0;
var prune = args.indexOf('--prune') >= 0;
var bower = args.indexOf('--bower') >= 0;
var config = {};
var packager;
/**
* Returns checksum of passed string.
*/
function checksum(str, algorithm, encoding) {
return crypto
.createHash(algorithm || 'md5')
.update(str, 'utf8')
.digest(encoding || 'hex');
}
/**
* Returns component folder path for dir.
*/
function componentsFolder(dir) {
if (packager.bin === 'bower') {
var bowerComponents = path.join(dir, 'bower_components');
try {
var bowerRc = JSON.parse(fs.readFileSync(
path.join(dir, '.bowerrc')));
packager.componentsFolder = path.join(dir, bowerRc.directory);
} catch (e) {}
return bowerComponents;
} else if (packager.bin === 'npm') {
return path.join(dir, 'node_modules');
} else {
throw 'Packager not supported';
}
}
/**
* Returns directories inside components folder.
*/
function dependenciesFolders(dir) {
var components = componentsFolder(dir);
try {
return fs.readdirSync(components)
.filter(function(file) {
return fs.statSync(path.join(components, file)).isDirectory();
}).map(function(file) {
return path.join(components, file);
});
} catch (e) {
return [];
}
}
/**
* Returns a hash built from all package.json dependencies and devDependencies
* project names and versions.
*/
function configJsonDepsHash(configJsonDir) {
return new Promise(function(resolve) {
fs.readFile(path.join(configJsonDir, packager.configJson),
function(err, data) {
if (err) {
resolve('noconfig');
return;
}
var packageJson = JSON.parse(data);
var newHash = 'emptydeps';
['dependencies', 'devDependencies'].forEach(
function(
dep) {
for (var key in packageJson[dep]) {
newHash = checksum(newHash +
key +
packageJson[dep][key]);
}
});
if (recursive) {
var hashPromises = dependenciesFolders(
configJsonDir).map(
function(dir) {
return configJsonDepsHash(dir);
});
Promise.all(hashPromises).then(function(hashes) {
for (var i = 0; i < hashes.length; i++) {
newHash = checksum(newHash + hashes[i]);
}
resolve(newHash);
}).catch(function(err) {
console.log(err);
//there was some problem here, log it but continue.
resolve('exception');
});
} else {
resolve(newHash);
}
});
});
}
function spawnProcessAndHandleClose(action,hash,cb){
action = [].concat(action);
console.log('Running ' + packager.bin + ' ' + action.join(' ') + '...');
var process = spawn(packager.bin, [action], {
stdio: 'inherit'
});
process.on('close', function(code) {
if (code !== 0) {
return;
}
if(typeof cb === "function"){
cb(hash);
}
});
}
function postInstall(hash){
config[hashKey] = hash;
//only save new hash if packager install was successful
fs.writeFile(hashFile, JSON.stringify(config), (err) => {
if (err) {
console.log(err);
}
});
if(prune){
spawnProcessAndHandleClose('prune');
}
}
try {
var config = JSON.parse(fs.readFileSync(hashFile));
} catch (e) {}
//determine what packager are we targeting
if (bower) {
packager = {
bin: 'bower',
configJson: './bower.json'
};
} else {
packager = {
bin: 'npm',
configJson: './package.json'
};
}
var hashKey = [packager.bin + '-hash'];
configJsonDepsHash(process.cwd()).then(function(hash) {
if (hash === config[hashKey]) {
console.log('Nothing to do.');
return;
}
//looks like configJson dependencies have changed
spawnProcessAndHandleClose(['install'].concat(filteredArgs),hash,postInstall);
}).catch(function(err) {
console.log(err);
});