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 = 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 execNode = function(path, params, callback) {
|
26 | execute('node ' + getBinaryPath(path), params, callback);
|
27 | };
|
28 |
|
29 | var 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 |
|
42 | switch (mode) {
|
43 |
|
44 | case 'prepublish':
|
45 | togglePostinstall(false);
|
46 | execute('coffee', '-o lib/ src/');
|
47 | break;
|
48 |
|
49 |
|
50 | case 'postpublish':
|
51 | togglePostinstall(true);
|
52 | break;
|
53 |
|
54 |
|
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 |
|
63 | case 'test':
|
64 | execNode('mocha',
|
65 | '--compilers coffee:coffee-script --require test/common.js --colors'
|
66 | );
|
67 | break;
|
68 | }
|