Skip to content
Myr

express

This file

Path

bin/express

178 lines · 3.5 KiB

Last changed by

Added app generation to bin/expressTj Holowaychukbefore adoption8338432

History of this file

Take it

1#!/usr/bin/env node23/**4 * Module dependencies.5 */67var sys = require('sys'),8    fs = require('fs'),9    childProcess = require('child_process');1011/**12 * Framework version.13 */1415var version = '1.0.0';1617/**18 * Usage documentation.19 */2021var usage = ''22    + '\x1b[1mUsage\x1b[0m: express [options] [PATH]\n'23    + '\n'24    + '\x1b[1mOptions\x1b[0m:\n'25    + '  -v, --version Output framework version\n'26    + '  -h, --help    Output help information\n';2728/**29 * Jade layout template.30 */3132var jadeLayout = [33    '!!!',34    'html',35    '  head',36    '    title= title',37    '    link(rel=\'stylesheet\', href=\'/stylesheets/style.css\')',38    '  body!= body'39].join('\n');4041/**42 * Default sass template.43 */4445var sass = [46    'body',47    '  :padding 50px',48    '  :font 14px "Lucida Grande", "Helvetica Nueue", Arial, sans-serif'49].join('\n');5051/**52 * Jade index template.53 */5455var jadeIndex = [56    'h1= title',57    'p Welcome to #{title}'58].join('\n');5960/**61 * App template.62 */6364var app = [65    '',66    '/**',67    ' * Module dependencies.',68    ' */',69    '',70    'var express = require(\'express\'),',71    '    connect = require(\'connect\');',72    '',73    '// Create and export Express app',74    '',75    'var app = module.exports = express.createServer();',76    '',77    '// Configuration',78    '',79    'app.configure(function(){',80    '    app.use(\'/\', connect.redirect());',81    '    app.use(\'/\', connect.bodyDecoder());',82    '    app.use(\'/\', connect.compiler({ src: __dirname + \'/public\', enable: [\'sass\'] }));',83    '    app.use(\'/\', connect.staticProvider(__dirname + \'/public\'));',84    '});',85    '',86    'app.configure(\'development\', function(){',87    '   app.use(\'/\', connect.errorHandler({ dumpExceptions: true, showStack: true })); ',88    '});',89    '',90    'app.configure(\'production\', function(){',91    '   app.use(\'/\', connect.errorHandler()); ',92    '});',93    '',94    '// Routes',95    '',96    'app.get(\'/\', function(req, res){',97    '    res.render(\'index.jade\', {',98    '        locals: {',99    '            title: \'Express\'',100    '        }',101    '    });',102    '});'103].join('\n');104105// Parse arguments106107var args = process.argv.slice(2),108    path = '.';109while (args.length) {110    var arg = args.shift();111    switch (arg) {112        case '-h':113        case '--help':114            abort(usage);115            break;116        case '-v':117        case '--version':118            abort(version);119            break;120        default:121            path = arg;122    }123}124125// Generate application126127(function createApplication(path) {128    mkdir(path, function(){129        mkdir(path + '/pids');130        mkdir(path + '/logs');131        mkdir(path + '/public/{javascripts,stylesheets,images}', function(){132            write(path + '/public/stylesheets/style.sass', sass);133        });134        mkdir(path + '/views/partials', function(){135            write(path + '/views/layout.jade', jadeLayout);136            write(path + '/views/index.jade', jadeIndex);137        });138        write(path + '/app.js', app);139    });140})(path);141142/**143 * echo str > path.144 *145 * @param {String} path146 * @param {String} str147 */148149function write(path, str) {150    fs.writeFile(path, str);151    sys.puts('   create : ' + path);152}153154/**155 * Mkdir -p.156 *157 * @param {String} path158 * @param {Function} fn159 */160161function mkdir(path, fn) {162    childProcess.exec('mkdir -p ' + path, function(err){163        if (err) throw err;164        sys.puts('   create : ' + path);165        fn && fn();166    });167}168169/**170 * Exit with the given `str`.171 *172 * @param {String} str173 */174175function abort(str) {176    sys.error(str);177    process.exit(1);178}