Skip to content

Commit

Permalink
feat: support customize version (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Aug 4, 2017
1 parent 22b02cd commit 6eed130
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 7 deletions.
14 changes: 11 additions & 3 deletions README.md
Expand Up @@ -160,15 +160,17 @@ Define the main logic of command
- `description` - {String} a getter, only show this description when it's a sub command in help console
- `helper` - {Object} helper instance
- `yargs` - {Object} yargs instance for advanced custom usage
- `options` - {Object} a setter, set yargs' options
- `version` - {String} customize version
- `parserOptions` - {Object} control `context` parse rule.
- `execArgv` - {Boolean} whether extract `execArgv` to `context.execArgv`
- `removeAlias` - {Boolean} whether remove alias key from `argv`
- `removeCamelCase` - {Boolean} whether remove camel case key from `argv`

You can define options using yargs
You can define options by set `this.options`

```js
this.yargs.options({
this.options = {
baseDir: {
alias: 'b',
demandOption: true,
Expand All @@ -184,7 +186,13 @@ this.yargs.options({
description: 'choose a size',
choices: ['xs', 's', 'm', 'l', 'xl']
},
});
};
```

You can define version by set `this.version`

```js
this.version = 'v1.0.0';
```

### Helper
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Expand Up @@ -11,6 +11,6 @@ install:
test_script:
- node --version
- npm --version
- npm run ci
- npm run test

build: off
13 changes: 12 additions & 1 deletion lib/command.js
Expand Up @@ -13,6 +13,7 @@ const chalk = require('chalk');
const DISPATCH = Symbol('Command#dispatch');
const PARSE = Symbol('Command#parse');
const COMMANDS = Symbol('Command#commands');
const VERSION = Symbol('Command#version');

class CommonBin {
constructor(rawArgv) {
Expand Down Expand Up @@ -172,6 +173,14 @@ class CommonBin {
return helper;
}

set version(ver) {
this[VERSION] = ver;
}

get version() {
return this[VERSION];
}

/**
* dispatch command, either `subCommand.exec` or `this.run`
* @param {Object} context - context object
Expand All @@ -187,10 +196,12 @@ class CommonBin {
.completion()
.help()
.wrap(120)
.version()
.alias('h', 'help')
.group([ 'help', 'version' ], 'Global Options:');

// support customize version
this[VERSION] ? this.yargs.version(this[VERSION]) : this.yargs.version();

// get parsed argument without handling helper and version
const parsed = yield this[PARSE](this.rawArgv);
const commandName = parsed._[0];
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/my-bin/index.js
Expand Up @@ -27,6 +27,7 @@ class MainCommand extends Command {
console.log('add by class');
}
});
this.version = '1.2.2';
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/my-bin-sub.test.js
Expand Up @@ -10,7 +10,7 @@ describe('test/my-bin-sub.test.js', () => {
it('my-bin-dev --version', done => {
coffee.fork(myBin, [ '--version' ], { cwd })
// .debug()
.expect('stdout', '3.0.0\n')
.expect('stdout', '1.2.2\n')
.expect('code', 0)
.end(done);
});
Expand Down
2 changes: 1 addition & 1 deletion test/my-bin.test.js
Expand Up @@ -39,7 +39,7 @@ describe('test/my-bin.test.js', () => {
it('my-bin --version', done => {
coffee.fork(myBin, [ '--version' ], { cwd })
// .debug()
.expect('stdout', '2.0.0\n')
.expect('stdout', '1.2.2\n')
.expect('code', 0)
.end(done);
});
Expand Down

0 comments on commit 6eed130

Please sign in to comment.