Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to ES2015 #307

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ extends: standard
rules:
no-param-reassign: error
no-shadow: error
prefer-const: error
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ This generator can also be further configured with the following command line fl

--version output the version number
-e, --ejs add ejs engine support
--es5 use ES5 syntax (defaults to ES2015 syntax)
--pug add pug engine support
--hbs add handlebars engine support
-H, --hogan add hogan.js engine support
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ test_script:
npm --version
# Run test script
- npm run test-ci
version: "{build}"
version: '{build}'
99 changes: 50 additions & 49 deletions bin/express-cli.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#!/usr/bin/env node

var ejs = require('ejs')
var fs = require('fs')
var minimatch = require('minimatch')
var mkdirp = require('mkdirp')
var parseArgs = require('minimist')
var path = require('path')
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
const ejs = require('ejs')
const fs = require('fs')
const minimatch = require('minimatch')
const mkdirp = require('mkdirp')
const parseArgs = require('minimist')
const path = require('path')
const readline = require('readline')
const sortedObject = require('sorted-object')
const util = require('util')

const MODE_0666 = parseInt('0666', 8)
const MODE_0755 = parseInt('0755', 8)
const TEMPLATE_DIR = path.join(__dirname, '..', 'templates')
const VERSION = require('../package').version

// parse args
var unknown = []
var args = parseArgs(process.argv.slice(2), {
const unknown = []
const args = parseArgs(process.argv.slice(2), {
alias: {
c: 'css',
e: 'ejs',
Expand All @@ -26,10 +26,10 @@ var args = parseArgs(process.argv.slice(2), {
H: 'hogan',
v: 'view'
},
boolean: ['ejs', 'force', 'git', 'hbs', 'help', 'hogan', 'pug', 'version'],
boolean: ['ejs', 'es5', 'force', 'git', 'hbs', 'help', 'hogan', 'pug', 'version'],
default: { css: true, view: true },
string: ['css', 'view'],
unknown: function (s) {
unknown: s => {
if (s.charAt(0) === '-') {
unknown.push(s)
}
Expand All @@ -46,12 +46,12 @@ main(args, exit)
*/

function confirm (msg, callback) {
var rl = readline.createInterface({
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})

rl.question(msg, function (input) {
rl.question(msg, input => {
rl.close()
callback(/^y|yes|ok|true$/i.test(input))
})
Expand All @@ -72,7 +72,7 @@ function copyTemplate (from, to) {
function copyTemplateMulti (fromDir, toDir, nameGlob) {
fs.readdirSync(path.join(TEMPLATE_DIR, fromDir))
.filter(minimatch.filter(nameGlob, { matchBase: true }))
.forEach(function (name) {
.forEach((name) => {
copyTemplate(path.join(fromDir, name), path.join(toDir, name))
})
}
Expand All @@ -90,8 +90,8 @@ function createApplication (name, dir, options, done) {
console.log()

// Package
var pkg = {
name: name,
const pkg = {
name,
version: '0.0.0',
private: true,
scripts: {
Expand All @@ -104,8 +104,8 @@ function createApplication (name, dir, options, done) {
}

// JavaScript
var app = loadTemplate('js/app.js')
var www = loadTemplate('js/www')
const app = loadTemplate('js/app.js')
const www = loadTemplate('js/www')

// App name
www.locals.name = name
Expand Down Expand Up @@ -288,24 +288,24 @@ function createApplication (name, dir, options, done) {
mkdir(dir, 'bin')
write(path.join(dir, 'bin/www'), www.render(), MODE_0755)

var prompt = launchedFromCmd() ? '>' : '$'
const prompt = launchedFromCmd() ? '>' : '$'

if (dir !== '.') {
console.log()
console.log(' change directory:')
console.log(' %s cd %s', prompt, dir)
console.log(` ${prompt} cd ${dir}`)
}

console.log()
console.log(' install dependencies:')
console.log(' %s npm install', prompt)
console.log(` ${prompt} npm install`)
console.log()
console.log(' run the app:')

if (launchedFromCmd()) {
console.log(' %s SET DEBUG=%s:* & npm start', prompt, name)
console.log(` ${prompt} SET DEBUG=${name}:* & npm start`)
} else {
console.log(' %s DEBUG=%s:* npm start', prompt, name)
console.log(` ${prompt} DEBUG=${name}:* npm start`)
}

console.log()
Expand Down Expand Up @@ -334,7 +334,7 @@ function createAppName (pathName) {
*/

function emptyDirectory (dir, fn) {
fs.readdir(dir, function (err, files) {
fs.readdir(dir, (err, files) => {
if (err && err.code !== 'ENOENT') throw err
fn(!files || !files.length)
})
Expand All @@ -348,8 +348,8 @@ function emptyDirectory (dir, fn) {

function error (message) {
console.error()
message.split('\n').forEach(function (line) {
console.error(' error: %s', line)
message.split('\n').forEach(line => {
console.error(` error: ${line}`)
})
console.error()
}
Expand All @@ -366,12 +366,12 @@ function exit (code) {
if (!(draining--)) process.exit(code)
}

var draining = 0
var streams = [process.stdout, process.stderr]
let draining = 0
const streams = [process.stdout, process.stderr]

exit.exited = true

streams.forEach(function (stream) {
streams.forEach(stream => {
// submit empty write request and wait for completion
draining += 1
stream.write('', done)
Expand All @@ -394,8 +394,8 @@ function launchedFromCmd () {
*/

function loadTemplate (name) {
var contents = fs.readFileSync(path.join(__dirname, '..', 'templates', (name + '.ejs')), 'utf-8')
var locals = Object.create(null)
const contents = fs.readFileSync(path.join(__dirname, '..', 'templates', (name + '.ejs')), 'utf-8')
const locals = Object.create(null)

function render () {
return ejs.render(contents, locals, {
Expand All @@ -404,8 +404,8 @@ function loadTemplate (name) {
}

return {
locals: locals,
render: render
locals,
render
}
}

Expand Down Expand Up @@ -435,10 +435,10 @@ function main (options, done) {
done(1)
} else {
// Path
var destinationPath = options._[0] || '.'
const destinationPath = options._[0] || '.'

// App name
var appName = createAppName(path.resolve(destinationPath)) || 'hello-world'
const appName = createAppName(path.resolve(destinationPath)) || 'hello-world'

// View engine
if (options.view === true) {
Expand Down Expand Up @@ -471,11 +471,11 @@ function main (options, done) {
}

// Generate application
emptyDirectory(destinationPath, function (empty) {
emptyDirectory(destinationPath, empty => {
if (empty || options.force) {
createApplication(appName, destinationPath, options, done)
} else {
confirm('destination is not empty, continue? [y/N] ', function (ok) {
confirm('destination is not empty, continue? [y/N] ', ok => {
if (ok) {
process.stdin.destroy()
createApplication(appName, destinationPath, options, done)
Expand All @@ -497,9 +497,9 @@ function main (options, done) {
*/

function mkdir (base, dir) {
var loc = path.join(base, dir)
const loc = path.join(base, dir)

console.log(' \x1b[36mcreate\x1b[0m : ' + loc + path.sep)
console.log(` \x1b[36mcreate\x1b[0m : ${loc} ${path.sep}`)
mkdirp.sync(loc, MODE_0755)
}

Expand All @@ -514,6 +514,7 @@ function usage () {
console.log(' Options:')
console.log('')
console.log(' -e, --ejs add ejs engine support')
console.log(' --es5 use ES5 syntax (defaults to ES2015 syntax)')
console.log(' --pug add pug engine support')
console.log(' --hbs add handlebars engine support')
console.log(' -H, --hogan add hogan.js engine support')
Expand Down Expand Up @@ -542,8 +543,8 @@ function version () {

function warning (message) {
console.error()
message.split('\n').forEach(function (line) {
console.error(' warning: %s', line)
message.split('\n').forEach(line => {
console.error(` warning: ${line}`)
})
console.error()
}
Expand All @@ -557,5 +558,5 @@ function warning (message) {

function write (file, str, mode) {
fs.writeFileSync(file, str, { mode: mode || MODE_0666 })
console.log(' \x1b[36mcreate\x1b[0m : ' + file)
console.log(` \x1b[36mcreate\x1b[0m : ${file}`)
}
31 changes: 26 additions & 5 deletions templates/js/app.js.ejs
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
<% if (view) { -%>
<% if (view && es5) { -%>
var createError = require('http-errors');
<% } else if (view) { -%>
const createError = require('http-errors');
<% } -%>

<% if (es5) { -%>
var express = require('express');
var path = require('path');
<% Object.keys(modules).sort().forEach(function (variable) { -%>
<% } else { -%>
const express = require('express');
const path = require('path');
<% } -%>

<% Object.keys(modules).sort().forEach(variable => { -%>
var <%- variable %> = require('<%- modules[variable] %>');
<% }); -%>

<% Object.keys(localModules).sort().forEach(function (variable) { -%>
<% Object.keys(localModules).sort().forEach(variable => { -%>
var <%- variable %> = require('<%- localModules[variable] %>');
<% }); -%>

<% if (es5) { -%>
var app = express();
<% } else { -%>
const app = express();
<% } -%>

<% if (view) { -%>
// view engine setup
Expand All @@ -22,22 +35,30 @@ app.set('views', path.join(__dirname, 'views'));
app.set('view engine', '<%- view.engine %>');

<% } -%>
<% uses.forEach(function (use) { -%>
<% uses.forEach(use => { -%>
app.use(<%- use %>);
<% }); -%>

<% mounts.forEach(function (mount) { -%>
<% mounts.forEach(mount => { -%>
app.use(<%= mount.path %>, <%- mount.code %>);
<% }); -%>

<% if (view) { -%>
// catch 404 and forward to error handler
<% if (es5) { -%>
app.use(function(req, res, next) {
<% } else { -%>
app.use((req, res, next) => {
<% } -%>
next(createError(404));
});

// error handler
<% if (es5) { -%>
app.use(function(err, req, res, next) {
<% } else { -%>
app.use((err, req, res, next) => {
<% } -%>
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
<% if (es5) { -%>
var express = require('express');
var router = express.Router();
<% } else { -%>
const express = require('express');
const router = express.Router();
<% } -%>

/* GET home page. */
<% if (es5) { -%>
router.get('/', function(req, res, next) {
<% } else { -%>
router.get('/', (req, res, next) => {
<% } -%>
res.render('index', { title: 'Express' });
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
<% if (es5) { -%>
var express = require('express');
var router = express.Router();
<% } else { -%>
const express = require('express');
const router = express.Router();
<% } -%>

/* GET users listing. */
<% if (es5) { -%>
router.get('/', function(req, res, next) {
<% } else { -%>
router.get('/', (req, res, next) => {
<% } -%>
res.send('respond with a resource');
});

Expand Down
Loading