From b90320a5dd3ef9e5808995c38d7d16d86b32ea03 Mon Sep 17 00:00:00 2001 From: Jeff Dickey <216188+jdxcode@users.noreply.github.com> Date: Wed, 31 Jan 2018 21:49:41 -0800 Subject: [PATCH] fix: consolidate commands and tests --- src/generators/app.ts | 25 ++++++++++++++---------- src/generators/command.ts | 6 +++--- templates/single/src/index.js.ejs | 16 --------------- templates/single/src/index.ts.ejs | 13 ------------ templates/single/test/index.test.js | 18 ----------------- templates/single/test/index.test.ts | 17 ---------------- templates/{ => src}/command.js.ejs | 0 templates/{ => src}/command.ts.ejs | 4 ++++ templates/{ => test}/command.test.js.ejs | 11 +++++++++++ templates/{ => test}/command.test.ts.ejs | 16 +++++++++++++-- 10 files changed, 47 insertions(+), 79 deletions(-) delete mode 100644 templates/single/src/index.js.ejs delete mode 100644 templates/single/src/index.ts.ejs delete mode 100644 templates/single/test/index.test.js delete mode 100644 templates/single/test/index.test.ts rename templates/{ => src}/command.js.ejs (100%) rename templates/{ => src}/command.ts.ejs (97%) rename templates/{ => test}/command.test.js.ejs (63%) rename templates/{ => test}/command.test.ts.ejs (52%) diff --git a/src/generators/app.ts b/src/generators/app.ts index f5c9b12..b04a868 100644 --- a/src/generators/app.ts +++ b/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'}) } } diff --git a/src/generators/command.ts b/src/generators/command.ts index 8dac05a..92fd09a 100644 --- a/src/generators/command.ts +++ b/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) } diff --git a/templates/single/src/index.js.ejs b/templates/single/src/index.js.ejs deleted file mode 100644 index 55fa72b..0000000 --- a/templates/single/src/index.js.ejs +++ /dev/null @@ -1,16 +0,0 @@ -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 %> diff --git a/templates/single/src/index.ts.ejs b/templates/single/src/index.ts.ejs deleted file mode 100644 index 6765b3f..0000000 --- a/templates/single/src/index.ts.ejs +++ /dev/null @@ -1,13 +0,0 @@ -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}!`) - } -} diff --git a/templates/single/test/index.test.js b/templates/single/test/index.test.js deleted file mode 100644 index bcda5e0..0000000 --- a/templates/single/test/index.test.js +++ /dev/null @@ -1,18 +0,0 @@ -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') - }) -}) diff --git a/templates/single/test/index.test.ts b/templates/single/test/index.test.ts deleted file mode 100644 index 9ecb789..0000000 --- a/templates/single/test/index.test.ts +++ /dev/null @@ -1,17 +0,0 @@ -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!') -}) diff --git a/templates/command.js.ejs b/templates/src/command.js.ejs similarity index 100% rename from templates/command.js.ejs rename to templates/src/command.js.ejs diff --git a/templates/command.ts.ejs b/templates/src/command.ts.ejs similarity index 97% rename from templates/command.ts.ejs rename to templates/src/command.ts.ejs index 6cb209c..9b1be51 100644 --- a/templates/command.ts.ejs +++ b/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) diff --git a/templates/command.test.js.ejs b/templates/test/command.test.js.ejs similarity index 63% rename from templates/command.test.js.ejs rename to templates/test/command.test.js.ejs index 70801bc..c90f25c 100644 --- a/templates/command.test.js.ejs +++ b/templates/test/command.test.js.ejs @@ -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 %>!') }) diff --git a/templates/command.test.ts.ejs b/templates/test/command.test.ts.ejs similarity index 52% rename from templates/command.test.ts.ejs rename to templates/test/command.test.ts.ejs index 436838b..a65ac06 100644 --- a/templates/command.test.ts.ejs +++ b/templates/test/command.test.ts.ejs @@ -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 %>!') })