Skip to content

Commit

Permalink
Fixed issue #29
Browse files Browse the repository at this point in the history
Added espree to parse the given paramater as a js function, this prevents invalid names
  • Loading branch information
MattMcFarland committed Nov 19, 2016
1 parent 3bc66bc commit 8777b43
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 10 deletions.
18 changes: 15 additions & 3 deletions bin/choo-generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,31 @@ const yaml = require('yamljs')
const fs = require('fs')
const help = require('./help')
const _ = require('lodash')
const { parse } = require('espree')
const chalk = require('chalk')
const args = process.argv.slice(2)
const resolvePath = require('path').resolve
const requestedGeneratorName = args[0]
const fileName = _.kebabCase(args[1])
const name = _.camelCase(args[1])
const { findConfig, findRootPath, generate, message } = require('../lib/utils')
const config = yaml.parse(fs.readFileSync(findConfig(), 'utf8'))
const chalk = require('chalk')
let config

// message(config);
try {
config = yaml.parse(fs.readFileSync(findConfig(), 'utf8'))
} catch (e) {
message(chalk.red(' choo.yaml not found'))
process.exit(1)
}
const availableGenerators = Object.keys(config.generators)

function createFromTemplate (options) {
try {
parse(`function ${options.name} () {}`)
} catch (e) {
message(chalk.red(options.name, 'is an invalid name'))
process.exit(1)
}
generate(options.templatePath, options.target, {
name: options.name,
fileName: options.fileName
Expand Down
11 changes: 9 additions & 2 deletions lib/genus.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const fs = require('fs')
const yaml = require('yamljs')
const rimraf = require('rimraf')
const mkdirp = require('mkdirp')
const { parse } = require('espree')

const choochoo = fs.readFileSync(path.resolve(__dirname, '../choo!')).toString()
function formatState (state) {
Expand All @@ -27,6 +28,12 @@ function formatState (state) {
}

module.exports = (props) => {
try {
parse(`function ${props.projectName} () {}`)
} catch (e) {
message(chalk.red(props.projectName, 'is an invalid name'))
process.exit(1)
}
const destinationPath = newProjectPath(props.projectName)
const templateRepo = (!props.templateRepo) ? 'trainyard/template-basic' : props.templateRepo
const generatorRootPath = path.join(destinationPath(), '.generators', '.downloaded', templateRepo.split('/').pop())
Expand Down Expand Up @@ -115,11 +122,11 @@ function generateApp (source, destinationPath, props, required) {
})

message(`
${chalk.yellow('HOORAY!')} Your application scaffold is complete!
${choochoo}
Running ${chalk.yellow.bold('npm install')} for you...
`)
xfs.commit(npmInstall)
}
1 change: 1 addition & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const findRootPath = once(() => {
}
}
})

const destinationPath = (dir) => resolvePath(findRootPath(), dir || '')
const npmInstall = process.env.NODE_ENV === 'test' ? () => { } : () => process.nextTick(() => {
exec('npm', ['install', '-s'], { cwd: destinationPath() })
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"chalkline": "0.0.5",
"cli-spinners": "^0.3.0",
"cross-spawn": "^5.0.1",
"espree": "^3.3.2",
"inquirer": "^1.1.2",
"left-pad": "^1.1.1",
"lodash": "^4.14.1",
Expand Down
13 changes: 11 additions & 2 deletions tests/generators/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ const test = require('tape')
const testUtils = require('../../lib/test-utils')
const clinton = require('clinton')
const exec = require('../../lib/exec')
const spawn = require('cross-spawn')

test('App Generator', t => {
t.plan(15)
exec('choo-new.js', ['temp'], {
cwd: testUtils.cwd
}, () => {
Expand Down Expand Up @@ -43,10 +45,17 @@ test('App Generator', t => {
t.notOk(check, check.message)
}
})
t.end()
const execShouldFail = spawn('choo-new', [500], {
env: process.env,
stdio: 'inherit'
})

execShouldFail.on('exit', (code) => {
t.assert(code === 1, 'choo-new should fail when given invalid syntax')
})
}).catch(errors => {
t.notOk(errors)
t.end()
})
})
})

11 changes: 10 additions & 1 deletion tests/generators/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
const test = require('tape')
const testUtils = require('../../lib/test-utils')
const exec = require('../../lib/exec')
const spawn = require('cross-spawn')

test('Element Generator', t => {
t.plan(2)
exec('choo-generate.js', ['element', 'testElement'], {
cwd: testUtils.tempDir
}, () => {
Expand All @@ -12,6 +14,13 @@ test('Element Generator', t => {
]).forEach(file => {
t.assert(file.exists, `${file.name} must be generated.`)
})
t.end()
})
const execShouldFail = spawn('choo-generate.js', ['element', 500], {
env: process.env,
stdio: 'inherit'
})

execShouldFail.on('exit', (code) => {
t.assert(code === 1, 'choo-new should fail when given invalid syntax')
})
})
11 changes: 10 additions & 1 deletion tests/generators/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
const test = require('tape')
const testUtils = require('../../lib/test-utils')
const exec = require('../../lib/exec')
const spawn = require('cross-spawn')

test('Model Generator', t => {
t.plan(2)
exec('choo-generate.js', ['model', 'testModel'], {
cwd: testUtils.tempDir
}, () => {
Expand All @@ -12,6 +14,13 @@ test('Model Generator', t => {
]).forEach(file => {
t.assert(file.exists, `${file.name} must be generated.`)
})
t.end()
})
const execShouldFail = spawn('choo-generate.js', ['element', 500], {
env: process.env,
stdio: 'inherit'
})

execShouldFail.on('exit', (code) => {
t.assert(code === 1, 'choo-new should fail when given invalid syntax')
})
})
11 changes: 10 additions & 1 deletion tests/generators/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
const test = require('tape')
const testUtils = require('../../lib/test-utils')
const exec = require('../../lib/exec')
const spawn = require('cross-spawn')

test('Page Generator', t => {
t.plan(2)
exec('choo-generate.js', ['page', 'testPage'], {
cwd: testUtils.tempDir
}, () => {
Expand All @@ -12,6 +14,13 @@ test('Page Generator', t => {
]).forEach(file => {
t.assert(file.exists, `${file.name} must be generated.`)
})
t.end()
})
const execShouldFail = spawn('choo-generate.js', ['element', 500], {
env: process.env,
stdio: 'inherit'
})

execShouldFail.on('exit', (code) => {
t.assert(code === 1, 'choo-new should fail when given invalid syntax')
})
})

0 comments on commit 8777b43

Please sign in to comment.