Skip to content
Myr

rewrote express(1) to use commander.js

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent d2df055 · Browse files · 2 files+24 added−120 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+24 added−120 removed

  • other:+24 added−120 removed

2 files+24 added−120 removed

modified:bin/express+23 added-120 removed
2 unmodified lines
3/**4 * Module dependencies.5 */67var fs = require('fs')8 , exec = require('child_process').exec;7var express = require('../')8 , exec = require('child_process').exec9 , program = require('commander')10 , fs = require('fs');1110/**11 * Framework version.12 */12// CLI1314var version = '2.4.3';14program15 .version(express.version)16 .option('-s, --sessions', 'add session support')17 .option('-t, --template <engine>', 'add template <engine> support (jade|ejs) [jade]', 'jade')18 .option('-c, --css <engine>', 'add stylesheet <engine> support (less|sass|stylus) [plain css]')19 .parse(process.argv);2016/**17 * Add session support.18 */21// Path2220var sessions = false;23var path = program.args.shift() || '.';2425/**23 * CSS engine to utilize.24 */2526var cssEngine;2728/**29 * Template engine to utilize.30 */3132var templateEngine = 'jade';3334/**35 * Usage documentation.36 */3738var usage = ''39 + '\n'40 + ' Usage: express [options] [path]\n'41 + '\n'42 + ' Options:\n'43 + ' -s, --sessions add session support\n'44 + ' -t, --template <engine> add template <engine> support (jade|ejs). default=jade\n'45 + ' -c, --css <engine> add stylesheet <engine> support (less|sass|stylus). default=plain css\n'46 + ' -v, --version output framework version\n'47 + ' -h, --help output help information\n'48 ;4950/**26 * Jade layout template.27 */2829var jadeLayout = [
139 unmodified lines
169 , 'console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);'170 , ''171].join('\n');172198// Parse arguments199200var args = process.argv.slice(2)201 , path = '.';202203while (args.length) {204 var arg = args.shift();205 switch (arg) {206 case '-h':207 case '--help':208 abort(usage);209 break;210 case '-v':211 case '--version':212 abort(version);213 break;214 case '-s':215 case '--session':216 case '--sessions':217 sessions = true;218 break;219 case '-c':220 case '--css':221 args.length222 ? (cssEngine = args.shift())223 : abort('--css requires an argument');224 break;225 case '-t':226 case '--template':227 args.length228 ? (templateEngine = args.shift())229 : abort('--template requires an argument');230 break;231 default:232 path = arg;233 }234}235173// Generate application174175(function createApplication(path) {176 emptyDirectory(path, function(empty){177 if (empty) {178 createApplicationAt(path);179 } else {243 confirm('destination is not empty, continue? ', function(ok){180 program.confirm('destination is not empty, continue? ', function(ok){181 if (ok) {182 process.stdin.destroy();183 createApplicationAt(path);184 } else {
14 unmodified lines
199 mkdir(path, function(){200 mkdir(path + '/public/javascripts');201 mkdir(path + '/public/images');202 mkdir(path + '/public/stylesheets', function(){266 switch (cssEngine) {203 switch (program.css) {204 case 'stylus':205 write(path + '/public/stylesheets/style.styl', stylus);206 break;207 case 'less':
5 unmodified lines
213 default:214 write(path + '/public/stylesheets/style.css', css);215 }216 });217218 mkdir(path + '/views', function(){281 switch (templateEngine) {219 switch (program.template) {220 case 'ejs':221 write(path + '/views/layout.ejs', ejsLayout);222 write(path + '/views/index.ejs', ejsIndex);223 break;
4 unmodified lines
228 }229 });230231 // CSS Engine support294 switch (cssEngine) {232 switch (program.css) {233 case 'sass':234 case 'less':235 app = app.replace('{css}', '\n app.use(express.compiler({ src: __dirname + \'/public\', enable: [\'' + cssEngine + '\'] }));');236 break;
4 unmodified lines
241 app = app.replace('{css}', '');242 }243244 // Session support307 app = app.replace('{sess}', sessions245 app = app.replace('{sess}', program.sessions246 ? '\n app.use(express.cookieParser());\n app.use(express.session({ secret: \'your secret here\' }));'247 : '');248249 // Template support312 app = app.replace(':TEMPLATE', templateEngine);250 app = app.replace(':TEMPLATE', program.template);251252 // package.json253 var json = '{\n';254 json += ' "name": "application-name"\n';255 json += ' , "version": "0.0.1"\n';256 json += ' , "private": true\n';257 json += ' , "dependencies": {\n';320 json += ' "express": "' + version + '"\n';321 if (cssEngine) json += ' , "' + cssEngine + '": ">= 0.0.1"\n';322 if (templateEngine) json += ' , "' + templateEngine + '": ">= 0.0.1"\n';258 json += ' "express": "' + express.version + '"\n';259 if (program.css) json += ' , "' + program.css + '": ">= 0.0.1"\n';260 if (program.template) json += ' , "' + program.template + '": ">= 0.0.1"\n';261 json += ' }\n';262 json += '}';263264
28 unmodified lines
293 console.log(' \x1b[36mcreate\x1b[0m : ' + path);294}295296/**359 * Prompt confirmation with the given `msg`.360 *361 * @param {String} msg362 * @param {Function} fn363 */364365function confirm(msg, fn) {366 prompt(msg, function(val){367 fn(/^ *y(es)?/i.test(val));368 });369}370371/**372 * Prompt input with the given `msg` and callback `fn`.373 *374 * @param {String} msg375 * @param {Function} fn376 */377378function prompt(msg, fn) {379 // prompt380 if (' ' == msg[msg.length - 1]) {381 process.stdout.write(msg);382 } else {383 console.log(msg);384 }385386 // stdin387 process.stdin.setEncoding('ascii');388 process.stdin.once('data', function(data){389 fn(data);390 }).resume();391}392393/**297 * Mkdir -p.298 *299 * @param {String} path300 * @param {Function} fn
20 unmodified lines
modified:package.json+1 added