Skip to content

Commit

Permalink
feat: version support lazy load (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored and fengmk2 committed Aug 15, 2017
1 parent fed50e6 commit 58506ff
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 25 deletions.
8 changes: 5 additions & 3 deletions README.md
Expand Up @@ -161,7 +161,7 @@ Define the main logic of command
- `helper` - {Object} helper instance
- `yargs` - {Object} yargs instance for advanced custom usage
- `options` - {Object} a setter, set yargs' options
- `version` - {String} customize version
- `version` - {String} customize version, can be defined as a getter to support lazy load.
- `parserOptions` - {Object} control `context` parse rule.
- `execArgv` - {Boolean} whether extract `execArgv` to `context.execArgv`
- `removeAlias` - {Boolean} whether remove alias key from `argv`
Expand Down Expand Up @@ -189,10 +189,12 @@ this.options = {
};
```

You can define version by set `this.version`
You can define version by define `this.version` getter:

```js
this.version = 'v1.0.0';
get version() {
return 'v1.0.0';
}
```

### Helper
Expand Down
10 changes: 7 additions & 3 deletions lib/command.js
Expand Up @@ -195,17 +195,21 @@ class CommonBin {
// .reset()
.completion()
.help()
.version()
.wrap(120)
.alias('h', 'help')
.alias('v', 'version')
.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];

if (parsed.version && this.version) {
console.log(this.version);
return;
}

// if sub command exist
if (this[COMMANDS].has(commandName)) {
const Command = this[COMMANDS].get(commandName);
Expand Down
1 change: 0 additions & 1 deletion test/async-bin.test.js
Expand Up @@ -13,7 +13,6 @@ if (nodeVersion >= 7.6) {
it('async-bin --help', done => {
coffee.fork(myBin, [ '--help' ], { cwd })
// .debug()
.expect('stdout', /Commands:/)
.expect('stdout', /sleep\s*sleep showcase/)
.expect('code', 0)
.end(done);
Expand Down
5 changes: 4 additions & 1 deletion test/fixtures/my-bin/index.js
Expand Up @@ -27,7 +27,10 @@ class MainCommand extends Command {
console.log('add by class');
}
});
this.version = '1.2.2';
}

get version() {
return '1.2.2';
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/test-files/package.json
@@ -1,6 +1,6 @@
{
"name": "test-files",
"dependencies": {
"egg-init-config": "^1.2.0"
"egg-init-config": "~1.2.0"
}
}
1 change: 0 additions & 1 deletion test/my-bin-sub.test.js
Expand Up @@ -19,7 +19,6 @@ describe('test/my-bin-sub.test.js', () => {
coffee.fork(myBin, [ '--help' ], { cwd })
// .debug()
.expect('stdout', /Usage: my-bin-sub <command> \[options]/)
.expect('stdout', /Commands:/)
.expect('stdout', /start.*start app override/)
.expect('stdout', /test.*test app/)
.expect('stdout', /sub.*sub app/)
Expand Down
8 changes: 3 additions & 5 deletions test/my-bin.test.js
Expand Up @@ -15,13 +15,12 @@ describe('test/my-bin.test.js', () => {
coffee.fork(myBin, [ '--help' ], { cwd })
// .debug()
.expect('stdout', /Usage: my-bin <command> \[options]/)
.expect('stdout', /Commands:/)
.expect('stdout', /start.*start app/)
.expect('stdout', /test.*test app/)
.notExpect('stdout', /start-cluster/)
.notExpect('stdout', /not-register/)
.expect('stdout', /Options:/)
.expect('stdout', /-h, --help.*Show help.*boolean/)
.expect('stdout', /-h, --help.*/)
.expect('code', 0)
.end(done);
});
Expand All @@ -30,7 +29,6 @@ describe('test/my-bin.test.js', () => {
coffee.fork(myBin, [ '-h' ], { cwd })
// .debug()
.expect('stdout', /Usage: my-bin <command> \[options]/)
.expect('stdout', /Commands:/)
.expect('stdout', /Options:/)
.expect('code', 0)
.end(done);
Expand Down Expand Up @@ -60,8 +58,8 @@ describe('test/my-bin.test.js', () => {
return coffee.fork(myBin, [ 'start', '--env', 'foo' ], { cwd })
// .debug()
// .coverage(false)
.expect('stderr', /YError: Invalid values:/)
.expect('stderr', /Argument: env, Given: "foo", Choices: "test"/)
.expect('stderr', /YError:/)
.expect('stderr', /.*env.*"foo".*"test"/)
.expect('code', 1)
.end();
});
Expand Down
11 changes: 3 additions & 8 deletions test/my-git.test.js
Expand Up @@ -13,12 +13,11 @@ describe('test/my-git.test.js', () => {
coffee.fork(myBin, [ '--help' ], { cwd })
// .debug()
.expect('stdout', /Usage: my-git <command> \[options]/)
.expect('stdout', /Commands:/)
.expect('stdout', /clone.*Clone a repository into a new directory/)
.expect('stdout', /remote.*Manage set of tracked repositories/)
.expect('stdout', /Options:/)
.expect('stdout', /-h, --help.*Show help.*boolean/)
.expect('stdout', /--version.*Show version number.*boolean/)
.expect('stdout', /-h, --help.*/)
.expect('stdout', /--version.*/)
.expect('code', 0)
.end(done);
});
Expand All @@ -27,7 +26,6 @@ describe('test/my-git.test.js', () => {
coffee.fork(myBin, [ '-h' ], { cwd })
// .debug()
.expect('stdout', /Usage: my-git <command> \[options]/)
.expect('stdout', /Commands:/)
.expect('stdout', /Options:/)
.expect('code', 0)
.end(done);
Expand All @@ -37,7 +35,6 @@ describe('test/my-git.test.js', () => {
coffee.fork(myBin, [ '-h', 'remote' ], { cwd })
// .debug()
.expect('stdout', /Usage: my-git remote/)
.expect('stdout', /Commands:/)
.expect('stdout', /add\s*Adds a remote/)
.expect('stdout', /remove\s*Remove the remote/)
.expect('code', 0)
Expand All @@ -48,7 +45,6 @@ describe('test/my-git.test.js', () => {
coffee.fork(myBin, [], { cwd })
// .debug()
.expect('stdout', /Usage: my-git <command> \[options]/)
.expect('stdout', /Commands:/)
.expect('stdout', /Options:/)
.expect('code', 0)
.end(done);
Expand Down Expand Up @@ -89,7 +85,7 @@ describe('test/my-git.test.js', () => {
// .debug()
// .coverage(false)
.expect('stdout', /Options:/)
.expect('stdout', /--depth\s*Create a shallow.*\[number]/)
.expect('stdout', /--depth\s*Create a shallow.*\[.*?]/)
.expect('code', 0)
.end(done);
});
Expand All @@ -101,7 +97,6 @@ describe('test/my-git.test.js', () => {
// .debug()
// .coverage(false)
.expect('stdout', /Usage:.*remote <add\/remove>/)
.expect('stdout', /Commands:/)
.expect('stdout', /add\s*Adds a remote named/)
.expect('stdout', /remove\s*Remove.*/)
// .expect('stdout', /remove <name>\s*Remove.*\[aliases: rm]/)
Expand Down
1 change: 0 additions & 1 deletion test/my-helper.test.js
Expand Up @@ -13,7 +13,6 @@ describe('test/my-helper.test.js', () => {
it('my-helper --help', done => {
coffee.fork(myBin, [ '--help' ], { cwd })
// .debug()
.expect('stdout', /Commands:/)
.expect('stdout', /fork/)
.expect('stdout', /install/)
.expect('stdout', /echo/)
Expand Down
2 changes: 1 addition & 1 deletion test/single-bin.test.js
Expand Up @@ -14,7 +14,7 @@ describe('test/single-bin.test.js', () => {
return coffee.fork(myBin, [ '--help' ], { cwd })
// .debug()
.expect('stdout', /Options:/)
.expect('stdout', /-h, --help.*Show help.*boolean/)
.expect('stdout', /-h, --help.*/)
.expect('stdout', /--baseDir.*target directory/)
.expect('code', 0)
.end();
Expand Down

0 comments on commit 58506ff

Please sign in to comment.