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
Provenance
- Commit
426ba62- Tree
27d241f- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
d2df055
2 files changed+24 added−120 removed
modified:bin/express+23 added-120 removed
2 unmodified lines3 /**4 * Module dependencies.5 */6 7-var fs = require('fs')8- , exec = require('child_process').exec;7+var express = require('../')8+ , exec = require('child_process').exec9+ , program = require('commander')10+ , fs = require('fs');11 10-/**11- * Framework version.12- */12+// CLI13 14-var version = '2.4.3';14+program15+ .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);20 16-/**17- * Add session support.18- */21+// Path22 20-var sessions = false;23+var path = program.args.shift() || '.';24 25 /**23- * CSS engine to utilize.24- */25-26-var cssEngine;27-28-/**29- * Template engine to utilize.30- */31-32-var templateEngine = 'jade';33-34-/**35- * Usage documentation.36- */37-38-var 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- ;49-50-/**26 * Jade layout template.27 */28 29 var jadeLayout = [139 unmodified lines169 , 'console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);'170 , ''171 ].join('\n');172 198-// Parse arguments199-200-var args = process.argv.slice(2)201- , path = '.';202-203-while (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-}235-173 // Generate application174 175 (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 lines199 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 lines213 default:214 write(path + '/public/stylesheets/style.css', css);215 }216 });217+218 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 lines228 }229 });230 231 // 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 lines241 app = app.replace('{css}', '');242 }243 244 // 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 : '');248 249 // Template support312- app = app.replace(':TEMPLATE', templateEngine);250+ app = app.replace(':TEMPLATE', program.template);251 252 // 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 += '}';263 264 28 unmodified lines293 console.log(' \x1b[36mcreate\x1b[0m : ' + path);294 }295 296 /**359- * Prompt confirmation with the given `msg`.360- *361- * @param {String} msg362- * @param {Function} fn363- */364-365-function confirm(msg, fn) {366- prompt(msg, function(val){367- fn(/^ *y(es)?/i.test(val));368- });369-}370-371-/**372- * Prompt input with the given `msg` and callback `fn`.373- *374- * @param {String} msg375- * @param {Function} fn376- */377-378-function prompt(msg, fn) {379- // prompt380- if (' ' == msg[msg.length - 1]) {381- process.stdout.write(msg);382- } else {383- console.log(msg);384- }385-386- // stdin387- process.stdin.setEncoding('ascii');388- process.stdin.once('data', function(data){389- fn(data);390- }).resume();391-}392-393-/**297 * Mkdir -p.298 *299 * @param {String} path300 * @param {Function} fn20 unmodified linesmodified:package.json+1 added
9 unmodified lines10111213141516171821 unmodified lines9 unmodified lines { "name": "Guillermo Rauch", "email": "rauchg@gmail.com" } ], "dependencies": { "connect": ">= 1.5.2 < 2.0.0", "commander": "0.0.4", "mime": ">= 0.0.1", "qs": ">= 0.3.1" }, "devDependencies": {21 unmodified lines