#!/usr/bin/env node /** * Module dependencies. */ var sys = require('sys'), fs = require('fs'), childProcess = require('child_process'); /** * Framework version. */ var version = '1.0.0'; /** * Usage documentation. */ var usage = '' + '\x1b[1mUsage\x1b[0m: express [options] [PATH]\n' + '\n' + '\x1b[1mOptions\x1b[0m:\n' + ' -v, --version Output framework version\n' + ' -h, --help Output help information\n'; /** * Jade layout template. */ var jadeLayout = [ '!!!', 'html', ' head', ' title= title', ' link(rel=\'stylesheet\', href=\'/stylesheets/style.css\')', ' body!= body' ].join('\n'); /** * Default sass template. */ var sass = [ 'body', ' :padding 50px', ' :font 14px "Lucida Grande", "Helvetica Nueue", Arial, sans-serif' ].join('\n'); /** * Jade index template. */ var jadeIndex = [ 'h1= title', 'p Welcome to #{title}' ].join('\n'); /** * App template. */ var app = [ '', '/**', ' * Module dependencies.', ' */', '', 'var express = require(\'express\'),', ' connect = require(\'connect\');', '', '// Create and export Express app', '', 'var app = module.exports = express.createServer();', '', '// Configuration', '', 'app.configure(function(){', ' app.use(\'/\', connect.redirect());', ' app.use(\'/\', connect.bodyDecoder());', ' app.use(\'/\', connect.compiler({ src: __dirname + \'/public\', enable: [\'sass\'] }));', ' app.use(\'/\', connect.staticProvider(__dirname + \'/public\'));', '});', '', 'app.configure(\'development\', function(){', ' app.use(\'/\', connect.errorHandler({ dumpExceptions: true, showStack: true })); ', '});', '', 'app.configure(\'production\', function(){', ' app.use(\'/\', connect.errorHandler()); ', '});', '', '// Routes', '', 'app.get(\'/\', function(req, res){', ' res.render(\'index.jade\', {', ' locals: {', ' title: \'Express\'', ' }', ' });', '});' ].join('\n'); // Parse arguments var args = process.argv.slice(2), path = '.'; while (args.length) { var arg = args.shift(); switch (arg) { case '-h': case '--help': abort(usage); break; case '-v': case '--version': abort(version); break; default: path = arg; } } // Generate application (function createApplication(path) { mkdir(path, function(){ mkdir(path + '/pids'); mkdir(path + '/logs'); mkdir(path + '/public/{javascripts,stylesheets,images}', function(){ write(path + '/public/stylesheets/style.sass', sass); }); mkdir(path + '/views/partials', function(){ write(path + '/views/layout.jade', jadeLayout); write(path + '/views/index.jade', jadeIndex); }); write(path + '/app.js', app); }); })(path); /** * echo str > path. * * @param {String} path * @param {String} str */ function write(path, str) { fs.writeFile(path, str); sys.puts(' create : ' + path); } /** * Mkdir -p. * * @param {String} path * @param {Function} fn */ function mkdir(path, fn) { childProcess.exec('mkdir -p ' + path, function(err){ if (err) throw err; sys.puts(' create : ' + path); fn && fn(); }); } /** * Exit with the given `str`. * * @param {String} str */ function abort(str) { sys.error(str); process.exit(1); }