@rushstack/heft
Version:
Build all your JavaScript projects the same way: A way that works.
96 lines • 3.67 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as path from 'node:path';
import glob from 'fast-glob';
import { Async } from '@rushstack/node-core-library';
function isWatchFileSystemAdapter(adapter) {
return !!adapter.getStateAndTrackAsync;
}
export async function watchGlobAsync(pattern, options) {
const { fs, cwd, absolute } = options;
if (!cwd) {
throw new Error(`"cwd" must be set in the options passed to "watchGlobAsync"`);
}
const rawFiles = await glob(pattern, options);
const results = new Map();
await Async.forEachAsync(rawFiles, async (file) => {
const state = await fs.getStateAndTrackAsync(absolute ? path.normalize(file) : path.resolve(cwd, file));
results.set(file, state);
}, {
concurrency: 20
});
return results;
}
export async function getFileSelectionSpecifierPathsAsync(options) {
const { fileGlobSpecifier, includeFolders, fileSystemAdapter } = options;
const rawEntries = await glob(fileGlobSpecifier.includeGlobs, {
fs: fileSystemAdapter,
cwd: fileGlobSpecifier.sourcePath,
ignore: fileGlobSpecifier.excludeGlobs,
onlyFiles: !includeFolders,
dot: true,
absolute: true,
objectMode: true
});
let results;
if (fileSystemAdapter && isWatchFileSystemAdapter(fileSystemAdapter)) {
results = new Map();
await Async.forEachAsync(rawEntries, async (entry) => {
const { path: filePath, dirent } = entry;
if (entry.dirent.isDirectory()) {
return;
}
const state = await fileSystemAdapter.getStateAndTrackAsync(path.normalize(filePath));
if (state.changed) {
results.set(filePath, dirent);
}
}, {
concurrency: 20
});
}
else {
results = new Map(rawEntries.map((entry) => [entry.path, entry.dirent]));
}
return results;
}
export function asAbsoluteFileSelectionSpecifier(rootPath, fileGlobSpecifier) {
const { sourcePath } = fileGlobSpecifier;
return {
...fileGlobSpecifier,
sourcePath: sourcePath ? path.resolve(rootPath, sourcePath) : rootPath,
includeGlobs: getIncludedGlobPatterns(fileGlobSpecifier),
fileExtensions: undefined
};
}
function getIncludedGlobPatterns(fileGlobSpecifier) {
const patternsToGlob = new Set();
// Glob file extensions with a specific glob to increase perf
const escapedFileExtensions = new Set();
for (const fileExtension of fileGlobSpecifier.fileExtensions || []) {
let escapedFileExtension;
if (fileExtension.charAt(0) === '.') {
escapedFileExtension = fileExtension.slice(1);
}
else {
escapedFileExtension = fileExtension;
}
escapedFileExtension = glob.escapePath(escapedFileExtension);
escapedFileExtensions.add(escapedFileExtension);
}
if (escapedFileExtensions.size > 1) {
patternsToGlob.add(`**/*.{${[...escapedFileExtensions].join(',')}}`);
}
else if (escapedFileExtensions.size === 1) {
patternsToGlob.add(`**/*.${[...escapedFileExtensions][0]}`);
}
// Now include the other globs as well
for (const include of fileGlobSpecifier.includeGlobs || []) {
patternsToGlob.add(include);
}
// Include a default glob if none are specified
if (!patternsToGlob.size) {
patternsToGlob.add('**/*');
}
return [...patternsToGlob];
}
//# sourceMappingURL=FileGlobSpecifier.js.map