Skip to content

Commit

Permalink
fix: updated with config/command changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Feb 5, 2018
1 parent a46508e commit e807e19
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 337 deletions.
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@
"fancy-test": "^1.0.1"
},
"devDependencies": {
"@anycli/command": "^1.2.4",
"@anycli/config": "^1.1.6",
"@anycli/command": "^1.2.6",
"@anycli/config": "^1.2.3",
"@anycli/tslint": "^0.2.5",
"@anycli/version": "^0.1.19",
"@types/chai": "^4.1.2",
"@types/mocha": "^2.2.48",
"@types/node": "^9.4.0",
"chai": "^4.1.2",
"cli-ux": "^3.3.15",
"concurrently": "^3.5.1",
"globby": "^7.1.1",
"husky": "^0.14.3",
"mocha": "^5.0.0",
"ts-node": "^4.1.0",
"typescript": "^2.7.1"
Expand Down
10 changes: 3 additions & 7 deletions src/exit.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {expect} from 'chai'

/**
* ensures that a anycli command or hook exits
*
Expand All @@ -6,13 +8,7 @@
*/
export default (code = 0) => ({
run() {
expect(process.exitCode).to.equal(code)
throw new Error(`Expected hook to exit with code ${code} but it ran without exiting`)
},
catch(ctx: {error: any}) {
const err = ctx.error
if (!err['cli-ux'] || typeof err['cli-ux'].exit !== 'number') throw err
if (err['cli-ux'].exit !== code) {
throw new Error(`Expected hook to exit with ${code} but exited with ${err['cli-ux'].exit}`)
}
}
})
6 changes: 3 additions & 3 deletions test/fixtures/multi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"private": true,
"anycli": {
"commands": "./src/commands",
"plugins": [
"@anycli/version"
]
"hooks": {
"foo": "./src/hooks/foo"
}
},
"engines": {
"node": ">=8.0.0"
Expand Down
9 changes: 4 additions & 5 deletions test/fixtures/multi/src/commands/foo/bar.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
const {Command, flags, parse} = require('@anycli/command')
const {cli} = require('cli-ux')
const {Command, flags} = require('@anycli/command')

class CLI extends Command {
constructor(args, opts) {
super(args, opts)
this.options = parse(args, CLI)
}

async run() {
const name = this.options.flags.name || 'world'
cli.log(`hello ${name}!`)
const {flags} = this.parse(CLI)
const name = flags.name || 'world'
this.log(`hello ${name}!`)
}
}

Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/multi/src/hooks/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (opts) {
this.log(`foo hook args: ${opts.argv}`)
}
21 changes: 2 additions & 19 deletions test/hook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,10 @@ import {expect, test} from '../src'
const root = path.join(__dirname, 'fixtures/multi')

describe('hooks', () => {
const stdout = `test/0.0.0 (${process.platform}-${process.arch}) node-${process.version}\n`

test
.loadConfig({root})
.stdout()
.hook('init', {id: '-v'}, {root})
.exit(0)
.do(output => expect(output.stdout).to.equal(stdout))
.it('catches -v')

test
.loadConfig({root})
.stdout()
.hook('init', {id: '--version'}, {root})
.exit(0)
.do(output => expect(output.stdout).to.equal(stdout))
.it('catches --version')

test
.loadConfig({root})
.stdout()
.hook('init', {}, {root})
.hook('foo', {argv: ['arg']}, {root})
.do(output => expect(output.stdout).to.equal('foo hook args: arg\n'))
.it()
})
Loading

0 comments on commit e807e19

Please sign in to comment.