@rushstack/heft
Version:
Build all your JavaScript projects the same way: A way that works.
123 lines • 5.36 kB
JavaScript
;
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.asAbsoluteFileSelectionSpecifier = exports.getFileSelectionSpecifierPathsAsync = exports.watchGlobAsync = void 0;
const path = __importStar(require("path"));
const fast_glob_1 = __importDefault(require("fast-glob"));
const node_core_library_1 = require("@rushstack/node-core-library");
function isWatchFileSystemAdapter(adapter) {
return !!adapter.getStateAndTrackAsync;
}
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 (0, fast_glob_1.default)(pattern, options);
const results = new Map();
await node_core_library_1.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;
}
exports.watchGlobAsync = watchGlobAsync;
async function getFileSelectionSpecifierPathsAsync(options) {
const { fileGlobSpecifier, includeFolders, fileSystemAdapter } = options;
const rawEntries = await (0, fast_glob_1.default)(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 node_core_library_1.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;
}
exports.getFileSelectionSpecifierPathsAsync = getFileSelectionSpecifierPathsAsync;
function asAbsoluteFileSelectionSpecifier(rootPath, fileGlobSpecifier) {
const { sourcePath } = fileGlobSpecifier;
return Object.assign(Object.assign({}, fileGlobSpecifier), { sourcePath: sourcePath ? path.resolve(rootPath, sourcePath) : rootPath, includeGlobs: getIncludedGlobPatterns(fileGlobSpecifier), fileExtensions: undefined });
}
exports.asAbsoluteFileSelectionSpecifier = asAbsoluteFileSelectionSpecifier;
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 = fast_glob_1.default.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