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

files-exist

v1.1.0

Published

Verify that all the filenames in an array are existing files

Downloads

26,288

Readme

files-exist

Coverage Status Build Status

This simple tool accepts an array of filenames (or a single filename as a string) with or without globbing wildcards and returns an identical array if all the filenames point to existing files on the file system.

Basic usage:

var filesExist = require('files-exist'),
files = filesExist(['package.json', 'node_modules/express/lib/express.js']);

Usage in unit testing

You can use files-existin your unit tests. Simply pass an array of files you want to exist and either check for errors or disable them and compare the array returned.

var chai = require('chai'),
filesExist = require('files-exist');

describe('images', function() {
  it('should contain at least one .png file', function() {
    var files = 'images/*.png';

    // Method one: pass a bound call and check for thrown errors
    chai.expect(filesExist.bind(filesExist, files, { checkGlobs: true })).to.not.throw(Error);

    // Method two: suppress errors and check the returned results
    var results = filesExist(files, { checkGlobs: true, throwOnMissing: false });
    chai.expect('images/*.png' in results).to.not.equal(false);
  });
});

Usage in build process

Use files-exist around your gulp.src() source files to notice when a soft dependency is broken. This can help you to spot newly added bower components that haven't been installed on your local machine yet.

var jsLibs = [
    'vendor/jquery/dist/jquery.js',
    'vendor/jquery.stellar/jquery.stellar.min.js',
    'vendor/autofill-event/src/autofill-event.js',
    'vendor/js-cookie/src/js.cookie.js',
    'vendor/modernizr/modernizr.js',
    'vendor/lodash/lodash.js',
    'app/*.js',
    '!app/config.js' // Items starting with an exclamation point are ignored
];

gulp.task('build:js', function() {
  return gulp.src(filesExist(jsLibs, { exceptionMessage: 'Please run `bower install` to install missing library' }))
  .pipe(gulp.dest(outputPath + '/js'));
});

Options

checkGlobs

By default, files-exist doesn't check whether globs match any files. Set this to true to enable glob evaluation.

onMissing

You can pass a callback function that gets called for every missing file. It will get one parameter, the name of the file or pattern that was not found. If you want to trigger an error, you may throw or return false from the callback.

options = { onMissing: function(file) {
  throw new TestError('Custom error for missing file: ' + file);
}};

Or alternatively:

options = { onMissing: function(file) {
  console.log('File not found: ' + file);
  return false;
}};

throwOnMissing

Set this to false if you don't want files-exist to throw any exceptions when missing files are encountered. Note that you'll need to check the returned array yourself in this case, as it will be returned without the missing files.

exceptionClass

Set the name of the exception class you want to use for the error message. Defaults to Error.

exceptionMessage

Customize the message of the exception. The name of the first missing file encountered will be appended.

Thanks

  • MartinKei for string parameter and option to ignore files