Browse by type
Tiny tool to run commands for modified, staged, and committed git files.
It help speeding up the run tests, linters, scripts, and more
picocolors).Benchmarks running time for 10 file:
$ node bench/running-time/index.js
- lint-staged 1.394 ms
+ nano-staged 0.968 ms
The space in node_modules including sub-dependencies:
$ node bench/size/index.js
Data from packagephobia.com
- lint-staged 6688 kB
+ nano-staged 47 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
nano-staged:terminal
npm install --save-dev nano-staged
nano-staged section and the commands to your package.json:For example:
json
"nano-staged": {
"*.{js,ts}": "prettier --write",
"*.css": ["stylelint", "eslint --fix"]
},
terminal
./node_modules/.bin/nano-staged
Nano Staged by default to run commands from the config for staged files.
You can use Nano Staged with a pre-commit tools to run it automatically after every commit.
Simple Git Hooks
simple-git-hooks as a dev dependency:terminal
npm install simple-git-hooks --save-dev
simple-git-hooks section to your package.json and fill in the pre-commit:For example:
json
"simple-git-hooks": {
"pre-commit": "./node_modules/.bin/nano-staged"
}
terminal
npx simple-git-hooks
package.json:json
"scripts": {
"postinstall": "npx simple-git-hooks"
}
Husky
husky as a dev dependency:terminal
npm install husky --save-dev
terminal
npx husky install
terminal
npx husky add .husky/pre-commit "./node_modules/.bin/nano-staged"
package.json:json
"scripts": {
"postinstall": "npx husky install"
}
Nano Staged supports multiple ways to define config.
nano-staged section in package.json:json
"nano-staged": {
"*": "your-cmd",
"*.ext": ["your-cmd", "your-cmd"]
}
.nano-staged.json, nano-staged.json or .nanostagedrc config file:json
{
"*": "your-cmd",
"*.ext": ["your-cmd", "your-cmd"]
}
.nano-staged.cjs or nano-staged.cjs config file to CommonJS modules:js
module.exports = {
'*': 'your-cmd',
'*.ext': ['your-cmd', 'your-cmd'],
}
.nano-staged.mjs or nano-staged.mjs config file to ECMAScript modules:js
export default {
'*': 'your-cmd',
'*.ext': ['your-cmd', 'your-cmd'],
}
.nano-staged.js or nano-staged.js config file:```js // 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'], } ```
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.jsonJS config files may export export either a single function or an object:
export default (api) => {
const jsFiles = api.files.filter((file) => path.extname(file) === '.js')
return [`eslint --fix ${jsFiles.join(' ')}`, `prettier --write ${jsFiles.join(' ')}`]
}
export default {
'*.js': (api) => `eslint --fix ${api.files.join(' ')}`,
}
The api object exposes:
api.filenames - working filenames
api.type - run type: staged, unstaged, diff
--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 -uRun commands from the config for only git unstaged files. Nano Staged by default use only staged git files.
--diff [<ref1> <ref2>]Run commands to changed files between the working tree and the index or a tree, to changed files between the index and a tree, to changed files between two trees, or to changed files between two index.
--allow-emptyWill allow creating an empty commit.
Special thanks to lint-staged. Some codes was borrowed from it.
The Nano Staged community can be found on GitHub Discussions, where you can ask questions, voice ideas, and share your projects.
$ claude mcp add nano-staged \
-- python -m otcore.mcp_server <graph>