UNPKG

1.76 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 = 'node ' + 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 togglePostinstall = function(add) {
26 var pkg = require('./package.json');
27
28 if (add) {
29 if (!pkg.scripts) pkg.scripts = {};
30 pkg.scripts.postinstall = 'node setup.js test';
31 } else if (pkg.scripts && pkg.scripts.postinstall) {
32 delete pkg.scripts.postinstall;
33 }
34
35 fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n');
36};
37
38switch (mode) {
39 // Remove `.postinstall` script to prevent stupid npm bugs.
40 case 'prepublish':
41 togglePostinstall(false);
42 execute(getBinaryPath('coffee'), '-o lib/ src/');
43 break;
44
45 // Bring back `.postinstall` script.
46 case 'postpublish':
47 togglePostinstall(true);
48 break;
49
50 // Compile coffeescript for git users.
51 case 'postinstall':
52 fsExists(sysPath.join(__dirname, 'lib'), function(exists) {
53 if (exists) return;
54 execute(getBinaryPath('coffee'), '-o lib/ src/');
55 });
56 break;
57
58 // Run tests.
59 case 'test':
60 execute(
61 getBinaryPath('mocha'),
62 '--compilers coffee:coffee-script --require test/common.js --colors'
63 );
64 break;
65}