A command-line tool that automatically runs Prettier formatting on changed files in Git and Mercurial repositories.
npx @tessl/cli install tessl/npm-pretty-quick@4.2.0Pretty Quick is a command-line tool that automatically runs Prettier formatting on changed files in Git and Mercurial repositories. It enables developers to selectively format only the files they've modified, making it ideal for incremental code formatting in large codebases and as a pre-commit hook.
npm install -D prettier pretty-quick or yarn add -D prettier pretty-quickESM (Node.js import):
import prettyQuick from "pretty-quick";CommonJS (Node.js require):
const prettyQuick = require("pretty-quick");import prettyQuick from "pretty-quick";
// Basic usage - format changed files in current directory
const result = await prettyQuick(process.cwd());
console.log(result.success); // boolean
console.log(result.errors); // string[]
// With options
const result = await prettyQuick(process.cwd(), {
staged: true, // Only format staged files (Git only)
verbose: true, // Show files being processed
pattern: "**/*.js", // Only process JavaScript files
check: false, // Format files (don't just check)
bail: false, // Don't exit on first formatting needed
});# Format all changed files
npx pretty-quick
# Format only staged files (pre-commit mode)
npx pretty-quick --staged
# Check formatting without modifying files
npx pretty-quick --check
# Filter by pattern
npx pretty-quick --pattern "**/*.{js,ts}"
# Compare against specific branch
npx pretty-quick --branch developPretty Quick is built around several key components:
The core pretty-quick function that formats changed files in a repository.
/**
* Format changed files in a repository using Prettier
* @param currentDirectory - Directory to run pretty-quick in
* @param options - Configuration options
* @returns Promise resolving to result with success status and any errors
*/
function prettyQuick(
currentDirectory: string,
options?: Partial<PrettyQuickOptions>
): Promise<PrettyQuickResult>;
interface PrettyQuickResult {
/** Whether the operation completed successfully without errors */
success: boolean;
/** Array of error codes if any issues occurred */
errors: string[];
}Usage Example:
import prettyQuick from "pretty-quick";
async function formatChanges() {
const result = await prettyQuick(process.cwd(), {
staged: true,
onWriteFile: (file) => console.log(`Formatted: ${file}`),
onCheckFile: (file, isFormatted) => {
if (!isFormatted) console.log(`Needs formatting: ${file}`);
}
});
if (!result.success) {
console.error("Formatting failed:", result.errors);
process.exit(1);
}
}Complete configuration interface for the pretty-quick function.
interface PrettyQuickOptions {
/** Path to Prettier configuration file */
config: string;
/** SCM revision to compare against (e.g., git commit hash) */
since: string;
/** Only format staged files (Git only) */
staged: boolean;
/** File pattern(s) to filter which files are processed */
pattern: string[] | string;
/** Re-stage files after formatting when using --staged */
restage: boolean;
/** Branch to compare against (defaults to 'master' for Git, 'default' for Mercurial) */
branch: string;
/** Exit with error code if any files need formatting */
bail: boolean;
/** Check formatting without modifying files */
check: boolean;
/** Path to ignore file (alternative to .prettierignore) */
ignorePath: string;
/** Resolve Prettier config for file extensions */
resolveConfig: boolean;
/** Enable verbose output showing each file being processed */
verbose: boolean;
/** Callback fired when SCM revision is determined */
onFoundSinceRevision(name: string, revision: string | null): void;
/** Callback fired when changed files are found */
onFoundChangedFiles(changedFiles: string[]): void;
/** Callback fired when a partially staged file is encountered */
onPartiallyStagedFile(file: string): void;
/** Callback fired when examining each file (only in verbose mode) */
onExamineFile(relative: string): void;
/** Callback fired when checking file formatting status */
onCheckFile(relative: string, isFormatted: boolean): void;
/** Callback fired when a file is written/formatted */
onWriteFile(relative: string): Promise<void> | void;
}The pretty-quick command-line interface.
# Command: pretty-quick [options]
# Core options
--staged # Format only staged files (Git only)
--no-restage # Skip re-staging files after formatting
--branch <branch> # Branch to compare against
--pattern <pattern> # File pattern filter (can be used multiple times)
--verbose # Show each file being processed
--bail # Exit with error if files need formatting
--check # Check formatting without modifying files
--ignore-path <path> # Alternative ignore file path
--no-resolve-config # Don't resolve Prettier config for extensions
# Hidden/undocumented options
--config <path> # Path to Prettier config file
--since <revision> # SCM revision to compare againstCLI Usage Examples:
# Basic formatting of changed files
pretty-quick
# Pre-commit hook usage
pretty-quick --staged
# Check formatting in CI
pretty-quick --check --branch main
# Format specific file types
pretty-quick --pattern "**/*.{js,ts,json}"
pretty-quick --pattern "src/**/*" --pattern "tests/**/*"
# Verbose output for debugging
pretty-quick --verbose
# Custom ignore file
pretty-quick --ignore-path .gitignore
# Don't resolve custom Prettier extensions
pretty-quick --no-resolve-configPretty Quick can fail with the following error types returned in the errors array:
"BAIL_ON_WRITE" - Files needed formatting and bail option was true"PARTIALLY_STAGED_FILE" - Partially staged files were found when using --staged"CHECK_FAILED" - Files were incorrectly formatted when using --checkThe function throws exceptions for:
// Error handling example
try {
const result = await prettyQuick(process.cwd(), { staged: true, bail: true });
if (!result.success) {
if (result.errors.includes('PARTIALLY_STAGED_FILE')) {
console.log('Some files are partially staged. Please stage all changes.');
}
if (result.errors.includes('BAIL_ON_WRITE')) {
console.log('Files needed formatting. Commit aborted.');
}
if (result.errors.includes('CHECK_FAILED')) {
console.log('Files are not properly formatted.');
}
}
} catch (error) {
console.error('Pretty Quick failed:', error.message);
}Pretty Quick supports the following source control managers:
.git directory.hg directoryPretty Quick respects standard Prettier configuration:
.prettierrc - Prettier configuration file (found by searching up file system).prettierignore - Files to ignore (found from repository root and working directory).editorconfig - Editor configuration (used by Prettier)Custom ignore file can be specified with --ignore-path option.
{
"simple-git-hooks": {
"pre-commit": "pretty-quick --staged"
}
}# In CI pipeline - check formatting
pretty-quick --check --branch main
# Fail CI if files are not formatted
if ! pretty-quick --check --branch main; then
echo "Code formatting check failed"
exit 1
fi{
"scripts": {
"format": "pretty-quick",
"format:staged": "pretty-quick --staged",
"format:check": "pretty-quick --check"
}
}