#!/usr/bin/env node // load minimist var minimist = require('minimist')(process.argv.slice(2)); if (minimist.debug) { require('coffee-script').register(); } // load packages var ascii = require('../cli/lib/ascii')(), autocode = require('../' + (minimist.debug ? 'src' : 'lib') + '/autocode'), bluebird = require('bluebird'), colors = require('colors'), fs = require('fs'), userHome = require('user-home'), yaml = require('js-yaml'); // handle uncaught exceptions process.on('uncaughtException', function (e) { if (minimist.debug) { throw e; } console.log(e.message.red); }); // get commands var commands = yaml.safeLoad(fs.readFileSync(__dirname + '/../cli/cmd.yml')); // get project var project = new autocode(); // validate commands for (var command in commands) { if (!project[command] && command != 'help') { throw new Error('Invalid command: ' + command); } } // get command var command = minimist._[0] || 'help'; // validate command if (!commands[command]) { throw new Error('Unknown command: ' + command); } // display ascii if (minimist.debug || command == 'help') { console.log(ascii.gray); } // get autocode output bluebird.try(function() { // change cwd if (command != 'info' && command != 'import' && command != 'install' && command != 'link' && command != 'search' && minimist._[1]) { var dest = minimist._[1].split('@'); var version = dest[1] || 'latest'; var module = dest[0]; var cwd = userHome + '/.autocode/module/github.com/' + module + '/' + version; if (!fs.existsSync(cwd)) { throw new Error('Module (' + module + '@' + version + ') does not exist at: ' + cwd); } process.chdir(cwd); } if (command == 'build' || command == 'config' || command == 'import' || command == 'run' || command == 'stop' || command == 'update') { project.load(process.cwd()); } switch (command) { case 'cache': project.cache( minimist._[1] || minimist.key, minimist._[2] || minimist.value, minimist.debug ? true : false ); break; case 'config': project.load(process.cwd()); console.log( yaml.safeDump(project.config).blue ); break; case 'help': var output = require(__dirname + '/../cli/lib/help')(commands); console.log(output); break; case 'import': project.import({ name: minimist._[1], force: minimist.f || minimist.force ? true : false }); break; case 'install': project.install({ name: minimist._[1], force: minimist.f || minimist.force ? true : false }); break; case 'link': var config = project.config('.'); var version = config.version; var id = config.id; if (minimist._[1]) { var dest = minimist._[1].split('@'); version = dest[1]; if (dest[0].length) { id = dest[0]; } } var collection = id.slice(0, id.lastIndexOf('.')); var module = id.slice(id.lastIndexOf('.')+1); if (minimist._[2]) { var src = minimist._[2]; } else { var src = process.cwd(); } var force = minimist.f === true || minimist.force === true ? true : false; project.build(minimist); project.link({ collection: collection, force: force, module: module, src: src, version: version }); break; case 'signup': project.signup(); break; default: if (minimist.f === true) { minimist.force = true; } if (minimist['skip-scripts'] === true) { minimist.skipScripts = true; } project[command](minimist); break; } }) .catch(function(e) { if (minimist.debug) { throw e; } console.log(e.message.red); });