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

process-files

v1.0.0

Published

Read files and process the contents

Downloads

21,911

Readme

processFiles

This library helps you process files, especially for command-line programs. It accepts a filename or an Array of filenames and will read the files sequentially. When the content loads for a file, your callback will be called. Has built-in support for reading from stdin, both when passed no files to load and when passed "-", per many Unix/Linux commands.

npm version Build Status Dependencies Dev Dependencies

Usage

Use npm to install this package.

npm install --save process-files

Next you need a bit of JavaScript. Let's say you want to just load up two files and display them to the screen.

var processFiles = require('process-files');

processFiles([
    'file1.txt',
    'file2.txt'
], function (err, data, filename) {
    // Check for errors
    if (err) {
        // When errors are passed, `data` and `filename` are undefined.
        console.error('Error reading ' + err.filename, err.toString());
    }

    console.log('Contents of ' + filename);
    console.log(data);
});

Examples

This library is geared for use in a program that uses the command line. When a filename of - is specified there, typically programs use stdin, such as cat - > test.txt would take all input and write it to test.txt.

processFiles('-', function (err, data) {
    if (err) {
        console.error('Error', err.toString());
    } else {
        console.log(data)
    }
});

When passed an empty list of files or when passed the file -, standard input is automatically read.

processFiles(function (err, data, filename) {
    // Even though no files were listed, this reads from stdin.
    console.log(data);
    // The file is always '-' when reading from stdin
    console.log('Filename: ' + filename);  // "Filename: -"
});

Need to work with data asynchronously? Not a problem. You have two choices.

// "done" callback
processFiles(arrayOfFiles, function (err, data, filename, done) {
    // Make text more 1337 for those h4ck3rs.
    data = data.replace(/e/ig, '3');
    data = data.replace(/l/ig, '1');
    data = data.replace(/a/ig, '4');
    data = data.replace(/t/ig, '7');
    fs.writeFile(filename, data, done);
});

// Promises
processFiles(arrayOfFiles, function (err, data, filename) {
    var promise, resolver;

    promise = new Promise(function (resolve) {
        setTimeout(resolve, 30000);  // 30 second delay
    });

    return promise;
});

That's all fine and good, but how do I know that I am done with the list of files? How about another callback?

processFiles(arrayOfFiles, function (err, data, filename) {
    // This is where you would process files
}, function () {
    console.log('All done processing files');
});

You can also specify options.

processFiles(arrayOfFiles, {
    encoding: 'utf8'
}, function (err, data, filename) {
    // Process file content here
});

processFiles([files], [options], callback, [completionCallback])

The library only exports this one function. Parameters are detailed below.

files

This can be a single string filename, an array of filenames, or omitted. When omitted or an empty array is passed, stdin is read instead.

If there is a file of "-", then stdin is read instead for that one file.

options

Options object.

  • encoding - Specify the type of file encoding for input files. Defaults to "utf8".

callback(err, data, filename, [done])

Callback to execute when the file's content is loaded.

When a fourth parameter is specified, then processFiles will wait until the done callback is called.

When fewer than four parameters are specified, the callback may return a promise. File loading will be suspended until the promise is resolved or rejected. If anything other than a promise is returned or if there was no returned value, processing will continue immediately.

completionCallback()

No data is passed to the completion callback. It is executed when processFiles is done processing all files.

Development

If you want to work on this library, you need to check out the repository and run npm install to get the dependencies.

Tests are always included. Make sure tests cover your changes. To run the current tests, just use npm test or grunt test (they will run the same test suite).

License

This software is licensed under an MIT license with an additional non-advertising clause.