What generally could be the cause of `on("end")` handler has not been called in Gulp pipeline?

66 Views Asked by At

Without the MWE, I don't expect that someone will answer exactly what is the cause in my case. What I am asking is what generally could be the cause. Knowing this, I'll continue the investigation myself.

Below is not the working example, but If to simplify my markup source code linter class (currently it has about 750 lines) based on Gulp, it will be:

import type { TransformCallback } from "stream";
import Stream from "stream";
import VinylFile from "vinyl";


class MarkupSourceCodeLinter {
 
  public static provideLintingIfMust(
    projectBuildingMasterConfigRepresentative: ProjectBuildingMasterConfigRepresentative
  ): () => NodeJS.ReadWriteStream {

    const dataHoldingSelfSoleInstance: MarkupSourceCodeLinter = new MarkupSourceCodeLinter(/* */);

    // ...

    /* First run */
    return dataHoldingSelfSoleInstance.lint(dataHoldingSelfSoleInstance.targetFilesGlobSelectors);

  }


  protected lint(globSelectorsOrAbsolutePathsOfTargetFiles: Array<string>): () => NodeJS.ReadWriteStream {


    return (): NodeJS.ReadWriteStream => Gulp.

        src(globSelectorsOrAbsolutePathsOfTargetFiles, { read: false }).
                
                // ...
                
                pipe(new Stream.Transform({
                    objectMode: true,
                    transform(chunk: unknown, _encoding: BufferEncoding, callback: TransformCallback): void {
                        
                        console.log("CHECKPOINT1");
                        
                        if (chunk instanceof VinylFile) {
                            if (VinylFile.isVinyl(chunk)) {
                                console.log(chunk.path);
                            }
                        }
                        
                        callback(null, chunk);
                        
                    }
                })).

                on("end", (): void => {
                    console.log("CHECKPOINT2");
                });

  }
    
    private onMarkupSourceFileHasBeenAddedOrUpdated(targetMarkupFileAbsolutePath: string): void {

        //
        this.lint([ targetMarkupFileAbsolutePath ])();
    
  }

}

When the lint() is being invoked externally (via public static provideLintingIfMust), the on("end", () => {}) handler is being invoked. However, when the lint() invoked from onMarkupSourceFileHasBeenAddedOrUpdated, the checkpoint console.log("CHECKPOINT1"); reached but console.log("CHECKPOINT2"); - no. There is only one file.

I suppose, I just don't know when on("end", () => {}) being actually invoked.

The example output on subsequent run:

CHECKPOINT1
D:\XXX\FunctionalTests\MarkupProcessing\IncrementalBuilding\01-Source\Com
ponents\Header.pug

Exactly one file has been processed, but on("end", () => {}) has not been invoked. What generally could be the cause?

1

There are 1 best solutions below

1
Hamada On

Note that the 'end' event will not fire unless the data is completely consumed. This can be done by switching into a flowing mode, or by calling stream.read() repeatedly until you get to the end event-end