Skip to content

Commit

Permalink
fix: consolidate commands and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Feb 1, 2018
1 parent 8c5c179 commit b90320a
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 79 deletions.
25 changes: 15 additions & 10 deletions src/generators/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class App extends Generator {
engines: {},
devDependencies: {},
dependencies: {},
anycli: {},
...this.fs.readJSON('package.json', {}),
}
let repository = this.destinationRoot().split(path.sep).slice(-2).join('/')
Expand Down Expand Up @@ -426,28 +427,32 @@ class App extends Generator {
let bin = this.pjson.anycli.bin || this.pjson.anycli.dirname || this.pjson.name
if (bin.includes('/')) bin = bin.split('/').pop()
const cmd = `${bin} hello`
this.fs.copyTpl(this.templatePath('plugin/bin/run'), this.destinationPath('bin/run'), this)
this.fs.copyTpl(this.templatePath('bin/run.cmd'), this.destinationPath('bin/run.cmd'), this)
this.fs.copyTpl(this.templatePath(`command.${this._ext}.ejs`), this.destinationPath(`src/commands/hello.${this._ext}`), {name: 'hello', _, bin, cmd})
const opts = {...this as any, _, bin, cmd}
this.fs.copyTpl(this.templatePath('plugin/bin/run'), this.destinationPath('bin/run'), opts)
this.fs.copyTpl(this.templatePath('bin/run.cmd'), this.destinationPath('bin/run.cmd'), opts)
this.fs.copyTpl(this.templatePath(`src/command.${this._ext}.ejs`), this.destinationPath(`src/commands/hello.${this._ext}`), {...opts, name: 'hello'})
if (this.ts) {
this.fs.copyTpl(this.templatePath('plugin/src/index.ts'), this.destinationPath('src/index.ts'), this)
this.fs.copyTpl(this.templatePath('plugin/src/index.ts'), this.destinationPath('src/index.ts'), opts)
}
if (this.mocha) {
this.fs.copyTpl(this.templatePath(`command.test.${this._ext}.ejs`), this.destinationPath(`test/commands/hello.test.${this._ext}`), {name: 'hello', _, bin, cmd})
this.fs.copyTpl(this.templatePath(`test/command.test.${this._ext}.ejs`), this.destinationPath(`test/commands/hello.test.${this._ext}`), {...opts, name: 'hello'})
}
}

