files | Cell 12 | mkdirp | Search

This JavaScript code imports modules, defines two functions (getDirectory and matchFilename) that handle directory and file matching, and exports them as a module. The matchFilename function filters files based on a pattern and returns a new path by joining the directory path with the file name, unless they are the same.

Run example

npm run import -- "file tools, match filename"

file tools, match filename

var importer = require('../Core')
var {minimatch} = importer.import("minimatch")

var getDirectory = (match) => match[match.length-1] === '/'
    ? match
    : (fs.existsSync(match) && fs.lstatSync(match).isDirectory()
       ? match
       : path.dirname(match));


function matchFilename(filename, matchOutput, projectOutput) {
    // TODO: examples of defensive programming
    if(!matchOutput || Object.keys(matchOutput).length === 0) {
         matchOutput = {'{**,**/*}': './'}
    }
    return Object.keys(matchOutput)
    .filter(match => minimatch(filename, match))
    .map(match => {
        var projectMatch = path.join(projectOutput, matchOutput[match])
        var dir = getDirectory(projectMatch);
        return path.join(dir, path.basename(projectMatch) === path.basename(dir)
            ? filename
            : path.basename(matchOutput[match]));
    })
}

module.exports = {
    matchFilename,
    getDirectory
}

What the code could have been:

/**
 * Import required modules and functions.
 */
const fs = require('fs');
const path = require('path');
const importer = require('../Core');
const { minimatch } = importer.import('minimatch');

/**
 * Returns the directory path of the given match.
 * If the match is already a directory path, it is returned as is.
 * If the match is a file path, the directory path is extracted.
 * 
 * @param {string} match - The path to check.
 * @returns {string} The directory path of the match.
 */
function getDirectory(match) {
    return path.dirname(match);
}

/**
 * Matches a filename against a pattern and returns the matched file paths.
 * If no patterns are provided, defaults to matching all files.
 * 
 * @param {string} filename - The filename to match.
 * @param {object} [matchOutput] - Patterns to match against.
 * @param {string} projectOutput - The root directory of the project.
 * @returns {string[]} The matched file paths.
 */
function matchFilename(filename, matchOutput, projectOutput) {
    // Set default matchOutput if not provided
    matchOutput = matchOutput || {};
    if (Object.keys(matchOutput).length === 0) {
        matchOutput = { '{**,**/*}': './' };
    }

    // Filter patterns that match the filename
    const matchingPatterns = Object.keys(matchOutput).filter(match => minimatch(filename, match));

    // Map patterns to file paths
    return matchingPatterns.map(match => {
        const projectMatch = path.join(projectOutput, matchOutput[match]);
        const dir = getDirectory(projectMatch);
        return path.join(dir, path.basename(projectMatch) === path.basename(dir)? filename : path.basename(matchOutput[match]));
    });
}

module.exports = {
    matchFilename,
    getDirectory
};

Code Breakdown

Importing Modules

var importer = require('../Core')
var {minimatch} = importer.import('minimatch')

getDirectory Function

var getDirectory = (match) => match[match.length-1] === '/'? match : 
    (fs.existsSync(match) && fs.lstatSync(match).isDirectory()? match : path.dirname(match));

matchFilename Function

function matchFilename(filename, matchOutput, projectOutput) {
    if(!matchOutput || Object.keys(matchOutput).length === 0) {
        matchOutput = {'{**,**/*}': './'}
    }
    return Object.keys(matchOutput)
   .filter(match => minimatch(filename, match))
   .map(match => {
        var projectMatch = path.join(projectOutput, matchOutput[match])
        var dir = getDirectory(projectMatch);
        return path.join(dir, path.basename(projectMatch) === path.basename(dir) 
           ? filename
            : path.basename(matchOutput[match]));
    })
}

Module Exports

module.exports = {
    matchFilename,
    getDirectory
}