UNPKG

autocode

Version:

spec-driven code generator

123 lines (110 loc) 3.61 kB
// Generated by CoffeeScript 1.10.0 (function() { var changecase, colors, cson, fs, mkdirp, mustache, path, pluralize, spawn; changecase = require('change-case'); colors = require('colors'); cson = require('season'); fs = require('fs'); mkdirp = require('mkdirp'); mustache = require('mustache'); path = require('path'); pluralize = require('pluralize'); spawn = require('cross-spawn'); exports.generate = require('./generate'); module.exports = function(opts) { var buildCmd, code, cwd, i, scripts; opts = opts || {}; this.path = (function() { switch (true) { case opts._ && opts._[1]: return opts._[1]; case typeof opts.path === 'string': return opts.path; case this.path !== void 0: return this.path; default: return process.cwd(); } }).call(this); if (!fs.existsSync(this.path)) { mkdirp.sync(this.path); } cwd = this.path; if (!this.config && (this.config = this.load()) === false) { throw new Error('Unable to load configuration.'); } if (this.config.name) { console.log(("\n" + this.config.name).bold); } if (this.config.description) { console.log("" + this.config.description); } if (this.config.author) { console.log("by " + this.config.author.name + " <" + this.config.author.email + "> (" + this.config.author.url + ")"); } console.log("at " + this.path + "\n"); if (opts.skipGeneration !== true) { code = this.generate(opts); if (opts === false) { throw new Error('Unable to generate code.'); } } if ((opts._ && (opts._[0] === 'publish' || opts._[0] === 'run')) || !this.config.scripts || !this.config.scripts.build || opts.skipScripts) { console.log("\n" + ' DONE! '.bgGreen.white); if (opts && opts.complete) { opts.complete(); } return; } console.log("\nBUILD:".bold); scripts = this.config.scripts; i = 0; buildCmd = function() { var command, description, dir, proc; if (!scripts.build[i]) { console.log("\n" + ' DONE! '.bgGreen.white); if (opts && opts.complete) { opts.complete(); } return; } if (scripts.build[i].title && !scripts.build[i].command) { console.log(' BUILD TITLE '.bgGreen.white + (' ' + scripts.build[i].title + ' ').bgWhite + " \n"); i++; buildCmd(); return; } description = scripts.build[i].description || scripts.build[i]; command = scripts.build[i].command || scripts.build[i]; if (scripts.build[i].path) { if (scripts.build[i].path.match(/^\//)) { dir = scripts.build[i].path; } else { dir = cwd + "/" + scripts.build[i].path; } dir = path.normalize(dir); } else { dir = cwd; } console.log(' BUILD SCRIPT '.bgGreen.white + (' ' + description + ' ').bgWhite + " \n" + command.gray); proc = spawn('bash', ['-c', command], { cwd: dir }); proc.stdout.on('data', function(data) { return console.log(data.toString()); }); proc.on('exit', function(err) { return buildCmd(); }); proc.on('error', function(err) { console.log("\n" + ' ERROR '.bgRed.white + (' ' + err.message + ' ').bgWhite); return console.log(JSON.stringify(err, null, ' ').red); }); return i++; }; if (opts && opts.skipScripts === true) { return; } return buildCmd(); }; }).call(this);