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

Create App with interactive Menu #209

Closed
wants to merge 11 commits into from
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ cd my-module/

- [Developing React Apps with nwb](/docs/guides/ReactApps.md)
- [Developing React Components and Libraries with nwb](/docs/guides/ReactComponents.md#developing-react-components-and-libraries-with-nwb)
- [Switching to nwb from create-react-app][https://github.com/insin/nwb-from-create-react-app/] (as an alternative to ejecting if you need configuration)
- [Switching to nwb from create-react-app](https://github.com/insin/nwb-from-create-react-app/) (as an alternative to ejecting if you need configuration)

## [Documentation](/docs/#table-of-contents)

Expand Down
52 changes: 29 additions & 23 deletions src/commands/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,39 @@ import path from 'path'

import glob from 'glob'

import {PROJECT_TYPES} from '../constants'
import createProject, {validateProjectType} from '../createProject'
import {UserError} from '../errors'
import interactiveNew from '../interactiveNew'

export default function new_(args, cb) {
if (args._.length === 1) {
return cb(new UserError(`usage: nwb new [${PROJECT_TYPES.join('|')}] <name>`))
}
Promise.resolve()
.then(() => {
if (args._.length > 1) {
return Object.assign({}, args._, {
projectType: args._[1],
projectName: args._[2]
})
}
return interactiveNew(args)
}).then(options => {
const { projectName, projectType } = options
try {
validateProjectType(projectType)
}
catch (e) {
return cb(e)
}
if (!projectName) {
return cb(new UserError('A project name must be provided'))
}
if (glob.sync(`${projectName}/`).length !== 0) {
return cb(new UserError(`A ${projectName}/ directory already exists`))
}

let projectType = args._[1]
try {
validateProjectType(projectType)
}
catch (e) {
return cb(e)
}
let targetDir = path.resolve(projectName)
let initialVowel = /^[aeiou]/.test(projectType)

let name = args._[2]
if (!name) {
return cb(new UserError('A project name must be provided'))
}
if (glob.sync(`${name}/`).length !== 0) {
return cb(new UserError(`A ${name}/ directory already exists`))
}

let targetDir = path.resolve(name)
let initialVowel = /^[aeiou]/.test(projectType)
console.log(`Creating ${initialVowel ? 'an' : 'a'} ${projectType} project...`)
createProject(args, projectType, name, targetDir, cb)
console.log(`Creating ${initialVowel ? 'an' : 'a'} ${projectType} project...`)
createProject(args, projectType, projectName, targetDir, cb)
}).catch(e => new UserError(e))
}
28 changes: 28 additions & 0 deletions src/interactiveNew.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import inquirer from 'inquirer'
import {PROJECT_TYPES} from './constants'

export default function interactiveNew_(args) {
const prompts = [
{
type: 'list',
name: 'projectType',
message: 'Select a Project Type',
choices: PROJECT_TYPES.map(type => {
const displayName = type.replace('-', ' ').split(' ').map(x => x[0].toUpperCase() + x.slice(1)).join(' ')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should create a new constant for every projectType with a pretty name?

return {
name: displayName,
value: type
}
})
},
{
type: 'input',
name: 'projectName',
message: 'Enter a Project Name',
filter: input => input.split(' ').join('-')
Copy link
Contributor Author

@ntwcklng ntwcklng Dec 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with that, its possible to create projects with spaces inside their names

}
]
return inquirer
.prompt(prompts)
.then(answers => Object.assign({}, args, answers))
}
28 changes: 14 additions & 14 deletions tests/commands/new-init-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,20 +173,20 @@ describe('command: nwb new', function() {

describe('with missing or invalid arguments', function() {
this.timeout(200)
it('prints usage info without any arguments', done => {
cli(['new'], err => {
expect(err).toExist()
expect(err.message).toContain('usage: nwb new')
done()
})
})
it('requires a project type', done => {
cli(['new', ''], err => {
expect(err).toExist()
expect(err.message).toContain('A project type must be provided')
done()
})
})
// it('prints usage info without any arguments', done => {
// cli(['new'], err => {
// expect(err).toExist()
// expect(err.message).toContain('usage: nwb new')
// done()
// })
// })
// it('requires a project type', done => {
// cli(['new', ''], err => {
// expect(err).toExist()
// expect(err.message).toContain('A project type must be provided')
// done()
// })
// })
it('requires a valid project type', done => {
cli(['new', 'test-app'], err => {
expect(err).toExist()
Expand Down