UNPKG

3.61 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.10.0
2(function() {
3 var changecase, colors, cson, fs, mkdirp, mustache, path, pluralize, spawn;
4
5 changecase = require('change-case');
6
7 colors = require('colors');
8
9 cson = require('season');
10
11 fs = require('fs');
12
13 mkdirp = require('mkdirp');
14
15 mustache = require('mustache');
16
17 path = require('path');
18
19 pluralize = require('pluralize');
20
21 spawn = require('cross-spawn');
22
23 exports.generate = require('./generate');
24
25 module.exports = function(opts) {
26 var buildCmd, code, cwd, i, scripts;
27 opts = opts || {};
28 this.path = (function() {
29 switch (true) {
30 case opts._ && opts._[1]:
31 return opts._[1];
32 case typeof opts.path === 'string':
33 return opts.path;
34 case this.path !== void 0:
35 return this.path;
36 default:
37 return process.cwd();
38 }
39 }).call(this);
40 if (!fs.existsSync(this.path)) {
41 mkdirp.sync(this.path);
42 }
43 cwd = this.path;
44 if (!this.config && (this.config = this.load()) === false) {
45 throw new Error('Unable to load configuration.');
46 }
47 if (this.config.name) {
48 console.log(("\n" + this.config.name).bold);
49 }
50 if (this.config.description) {
51 console.log("" + this.config.description);
52 }
53 if (this.config.author) {
54 console.log("by " + this.config.author.name + " <" + this.config.author.email + "> (" + this.config.author.url + ")");
55 }
56 console.log("at " + this.path + "\n");
57 if (opts.skipGeneration !== true) {
58 code = this.generate(opts);
59 if (opts === false) {
60 throw new Error('Unable to generate code.');
61 }
62 }
63 if ((opts._ && (opts._[0] === 'publish' || opts._[0] === 'run')) || !this.config.scripts || !this.config.scripts.build || opts.skipScripts) {
64 console.log("\n" + ' DONE! '.bgGreen.white);
65 if (opts && opts.complete) {
66 opts.complete();
67 }
68 return;
69 }
70 console.log("\nBUILD:".bold);
71 scripts = this.config.scripts;
72 i = 0;
73 buildCmd = function() {
74 var command, description, dir, proc;
75 if (!scripts.build[i]) {
76 console.log("\n" + ' DONE! '.bgGreen.white);
77 if (opts && opts.complete) {
78 opts.complete();
79 }
80 return;
81 }
82 if (scripts.build[i].title && !scripts.build[i].command) {
83 console.log(' BUILD TITLE '.bgGreen.white + (' ' + scripts.build[i].title + ' ').bgWhite + " \n");
84 i++;
85 buildCmd();
86 return;
87 }
88 description = scripts.build[i].description || scripts.build[i];
89 command = scripts.build[i].command || scripts.build[i];
90 if (scripts.build[i].path) {
91 if (scripts.build[i].path.match(/^\//)) {
92 dir = scripts.build[i].path;
93 } else {
94 dir = cwd + "/" + scripts.build[i].path;
95 }
96 dir = path.normalize(dir);
97 } else {
98 dir = cwd;
99 }
100 console.log(' BUILD SCRIPT '.bgGreen.white + (' ' + description + ' ').bgWhite + " \n" + command.gray);
101 proc = spawn('bash', ['-c', command], {
102 cwd: dir
103 });
104 proc.stdout.on('data', function(data) {
105 return console.log(data.toString());
106 });
107 proc.on('exit', function(err) {
108 return buildCmd();
109 });
110 proc.on('error', function(err) {
111 console.log("\n" + ' ERROR '.bgRed.white + (' ' + err.message + ' ').bgWhite);
112 return console.log(JSON.stringify(err, null, ' ').red);
113 });
114 return i++;
115 };
116 if (opts && opts.skipScripts === true) {
117 return;
118 }
119 return buildCmd();
120 };
121
122}).call(this);