Skip to content

Commit

Permalink
feat(bin): adds stream
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed May 1, 2019
1 parent dc38d92 commit a402da5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/bin/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { loadPackage, flags, safePairs, splitBy } from 'cli-belt';
import { stripIndent as indent } from 'common-tags';
import arg from 'arg';
import chalk from 'chalk';
import join from 'command-join';
import core, { options } from '~/core';
import { TLogger, IOfType } from '~/types';
import { run } from '~/public';
Expand All @@ -11,6 +12,7 @@ import series from './series';
import parallel from './parallel';
import list from './list';
import raise from './raise';
import stream from './stream';
import logger from '~/utils/logger';

export default async function main(argv: string[]): Promise<void> {
Expand All @@ -35,11 +37,11 @@ export default async function main(argv: string[]): Promise<void> {
Commands:
:run Default command -it can be omitted
:cmd, : Run a command within a project context
:series Run commands in series within a project context
:parallel Run commands in parallel within a project context
:list List available tasks
:raise Raise tasks to package
:link Link packages
:series Run commands in series within a project context
:parallel Run commands in parallel within a project context
:stream Stream tasks or commands on children scopes
Examples:
$ kpo foo bar baz
Expand Down Expand Up @@ -110,26 +112,28 @@ export default async function main(argv: string[]): Promise<void> {
// Log full command to be run w/ resolved scopes
const scopes = core.state.scopes;
logger.info(
`Running: ${chalk.bold('kpo')}` +
chalk.bold('kpo') +
(scopes.length ? chalk.bold.yellow(' @' + scopes.join(' @')) : '') +
chalk.bold.blue(' ' + first) +
(cmd._.length ? ` "${cmd._.join('" "')}"` : '')
` ${join(cmd._)}`
);

switch (first) {
case ':run':
const [tasks, args] = splitBy(cmd._);
return run(tasks)(args);
case ':list':
return list(cmd._);
case ':raise':
return raise(cmd._);
case ':cmd':
return _cmd(cmd._);
case ':series':
return series(cmd._);
case ':parallel':
return parallel(cmd._);
case ':list':
return list(cmd._);
case ':raise':
return raise(cmd._);
case ':run':
const [tasks, args] = splitBy(cmd._);
return run(tasks)(args);
case ':stream':
return stream(cmd._);
default:
throw Error('Unknown command ' + first);
}
Expand Down
50 changes: 50 additions & 0 deletions src/bin/main/stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* eslint-disable no-console */
import { stripIndent as indent } from 'common-tags';
import arg from 'arg';
import { flags, safePairs } from 'cli-belt';
import { stream as command } from '~/public';

export default async function stream(argv: string[]): Promise<void> {
const help = indent`
Usage:
$ kpo :stream [options] [commands]
Streams kpo commands for children projects
Options:
--include <values> Include children by name, comma separated
--exclude <values> Exclude children by name, comma separated
--parallel Execute streaming in parallel
--force Continue execution even if some process fails
-h, --help Show help
Examples:
$ kpo :stream :cmd foo --bar --baz
`;

const types = {
'--include': String,
'--exclude': String,
'--parallel': Boolean,
'--force': Boolean,
'--help': Boolean
};

const { options, aliases } = flags(help);
safePairs(types, options, { fail: true, bidirectional: true });
Object.assign(types, aliases);
const cmd = arg(types, { argv, permissive: false, stopAtPositional: true });

if (cmd['--help']) return console.log(help);

if (!cmd._.length) {
console.log(help + '\n');
throw Error(`A command is required`);
}
return command.fn(cmd._, {
include: cmd['--include'] ? cmd['--include'].split(',') : undefined,
exclude: cmd['--exclude'] ? cmd['--exclude'].split(',') : undefined,
parallel: cmd['--parallel'],
force: cmd['--force']
});
}

0 comments on commit a402da5

Please sign in to comment.