UNPKG

jackson

Version:

Jackson, the web application framework

191 lines (171 loc) 6.78 kB
// Generated by CoffeeScript 1.7.1 (function() { var CLI, clone, extend, fs, jacksonVersion, minimist, open, paramCase, pascalCase, path, snakeCase, spawn, _ref, __slice = [].slice, __hasProp = {}.hasOwnProperty; fs = require('fs'); path = require('path'); spawn = require('child_process').spawn; minimist = require('minimist'); pascalCase = require('pascal-case'); snakeCase = require('snake-case'); paramCase = require('param-case'); open = require('open'); _ref = require('./util'), jacksonVersion = _ref.jacksonVersion, extend = _ref.extend, clone = _ref.clone; CLI = (function() { function CLI(app, argv) { this.app = app; this.argv = argv || process.argv.slice(2); this.args = minimist(this.argv); this.commands = clone(this.jacksonCommands); if (this.app) { extend(this.commands, this.appCommands); } } CLI.prototype.run = function() { var arg, command, handler, params, value, _ref1, _ref2; _ref1 = this.args._, command = _ref1[0], params = 2 <= _ref1.length ? __slice.call(_ref1, 1) : []; _ref2 = this.args; for (arg in _ref2) { if (!__hasProp.call(_ref2, arg)) continue; value = _ref2[arg]; if (value === true && !command) { if (this.commands[arg]) { command = arg; } } } command || (command = 'default'); handler = this.handler(command); if (handler) { handler.apply(this, params); return true; } else { this.handler('missing').apply(this, this.args._); return false; } }; CLI.prototype.printUsage = function() { var command, usage; command = arguments[0], usage = 2 <= arguments.length ? __slice.call(arguments, 1) : []; return console.log.apply(console, ["usage:", "jack".yellow, command.yellow].concat(__slice.call(usage))); }; CLI.prototype.handler = function(command) { var handler; handler = this.commands[command]; while (typeof handler === 'string') { handler = this.commands[handler]; } return handler; }; CLI.prototype.jacksonCommands = { "default": 'help', missing: 'help', h: 'help', help: function() { var appHelp, jacksonHelp, versionString; versionString = "Jackson v" + jacksonVersion; console.log(Array(versionString.length + 1).join("-")); console.log(versionString); console.log(Array(versionString.length + 1).join("-")); console.log(); jacksonHelp = "Jackson commands:\n " + 'new'.yellow + ": Create a new Jackson project\n usage: jack new MyApp"; appHelp = "Application commands:\n " + 'server'.yellow + ": Start the web server (alias 's')\n --port=1234 - Listen on a port (default, alias '-p')\n --host=localhost - Bind to a specific host when listening on a port\n --socket=/path/to/socket - Listen on a socket\n\n --open - Open the URL in your default web browser after listening\n\n " + 'repl'.yellow + ": Start a REPL (alias 'r')"; if (this.app) { console.log(jacksonHelp); console.log(""); return console.log(appHelp); } else { return console.log(jacksonHelp); } }, "new": function(applicationName, directory) { var copyFiles, install, skelDir, vars; if (!applicationName) { this.printUsage("new", "MyApp".white, "[directory]"); return; } directory || (directory = path.join(process.cwd(), snakeCase(applicationName))); vars = { APPLICATION_NAME: pascalCase(applicationName), PACKAGE_NAME: paramCase(applicationName), JACKSON_VERSION: jacksonVersion }; if (!fs.existsSync(directory) || !fs.statSync(directory).isDirectory()) { fs.mkdirSync(directory); } copyFiles = function(source, dest) { var contents, destPath, fileName, name, sourcePath, value, _i, _len, _ref1, _results; _ref1 = fs.readdirSync(source); _results = []; for (_i = 0, _len = _ref1.length; _i < _len; _i++) { fileName = _ref1[_i]; sourcePath = path.join(source, fileName); destPath = path.join(dest, fileName); if (fs.statSync(sourcePath).isDirectory()) { if (!fs.existsSync(destPath)) { fs.mkdirSync(destPath); } _results.push(copyFiles(sourcePath, destPath)); } else { console.log("Copying".yellow, sourcePath.white, "=>".yellow, destPath.white); contents = fs.readFileSync(sourcePath).toString(); for (name in vars) { if (!__hasProp.call(vars, name)) continue; value = vars[name]; contents = contents.split("$" + name + "$").join(value); } _results.push(fs.writeFileSync(destPath, contents.trim())); } } return _results; }; skelDir = __dirname + '/../' + (this.args.js ? 'skel.js' : 'skel'); copyFiles(skelDir, directory); console.log(); console.log("Running npm install..."); install = spawn("npm", ["install"], { cwd: directory, stdio: ['ignore', 'ignore', 'ignore'] }); return install.on('close', function(status) { console.log(""); if (status === 0) { return console.log("New application " + vars.APPLICATION_NAME.yellow + " created in " + directory.yellow); } else { console.log("Error during npm install".red); return console.log("The new application has been left in " + directory + " for you to investigate"); } }); }, v: 'version', version: function() { return console.log("Jackson v" + jacksonVersion); } }; CLI.prototype.appCommands = { "default": 'server', s: 'server', server: function() { var bindOn, socketOrPort; socketOrPort = this.args.socket || parseInt(this.args.port || this.args.p) || 1234; bindOn = this.args.host; return this.app.listen(socketOrPort, bindOn, (function(_this) { return function(port, host) { if (_this.args.open && typeof socketOrPort === 'number') { host || (host = "localhost"); return open("http://" + host + ":" + port + "/"); } }; })(this)); }, c: 'repl', r: 'repl', repl: function() { return this.app.startRepl(); } }; return CLI; })(); module.exports = CLI; }).call(this);