Skip to content

Commit

Permalink
Build Tooling: Implement build files sourcing as stream
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed May 25, 2019
1 parent ab06458 commit 6663433
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions bin/packages/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ const path = require( 'path' );
const glob = require( 'fast-glob' );
const ProgressBar = require( 'progress' );
const workerFarm = require( 'worker-farm' );
const { Readable } = require( 'stream' );

let files = process.argv.slice( 2 );
const files = process.argv.slice( 2 );

/**
* Path to packages directory.
Expand All @@ -17,7 +18,13 @@ const PACKAGES_DIR = path.resolve( __dirname, '../../packages' );

let onFileComplete = () => {};

if ( ! files.length ) {
let stream;

if ( files.length ) {
stream = new Readable( { encoding: 'utf8' } );
files.forEach( ( file ) => stream.push( file ) );
stream.push( null );
} else {
const bar = new ProgressBar( 'Build Progress: [:bar] :percent', {
width: 30,
incomplete: ' ',
Expand All @@ -26,7 +33,7 @@ if ( ! files.length ) {

bar.tick( 0 );

files = glob.sync( [
stream = glob.stream( [
`${ PACKAGES_DIR }/*/src/**/*.js`,
`${ PACKAGES_DIR }/*/src/*.scss`,
], {
Expand All @@ -37,7 +44,15 @@ if ( ! files.length ) {
onlyFiles: true,
} );

bar.total = files.length;
// Pause to avoid data flow which would begin on the `data` event binding,
// but should wait until worker processing below.
//
// See: https://nodejs.org/api/stream.html#stream_two_reading_modes
stream
.pause()
.on( 'data', ( file ) => {
bar.total = files.push( file );
} );

onFileComplete = () => {
bar.tick();
Expand All @@ -46,14 +61,16 @@ if ( ! files.length ) {

const worker = workerFarm( require.resolve( './build-worker' ) );

let complete = 0;
let ended = false,
complete = 0;

files.forEach( ( file ) => {
worker( file, () => {
stream
.on( 'data', ( file ) => worker( file, () => {
onFileComplete();

if ( ++complete === files.length ) {
if ( ended && ++complete === files.length ) {
workerFarm.end( worker );
}
} );
} );
} ) )
.on( 'end', () => ended = true )
.resume();

0 comments on commit 6663433

Please sign in to comment.