rslint-staged
v0.0.15
Published
Run linters on git staged files in Rust
Downloads
42
Readme
rslint-staged
A Rust-based tool for running linters on Git staged files like lint-staged.
Support npm, yarn, pnpm etc.
Introduction
rslint-staged allows you to run lint tools only on files in the Git staging area, rather than checking the entire codebase, thereby speeding up the development process. This tool is written in Rust and provided as a Node.js module, offering higher performance and resource efficiency.
Performance
Performance tests conducted with 22 JavaScript files in the Git staging area.
hyperfine --warmup 5 'npm run lint-staged' 'npm run rslint-staged' hyperfine --warmup 5 'npm run rslint-staged' 'npm run lint-staged' Features
- Run lint tools only on Git staged files
- Support for multiple configuration formats (JSON, TOML, YAML, etc.)
- High-performance Rust implementation with Node.js integration
- Support for custom working directories
- Parallel execution of multiple lint tasks
- Cross-platform compatible binary
- Automatically executes commands from package.json's lint-staged configuration
Installation
Global Installation (Recommended)
npm install -g rslint-stagedLocal Installation
npm install rslint-staged --save-devInstallation with yarn
yarn add -D rslint-stagedInstallation with pnpm
pnpm add -D rslint-stagedNote: During installation, the package will attempt to compile a native binary for your platform. This requires:
- Rust toolchain to be installed on your system (https://www.rust-lang.org/tools/install)
- Node.js build tools
If you encounter any issues during installation, you can try adding the --ignore-scripts flag, but note that you'll need to manually compile the binary afterward.
Usage
Command Line Usage
After global installation, you can use it directly from the command line:
rslint-stagedWith local installation, add a script to your package.json:
{
"scripts": {
"lint-staged": "rslint-staged"
}
}Then run:
npm run lint-stagedConfiguration (in package.json)
Configure your linting tasks in package.json's lint-staged section:
{
"scripts": {
"lint-staged": "rslint-staged"
},
"lint-staged": {
"*.js": ["eslint --fix", "prettier --write", "git add"],
"*.ts": ["tsc --noEmit", "eslint --fix"],
"*.css": ["stylelint --fix"]
}
}The Rust implementation of rslint-staged will:
- Identify files in the Git staging area
- Read the
lint-stagedconfiguration from package.json - Apply the specified commands to matching files
- Report success or failure based on the command results
Integration with Husky
Integrate with husky to run automatically before each commit:
# Install husky
npm install -D husky
# Initialize husky (if not already done)
npx husky install
# Add pre-commit hook : husky - add command is DEPRECATED
# npx husky add .husky/pre-commit "npx rslint-staged"
echo "npm lint-staged" > .husky/pre-commitAPI Usage
const { lintStaged } = require('rslint-staged');
// Async execution
async function run() {
const passed = await lintStaged({
cwd: process.cwd(),
configPath: './custom-config.json'
});
if (passed) {
console.log('All lint checks passed!');
} else {
console.log('Lint checks failed');
}
}
run();Configuration Options
rslint-staged supports the following configuration options:
cwd: Working directory pathconfigPath: Custom configuration file pathverbose: Whether to output detailed logsconcurrent: Whether to run tasks in parallel
Development
Building the project
# Install dependencies
npm install
# Build Rust library
npm run buildManual Compilation
If you need to manually compile the binary:
# Install Rust if you haven't already
# Visit https://www.rust-lang.org/tools/install
# Clone the repository
git clone https://github.com/[author]/rslint-staged.git
cd rslint-staged
# Install dependencies
npm install
# Build the binary
npm run buildThe build process will create a single rslint-staged.node file that is compatible with your current platform.
Testing
npm testTroubleshooting
"rslint-staged is not recognized as an internal or external command" Error
If you encounter this error on Windows, try the following methods:
- Ensure global installation:
npm install -g rslint-staged - Or use npx to run:
npx rslint-staged - Or add to package.json scripts:
"lint-staged": "rslint-staged", then runnpm run lint-staged
Binary File Loading Failure
If the binary file fails to load, it may be because:
- The compilation process during installation failed
- You're missing the Rust toolchain
To fix this:
- Install Rust: https://www.rust-lang.org/tools/install
- Manually compile by running:
npm run buildin the package directory
License
MIT
