Skip to content

Commit

Permalink
Merge pull request #59 from kumar303/program
Browse files Browse the repository at this point in the history
Split Program into its own module
  • Loading branch information
kumar303 committed Feb 20, 2016
2 parents 9d60403 + 9a00b48 commit a9511f3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 49 deletions.
48 changes: 1 addition & 47 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,9 @@
/* @flow */
import path from 'path';
import {readFileSync} from 'fs';
import yargs from 'yargs';

import build from './build';


export class Program {
yargs: any;
commands: { [key: string]: Function };

constructor(argv: ?Array<string>) {
if (argv !== undefined) {
this.yargs = yargs(argv);
} else {
this.yargs = yargs;
}
this.commands = {};
}

command(name: string, description: string, executor: Function,
configureCommand: ?Function): Program {
this.yargs.command(name, description, configureCommand);
this.commands[name] = executor;
return this;
}

run(): Promise {
let argv = this.yargs.argv;
let cmd = argv._[0];
return new Promise(
(resolve) => {
if (cmd === undefined) {
throw new Error('No sub-command was specified in the args');
}
if (!this.commands[cmd]) {
throw new Error(`unknown command: ${cmd}`);
}
resolve();
})
.then(() => this.commands[cmd](argv))
.catch((error) => {
let prefix = 'error:';
if (cmd) {
prefix = `${cmd} ${prefix}`;
}
console.error(prefix, error);
throw error;
});
}
}
import {Program} from './program';


export function main() {
Expand Down
48 changes: 48 additions & 0 deletions src/program.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* @flow */
import yargs from 'yargs';


export class Program {
yargs: any;
commands: { [key: string]: Function };

constructor(argv: ?Array<string>) {
if (argv !== undefined) {
this.yargs = yargs(argv);
} else {
this.yargs = yargs;
}
this.commands = {};
}

command(name: string, description: string, executor: Function,
configureCommand: ?Function): Program {
this.yargs.command(name, description, configureCommand);
this.commands[name] = executor;
return this;
}

run(): Promise {
let argv = this.yargs.argv;
let cmd = argv._[0];
return new Promise(
(resolve) => {
if (cmd === undefined) {
throw new Error('No sub-command was specified in the args');
}
if (!this.commands[cmd]) {
throw new Error(`unknown command: ${cmd}`);
}
resolve();
})
.then(() => this.commands[cmd](argv))
.catch((error) => {
let prefix = 'error:';
if (cmd) {
prefix = `${cmd} ${prefix}`;
}
console.error(prefix, error);
throw error;
});
}
}
4 changes: 2 additions & 2 deletions tests/cli/test.cli.js → tests/program/test.program.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {assert} from 'chai';
import {spy} from 'sinon';

import {Program} from '../../src/cli';
import {Program} from '../../src/program';


describe('cli.Program', () => {
describe('program.Program', () => {

it('executes a command callback', () => {
let thing = spy(() => new Promise((resolve) => resolve()));
Expand Down

0 comments on commit a9511f3

Please sign in to comment.