1 |
|
2 | (function() {
|
3 | var path;
|
4 |
|
5 | path = require('path');
|
6 |
|
7 | module.exports = function(opts) {
|
8 | var autocode, cwd, i, runCmd, scripts, spawn;
|
9 | autocode = this;
|
10 | spawn = require('cross-spawn');
|
11 | this.build(opts);
|
12 | if (!this.config.scripts || !this.config.scripts.run) {
|
13 | return "\n" + ' DONE! '.bgGreen.white;
|
14 | }
|
15 | cwd = this.path;
|
16 | console.log("\nRUN:".bold);
|
17 | scripts = this.config.scripts;
|
18 | i = 0;
|
19 | runCmd = function() {
|
20 | var command, description, dir, proc;
|
21 | if (!scripts.run[i]) {
|
22 | console.log("\n" + ' DONE! '.bgGreen.white);
|
23 | return;
|
24 | }
|
25 | if (scripts.run[i].title && !scripts.run[i].command) {
|
26 | console.log(' RUN TITLE '.bgGreen.white + (' ' + scripts.run[i].title + ' ').bgWhite + " \n");
|
27 | i++;
|
28 | runCmd();
|
29 | return;
|
30 | }
|
31 | description = scripts.run[i].description || scripts.run[i];
|
32 | command = scripts.run[i].command || scripts.run[i];
|
33 | if (scripts.run[i].path) {
|
34 | if (scripts.run[i].path.match(/^\//)) {
|
35 | dir = scripts.run[i].path;
|
36 | } else {
|
37 | dir = cwd + "/" + scripts.run[i].path;
|
38 | }
|
39 | dir = path.normalize(dir);
|
40 | } else {
|
41 | dir = cwd;
|
42 | }
|
43 | console.log(' RUN SCRIPT '.bgGreen.white + (' ' + description + ' ').bgWhite + " \n" + command.gray);
|
44 | proc = spawn('bash', ['-c', command], {
|
45 | cwd: dir
|
46 | });
|
47 | proc.on('close', function(err) {
|
48 | return runCmd();
|
49 | });
|
50 | proc.on('error', function(err) {
|
51 | return console.log("\n" + ' ERROR '.bgRed.white + (' ' + err.message + ' ').bgWhite);
|
52 | });
|
53 | return i++;
|
54 | };
|
55 | runCmd();
|
56 | return 'Running...';
|
57 | };
|
58 |
|
59 | }).call(this);
|