UNPKG

1.81 kBJavaScriptView Raw
1var exec = require('child_process').exec;
2var sysPath = require('path');
3var fs = require('fs');
4
5// Cross-platform node.js postinstall & test script for coffeescript projects.
6
7var mode = process.argv[2];
8
9var fsExists = fs.exists || sysPath.exists;
10
11var getBinaryPath = function(binary) {
12 return sysPath.join('node_modules', '.bin', binary);
13};
14
15var execute = function(path, params, callback) {
16 if (callback == null) callback = function() {};
17 var command = path + ' ' + params;
18 console.log('Executing', command);
19 exec(command, function(error, stdout, stderr) {
20 if (error != null) return process.stderr.write(stderr.toString());
21 console.log(stdout.toString());
22 });
23};
24
25var execNode = function(path, params, callback) {
26 execute('node ' + getBinaryPath(path), params, callback);
27};
28
29var togglePostinstall = function(add) {
30 var pkg = require('./package.json');
31
32 if (add) {
33 if (!pkg.scripts) pkg.scripts = {};
34 pkg.scripts.postinstall = 'node setup.js test';
35 } else if (pkg.scripts && pkg.scripts.postinstall) {
36 delete pkg.scripts.postinstall;
37 }
38
39 fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n');
40};
41
42switch (mode) {
43 // Remove `.postinstall` script to prevent stupid npm bugs.
44 case 'prepublish':
45 togglePostinstall(false);
46 execute('coffee', '-o lib/ src/');
47 break;
48
49 // Bring back `.postinstall` script.
50 case 'postpublish':
51 togglePostinstall(true);
52 break;
53
54 // Compile coffeescript for git users.
55 case 'postinstall':
56 fsExists(sysPath.join(__dirname, 'lib'), function(exists) {
57 if (exists) return;
58 execute('coffee', '-o lib/ src/');
59 });
60 break;
61
62 // Run tests.
63 case 'test':
64 execNode('mocha',
65 '--compilers coffee:coffee-script --require test/common.js --colors'
66 );
67 break;
68}