Skip to content

Commit 051be16

Browse files
authored
Merge pull request microsoft#85 from cliffkoh/master
Fix _flatten and make serial/parallel more robust
2 parents ea71ecc + 58189ca commit 051be16

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/gulp-core-build",
5+
"comment": "Fix _flatten and make serial/parallel more robust",
6+
"type": "patch"
7+
}
8+
],
9+
"email": "cliffkoh@users.noreply.github.com"
10+
}

gulp-core-build/src/index.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ export function watch(watchMatch: string | string[], task: IExecutable): IExecut
238238
* Takes in IExecutables as arguments and returns an IExecutable that will execute them in serial.
239239
*/
240240
export function serial(...tasks: Array<IExecutable[] | IExecutable>): IExecutable {
241-
const flatTasks: IExecutable[] = <IExecutable[]>_flatten(tasks);
241+
// tslint:disable-next-line:no-null-keyword
242+
const flatTasks: IExecutable[] = <IExecutable[]>_flatten(tasks).filter(task => task !== null && task !== undefined);
242243

243244
for (const task of flatTasks) {
244245
_trackTask(task);
@@ -261,7 +262,8 @@ export function serial(...tasks: Array<IExecutable[] | IExecutable>): IExecutabl
261262
* Takes in IExecutables as arguments and returns an IExecutable that will execute them in parallel.
262263
*/
263264
export function parallel(...tasks: Array<IExecutable[] | IExecutable>): IExecutable {
264-
const flattenTasks: IExecutable[] = _flatten<IExecutable>(tasks);
265+
// tslint:disable-next-line:no-null-keyword
266+
const flattenTasks: IExecutable[] = _flatten<IExecutable>(tasks).filter(task => task !== null && task !== undefined);
265267

266268
for (const task of flattenTasks) {
267269
_trackTask(task);
@@ -379,17 +381,21 @@ function _trackTask(task: IExecutable): void {
379381
/**
380382
* Flattens a set of arrays into a single array.
381383
*/
382-
function _flatten<T>(arr: Array<T | T[]>): T[] {
383-
let output: T[] = [];
384-
385-
for (const toFlatten of arr) {
386-
if (Array.isArray(toFlatten)) {
387-
output = output.concat(toFlatten);
388-
} else {
389-
output.push(toFlatten);
384+
function _flatten<T>(oArr: Array<T | T[]>): T[] {
385+
const output: T[] = [];
386+
387+
function traverse(arr: Array<T | T[]>): void {
388+
for (let i: number = 0; i < arr.length; ++i) {
389+
if (Array.isArray(arr[i])) {
390+
traverse(arr[i] as T[]);
391+
} else {
392+
output.push(arr[i] as T);
393+
}
390394
}
391395
}
392396

397+
traverse(oArr);
398+
393399
return output;
394400
}
395401

0 commit comments

Comments
 (0)