Skip to content

Commit

Permalink
[cli] Remove meow and use own cli framework #5
Browse files Browse the repository at this point in the history
- add .ayi.yml to use Ayi test instead of npm run ft
- add cmd folder
- don't load config by default, should do it before executing a callback
  • Loading branch information
at15 committed Sep 11, 2016
1 parent 631c84c commit 50fa7b5
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 51 deletions.
2 changes: 2 additions & 0 deletions .ayi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test:
- npm run ft
11 changes: 9 additions & 2 deletions bin/about
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#!/usr/bin/env node

'use strict';
const cli = require('../lib/cli');
cli.run();
// const cli = require('../lib/cli');
// cli.run();

const Cli = require('../lib/cli/app');
const rootCmd = require('../cmd/root');

let app = new Cli();
app.setRootCmd(rootCmd);
app.run();
16 changes: 16 additions & 0 deletions cmd/root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Created by at15 on 2016/9/11.
*/
'use strict';

const Command = require('../lib/cli/command');
const versionCmd = require('./version');

let rootCmd = new Command();
rootCmd.name = 'about-cli';
rootCmd.registerCommand(versionCmd);
rootCmd.setFunc(() => {
console.log('I should show some help');
});

module.exports = rootCmd;
15 changes: 15 additions & 0 deletions cmd/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Created by at15 on 2016/9/10.
*/
'use strict';

const Command = require('../lib/cli/command');
const version = require('../lib/constant').VERSION;

let versionCmd = new Command();
versionCmd.name = 'version';
versionCmd.setFunc(() => {
console.log(version);
});

module.exports = versionCmd;
43 changes: 0 additions & 43 deletions lib/cli.js

This file was deleted.

18 changes: 16 additions & 2 deletions lib/cli/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const argv = require('minimist')(process.argv.slice(2));

const Command = require('./command');
const logger = require('../logger');
const configLocator = require('../config/locator');

/**
* @property rootCmd {Command}
Expand All @@ -32,10 +33,23 @@ class Cli {
delete argv._;
const flags = argv;
const app = this;
// config logger
logger.config(flags);
logger.debug('args', args);
logger.debug('flags', flags);

// run all the registered commands
// TODO: try catch and deal with promise failure that is not handled
// TODO: pass cli, args, flags
this.rootCmd.execute(app, args, argv);
this.rootCmd.execute(app, args, flags);
}

loadConfigOrExit(){
// locate config file
// TODO: store in the app object
let configFilePath = configLocator.detectConfigFile(flags);
if (configFilePath == '') {
this.exit(1);
}
}

exit(code) {
Expand Down
4 changes: 2 additions & 2 deletions lib/config/locator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
const logger = require('../logger');
const fsUtil = require('../util/fs');
const DEFAULT_CONFIG_FILE = '.about.yml';
const DEFAULT_CONFIG_FILE = require('../constant').DEFAULT_CONFIG_FILE;

// TODO: use sinon for test, I guess stub can handle the file problem
function detectConfigFile(flags) {
Expand All @@ -23,7 +23,7 @@ function detectConfigFile(flags) {
} else {
logger.warn(`default config file can't be found`, {file: DEFAULT_CONFIG_FILE});
}
logger.error('no config file found');
logger.error('config file NOT found');
return '';
}

Expand Down
9 changes: 9 additions & 0 deletions lib/constant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Created by at15 on 2016/9/10.
*/
'use strict';

module.exports = {
VERSION: '0.0.1',
DEFAULT_CONFIG_FILE: '.about.yml'
};
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"chalk": "^1.1.3",
"js-yaml": "^3.6.1",
"lodash": "^4.15.0",
"meow": "^3.7.0",
"minimist": "^1.2.0",
"winston": "^2.2.0"
},
Expand Down
20 changes: 19 additions & 1 deletion test/cli/command-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ describe('Command', ()=> {
expect(rootCmd.subCommands).to.have.property(subCmd.name);
let subCmdWithoutName = new Command();
expect(rootCmd.registerCommand(subCmdWithoutName)).to.eqls(false);

});

it('can execute callback function', (done) => {
Expand Down Expand Up @@ -48,4 +47,23 @@ describe('Command', ()=> {
rootCmd.registerCommand(gitCmd);
rootCmd.execute(null, ['git', 'status'], {verbose: true});
});

it('non root cmd can have sub command', (done) => {
let rootCmd = new Command();
rootCmd.name = 'dummy';
let gitCmd = new Command();
gitCmd.name = 'git';
let cloneCmd = new Command();
cloneCmd.name = 'clone';
let cloneURL = '[email protected]/tongquhq/about.git';
gitCmd.registerCommand(cloneCmd);
rootCmd.registerCommand(gitCmd);
cloneCmd.setFunc((app, args, flags) => {
expect(app).to.eql(null);
expect(args).to.eql([cloneURL]);
expect(flags).to.eql({verbose: true});
done();
});
rootCmd.execute(null, ['git', 'clone', cloneURL], {verbose: true});
});
});

0 comments on commit 50fa7b5

Please sign in to comment.