npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@eklingen/vinyl-stream-gears

v4.0.5

Published

Tiny Vinyl-stream utilities -aka Gulp plugins- for tiny stream operations.

Downloads

107

Readme

Vinyl-stream gears

Tiny Vinyl-stream utilities -aka Gulp plugins- for tiny stream operations. No dependencies. These are not full-featured wrappers for other libraries. Most of these functions only work on Vinyl Buffer objects. They have only been tested on Vinyl Buffer objects. Optional argument objects are not sanitized.

Installation

yarn install. Or npm install. Or just copy the files to your own project.

Usage

You can import only what you need.

const { append, prepend } = require('@eklingen/vinyl-stream-gears')

Append

Append text to the end of a file.

const { append } = require('@eklingen/vinyl-stream-gears')
stream.pipe(append('Merry Christmas'))

Apply

Apply a callback function to the file in the stream. Useful for inspecting properties, for example. This is internally also used by the tests. Don't forget to check for .isBuffer() or .contents etc!

const { apply } = require('@eklingen/vinyl-stream-gears')
stream.pipe(apply(file => {
  // do something with file
}))

(since v2.0.0) This used to have a flush callback as well, but as it turns out, the flush event is inconsistently handled in Node. It's best to use stream.Finished or an end handler.


Changed

Leaves only changed files in the stream, as compared to a destination directory. Useful to prevent unnecessary writes.

const { changed } = require('@eklingen/vinyl-stream-gears')
stream.pipe(changed('path/to/destination'))

You can choose between comparing either the file contents (default) via the option method: 'contents' or the file's modified timestamp via the option method: 'mtime'. The last one is less reliable in combination with source control.

stream.pipe(changed('path/to/destination', { method: 'mtime' })) // Compare via modified timestamp

If you don't care about the file contents, just that it is physically present, you could set the option method: 'exists'.

stream.pipe(changed('path/to/destination', { method: 'exists '})) // Skips if a file with the same name already exists

When comparing file contents, keep in mind that anything that is added later in the stream will show up as a difference. If a sourcemap comment is added later in the stream, this shows up as a difference. Use the option injectSourceMapComment: true to append a sourcemap comment to the file contents when comparing. Appending it to a the file itself could cause some tools to output it twice.

stream.pipe(changed('path/to/destination', { injectSourceMapComment: true })) // Pretends the source file has a sourcemap comment added for comparison

Changed in place

Leaves only changed files in the stream, as compared to a destination directory. It also saves a cache file .cache-file to disk in the destination folder, with the filenames and their resulting value. It will add new files to the cache and remove non-existing files the next time around. Useful if you want to write changes back to the source directory, to prevent an infinite loop and unnecessary writes. When compressing images, for example. Don't forget to call .remember() in the finish event of the stream.

const { changedInPlace } = require('@eklingen/vinyl-stream-gears')
stream.pipe(changedInPlace.filter('path/to/destination'))
...
stream.on('finish', () => changedInPlace.remember('path/to/destination')) // Save cache file
return stream

You can choose between comparing either the SHA1 hash of the file contents (default) via the option method: 'hash' or the file's modified timestamp via the option method: 'mtime'. The last one is less reliable in combination with source control.

stream.pipe(changedInPlace.filter('path/to/destination', { method: 'mtime' })) // Compare via modified timestamp

Filesize

Report the size of the file passing through the stream.

const { filesize } = require('@eklingen/vinyl-stream-gears')
stream.pipe(filesize())

By default, both the raw and the gzipped size is shown. Disable the latter via the option showGzip: false.

stream.pipe(filesize({ showGzip: false }))

Filesize diff

Reports the size difference of the file passing through the stream after operations have been performed.

const { filesizeDiff } = require('@eklingen/vinyl-stream-gears')
stream.pipe(filesizeDiff.start())
...
stream.pipe(filesizeDiff.report())

You can choose to report only the files that have been changed in size (default) via the option reportOnlyChanged: true or report all the files via the option reportOnlyChanged: false.

stream.pipe(filesizeDiff.report({ reportOnlyChanged: false }))

You can also use this to filter out files from the stream that have not decreased in size. Useful when compressing images. You can give the change a bit of slack (default is 100) via the option slack: <Number>. This number is the amount of bytes it should ignore. This is useful if you don't want to write images to disk that have only decreased a couple of bytes.

stream.pipe(filesizeDiff.start())
...
stream.pipe(filesizeDiff.filter({ slack: 100 }))
...
stream.pipe(filesizeDiff.report())

Filter

Filter files from the stream by property. Useful if you want to remove certain files from the stream.

const { filter } = require('@eklingen/vinyl-stream-gears')
stream.pipe(filter([ file => path.extname(file.relative) === '.map', ... ]))

Log

Reports the file in the stream to the console.

const { log } = require('@eklingen/vinyl-stream-gears')
stream.pipe(log())

You can use the option showContents: true to show the contents. This can be helpful when verifying if a plugin is behaving itself.

stream.pipe(log({ showContents> true }))

Passthrough

Does nothing. Wrapper around PassThrough. Can be used for tertiary if else statements within streams.

const { passthrough } = require('@eklingen/vinyl-stream-gears')
stream.pipe(debug ? doStuff() : passthrough())

Prepend

Prepend text to the beginning of a file.

const { prepend } = require('@eklingen/vinyl-stream-gears')
stream.pipe(prepend('Greetings!'))

Rename

Rename files in the stream. Also renames sourcemaps, if the file has a .sourceMap property. Possible properties in the mutation object are: filename, extname and dirname.

const { rename } = require('@eklingen/vinyl-stream-gears')
stream.pipe(rename(filename => ({ extname: 'css' })))

Replace

Performs a content replacement on the file in the stream. Can take an array of replacements, in the format [ { replace: (string or regex), value: (string) }, ... ]. These replacements are run in the order they are given.

const { replace } = require('@eklingen/vinyl-stream-gears')
stream.pipe(replace([
  { replace: '80s', value: '90s' },
  { replace: /(complicated|regex)/g, value: '' }
]))

Run

Run a shell command. This one is relatively untested.

const { run } = require('@eklingen/vinyl-stream-gears')
stream.pipe(run('ls -ll'))

In this use, it just executes the command. You can also pass options. These are the defaults:

options = {
  env: process.env,
  cwd: process.cwd(),
  input: false, // STDIO input
  quiet: false, // Don't report anything
  stdio: ['pipe', 'pipe', 'pipe'],
  windowsHide: true // hide console on Windows
}

Giving a string to input should pass that to STDIO. But again, this is relatively untested.


Copyright (c) 2019 Elco Klingen. MIT License.