Skip to content

Commit

Permalink
feat(exposed/exec): adds series.env and parallel.env
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Apr 26, 2019
1 parent 856c793 commit 063fcba
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
36 changes: 29 additions & 7 deletions src/exposed/exec/parallel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import core from '~/core';
import { IExecOptions, TScript } from '~/types';
import { IExecOptions, TScript, IOfType } from '~/types';
import logger from '~/utils/logger';
import { wrap } from '~/utils/errors';

export interface IParallelOptions extends IExecOptions {
names?: string[];
Expand All @@ -15,10 +16,19 @@ export interface IParallelOptions extends IExecOptions {
silent?: boolean;
}

export default function parallel(
commands: string | string[],
options: IParallelOptions = {}
): TScript {
/**
* Signature for `parallel`. Note that you can call `parallel.env` to pass only environment variables as a second argument. See `parallel`.
*/
export interface IParallel {
(commands: string | string[], options?: IParallelOptions): TScript;
env(commands: string | string[], env: IOfType<string>): TScript;
}

/**
* Runs `commands` in parallel, with optional environment variables, names and colors assigned to processes, and more. See `IParallel` and `IParallelOptions`.
* @returns A `TScript`, as a function, that won't be executed until called by `kpo` -hence, calling `parallel` won't have any effect until the returned function is called.
*/
const parallel: IParallel = function parallel(commands, options = {}) {
return async function parallel(args?: string[]): Promise<void> {
const argv: string[] = Array.isArray(commands)
? commands.concat()
Expand All @@ -41,9 +51,21 @@ export default function parallel(
true,
options
);
} catch (err) {
} catch (e) {
const err = wrap.ensure(e, {
message: 'Parallel commands execution failed'
});
if (options.silent) logger.error(err);
else throw err;
}
};
}
};

parallel.env = function env(
commands: string | string[],
env: IOfType<string>
): TScript {
return parallel(commands, { env });
};

export default parallel;
30 changes: 24 additions & 6 deletions src/exposed/exec/series.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import core from '~/core';
import { IExecOptions, TScript } from '~/types';
import { IExecOptions, TScript, IOfType } from '~/types';
import logger from '~/utils/logger';

export interface ISeriesOptions extends IExecOptions {
Expand All @@ -13,10 +13,19 @@ export interface ISeriesOptions extends IExecOptions {
force?: boolean;
}

export default function series(
commands: string | string[],
options: ISeriesOptions = {}
): TScript {
/**
* Signature for `series`. Note that you can call `series.env` to pass only environment variables as a second argument. See `series`.
*/
export interface ISeries {
(commands: string | string[], options?: ISeriesOptions): TScript;
env(commands: string | string[], env: IOfType<string>): TScript;
}

/**
* Runs `commands` in series, with optional environment variables, names and colors assigned to processes, and more. See `ISeries` and `ISeriesOptions`.
* @returns A `TScript`, as a function, that won't be executed until called by `kpo` -hence, calling `series` won't have any effect until the returned function is called.
*/
const series: ISeries = function series(commands, options = {}) {
return async function series(args?: string[]): Promise<void> {
if (!Array.isArray(commands)) commands = [commands];

Expand All @@ -33,4 +42,13 @@ export default function series(
}
if (err && !options.silent) throw err;
};
}
};

series.env = function env(
commands: string | string[],
env: IOfType<string>
): TScript {
return series(commands, { env });
};

export default series;

0 comments on commit 063fcba

Please sign in to comment.