Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
141 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {flags} from '@oclif/command' | ||
|
||
import Base from '../command_base' | ||
|
||
export interface Options { | ||
name: string | ||
defaults?: boolean | ||
force?: boolean | ||
event: string | ||
} | ||
|
||
export default abstract class HookCommand extends Base { | ||
static description = 'add a hook to an existing CLI or plugin' | ||
|
||
static flags = { | ||
defaults: flags.boolean({description: 'use defaults for every setting'}), | ||
force: flags.boolean({description: 'overwrite existing files'}), | ||
event: flags.string({description: 'event to run hook on', default: 'init'}), | ||
} | ||
static args = [ | ||
{name: 'name', description: 'name of hook (snake_case)', required: true} | ||
] | ||
|
||
async run() { | ||
const {flags, args} = this.parse(HookCommand) | ||
await super.generate('hook', { | ||
name: args.name, | ||
event: flags.event, | ||
defaults: flags.defaults, | ||
force: flags.force, | ||
} as Options) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// 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/hook' | ||
|
||
const {version} = require('../../package.json') | ||
|
||
class HookGenerator extends Generator { | ||
pjson!: any | ||
|
||
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') | ||
this.pjson.oclif = this.pjson.oclif || {} | ||
if (!this.pjson) throw new Error('not in a project directory') | ||
this.log(yosay(`Adding a ${this.options.event} hook to ${this.pjson.name} Version: ${version}`)) | ||
} | ||
|
||
writing() { | ||
this.sourceRoot(path.join(__dirname, '../../templates')) | ||
this.fs.copyTpl(this.templatePath(`src/hook.${this._ext}.ejs`), this.destinationPath(`src/hooks/${this.options.event}/${this.options.name}.${this._ext}`), this) | ||
if (this._mocha) { | ||
this.fs.copyTpl(this.templatePath(`test/hook.test.${this._ext}.ejs`), this.destinationPath(`test/commands/${this._path}.test.${this._ext}`), this) | ||
} | ||
this.pjson.oclif = this.pjson.oclif || {} | ||
let hooks = this.pjson.oclif.hooks = this.pjson.oclif.hooks || {} | ||
let p = `./${this._ts ? 'lib' : 'src'}/hooks/${this.options.event}/${this.options.name}.js` | ||
if (hooks[this.options.event]) { | ||
hooks[this.options.event] = _.castArray(hooks[this.options.event]) | ||
hooks[this.options.event].push(p) | ||
} else { | ||
this.pjson.oclif.hooks[this.options.event] = p | ||
} | ||
this.fs.writeJSON(this.destinationPath('./package.json'), this.pjson) | ||
} | ||
} | ||
|
||
export = HookGenerator |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = async function (opts) { | ||
process.stdout.write(`example hook running ${opts.id}\n`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {Hook} from '@oclif/config' | ||
|
||
const hook: Hook<'<%- options.event %>'> = async function (opts) { | ||
process.stdout.write(`example hook running ${opts.id}\n`) | ||
} | ||
|
||
export default hook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const {expect, test} = require('@oclif/test') | ||
|
||
describe('hooks', () => { | ||
test | ||
.stdout() | ||
.hook('init', {id: 'mycommand'}) | ||
.do(output => expect(output.stdout).to.contain('example hook running mycommand')) | ||
.it('shows a message') | ||
}) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('../../run')(module.filename) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('../../run')(module.filename) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters