Internal task generation system that analyzes glob patterns and creates optimized file system traversal tasks. The task system groups patterns by base directory to minimize redundant file system operations and improve performance.
Generate internal task representation from glob patterns for optimized file system traversal.
/**
* Generate internal task representation from patterns
* @param patterns - Single pattern or array of glob patterns
* @param options - Configuration options affecting task generation
* @returns Array of Task objects representing optimized traversal plan
*/
function generateTasks(patterns: string | string[], options?: Options): Task[];
/**
* Internal task representation for optimized file system traversal
*/
interface Task {
/** Base directory where traversal starts */
base: string;
/** Whether task contains dynamic patterns requiring traversal */
dynamic: boolean;
/** All patterns in this task (positive and negative combined) */
patterns: string[];
/** Positive patterns that match files */
positive: string[];
/** Negative patterns that exclude files */
negative: string[];
}Usage Examples:
import fg from "fast-glob";
// Simple pattern generates single task
const simpleTasks = fg.generateTasks('*.js');
// [{
// base: '.',
// dynamic: true,
// patterns: ['*.js'],
// positive: ['*.js'],
// negative: []
// }]
// Multiple patterns may generate multiple tasks
const complexTasks = fg.generateTasks([
'src/**/*.js',
'lib/**/*.ts',
'!**/*.test.*'
]);
// [
// {
// base: 'src',
// dynamic: true,
// patterns: ['src/**/*.js', '!**/*.test.*'],
// positive: ['src/**/*.js'],
// negative: ['**/*.test.*']
// },
// {
// base: 'lib',
// dynamic: true,
// patterns: ['lib/**/*.ts', '!**/*.test.*'],
// positive: ['lib/**/*.ts'],
// negative: ['**/*.test.*']
// }
// ]Each task contains specific information for optimizing file system operations:
The base property indicates the starting directory for traversal, minimizing unnecessary file system access:
// Patterns starting from different directories create separate tasks
const tasks = fg.generateTasks([
'src/**/*.js', // base: 'src'
'docs/**/*.md', // base: 'docs'
'*.json' // base: '.'
]);
// Result: 3 separate tasks with different base directories
console.log(tasks.map(task => task.base));
// ['src', 'docs', '.']The dynamic property indicates whether the task requires file system traversal:
// Static patterns (no wildcards)
const staticTasks = fg.generateTasks('package.json');
// [{ base: '.', dynamic: false, ... }]
// Dynamic patterns (with wildcards)
const dynamicTasks = fg.generateTasks('**/*.js');
// [{ base: '.', dynamic: true, ... }]
// Mixed patterns
const mixedTasks = fg.generateTasks(['package.json', '*.js']);
// May result in separate static and dynamic tasksTasks organize patterns into positive (matching) and negative (excluding) arrays:
const tasks = fg.generateTasks([
'src/**/*.{js,ts}',
'!src/**/*.test.*',
'lib/**/*.js',
'!lib/internal/**'
]);
// Each task contains:
// positive: patterns that match files
// negative: patterns that exclude files
// patterns: all patterns combined (negative patterns converted to exclusion format)The task system provides several performance benefits:
Patterns are grouped by base directory to minimize file system traversal:
// These patterns will be grouped into a single task
const groupedTasks = fg.generateTasks([
'src/**/*.js',
'src/**/*.ts',
'src/**/*.json'
]);
// Result: Single task with base: 'src'
// These patterns create separate tasks
const separateTasks = fg.generateTasks([
'src/**/*.js',
'lib/**/*.js',
'docs/**/*.md'
]);
// Result: Three tasks with bases: 'src', 'lib', 'docs'When patterns target the root directory (.), the task system optimizes by merging related tasks:
const rootTasks = fg.generateTasks([
'src/**/*.js',
'lib/**/*.ts',
'*' // Root pattern - merges everything into single task
]);
// Result: Single task with base: '.' covering all patterns
// This prevents multiple separate directory traversalsUse task generation to analyze pattern complexity and optimize glob operations:
function analyzePatterns(patterns: string | string[]) {
const tasks = fg.generateTasks(patterns);
return {
taskCount: tasks.length,
baseDirectories: tasks.map(task => task.base),
hasDynamicPatterns: tasks.some(task => task.dynamic),
hasStaticPatterns: tasks.some(task => !task.dynamic),
totalPatterns: tasks.reduce((sum, task) => sum + task.patterns.length, 0)
};
}
// Analyze pattern efficiency
const analysis = analyzePatterns([
'src/**/*.{js,ts}',
'lib/**/*.js',
'package.json',
'!**/*.test.*'
]);
console.log(analysis);
// {
// taskCount: 3,
// baseDirectories: ['src', 'lib', '.'],
// hasDynamicPatterns: true,
// hasStaticPatterns: true,
// totalPatterns: 8
// }Task generation respects configuration options that affect pattern interpretation:
// Options affecting task generation
const tasksWithOptions = fg.generateTasks('file.js', {
baseNameMatch: true, // Converts to **/file.js
cwd: '/custom/path' // Affects base directory calculation
});
// Brace expansion affects pattern count
const expandedTasks = fg.generateTasks('*.{js,ts,json}', {
braceExpansion: true // Expands to ['*.js', '*.ts', '*.json']
});Use task generation to debug complex pattern sets and optimize performance:
function debugPatterns(patterns: string | string[]) {
const tasks = fg.generateTasks(patterns);
console.log(`Generated ${tasks.length} tasks:`);
tasks.forEach((task, index) => {
console.log(`Task ${index + 1}:`);
console.log(` Base: ${task.base}`);
console.log(` Dynamic: ${task.dynamic}`);
console.log(` Positive patterns: ${JSON.stringify(task.positive)}`);
console.log(` Negative patterns: ${JSON.stringify(task.negative)}`);
});
}
// Debug complex pattern set
debugPatterns([
'src/**/*.{js,ts}',
'lib/**/*.js',
'docs/**/*.md',
'!**/*.{test,spec}.*',
'package.json'
]);The task management system is primarily used internally by fast-glob but provides valuable insights for pattern optimization and debugging complex glob operations.