makes condition work inside a series or parallel

This commit is contained in:
Ken 2018-12-07 14:07:00 -08:00
Родитель d1cd4be31f
Коммит cd20c2daf7
3 изменённых файлов: 39 добавлений и 17 удалений

Просмотреть файл

@ -20,16 +20,30 @@ task('build', parallel('tslint', series('clean', 'ts', 'webpack')));
task(
'yes',
condition('build', argv => {
return true;
})
parallel(
'tslint',
series(
'clean',
condition('ts', argv => {
return true;
}),
'webpack'
)
)
);
task(
'no',
condition('build', argv => {
return false;
})
parallel(
'tslint',
series(
'clean',
condition('ts', argv => {
return false;
}),
'webpack'
)
)
);
task('default', parallel('build'));

Просмотреть файл

@ -1,15 +1,19 @@
import yargs from 'yargs';
import { parallel } from './undertaker';
import { ILogger } from './logger';
import chalk from 'chalk';
import { task } from './task';
export function condition(taskName: string, conditional: (argv: yargs.Arguments) => boolean) {
return function condition(this: { logger: ILogger; argv: yargs.Arguments }, done: (err?: Error) => void) {
if (conditional && conditional(this.argv)) {
return parallel(taskName)(done);
}
const conditionalTaskName = `${taskName}?`;
this.logger.info(`skipped '${chalk.cyan(taskName)}'`);
done();
return function(done: any) {
task(conditionalTaskName, function(cb) {
if (conditional && conditional(this.argv)) {
return parallel(taskName)(cb);
}
cb();
});
parallel(conditionalTaskName)(done);
};
}

Просмотреть файл

@ -9,8 +9,12 @@ let topLevelTask: string | undefined = undefined;
let errorReported: boolean = false;
let tasksInProgress: { [key: string]: boolean } = {};
function shouldLog(taskArgs: any) {
return !taskArgs.branch && taskArgs.name !== '<anonymous>' && !taskArgs.name.endsWith('?');
}
undertaker.on('start', function(args: any) {
if (!args.branch) {
if (shouldLog(args)) {
if (!topLevelTask) {
topLevelTask = args.name;
}
@ -22,7 +26,7 @@ undertaker.on('start', function(args: any) {
});
undertaker.on('stop', function(args: any) {
if (!args.branch) {
if (shouldLog(args)) {
const duration = args.duration;
const durationInSecs = Math.round(((duration[0] * NS_PER_SEC + duration[1]) / NS_PER_SEC) * 100) / 100;
@ -41,7 +45,7 @@ undertaker.on('error', function(args: any) {
taskLogger(args.name).error(chalk.yellow('------------------------------------'));
taskLogger(args.name).error(chalk.yellow(args.error));
taskLogger(args.name).error(chalk.yellow('------------------------------------'));
} else if (!args.branch) {
} else if (shouldLog(args)) {
taskLogger(args.name).error(chalk.dim(`Error previously detected. See above for error messages.`));
}