Gulp Windowed NPM Module

Processes and maps files in windows/arrays of a specified size.

Install

Install with npm:

$ npm i gulp-windowed --save

Usage

This example concatenates groups of 5 markdown files. If src/posts contained files post0.md through post99.md then the next pipe would receive a stream containing page0.md through page19.md where page0.md is the concatenation of post0.md through post4.md. The ordering is guaranteed by the use of gulp-sort.

const gulp = require('gulp')
const sort = require('gulp-sorted')
const windowed = require('gulp-windowed')
const concat = require('gulp-concat')

gulp.task('default', () =>
  gulp.src('src/posts/*.md') // Posts in markdown
    .pipe(sort()) // Sorted by filename
    .pipe(windowed(5, (files, i) => // In groups of 5
      files.pipe(concat(`page${i}.md`)) // Concatenated into one page where 'i' is the window number
    ))
    // More pipes...
)

Here it is used to split groups of files into different folders.

const gulp = require('gulp')
const windowed = require('gulp-windowed')
const rename = require('gulp-rename')

gulp.task('default', () =>
  gulp.src('src/*')
    .pipe(windowed(10, (files, i, done) =>
      files.pipe(rename({
        dirname: `folder${i}`
      }))
    ))
    // More pipes...
)

Here it is used to skip every other file.

const gulp = require('gulp')
const windowed = require('gulp-windowed')

gulp.task('default', () =>
  gulp.src('src/*')
    .pipe(windowed(1, (files, i, done) =>
      i % 2 === 0 ? files : done()
    ))
    // More pipes...
)

Notice that the stream of File objects in the callback can be returned from the callback. They are subsequently written to the resulting stream for the next pipe outside the callback. This is useful because it allows you to perform stream operations on the groups of files, while also allowing you to merge the resulting streams back together to continue performing operations.

If you’d like an array of File objects instead of a stream then call windowed.array instead.

Method

windowed(n, cb) -> DuplexObjectStream<File>

Calls cb with a readable object stream (or array for window.array) containing n vinyl File objects each time n are written to it. On stream end if there are any remaining File objects because the n threshold was not met then cb is called with a readable object stream (or array for window.array) containing the remaining [0, n) File objects. The contents of the duplex object stream returned by the method depends on cb (explained below).

Parameters:

Pairs Well With

Contributing

Pull requests and stars on the GitHub repository are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Install dev dependencies:

$ npm i -d && npm test

License

Copyright © 2018 Tomer Aberbach Released under the MIT license.