fix: consolidate commands and tests
MODIFY
src/generators/app.ts
src/generators/app.ts
@@ -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('/')
@@ -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'})
}
}

MODIFY
src/generators/command.ts
src/generators/command.ts
@@ -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)
}

DELETE
templates/single/src/index.js.ejs
-const {Command, flags} = require('@anycli/command')
-const {cli} = require('cli-ux')
-<%_ const klass = _.upperFirst(_.camelCase(name.split(':').slice(-1).join(':') + ':command')) _%>
-
-class <%- klass %> extends Command {
- async run() {
- const name = this.flags.name || 'world'
- cli.log(`hello ${name}!`)
- }
-}
-
-<%- klass %>.flags = {
- name: flags.string({char: 'n', description: 'name to print'}),
-}
-
-module.exports = <%- klass %>

DELETE
templates/single/src/index.ts.ejs
-import {Command, flags} from '@anycli/command'
-import cli from 'cli-ux'
-
-export default class <%- _.upperFirst(_.camelCase(name)) %> extends Command {
- static flags = {
- name: flags.string({char: 'n', description: 'name to print'})
- }
-
- async run() {
- const name = this.flags.name || 'world'
- cli.log(`hello ${name}!`)
- }
-}

DELETE
templates/single/test/index.test.js
-const {expect, test} = require('@anycli/test')
-const cmd = require('..')
-
-describe('command', () => {
- test
- .stdout()
- .do(() => cmd.run([]))
- .it('says hello world!', ctx => {
- expect(ctx.stdout).to.equal('hello world!\n')
- })
-
- test
- .stdout()
- .do(() => cmd.run(['--name', 'jeff']))
- .it('says hello jeff!', ctx => {
- expect(ctx.stdout).to.equal('hello jeff!\n')
- })
-})

DELETE
templates/single/test/index.test.ts
-import {expect, test} from '@anycli/test'
-
-import cmd from '../src'
-
-describe('command', () => {
- test
- .stdout()
- .do(() => cmd.run([]))
- .do(output => expect(output.stdout).to.equal('hello world!\n'))
- .it('says hello world!')
-
- test
- .stdout()
- .do(() => cmd.run(['--name', 'jeff']))
- .do(output => expect(output.stdout).to.equal('hello jeff!\n'))
- .it('says hello jeff!')
-})

RENAME
templates/command.js.ejs
templates/src/command.js.ejs
RENAME
templates/command.ts.ejs
templates/src/command.ts.ejs
@@ -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
@@ -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)

RENAME
templates/command.test.js.ejs
templates/test/command.test.js.ejs
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 %>!')
})

RENAME
templates/command.test.ts.ejs
templates/test/command.test.ts.ejs
import {expect, test} from '@anycli/test'
+<%_ if (type === 'single') { _%>
+
+import cmd from '../src'
+<%_ } _%>
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 %>!')
})

fix: consolidate commands and tests