forked from expressjs/generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress-cli.js
executable file
·539 lines (457 loc) · 13.2 KB
/
express-cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#!/usr/bin/env node
var ejs = require('ejs')
var fs = require('fs')
var minimatch = require('minimatch')
var mkdirp = require('mkdirp')
var path = require('path')
var program = require('commander')
var readline = require('readline')
var sortedObject = require('sorted-object')
var util = require('util')
var MODE_0666 = parseInt('0666', 8)
var MODE_0755 = parseInt('0755', 8)
var TEMPLATE_DIR = path.join(__dirname, '..', 'templates')
var VERSION = require('../package').version
var _exit = process.exit
// Re-assign process.exit because of commander
// TODO: Switch to a different command framework
process.exit = exit
// CLI
around(program, 'optionMissingArgument', function (fn, args) {
program.outputHelp()
fn.apply(this, args)
return { args: [], unknown: [] }
})
before(program, 'outputHelp', function () {
// track if help was shown for unknown option
this._helpShown = true
})
before(program, 'unknownOption', function () {
// allow unknown options if help was shown, to prevent trailing error
this._allowUnknownOption = this._helpShown
// show help if not yet shown
if (!this._helpShown) {
program.outputHelp()
}
})
program
.name('express')
.version(VERSION, ' --version')
.usage('[options] [dir]')
.option('-e, --ejs', 'add ejs engine support', renamedOption('--ejs', '--view=ejs'))
.option(' --pug', 'add pug engine support', renamedOption('--pug', '--view=pug'))
.option(' --hbs', 'add handlebars engine support', renamedOption('--hbs', '--view=hbs'))
.option('-H, --hogan', 'add hogan.js engine support', renamedOption('--hogan', '--view=hogan'))
.option('-v, --view <engine>', 'add view <engine> support (dust|ejs|hbs|hjs|jade|pug|twig|vash) (defaults to jade)')
.option(' --no-view', 'use static html instead of view engine')
.option('-c, --css <engine>', 'add stylesheet <engine> support (less|stylus|compass|sass) (defaults to plain css)')
.option(' --git', 'add .gitignore')
.option('-f, --force', 'force on non-empty directory')
.parse(process.argv)
if (!exit.exited) {
main()
}
/**
* Install an around function; AOP.
*/
function around (obj, method, fn) {
var old = obj[method]
obj[method] = function () {
var args = new Array(arguments.length)
for (var i = 0; i < args.length; i++) args[i] = arguments[i]
return fn.call(this, old, args)
}
}
/**
* Install a before function; AOP.
*/
function before (obj, method, fn) {
var old = obj[method]
obj[method] = function () {
fn.call(this)
old.apply(this, arguments)
}
}
/**
* Prompt for confirmation on STDOUT/STDIN
*/
function confirm (msg, callback) {
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rl.question(msg, function (input) {
rl.close()
callback(/^y|yes|ok|true$/i.test(input))
})
}
/**
* Copy file from template directory.
*/
function copyTemplate (from, to) {
write(to, fs.readFileSync(path.join(TEMPLATE_DIR, from), 'utf-8'))
}
/**
* Copy multiple files from template directory.
*/
function copyTemplateMulti (fromDir, toDir, nameGlob) {
fs.readdirSync(path.join(TEMPLATE_DIR, fromDir))
.filter(minimatch.filter(nameGlob, { matchBase: true }))
.forEach(function (name) {
copyTemplate(path.join(fromDir, name), path.join(toDir, name))
})
}
/**
* Create application at the given directory.
*
* @param {string} name
* @param {string} dir
*/
function createApplication (name, dir) {
console.log()
// Package
var pkg = {
name: name,
version: '0.0.0',
private: true,
scripts: {
start: 'node ./bin/www'
},
dependencies: {
debug: '~2.6.9',
express: '~4.17.1'
}
}
// JavaScript
var app = loadTemplate('js/app.js')
var www = loadTemplate('js/www')
// App name
www.locals.name = name
// App modules
app.locals.localModules = Object.create(null)
app.locals.modules = Object.create(null)
app.locals.mounts = []
app.locals.uses = []
// Request logger
app.locals.modules.logger = 'morgan'
app.locals.uses.push("logger('dev')")
pkg.dependencies.morgan = '~1.10.0'
// Body parsers
app.locals.uses.push('express.json()')
app.locals.uses.push('express.urlencoded({ extended: false })')
// Cookie parser
app.locals.modules.cookieParser = 'cookie-parser'
app.locals.uses.push('cookieParser()')
pkg.dependencies['cookie-parser'] = '~1.4.5'
if (dir !== '.') {
mkdir(dir, '.')
}
mkdir(dir, 'public')
mkdir(dir, 'public/javascripts')
mkdir(dir, 'public/images')
mkdir(dir, 'public/stylesheets')
// copy css templates
switch (program.css) {
case 'less':
copyTemplateMulti('css', dir + '/public/stylesheets', '*.less')
break
case 'stylus':
copyTemplateMulti('css', dir + '/public/stylesheets', '*.styl')
break
case 'compass':
copyTemplateMulti('css', dir + '/public/stylesheets', '*.scss')
break
case 'sass':
copyTemplateMulti('css', dir + '/public/stylesheets', '*.sass')
break
default:
copyTemplateMulti('css', dir + '/public/stylesheets', '*.css')
break
}
// copy route templates
mkdir(dir, 'routes')
copyTemplateMulti('js/routes', dir + '/routes', '*.js')
if (program.view) {
// Copy view templates
mkdir(dir, 'views')
pkg.dependencies['http-errors'] = '~1.7.2'
switch (program.view) {
case 'dust':
copyTemplateMulti('views', dir + '/views', '*.dust')
break
case 'ejs':
copyTemplateMulti('views', dir + '/views', '*.ejs')
break
case 'hbs':
copyTemplateMulti('views', dir + '/views', '*.hbs')
break
case 'hjs':
copyTemplateMulti('views', dir + '/views', '*.hjs')
break
case 'jade':
copyTemplateMulti('views', dir + '/views', '*.jade')
break
case 'pug':
copyTemplateMulti('views', dir + '/views', '*.pug')
break
case 'twig':
copyTemplateMulti('views', dir + '/views', '*.twig')
break
case 'vash':
copyTemplateMulti('views', dir + '/views', '*.vash')
break
}
} else {
// Copy extra public files
copyTemplate('js/index.html', path.join(dir, 'public/index.html'))
}
// CSS Engine support
switch (program.css) {
case 'compass':
app.locals.modules.compass = 'node-compass'
app.locals.uses.push("compass({ mode: 'expanded' })")
pkg.dependencies['node-compass'] = '0.2.3'
break
case 'less':
app.locals.modules.lessMiddleware = 'less-middleware'
app.locals.uses.push("lessMiddleware(path.join(__dirname, 'public'))")
pkg.dependencies['less-middleware'] = '~2.2.1'
break
case 'sass':
app.locals.modules.sassMiddleware = 'node-sass-middleware'
app.locals.uses.push("sassMiddleware({\n src: path.join(__dirname, 'public'),\n dest: path.join(__dirname, 'public'),\n indentedSyntax: true, // true = .sass and false = .scss\n sourceMap: true\n})")
pkg.dependencies['node-sass-middleware'] = '0.11.0'
break
case 'stylus':
app.locals.modules.stylus = 'stylus'
app.locals.uses.push("stylus.middleware(path.join(__dirname, 'public'))")
pkg.dependencies.stylus = '0.54.5'
break
}
// Index router mount
app.locals.localModules.indexRouter = './routes/index'
app.locals.mounts.push({ path: '/', code: 'indexRouter' })
// User router mount
app.locals.localModules.usersRouter = './routes/users'
app.locals.mounts.push({ path: '/users', code: 'usersRouter' })
// Template support
switch (program.view) {
case 'dust':
app.locals.modules.adaro = 'adaro'
app.locals.view = {
engine: 'dust',
render: 'adaro.dust()'
}
pkg.dependencies.adaro = '~1.0.4'
break
case 'ejs':
app.locals.view = { engine: 'ejs' }
pkg.dependencies.ejs = '~2.6.1'
break
case 'hbs':
app.locals.view = { engine: 'hbs' }
pkg.dependencies.hbs = '~4.0.4'
break
case 'hjs':
app.locals.view = { engine: 'hjs' }
pkg.dependencies.hjs = '~0.0.6'
break
case 'jade':
app.locals.view = { engine: 'jade' }
pkg.dependencies.jade = '~1.11.0'
break
case 'pug':
app.locals.view = { engine: 'pug' }
pkg.dependencies.pug = '2.0.0-beta11'
break
case 'twig':
app.locals.view = { engine: 'twig' }
pkg.dependencies.twig = '~0.10.3'
break
case 'vash':
app.locals.view = { engine: 'vash' }
pkg.dependencies.vash = '~0.12.6'
break
default:
app.locals.view = false
break
}
// Static files
app.locals.uses.push("express.static(path.join(__dirname, 'public'))")
if (program.git) {
copyTemplate('js/gitignore', path.join(dir, '.gitignore'))
}
// sort dependencies like npm(1)
pkg.dependencies = sortedObject(pkg.dependencies)
// write files
write(path.join(dir, 'app.js'), app.render())
write(path.join(dir, 'package.json'), JSON.stringify(pkg, null, 2) + '\n')
mkdir(dir, 'bin')
write(path.join(dir, 'bin/www'), www.render(), MODE_0755)
var prompt = launchedFromCmd() ? '>' : '$'
if (dir !== '.') {
console.log()
console.log(' change directory:')
console.log(' %s cd %s', prompt, dir)
}
console.log()
console.log(' install dependencies:')
console.log(' %s npm install', prompt)
console.log()
console.log(' run the app:')
if (launchedFromCmd()) {
console.log(' %s SET DEBUG=%s:* & npm start', prompt, name)
} else {
console.log(' %s DEBUG=%s:* npm start', prompt, name)
}
console.log()
}
/**
* Create an app name from a directory path, fitting npm naming requirements.
*
* @param {String} pathName
*/
function createAppName (pathName) {
return path.basename(pathName)
.replace(/[^A-Za-z0-9.-]+/g, '-')
.replace(/^[-_.]+|-+$/g, '')
.toLowerCase()
}
/**
* Check if the given directory `dir` is empty.
*
* @param {String} dir
* @param {Function} fn
*/
function emptyDirectory (dir, fn) {
fs.readdir(dir, function (err, files) {
if (err && err.code !== 'ENOENT') throw err
fn(!files || !files.length)
})
}
/**
* Graceful exit for async STDIO
*/
function exit (code) {
// flush output for Node.js Windows pipe bug
// https://github.com/joyent/node/issues/6247 is just one bug example
// https://github.com/visionmedia/mocha/issues/333 has a good discussion
function done () {
if (!(draining--)) _exit(code)
}
var draining = 0
var streams = [process.stdout, process.stderr]
exit.exited = true
streams.forEach(function (stream) {
// submit empty write request and wait for completion
draining += 1
stream.write('', done)
})
done()
}
/**
* Determine if launched from cmd.exe
*/
function launchedFromCmd () {
return process.platform === 'win32' &&
process.env._ === undefined
}
/**
* Load template file.
*/
function loadTemplate (name) {
var contents = fs.readFileSync(path.join(__dirname, '..', 'templates', (name + '.ejs')), 'utf-8')
var locals = Object.create(null)
function render () {
return ejs.render(contents, locals, {
escape: util.inspect
})
}
return {
locals: locals,
render: render
}
}
/**
* Main program.
*/
function main () {
// Path
var destinationPath = program.args.shift() || '.'
// App name
var appName = createAppName(path.resolve(destinationPath)) || 'hello-world'
// View engine
if (program.view === true) {
if (program.ejs) program.view = 'ejs'
if (program.hbs) program.view = 'hbs'
if (program.hogan) program.view = 'hjs'
if (program.pug) program.view = 'pug'
}
// Default view engine
if (program.view === true) {
warning('the default view engine will not be jade in future releases\n' +
"use `--view=jade' or `--help' for additional options")
program.view = 'jade'
}
// Generate application
emptyDirectory(destinationPath, function (empty) {
if (empty || program.force) {
createApplication(appName, destinationPath)
} else {
confirm('destination is not empty, continue? [y/N] ', function (ok) {
if (ok) {
process.stdin.destroy()
createApplication(appName, destinationPath)
} else {
console.error('aborting')
exit(1)
}
})
}
})
}
/**
* Make the given dir relative to base.
*
* @param {string} base
* @param {string} dir
*/
function mkdir (base, dir) {
var loc = path.join(base, dir)
console.log(' \x1b[36mcreate\x1b[0m : ' + loc + path.sep)
mkdirp.sync(loc, MODE_0755)
}
/**
* Generate a callback function for commander to warn about renamed option.
*
* @param {String} originalName
* @param {String} newName
*/
function renamedOption (originalName, newName) {
return function (val) {
warning(util.format("option `%s' has been renamed to `%s'", originalName, newName))
return val
}
}
/**
* Display a warning similar to how errors are displayed by commander.
*
* @param {String} message
*/
function warning (message) {
console.error()
message.split('\n').forEach(function (line) {
console.error(' warning: %s', line)
})
console.error()
}
/**
* echo str > file.
*
* @param {String} file
* @param {String} str
*/
function write (file, str, mode) {
fs.writeFileSync(file, str, { mode: mode || MODE_0666 })
console.log(' \x1b[36mcreate\x1b[0m : ' + file)
}