usmanyunusov/nano-staged
About
Tiny tool to run commands for modified, staged, and committed files in a GIT repository.
Developer ToolsWhat you can do
- 📦 Small: 52kB (2x+ lighter than lint-staged).
- 🥇 No dependencies
- ☯️ Support multiple file states like staged, unstaged, last-commit, changed etc
Measured star growth
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- +2
- in 1 month
- Collecting history
Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.
README.md
Original
Nano Staged
Tiny tool to run commands for modified, staged, and committed files in a GIT repository.
It helps speed up running of the tests, linters, scripts, and more.
Features
- 📦 Small: 52kB (2x+ lighter than lint-staged).
- 🥇 No dependencies
- ☯️ Support multiple file states like staged, unstaged, last-commit, changed etc
Benchmarks
Benchmarks running time for 10 file:
$ node bench/running-time/index.js
- lint-staged 1.145 ms
+ nano-staged 0.652 ms
The space in node_modules including sub-dependencies:
$ node bench/size/index.js
Data from packagephobia.com
- lint-staged 147 kB
+ nano-staged 52 kB
The performance results were generated on a MBP Late 2013, 2.3 GHz Intel Core i7 by running npm run bench in the library folder. See bench/running-time/index.js
Usage
Getting Started
Install
nano-staged:npm install --save-dev nano-stagedor
yarn add nano-staged -DAdd the
nano-stagedsection and the commands to yourpackage.json:For example:
"nano-staged": { "*.{js,ts}": "prettier --write", "*.css": ["stylelint", "eslint --fix"] },Run commands with Nano Staged:
./node_modules/.bin/nano-stagedNano Staged by default to run commands from the config for staged files.
Pre-commit Hook
You can use Nano Staged with a pre-commit tools to run it automatically before every commit.
Simple Git Hooks
Install
simple-git-hooksas a dev dependency:npm install simple-git-hooks --save-devAdd the
simple-git-hookssection to yourpackage.jsonand fill in thepre-commit:For example:
"simple-git-hooks": { "pre-commit": "./node_modules/.bin/nano-staged" }Run the CLI script to update the git hooks with the commands from the config:
npx simple-git-hooksTo automatically have Git hooks enabled after install, edit
package.json:"scripts": { "postinstall": "npx simple-git-hooks" }
Husky
Install
huskyas a dev dependency:npm install husky --save-devEnable Git hooks:
npx husky installAdd a command to a hook:
npx husky add .husky/pre-commit "./node_modules/.bin/nano-staged"To automatically have Git hooks enabled after install, edit
package.json:"scripts": { "postinstall": "npx husky install" }
Configuration
Nano Staged supports multiple ways to define config.
nano-stagedsection inpackage.json:"nano-staged": { "*": "your-cmd", "*.ext": ["your-cmd", "your-cmd"] }or a separate
.nano-staged.json,nano-staged.jsonor.nanostagedrcconfig file:{ "*": "your-cmd", "*.ext": ["your-cmd", "your-cmd"] }or a more flexible
.nano-staged.cjsornano-staged.cjsconfig file to CommonJS modules:module.exports = { '*': 'your-cmd', '*.ext': ['your-cmd', 'your-cmd'], }or a more flexible
.nano-staged.mjsornano-staged.mjsconfig file to ECMAScript modules:export default { '*': 'your-cmd', '*.ext': ['your-cmd', 'your-cmd'], }or a more flexible
.nano-staged.jsornano-staged.jsconfig file:// package.json => "type": "module" export default { '*': 'your-cmd', '*.ext': ['your-cmd', 'your-cmd'], } // package.json => "type": "commonjs" module.exports = { '*': 'your-cmd', '*.ext': ['your-cmd', 'your-cmd'], }
Format priorities:
If there are multiple configuration files in the same directory, Nano Staged will only use one. The priority order is as follows:
.nano-staged.jsnano-staged.js.nano-staged.cjsnano-staged.cjs.nano-staged.mjsnano-staged.mjs.nano-staged.jsonnano-staged.json.nanostagedrcpackage.json
Config Function API:
JS config files may export export either a single function or an object:
export default (api) => {
const jsFiles = api.filenames.filter((file) => path.extname(file) === '.js')
return [`eslint --fix ${jsFiles.join(' ')}`, `prettier --write ${jsFiles.join(' ')}`]
}
export default {
'*.js': (api) => `eslint --fix ${api.filenames.join(' ')}`,
}
The api object exposes:
api.filenames - working filenames
api.type - run type: staged, unstaged, diff
Command Line Interface
--config [<path>] or -c [<path>]
Path to file that contains your configuration object. The path should be either absolute or relative to the directory that your process is running from.
--unstaged or -u
Run commands from the config only for git unstaged files. Nano Staged by default uses only staged git files.
--diff [<ref1> <ref2>]
Run commands on files changed between the working tree and the index or a tree, on files changed between the index and a tree, files changed between two trees, or on files changed between two indexes (commit hashes).
--allow-empty
Will allow creating an empty commit.
--quiet
Shows only that work is done and errors occured.
--bail
Stop running commands after the first failure.
Thanks
Special thanks to lint-staged. Some codes was borrowed from it.
Community
The Nano Staged community can be found on GitHub Discussions, where you can ask questions, voice ideas, and share your projects.
Explore related projects
Loading…