diff --git a/.ayi.yml b/.ayi.yml new file mode 100644 index 0000000..c89e13a --- /dev/null +++ b/.ayi.yml @@ -0,0 +1,2 @@ +test: + - npm run ft \ No newline at end of file diff --git a/bin/about b/bin/about index bb20415..e48be2e 100644 --- a/bin/about +++ b/bin/about @@ -1,5 +1,12 @@ #!/usr/bin/env node 'use strict'; -const cli = require('../lib/cli'); -cli.run(); \ No newline at end of file +// 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(); \ No newline at end of file diff --git a/cmd/root.js b/cmd/root.js new file mode 100644 index 0000000..711f621 --- /dev/null +++ b/cmd/root.js @@ -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; \ No newline at end of file diff --git a/cmd/version.js b/cmd/version.js new file mode 100644 index 0000000..408115f --- /dev/null +++ b/cmd/version.js @@ -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; diff --git a/lib/cli.js b/lib/cli.js deleted file mode 100644 index 49439c8..0000000 --- a/lib/cli.js +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Created by at15 on 2016/9/6. - */ -'use strict'; -const logger = require('./logger'); -const configLocator = require('./config/locator'); - -const meow = require('meow'); -const cli = meow(` - Usage - $ about - - Options - --verbose, -v verbose log - --config, -c Specify config file - - Examples - $ about --config ./example/config.yaml - $ about -c ./example/config.yaml -`, { - alias: { - c: 'config', - v: 'verbose' - } -}); - -module.exports = { - flags: cli.flags, - run: () => { - // reconfigure the logger - logger.config(cli.flags); - - logger.debug('cli input', cli.input); - logger.debug('cli flags', cli.flags); - - let configFilePath = configLocator.detectConfigFile(cli.flags); - if (configFilePath === '') { - process.exit(1); - } - - logger.debug('keep moving!'); - } -}; \ No newline at end of file diff --git a/lib/cli/app.js b/lib/cli/app.js index 9e98d21..db4b8dc 100644 --- a/lib/cli/app.js +++ b/lib/cli/app.js @@ -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} @@ -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) { diff --git a/lib/config/locator.js b/lib/config/locator.js index 593ca7b..bb50f86 100644 --- a/lib/config/locator.js +++ b/lib/config/locator.js @@ -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) { @@ -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 ''; } diff --git a/lib/constant.js b/lib/constant.js new file mode 100644 index 0000000..7e1d85f --- /dev/null +++ b/lib/constant.js @@ -0,0 +1,9 @@ +/** + * Created by at15 on 2016/9/10. + */ +'use strict'; + +module.exports = { + VERSION: '0.0.1', + DEFAULT_CONFIG_FILE: '.about.yml' +}; \ No newline at end of file diff --git a/package.json b/package.json index 7368f7a..2a33dca 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/test/cli/command-spec.js b/test/cli/command-spec.js index e1e43a3..ab9dd61 100644 --- a/test/cli/command-spec.js +++ b/test/cli/command-spec.js @@ -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) => { @@ -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 = 'git@github.com/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}); + }); }); \ No newline at end of file