files | Cell 4 | common ignore paths | Search

This code imports modules and exports a listInProject function that searches for files in a specified directory using glob patterns.

Run example

npm run import -- "list project files"

list project files

var path = require('path');
var {ignores} = importer.import("common ignore paths");
var {glob} = importer.import("glob files")

// TODO: pull all from gitignore library and add to ignore list
//  https://github.com/github/gitignore
// TODO: submit script to github project for fame and fortune!

// But we also want to automatically load projects?
function listInProject(root, match = '{,*,*/,*/*/*,*/*/*/*,*/*/*/*/*}package.json') {
    return glob(match, path.resolve(root))
}

module.exports = {
    listInProject
}

What the code could have been:

const path = require('path');
const { ignores } = require('gitignore-webpack-plugin');
const glob = require('glob');

/**
 * Lists files in a project directory based on a glob pattern.
 *
 * @param {string} root - The project root directory.
 * @param {string} [match='{,*,*/,*/*/*,*/*/*/*,*/*/*/*/*}package.json'] - Glob pattern to match files.
 * @returns {string[]} - An array of file paths that match the glob pattern.
 */
function listInProject(root, match = '{,*,*/,*/*/*,*/*/*/*,*/*/*/*/*}package.json') {
  return glob.sync(match, { cwd: path.resolve(root) });
}

module.exports = {
  /**
   * Lists files in a project directory based on a glob pattern.
   *
   * @param {string} root - The project root directory.
   * @param {string} [match='{,*,*/,*/*/*,*/*/*/*,*/*/*/*/*}package.json'] - Glob pattern to match files.
   * @returns {string[]} - An array of file paths that match the glob pattern.
   */
  listInProject,
};

Code Breakdown

Importing Modules

var path = require('path');
var {ignores} = importer.import('common ignore paths');
var {glob} = importer.import('glob files')

This code imports two modules:

  1. path: a built-in Node.js module for working with file paths.
  2. common ignore paths and glob files from an importer module:

Function listInProject

function listInProject(root, match = '{,*,*/,*/*/*,*/*/*/*,*/*/*/*/*}package.json') {
    return glob(match, path.resolve(root))
}

This function takes two parameters:

  1. root: the root directory to search for files.
  2. match (optional, defaults to a glob pattern matching certain files): a glob pattern to use for finding files.

The function uses the glob function to find files matching the provided glob pattern, in the specified root directory. It then resolves the root path using path.resolve().

Module Exports

module.exports = {
    listInProject
}

This code exports the listInProject function as a module, making it available for use in other parts of the application.