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.
npm run import -- "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
}
/**
* 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
var importer = require('../Core')
var {minimatch} = importer.import('minimatch')
importer
from ../Core
.minimatch
function from the module named minimatch
using importer.import
.var getDirectory = (match) => match[match.length-1] === '/'? match :
(fs.existsSync(match) && fs.lstatSync(match).isDirectory()? match : path.dirname(match));
getDirectory
function takes a match
parameter.match
string is a forward slash /
. If so, it returns the match
string as is.match
string exists as a file or directory using fs
functions and if it is a directory. If so, it returns the match
string as is. Otherwise, it returns the directory name of the match
string using path.dirname
.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]));
})
}
matchFilename
function takes three parameters: filename
, matchOutput
, and projectOutput
.matchOutput
is not provided or is an empty object, it is initialized with a default value {'{**,**/*}': './'}
.Object.keys
method to get an array of keys from matchOutput
and filters them using the minimatch
function to match against the filename
.path.join
and calls the getDirectory
function to get the directory path.filename
.module.exports = {
matchFilename,
getDirectory
}
matchFilename
and getDirectory
.