Skip to content

Commit

Permalink
feat: added command generator
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 28, 2018
1 parent 6849aaf commit f6fef1f
Show file tree
Hide file tree
Showing 69 changed files with 247 additions and 267 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
"yosay": "^2.0.1"
},
"devDependencies": {
"@dxcli/dev": "^2.0.13",
"@dxcli/dev": "^2.0.14",
"@dxcli/semantic-release": "^0.3.3",
"@dxcli/test": "^0.9.17",
"@dxcli/tslint": "^0.0.24",
"@dxcli/tslint": "^0.1.0",
"@semantic-release/exec": "^2.0.0",
"@types/shelljs": "^0.7.7",
"@types/yeoman-generator": "^2.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/app_command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default abstract class AppCommand extends Base {

abstract type: string

async run() {
async run () {
const options = this.flags.options ? this.flags.options.split(',') : []

await super.generate('app', {
Expand Down
2 changes: 1 addition & 1 deletion src/command_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Command from '@dxcli/command'
import {createEnv} from 'yeoman-environment'

export default abstract class CommandBase extends Command {
protected async generate(type: string, generatorOptions: object = {}) {
protected async generate (type: string, generatorOptions: object = {}) {
const env = createEnv()

env.register(
Expand Down
27 changes: 27 additions & 0 deletions src/commands/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {flags} from '@dxcli/command'

import Base from '../command_base'

export interface Options {
name: string
defaults?: boolean
force?: boolean
}

export default abstract class AppCommand extends Base {
static flags: flags.Input = {
defaults: flags.boolean({description: 'use defaults for every setting'}),
force: flags.boolean({description: 'overwrite existing files'}),
}
static args = [
{name: 'name', description: 'name of command', required: true}
]

async run () {
await super.generate('command', {
name: this.args.name,
defaults: this.flags.defaults,
force: this.flags.force
})
}
}
39 changes: 16 additions & 23 deletions src/generators/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const fixpack = require('fixpack')
const debug = require('debug')('generator-dxcli')
const {version} = require('../../../package.json')

function stringToArray(s: string) {
function stringToArray (s: string) {
const keywords: string[] = []

s.split(',').forEach((keyword: string) => {
Expand Down Expand Up @@ -56,9 +56,9 @@ class App extends Generator {
mocha: boolean
semantic_release: boolean
ts: boolean
get _ext() { return this.ts ? 'ts' : 'js' }
get _ext () { return this.ts ? 'ts' : 'js' }

constructor(args: any, opts: any) {
constructor (args: any, opts: any) {
super(args, opts)

this.type = opts.type
Expand All @@ -71,7 +71,7 @@ class App extends Generator {
}
}

async prompting() {
async prompting () {
if (process.env.DXCLI_CREATE_DEFAULTS === '1') this.options.defaults = true
let msg
switch (this.type) {
Expand Down Expand Up @@ -230,8 +230,8 @@ class App extends Generator {
}
}

writing() {
this.sourceRoot(path.join(__dirname, '../../../templates/app'))
writing () {
this.sourceRoot(path.join(__dirname, '../../../templates'))

switch (this.type) {
case 'multi':
Expand Down Expand Up @@ -296,7 +296,7 @@ class App extends Generator {
}
}

install() {
install () {
const dependencies: string[] = []
const devDependencies = [
'@dxcli/dev',
Expand Down Expand Up @@ -362,7 +362,7 @@ class App extends Generator {
})
}

private _gitignore(): string {
private _gitignore (): string {
const existing = this.fs.exists(this.destinationPath('.gitignore')) ? this.fs.read(this.destinationPath('.gitignore')).split('\n') : []
return _([
'*-debug.log',
Expand All @@ -381,7 +381,7 @@ class App extends Generator {
.join('\n') + '\n'
}

private _eslintignore(): string {
private _eslintignore (): string {
const existing = this.fs.exists(this.destinationPath('.eslintignore')) ? this.fs.read(this.destinationPath('.eslintignore')).split('\n') : []
return _([
this.ts && '/lib',
Expand All @@ -393,7 +393,7 @@ class App extends Generator {
.join('\n') + '\n'
}

private _writeSingle() {
private _writeSingle () {
if (!this.fromScratch) return
this.fs.copyTpl(this.templatePath(`single/bin/run.${this._ext}`), this.destinationPath('bin/run'), this)
this.fs.copyTpl(this.templatePath(`single/src/index.${this._ext}`), this.destinationPath(`src/index.${this._ext}`), this)
Expand All @@ -402,33 +402,26 @@ class App extends Generator {
}
}

private _writeMulti() {
private _writeMulti () {
if (!this.fromScratch) return
this._writePlugin()
this.fs.copyTpl(this.templatePath(`multi/bin/run.${this._ext}`), this.destinationPath('bin/run'), this)
this.fs.copyTpl(this.templatePath(`multi/src/index.${this._ext}`), this.destinationPath(`src/index.${this._ext}`), this)
this.fs.copyTpl(this.templatePath(`plugin/src/commands/hello.${this._ext}`), this.destinationPath(`src/commands/hello.${this._ext}`), this)
// 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/commands/hello.test.${this._ext}`), this.destinationPath(`test/commands/hello.test.${this._ext}`), this)
// this.fs.copyTpl(this.templatePath(`plugin/test/hooks/init.test.${this._ext}`), this.destinationPath(`test/hooks/init.test.${this._ext}`), this)
}
}

private _writePlugin() {
private _writePlugin () {
if (!this.fromScratch) return
this.fs.copyTpl(this.templatePath(`plugin/bin/run.${this._ext}`), this.destinationPath('bin/run'), this)
this.fs.copyTpl(this.templatePath(`plugin/src/commands/hello.${this._ext}`), this.destinationPath(`src/commands/hello.${this._ext}`), this)
// this.fs.copyTpl(this.templatePath(`plugin/src/hooks/init.${this._ext}`), this.destinationPath(`src/hooks/init.${this._ext}`), this)
this.fs.copyTpl(this.templatePath(`command.${this._ext}.ejs`), this.destinationPath(`src/commands/hello.${this._ext}`), {name: 'hello', _})
if (this.ts) {
this.fs.copyTpl(this.templatePath('plugin/src/index.ts'), this.destinationPath('src/index.ts'), this)
}
if (this.mocha) {
this.fs.copyTpl(this.templatePath(`plugin/test/commands/hello.test.${this._ext}`), this.destinationPath(`test/commands/hello.test.${this._ext}`), this)
// 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/hello.test.${this._ext}`), {name: 'hello', _})
}
}

private _writeBase() {
private _writeBase () {
if (!this.fromScratch) return
this.fs.copyTpl(this.templatePath(`base/src/index.${this._ext}`), this.destinationPath(`src/index.${this._ext}`), this)
if (this.mocha) {
Expand Down
46 changes: 46 additions & 0 deletions src/generators/command/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// tslint:disable no-floating-promises
// tslint:disable no-console

import * as _ from 'lodash'
import * as path from 'path'
import * as Generator from 'yeoman-generator'
import yosay = require('yosay')

import {Options} from '../../commands/command'

const {version} = require('../../../package.json')

class CommandGenerator extends Generator {
pjson: {
name: string
devDependencies: {[name: string]: string}
}

get _path () { return this.options.name.split(':').join('/') }
get _ts () { return this.pjson.devDependencies.typescript }
get _ext () { return this._ts ? 'ts' : 'js' }
get _mocha () { return this.pjson.devDependencies.mocha }

constructor (args: any, public options: Options) {
super(args, options)
}

async prompting () {
this.pjson = this.fs.readJSON('package.json')
if (!this.pjson) throw new Error('not in a project directory')
this.log(yosay(`Adding a command to ${this.pjson.name} Version: ${version}`))
}

writing () {
this.sourceRoot(path.join(__dirname, '../../../templates'))
this.fs.copyTpl(this.templatePath(`command.${this._ext}.ejs`), this.destinationPath(`src/commands/${this._path}.${this._ext}`), {...this.options, _})
// 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}`), {...this.options, _})
}
// this.fs.writeJSON(this.destinationPath('./package.json'), this.pjson)
}
}

export = CommandGenerator
File renamed without changes.
15 changes: 0 additions & 15 deletions templates/app/plugin/src/commands/hello.js

This file was deleted.

13 changes: 0 additions & 13 deletions templates/app/plugin/src/commands/hello.ts

This file was deleted.

15 changes: 0 additions & 15 deletions templates/app/plugin/test/commands/hello.test.js

This file was deleted.

17 changes: 0 additions & 17 deletions templates/app/plugin/test/commands/hello.test.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions templates/command.js.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const {Command, flags} = require('@dxcli/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} from <%- name %>!`)
}
}

<%- klass %>.flags = {
name: flags.string({char: 'n', description: 'name to print'}),
}

module.exports = <%- klass %>
17 changes: 17 additions & 0 deletions templates/command.test.js.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const {expect, test} = require('@dxcli/test')

const command = '<%- name %>'

describe(command, () => {
test
.stdout()
.command([command])
.do(ctx => expect(ctx.stdout).to.equal('hello world from <%- name %>!\n'))
.it()

test
.stdout()
.command([command, '--name', 'jeff'])
.do(ctx => expect(ctx.stdout).to.equal('hello jeff from <%- name %>!\n'))
.it()
})
17 changes: 17 additions & 0 deletions templates/command.test.ts.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {expect, test} from '@dxcli/test'

const command = '<%- name %>'

describe(command, () => {
test
.stdout()
.command([command])
.do(ctx => expect(ctx.stdout).to.contain('hello world from <%- name %>!'))
.it()

test
.stdout()
.command([command, '--name', 'jeff'])
.do(ctx => expect(ctx.stdout).to.contain('hello jeff from <%- name %>!'))
.it()
})
13 changes: 13 additions & 0 deletions templates/command.ts.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {Command, flags} from '@dxcli/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} from <%- name %>!`)
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 1 addition & 6 deletions test/commands/base/everything.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
const run = require('../../run')

describe('base', () => {
run('base', 'everything')
.it()
})
require('../../run')(module.filename)
7 changes: 1 addition & 6 deletions test/commands/base/mocha.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
const run = require('../../run')

describe('base', () => {
run('base', 'mocha')
.it()
})
require('../../run')(module.filename)
7 changes: 1 addition & 6 deletions test/commands/base/plain.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
const run = require('../../run')

describe('base', () => {
run('base', 'plain')
.it()
})
require('../../run')(module.filename)
7 changes: 1 addition & 6 deletions test/commands/base/typescript.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
const run = require('../../run')

describe('base', () => {
run('base', 'typescript')
.it()
})
require('../../run')(module.filename)
1 change: 1 addition & 0 deletions test/commands/command/everything.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('../../run')(module.filename)
1 change: 1 addition & 0 deletions test/commands/command/mocha.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('../../run')(module.filename)

0 comments on commit f6fef1f

Please sign in to comment.