private _writeSingle() {
if (!this.fromScratch) return
let bin = this.pjson.anycli.bin || this.pjson.anycli.dirname || this.pjson.name
if (bin.includes('/')) bin = bin.split('/').pop()
const opts = {...this as any, _, bin, cmd: bin}
if (this.ts) {
this.fs.copyTpl(this.templatePath('single/bin/run.ts'), this.destinationPath('bin/run'), this)
this.fs.copyTpl(this.templatePath('single/bin/run.ts'), this.destinationPath('bin/run'), opts)
} else {
this.fs.copyTpl(this.templatePath('bin/run'), this.destinationPath('bin/run'), this)
this.fs.copyTpl(this.templatePath('bin/run'), this.destinationPath('bin/run'), opts)
}
this.fs.copyTpl(this.templatePath('bin/run.cmd'), this.destinationPath('bin/run.cmd'), this)
this.fs.copyTpl(this.templatePath(`single/src/index.${this._ext}.ejs`), this.destinationPath(`src/index.${this._ext}`), {name: this.pjson.name, _})
this.fs.copyTpl(this.templatePath('bin/run.cmd'), this.destinationPath('bin/run.cmd'), opts)
this.fs.copyTpl(this.templatePath(`src/command.${this._ext}.ejs`), this.destinationPath(`src/index.${this._ext}`), {...opts, name: 'hello'})
if (this.mocha) {
this.fs.copyTpl(this.templatePath(`single/test/index.test.${this._ext}`), this.destinationPath(`test/index.test.${this._ext}`), this)
this.fs.copyTpl(this.templatePath(`test/command.test.${this._ext}.ejs`), this.destinationPath(`test/index.test.${this._ext}`), {...opts, name: 'hello'})
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/generators/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ class CommandGenerator extends Generator {
let bin = this.pjson.anycli.bin || this.pjson.anycli.dirname || this.pjson.name
if (bin.includes('/')) bin = bin.split('/').pop()
const cmd = `${bin} ${this.options.name}`
const opts = {...this.options, bin, cmd, _}
this.fs.copyTpl(this.templatePath(`command.${this._ext}.ejs`), this.destinationPath(`src/commands/${this._path}.${this._ext}`), opts)
const opts = {...this.options, bin, cmd, _, type: 'command'}
this.fs.copyTpl(this.templatePath(`src/command.${this._ext}.ejs`), this.destinationPath(`src/commands/${this._path}.${this._ext}`), opts)
// this.fs.copyTpl(this.templatePath(`plugin/src/hooks/init.${this._ext}`), this.destinationPath(`src/hooks/init.${this._ext}`), this)
if (this._mocha) {
// this.fs.copyTpl(this.templatePath(`plugin/test/hooks/init.test.${this._ext}`), this.destinationPath(`test/hooks/init.test.${this._ext}`), this)
this.fs.copyTpl(this.templatePath(`command.test.${this._ext}.ejs`), this.destinationPath(`test/commands/${this._path}.test.${this._ext}`), opts)
this.fs.copyTpl(this.templatePath(`test/command.test.${this._ext}.ejs`), this.destinationPath(`test/commands/${this._path}.test.${this._ext}`), opts)
}
// this.fs.writeJSON(this.destinationPath('./package.json'), this.pjson)
}
Expand Down
16 changes: 0 additions & 16 deletions templates/single/src/index.js.ejs

This file was deleted.

13 changes: 0 additions & 13 deletions templates/single/src/index.ts.ejs

This file was deleted.

18 changes: 0 additions & 18 deletions templates/single/test/index.test.js

This file was deleted.

17 changes: 0 additions & 17 deletions templates/single/test/index.test.ts

This file was deleted.

File renamed without changes.
4 changes: 4 additions & 0 deletions templates/command.ts.ejs → templates/src/command.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import cli from 'cli-ux'

export default class <%- klass %> extends Command {
static title = 'scaffolded command that says hello'
<%_ if (type !== 'single') { _%>
// hide the command from help
// can also set hidden on args and flags
// static hidden = true
<%_ } _%>

// usage is set by default
// add your own by setting this variable
Expand All @@ -30,9 +32,11 @@ hello myname from <%- name %>!
'$ <%- cmd %> file outputs "hello world!" to file',
'$ <%- cmd %> --force',
]
<%_ if (type !== 'single') { _%>
// allow running this command by running `$ <%- bin %> foobar`
// static aliases = ['foobar']
<%_ } _%>

static flags = {
// flag with a value (-n, --name=VALUE)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
const {expect, test} = require('@anycli/test')
<%_ if (type === 'single') { _%>
const cmd = require('..')
<%_ } _%>

const command = '<%- name %>'

describe(command, () => {
test
.stdout()
<%_ if (type === 'single') { _%>
.do(() => cmd.run([]))
<%_ } else { _%>
.command([command])
<%_ } _%>
.it('runs hello', ctx => {
expect(ctx.stdout).to.contain('hello world from <%- name %>!')
})

test
.stdout()
<%_ if (type === 'single') { _%>
.do(() => cmd.run(['--name', 'jeff']))
<%_ } else { _%>
.command([command, '--name', 'jeff'])
<%_ } _%>
.it('runs hello --name jeff', ctx => {
expect(ctx.stdout).to.contain('hello jeff from <%- name %>!')
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import {expect, test} from '@anycli/test'
<%_ if (type === 'single') { _%>
import cmd from '../src'
<%_ } _%>

const command = '<%- name %>'

describe(command, () => {
test
.stdout()
.command([command])
<%_ if (type === 'single') { _%>
.do(() => cmd.run([]))
<%_ } else { _%>
.command([command])
<%_ } _%>
.it('runs hello', ctx => {
expect(ctx.stdout).to.contain('hello world from <%- name %>!')
})

test
.stdout()
.command([command, '--name', 'jeff'])
<%_ if (type === 'single') { _%>
.do(() => cmd.run(['--name', 'jeff']))
<%_ } else { _%>
.command([command, '--name', 'jeff'])
<%_ } _%>
.it('runs hello --name jeff', ctx => {
expect(ctx.stdout).to.contain('hello jeff from <%- name %>!')
})
Expand Down

0 comments on commit b90320a

Please sign in to comment.