postinstall.js
1.3 KB
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
'use strict';
function onError(err) {
try {
var str = '' + (err ? (err.stack || err.message || err) : 'null');
require('fs').writeFileSync(__dirname + '/error.log', str);
} catch (ex) {
}
}
try {
var fs = require('fs');
var cp = require('child_process');
var REQUIRES_UPDATE = false;
var pkg = JSON.parse(fs.readFileSync(__dirname + '/package.json', 'utf8'));
if (cp.spawnSync || __dirname.indexOf('node_modules') === -1) {
if(pkg.dependencies['try-thread-sleep']){
delete pkg.dependencies['try-thread-sleep'];
REQUIRES_UPDATE = true;
}
} else {
if(!pkg.dependencies['try-thread-sleep']){
pkg.dependencies['try-thread-sleep'] = "^1.0.0";
REQUIRES_UPDATE = true;
console.log('Installing native dependencies (this may take up to a minute)');
}
}
if (REQUIRES_UPDATE && __dirname.indexOf('node_modules') !== -1) {
fs.writeFileSync(__dirname + '/package.json', JSON.stringify(pkg, null, ' '));
cp.exec((process.env.npm_execpath ? ('"' + process.argv[0] + '" "' + process.env.npm_execpath + '"') : 'npm') +
' install --production', {
cwd: __dirname
}, function (err) {
if (err) onError(err);
process.exit(0);
});
setTimeout(function () {
process.exit(0);
}, 60000);
}
} catch (ex) {
onError(ex);
}