Combines Prettier formatting with ESLint Standard linting into a unified command-line tool for JavaScript code quality.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Batch file processing system that can format and lint multiple files with glob patterns, Git integration, and configurable processing modes. The run function is the core engine that powers both the CLI and programmatic usage.
Main execution function that processes files according to configuration with support for Git integration, linting, and various processing modes.
/**
* Main execution function that processes files according to configuration
* @param cwd - Working directory path where files will be processed
* @param config - Configuration object defining processing behavior
* @returns Promise that resolves to undefined on success or Error object on failure
*/
async function run(cwd, config);
interface RunConfig {
patterns?: string[]; // File patterns to process (defaults to ['**/*'] in git repos)
check?: boolean; // Check formatting without modifying files (default: false)
lint?: boolean; // Perform ESLint linting after formatting (default: false)
changed?: boolean; // Process only changed files since HEAD (default: false)
since?: string; // Process changes since given branch/revision (default: undefined)
staged?: boolean; // Process only git staged files (default: false)
lines?: boolean; // Process only changed lines - experimental (default: false)
options?: PrettierOptions; // Prettier configuration options
onStart?: (params: { engine?: object }) => void; // Callback called at processing start
onProcessed?: (result: ProcessResult) => void; // Callback for each processed file
}
interface ProcessResult {
file: string; // Relative file path that was processed
runtime: number; // Processing time in milliseconds
formatted: boolean; // Whether file needed formatting (false if already formatted)
report?: object; // ESLint report object if linting enabled
check?: boolean; // Whether processing was in check mode
}Basic Usage Examples:
const { run } = require('prettier-standard');
// Format all supported files in current directory
await run(process.cwd(), {
patterns: ['**/*.js', '**/*.ts', '**/*.json']
});
// Check formatting without modifying files
await run(process.cwd(), {
patterns: ['src/**/*.js'],
check: true
});
// Format and lint with callbacks
await run(process.cwd(), {
patterns: ['src/**/*.js'],
lint: true,
onProcessed: (result) => {
console.log(`Processed ${result.file} in ${result.runtime}ms`);
if (result.report && result.report.errorCount > 0) {
console.log('Linting errors found');
}
}
});Git Integration Examples:
// Process only changed files since HEAD
await run(process.cwd(), {
changed: true,
lint: true
});
// Process changes since specific branch
await run(process.cwd(), {
since: 'master',
lint: true
});
// Process only staged files (useful for pre-commit hooks)
await run(process.cwd(), {
staged: true,
lint: true
});
// Process only changed lines (experimental)
await run(process.cwd(), {
lines: true,
changed: true
});The patterns parameter supports glob patterns for flexible file selection:
// Glob pattern examples
const patterns = [
'**/*.js', // All JS files recursively
'src/**/*.{ts,tsx}', // TypeScript files in src directory
'**/*.json', // All JSON files
'!node_modules/**', // Exclude node_modules (included by default)
'!**/*.min.js' // Exclude minified files (included by default)
];Default ignore patterns (automatically applied):
!**/*.min.js - Minified JavaScript files!**/{node_modules,coverage,vendor}/** - Common ignore directories!./{node_modules,coverage,vendor}/** - Local ignore directories!**/.{git,svn,hg}/** - Version control directories!./.{git,svn,hg}/** - Local version control directoriesThe system respects ignore files for flexible exclusion:
.prettierignore: Files/patterns to exclude from formatting.eslintignore: Files/patterns to format but exclude from lintingExample .prettierignore:
dist/
build/
**/*.min.js
coverage/Validates formatting without modifying files:
const results = [];
await run(process.cwd(), {
check: true,
patterns: ['src/**/*.js'],
onProcessed: (result) => {
if (!result.formatted) {
results.push(result.file); // File needs formatting
}
}
});
if (results.length > 0) {
console.log('Files need formatting:', results);
process.exit(1);
}Performs ESLint linting with Standard rules after formatting:
let hasErrors = false;
await run(process.cwd(), {
lint: true,
patterns: ['src/**/*.js'],
onStart: ({ engine }) => {
console.log('ESLint engine initialized');
},
onProcessed: (result) => {
if (result.report && result.report.errorCount > 0) {
hasErrors = true;
}
}
});
if (hasErrors) {
console.log('Linting errors found');
process.exit(1);
}Processes only Git staged files using lint-staged integration:
// This automatically handles git staging after processing
await run(process.cwd(), {
staged: true,
lint: true
});Process files changed since a specific revision:
// Files changed since HEAD
await run(process.cwd(), { changed: true });
// Files changed since master branch
await run(process.cwd(), { since: 'master' });
// Files changed since specific commit
await run(process.cwd(), { since: 'abc123' });Process only the specific lines that have changed:
// Only format changed lines within changed files
await run(process.cwd(), {
lines: true,
changed: true
});Note: Line-based processing is experimental due to Prettier limitations with range formatting.
The run function returns errors rather than throwing them:
const error = await run(process.cwd(), {
patterns: ['invalid-pattern['],
lint: true
});
if (error instanceof Error) {
console.error('Processing failed:', error.message);
// Common error types:
if (error.message.includes('glob pattern')) {
console.error('Invalid glob pattern provided');
} else if (error.message.includes('git repository')) {
console.error('Git operations require a git repository');
} else if (error.message.includes('patterns should be an array')) {
console.error('Patterns must be provided as an array');
}
}Multiple filtering layers optimize performance:
.prettierignore patterns are appliedUse callbacks for progress tracking and early termination:
let processedCount = 0;
const maxFiles = 100;
await run(process.cwd(), {
patterns: ['**/*.js'],
onProcessed: (result) => {
processedCount++;
console.log(`Progress: ${processedCount} files processed`);
if (processedCount >= maxFiles) {
console.log('Stopping after processing limit reached');
// Note: Cannot stop mid-processing, but useful for monitoring
}
}
});When lint: true is specified, the system configures ESLint with:
babel-eslint for JavaScript parsingCustom ESLint configuration can be provided via .eslintrc files in the project:
{
"rules": {
"eqeqeq": "off",
"no-console": "warn"
}
}Install with Tessl CLI
npx tessl i tessl/npm-prettier-standard