Skip to content

Commit

Permalink
gen: add all middleware to template locals
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jan 14, 2018
1 parent adaa5f1 commit 64ccae9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
51 changes: 33 additions & 18 deletions bin/express-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ function copyTemplateMulti (fromDir, toDir, nameGlob) {
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.16.0'
}
}

// JavaScript
var app = loadTemplate('js/app.js')
var www = loadTemplate('js/www')
Expand All @@ -146,6 +160,20 @@ function createApplication (name, dir) {
app.locals.modules = Object.create(null)
app.locals.uses = []

// Request logger
app.locals.modules.logger = 'morgan'
app.locals.uses.push("logger('dev')")
pkg.dependencies.morgan = '~1.9.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.3'

if (dir !== '.') {
mkdir(dir, '.')
}
Expand Down Expand Up @@ -243,21 +271,8 @@ function createApplication (name, dir) {
break
}

// package.json
var pkg = {
name: name,
version: '0.0.0',
private: true,
scripts: {
start: 'node ./bin/www'
},
dependencies: {
'cookie-parser': '~1.4.3',
'debug': '~2.6.9',
'express': '~4.16.0',
'morgan': '~1.9.0'
}
}
// Static files
app.locals.uses.push("express.static(path.join(__dirname, 'public'))")

switch (program.view) {
case 'dust':
Expand Down Expand Up @@ -302,13 +317,13 @@ function createApplication (name, dir) {
break
}

// sort dependencies like npm(1)
pkg.dependencies = sortedObject(pkg.dependencies)

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')
Expand Down
9 changes: 1 addition & 8 deletions templates/js/app.js.ejs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
<% Object.keys(modules).forEach(function (variable) { -%>
<% Object.keys(modules).sort().forEach(function (variable) { -%>
var <%- variable %> = require('<%- modules[variable] %>');
<% }); -%>

Expand All @@ -18,14 +16,9 @@ app.engine('<%- view.engine %>', <%- view.render %>);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', '<%- view.engine %>');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
<% uses.forEach(function (use) { -%>
app.use(<%- use %>);
<% }); -%>
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/users', users);
Expand Down

0 comments on commit 64ccae9

Please sign in to comment.