This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
/
setup.js
69 lines (57 loc) · 1.91 KB
/
setup.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
var exec = require('child_process').exec;
var sysPath = require('path');
var fs = require('fs');
// Cross-platform node.js postinstall & test script for coffeescript projects.
var mode = process.argv[2];
var fsExists = fs.exists || sysPath.exists;
var fsExistsSync = fs.existsSync || sysPath.existsSync;
var getBinaryPath = function(binary) {
var path;
if (fsExistsSync(path = sysPath.join('node_modules', '.bin', binary))) return path;
if (fsExistsSync(path = sysPath.join('..', '.bin', binary))) return path;
return binary;
};
var execute = function(path, params, callback) {
if (callback == null) callback = function() {};
var command = path + ' ' + params;
console.log('Executing', command);
exec(command, function(error, stdout, stderr) {
if (error != null) return process.stderr.write(stderr.toString());
console.log(stdout.toString());
});
};
var togglePostinstall = function(add) {
var pkg = require('./package.json');
if (add) {
if (!pkg.scripts) pkg.scripts = {};
pkg.scripts.postinstall = 'node setup.js postinstall';
} else if (pkg.scripts && pkg.scripts.postinstall) {
delete pkg.scripts.postinstall;
}
fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n');
};
switch (mode) {
// Remove `.postinstall` script to prevent stupid npm bugs.
case 'prepublish':
togglePostinstall(false);
execute(getBinaryPath('coffee'), '-o lib/ src/');
break;
// Bring back `.postinstall` script.
case 'postpublish':
togglePostinstall(true);
break;
// Compile coffeescript for git users.
case 'postinstall':
fsExists(sysPath.join(__dirname, 'lib'), function(exists) {
if (exists) return;
execute(getBinaryPath('coffee'), '-o lib/ src/');
});
break;
// Run tests.
case 'test':
execute(
getBinaryPath('mocha'),
'--compilers coffee:coffee-script --require test/common.coffee --colors'
);
break;
}