0

I'm using Gulp to build my Typescript application for Node.js.

For my purposes I need to scan all .ts files, retrieve some information, and then append this information to some of .ts files.

I'm using gulp-modify to inject my file handler to the pipe().

gulp.task(...., function() {
    return gulp.src('*.ts')
        .pipe(modify(statisticsProccesor))
        .pipe(modify(injectData))
        .pipe(gulp.dest(...))
});

Where statisticsProccesor and injectData are function(file, content) implementations to process exact file from sources stream.

Using this approach I can retrieve some statistics from sources. But I can not inject it back because gulp.src() processes all files one by one (and calls all handlers too).

Once I have 2 files 1.ts and 2.ts this task will process it in such order:

1.ts: statisticsProccesor(), injectData()
2.ts: statisticsProccesor(), injectData()

while I need to do such magic:

1.ts: statisticsProccesor()
1.ts: statisticsProccesor()
2.ts: injectData()
2.ts: injectData()

I've tried to add 2 gulp.src().pipe(modify) commands to the task. And also to separate these parts to 2 different tasks to launch one by one. But Gulp optimizes execution, so I can't preprocess sources and iterate them twice.

dr11
  • 5,166
  • 11
  • 35
  • 77
  • 1
    You **can** do this with two tasks. You just need to properly hint execution order: http://stackoverflow.com/questions/22824546/ – Sven Schoenung Jul 25 '16 at 11:43

1 Answers1

0

As mentioned Sven it is necessary to breakdown task into few dependent tasks.

gulp.task('doGood, ['subDo'], function(callback) {
    gulp.src('*.ts')
        .pipe(modify(statisticsProccesor))
        .on('finish', callback);
});

gulp.task('subDo', function(callback) {
    gulp.src('*.ts')
        .pipe(modify(injectData))
        .pipe(gulp.dest(...));
});

gulp doGood

Few important things:

1. `subDo` should not return the stream
2. `subDo` should contain `on('finish')` instruction
dr11
  • 5,166
  • 11
  • 35
  • 77