1 | var exec = require('child_process').exec;
|
2 | var sysPath = require('path');
|
3 | var fs = require('fs');
|
4 |
|
5 |
|
6 |
|
7 | var mode = process.argv[2];
|
8 |
|
9 | var fsExists = fs.exists || sysPath.exists;
|
10 |
|
11 | var getBinaryPath = function(binary) {
|
12 | return sysPath.join('node_modules', '.bin', binary);
|
13 | };
|
14 |
|
15 | var 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 |
|
25 | var 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 |
|
38 | switch (mode) {
|
39 |
|
40 | case 'prepublish':
|
41 | togglePostinstall(false);
|
42 | execute(getBinaryPath('coffee'), '-o lib/ src/');
|
43 | break;
|
44 |
|
45 |
|
46 | case 'postpublish':
|
47 | togglePostinstall(true);
|
48 | break;
|
49 |
|
50 |
|
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 |
|
59 | case 'test':
|
60 | execute(
|
61 | getBinaryPath('mocha'),
|
62 | '--compilers coffee:coffee-script --require test/common.js --colors'
|
63 | );
|
64 | break;
|
65 | }
|