UNPKG

@rushstack/heft

Version:

Build all your JavaScript projects the same way: A way that works.

137 lines 5.57 kB
"use strict"; // 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __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.watchGlobAsync = watchGlobAsync; exports.getFileSelectionSpecifierPathsAsync = getFileSelectionSpecifierPathsAsync; exports.asAbsoluteFileSelectionSpecifier = asAbsoluteFileSelectionSpecifier; 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; } 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; } 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 = 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