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

Improves generated CLI, adds example tests #409

Merged
merged 2 commits into from
Dec 19, 2018
Merged
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
9 changes: 5 additions & 4 deletions src/cli/commands/new.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function createFakeToolbox(): Toolbox {
}
fakeContext.system = {
spawn: sinon.stub(),
which: sinon.stub(),
}
fakeContext.template = { generate: sinon.stub() }
fakeContext.print = {
Expand Down Expand Up @@ -93,6 +94,7 @@ test('generates properly', async () => {
})

const DEFAULT_FILES = [
['__tests__/cli-integration.test.js.ejs', '__tests__/cli-integration.test.js'],
['docs/commands.md.ejs', 'docs/commands.md'],
['docs/plugins.md.ejs', 'docs/plugins.md'],
['src/commands/generate.js.ejs', 'src/commands/generate.js'],
Expand All @@ -101,7 +103,6 @@ test('generates properly', async () => {
['src/templates/model.js.ejs.ejs', 'src/templates/model.js.ejs'],
['src/cli.js.ejs', 'src/cli.js'],
['LICENSE.ejs', 'LICENSE'],
['.prettierrc.ejs', '.prettierrc'],
['package.json.ejs', 'package.json'],
['readme.md.ejs', 'readme.md'],
['.gitignore.ejs', '.gitignore'],
Expand All @@ -121,7 +122,7 @@ test('generates properly', async () => {

// test package installation
expect(spawn.firstCall.args).toEqual([
`cd ${props.name} && npm install --quiet && npm run --quiet format`,
Copy link
Collaborator

Choose a reason for hiding this comment

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

oops

`cd ${props.name} && npm install --silent && npm run --quiet format`,
{ shell: true, stdio: 'inherit', stderr: 'inherit' },
])

Expand Down Expand Up @@ -159,6 +160,7 @@ test('generates with typescript', async () => {
})

const DEFAULT_FILES = [
['__tests__/cli-integration.test.js.ejs', '__tests__/cli-integration.test.ts'],
['docs/commands.md.ejs', 'docs/commands.md'],
['docs/plugins.md.ejs', 'docs/plugins.md'],
['src/commands/generate.js.ejs', 'src/commands/generate.ts'],
Expand All @@ -167,7 +169,6 @@ test('generates with typescript', async () => {
['src/templates/model.js.ejs.ejs', 'src/templates/model.ts.ejs'],
['src/cli.js.ejs', 'src/cli.ts'],
['LICENSE.ejs', 'LICENSE'],
['.prettierrc.ejs', '.prettierrc'],
['package.json.ejs', 'package.json'],
['readme.md.ejs', 'readme.md'],
['.gitignore.ejs', '.gitignore'],
Expand All @@ -187,7 +188,7 @@ test('generates with typescript', async () => {

// test package installation
expect(spawn.firstCall.args).toEqual([
`cd ${props.name} && npm install --quiet && npm run --quiet format`,
`cd ${props.name} && npm install --silent && npm run --quiet format`,
{ shell: true, stdio: 'inherit', stderr: 'inherit' },
])

Expand Down
11 changes: 8 additions & 3 deletions src/cli/commands/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default {
)

const files = [
'__tests__/cli-integration.test.js.ejs',
'docs/commands.md.ejs',
'docs/plugins.md.ejs',
'src/commands/generate.js.ejs',
Expand All @@ -53,14 +54,14 @@ export default {
'src/templates/model.js.ejs.ejs',
'src/cli.js.ejs',
'LICENSE.ejs',
'.prettierrc.ejs',
'package.json.ejs',
'readme.md.ejs',
'.gitignore.ejs',
]

if (props.typescript) {
files.push('tsconfig.json.ejs')
files.push('tslint.json.ejs')
}

active = files.reduce((prev, file) => {
Expand All @@ -84,7 +85,10 @@ export default {
const ext = props.typescript ? 'ts' : 'js'
filesystem.rename(`${props.name}/src/commands/default.${ext}`, `${props.name}.${ext}`)

await system.spawn(`cd ${props.name} && npm install --quiet && npm run --quiet format`, {
// install with yarn or npm i
const yarnOrNpm = system.which('yarn') ? 'yarn' : 'npm'

await system.spawn(`cd ${props.name} && ${yarnOrNpm} install --silent && ${yarnOrNpm} run --quiet format`, {
shell: true,
stdio: 'inherit',
stderr: 'inherit',
Expand All @@ -94,7 +98,8 @@ export default {
print.info(``)
print.info(`Next:`)
print.info(` $ cd ${props.name}`)
print.info(` $ npm link`)
print.info(` $ ${yarnOrNpm} test`)
print.info(` $ ${yarnOrNpm} link`)
print.info(` $ ${props.name}`)
print.info(``)

Expand Down
10 changes: 0 additions & 10 deletions src/cli/templates/cli/.prettierrc.ejs

This file was deleted.

33 changes: 33 additions & 0 deletions src/cli/templates/cli/__tests__/cli-integration.test.js.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { system, filesystem } = require('gluegun')
const { resolve } = require('path')

const src = resolve(__dirname, '..')

const cli = async cmd => system.run(resolve(src, 'bin', '<%= props.name %>') + ` ${cmd}`)

test('outputs version', async () => {
const output = await cli('--version')
expect(output).toContain('0.0.1')
})

test('outputs help', async () => {
const output = await cli('--help')
expect(output).toContain('0.0.1')
})

test('generates file', async () => {
const output = await cli('generate foo')
<% if (props.typescript) { %>
expect(output).toContain('Generated file at models/foo-model.ts')
const foomodel = filesystem.read('models/foo-model.ts')
<% } else { %>
expect(output).toContain('Generated file at models/foo-model.js')
const foomodel = filesystem.read('models/foo-model.js')
<% } %>

expect(foomodel).toContain(`module.exports = {`)
expect(foomodel).toContain(`name: 'foo'`)

// cleanup artifact
filesystem.remove('models')
})
29 changes: 23 additions & 6 deletions src/cli/templates/cli/package.json.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"scripts": {
<% if (props.typescript) { %>
"format": "prettier --write **/*.{js,ts,tsx,json}",
"lint": "tslint -p .",
<% } else { %>
"format": "prettier --write **/*.{js,json} && standard --fix",
"lint": "standard",
Expand All @@ -21,6 +22,7 @@
"files": [
<% if (props.typescript) { %>
"tsconfig.json",
"tslint.json",
<% } %>
"LICENSE",
"readme.md",
Expand All @@ -39,17 +41,32 @@
"devDependencies": {
<% if (props.typescript) { %>
"@types/node": "^10.12.12",
"@types/jest": "^23.3.10",
"ts-jest": "^23.10.5",
"tslint": "^5.12.0",
"tslint-config-prettier": "^1.17.0",
"tslint-config-standard": "^8.0.1",
<% } else { %>
"standard": "^12.0.1",
"babel-eslint": "^10.0.1",
<% } %>
"prettier": "^1.12.1",
"jest": "^23.6.0"
}
<% if (!props.typescript) { %>,
"standard": {
"parser": "babel-eslint"
}
},
"jest": {
<% if (props.typescript) { %>
"preset": "ts-jest",
<% } %>
"testEnvironment": "node"
},
<% if (!props.typescript) { %>
"standard": {
"env": [
"jest"
]
},
<% } %>
"prettier": {
"semi": false,
"singleQuote": true
}
}
14 changes: 10 additions & 4 deletions src/cli/templates/cli/src/commands/generate.js.ejs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
module.exports = {
name: 'generate',
alias: ['g'],
run: async (context) => {
const { parameters, template: { generate } } = context
run: async context => {
const {
parameters,
template: { generate },
print: { info },
} = context

const name = parameters.first

await generate({
template: 'model.<%= props.extension %>.ejs',
target: `models/${name}-model.<%= props.extension %>`,
props: { name }
props: { name },
})
}

info(`Generated file at models/${name}-model.<%= props.extension %>`)
},
}
9 changes: 9 additions & 0 deletions src/cli/templates/cli/tslint.json.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": ["tslint-config-standard", "tslint-config-prettier"],
"rules": {
"strict-type-predicates": false
},
"env": {
"jest": true
}
}