Utilities

Small support functions module for non-application logic. Extracted from the main source files to reduce boilerplate.

Recursive Directory Enumeration

Since input paths may be directories, we need to be able to enumerate the contents of directories recursively.

const fs = require('fs-extra');
const dir = require('node-dir');

module.exports.recursivelyEnumerate = function recursivelyEnumerate(filename) {
  let expanded = [ filename ];
  if (fs.statSync(filename).isDirectory()) {
    expanded = dir.files(filename, { sync: true });
  }

  return expanded;
};

Glob Expansion

Paths may be specified using glob wildcard syntax. Expand the provided inputs to an array of non-wildcard paths.

const glob = require('glob');
const uniq = require('lodash/uniq');

module.exports.expand = function expand(paths) {
  const output = [];

  paths.forEach(path => {
    const expanded = glob.sync(path);

If the path does not expand, include it unchanged. Otherwise add the expansions to the output list.

  if (expanded.length === 0) {
    output.push(path);
  } else {
    output.push(...expanded);
  }
});

Take only the unique results since the wildcard patterns may match an individual path multiple times.

  return uniq(output);
};