👨💻 Последние статьи из моего блога:
Обновлено 2024-05-06 09:01
Tiny tool to run commands for modified, staged, and committed files in a GIT repository.
Home Page: https://npm.im/nano-staged
License: MIT License
👨💻 Последние статьи из моего блога:
Обновлено 2024-05-06 09:01
I have script to be sure that I changed versions everywhere. If I made a mistake, this script will exit with 1
code and prints:
Update pnpm version in .github/workflows/test.yml
I add this script to nano-staged
and it works well. But nano-staged
has unnecessary Error
block at the end of output.
Current output:
Nano Staged v0.4.1
- Preparing pipeliner...
» Done backing up original repo state.
- Running tasks...
*.(js|ts) no staged files matching the pattern were found.
*.css no staged files matching the pattern were found.
*.svelte no staged files matching the pattern were found.
.tool-versions node ./scripts/check-versions.js
- Restoring to its original state...
» Done restoring up to its original state.
- Removing patch files...
» Done removing up patch files.
node ./scripts/check-versions.js:
Update pnpm version in .github/workflows/test.yml
Error
at Object.runTasks (file:///home/ai/Dev/slowreader/node_modules/.pnpm/[email protected]/node_modules/nano-staged/lib/pipeliner.js:118:21)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Object.backupUnstagedFiles (file:///home/ai/Dev/slowreader/node_modules/.pnpm/[email protected]/node_modules/nano-staged/lib/pipeliner.js:62:7)
at async Object.run (file:///home/ai/Dev/slowreader/node_modules/.pnpm/[email protected]/node_modules/nano-staged/lib/pipeliner.js:42:7)
at async run (file:///home/ai/Dev/slowreader/node_modules/.pnpm/[email protected]/node_modules/nano-staged/lib/run.js:58:5) {
tasks: '\x1B[31mnode ./scripts/check-versions.js:\n' +
'\x1B[39mUpdate pnpm version in .github/workflows/test.yml'
}
Expected output:
Nano Staged v0.4.1
- Preparing pipeliner...
» Done backing up original repo state.
- Running tasks...
*.(js|ts) no staged files matching the pattern were found.
*.css no staged files matching the pattern were found.
*.svelte no staged files matching the pattern were found.
.tool-versions node ./scripts/check-versions.js
- Restoring to its original state...
» Done restoring up to its original state.
- Removing patch files...
» Done removing up patch files.
node ./scripts/check-versions.js:
Update pnpm version in .github/workflows/test.yml
https://github.com/usmanyunusov/nano-staged/blob/master/lib/glob-to-regex.js
*.(js|css)
*js,*.css
Our project use kolorist and zx npm package to handle some config and log color in the terminal and CI/CD --> I thinks nano-staged should keep the raw result scripts from output.
Raw output from our scripts -->
Here the nano-staged output
Sorry, I don't know it yet 🥲 - Thanks for reading this 🙏
No response
No response
Facing similar issue on Windows with nano-staged (specifically, receiving a 'command line is too long' when running nano-staged pre-commit hooks w/a large number of files).
lint-staged v10 allegedly fixes this issue by batching / parallelizing the list of files into chunks.
batch, lint-staged/lint-staged#147 (comment)
No response
No response
It will be nice to have support of .nano-staged.json
config in additional to package.json
. In webapps my team prefer separated configs.
Currently git worktree
is not supported. The following error appears:
before attempting to read .git
as a directory stat it to determine if it is a worktree (where .git is a file containing a path) or not (where it is a directory)
No response
To recreate create a new project:
mkdir test-project-master
cd test-project
git init
yarn init -y && yarn add nano-staged prettier
Then add a config such as this one to package.json
"nano-staged": {
"**/*.js": [
"prettier -w"
],
}
Now commit the initial setup, then create a new worktree and attempt to make a commit there
git add -A
git commit -m "Initial setup"
git worktree add ../test-project-dev -b dev-branch # creates new worktree with its own branch
# note that a branch cannot be checked out by multiple worktrees,
# thus a new branch is required
cd ../test-project-dev
yarn # install in the new worktree
touch a-file.js
git add a-file.js
git commit -m "added a file"
You should now get the error as in the current working directory .git
is a file describing the location of its location in the .git
directory of the main tree. For more information on worktrees see git-worktree
Hey, is it possible to improve the message nano-staged
produces? When a user uses a git client that doesn't support colors, the message looks unreadable
lint-staged
:
yarn run v1.22.17
$ /Users/pavel/t/main/node_modules/.bin/lint-staged
[STARTED] Preparing...
[SUCCESS] Preparing...
[STARTED] Running tasks...
[STARTED] Running tasks for *.{js,jsx,ts,tsx}
[STARTED] Running tasks for *.scss
[SKIPPED] No staged files match *.scss
[STARTED] eslint --fix
[FAILED] eslint --fix [FAILED]
[SUCCESS] Running tasks...
[STARTED] Applying modifications...
[SKIPPED] Skipped because of errors from tasks.
[STARTED] Reverting to original state because of errors...
[SUCCESS] Reverting to original state because of errors...
[STARTED] Cleaning up...
[SUCCESS] Cleaning up...
✖ eslint --fix:
/Users/pavel/t/main/app/javascript/__tests__/sites/reports/loyalty/loyalty_members_report/loyalty_member_details/loyalty_member_details_render.test.js
442:12 error Do not access a member directly from an await expression unicorn/no-await-expression-member
✖ 1 problem (1 error, 0 warnings)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
husky - pre-commit hook exited with code 1 (error)
nano-staged
:
yarn run v1.22.17
$ /Users/pavel/t/main/node_modules/.bin/nano-staged
Nano Staged v0.4.1
- Preparing pipeliner...
» Done backing up original repo state.
- Running tasks...
*.scss no staged files matching the pattern were found.
*.{js,jsx,ts,tsx} eslint --fix
*.{js,jsx,ts,tsx} stylelint --config '.stylelint_css_in_js.js' --syntax scss
- Restoring to its original state...
» Done restoring up to its original state.
- Removing patch files...
» Done removing up patch files.
eslint --fix:
�[0m�[0m
�[0m�[4m/Users/pavel/t/main/app/javascript/__tests__/sites/reports/loyalty/loyalty_members_report/loyalty_member_details/loyalty_member_details_render.test.js�[24m�[0m
�[0m �[2m442:12�[22m �[31merror�[39m Do not access a member directly from an await expression �[2municorn/no-await-expression-member�[22m�[0m
�[0m�[0m
�[0m�[31m�[1m✖ 1 problem (1 error, 0 warnings)�[22m�[39m�[0m
�[0m�[31m�[1m�[22m�[39m�[0m
Error
at Object.runTasks (file:///Users/pavel/t/main/node_modules/nano-staged/lib/pipeliner.js:118:21)
at async Object.backupUnstagedFiles (file:///Users/pavel/t/main/node_modules/nano-staged/lib/pipeliner.js:62:7)
at async Object.run (file:///Users/pavel/t/main/node_modules/nano-staged/lib/pipeliner.js:42:7)
at async run (file:///Users/pavel/t/main/node_modules/nano-staged/lib/run.js:58:5) {
tasks: 'eslint --fix:\n' +
'\x1B[0m\x1B[0m\n' +
'\x1B[0m\x1B[4m/Users/pavel/t/main/app/javascript/__tests__/sites/reports/loyalty/loyalty_members_report/loyalty_member_details/loyalty_member_details_render.test.js\x1B[24m\x1B[0m\n' +
'\x1B[0m \x1B[2m442:12\x1B[22m \x1B[31merror\x1B[39m Do not access a member directly from an await expression \x1B[2municorn/no-await-expression-member\x1B[22m\x1B[0m\n' +
'\x1B[0m\x1B[0m\n' +
'\x1B[0m\x1B[31m\x1B[1m✖ 1 problem (1 error, 0 warnings)\x1B[22m\x1B[39m\x1B[0m\n' +
'\x1B[0m\x1B[31m\x1B[1m\x1B[22m\x1B[39m\x1B[0m\n'
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
husky - pre-commit hook exited with code 1 (error)
lint-staged
:
[~/t/main]$ git commit -m '.' *[pp-js-deps][ruby-3.0.2@t]
yarn run v1.22.17
$ /Users/pavel/t/main/node_modules/.bin/lint-staged
✔ Preparing...
⚠ Running tasks...
❯ Running tasks for *.{js,jsx,ts,tsx}
✖ eslint --fix [FAILED]
◼ stylelint --config '.stylelint_css_in_js.js' --syntax scss
↓ No staged files match *.scss [SKIPPED]
↓ Skipped because of errors from tasks. [SKIPPED]
✔ Reverting to original state because of errors...
✔ Cleaning up...
✖ eslint --fix:
/Users/pavel/t/main/app/javascript/__tests__/sites/reports/loyalty/loyalty_members_report/loyalty_member_details/loyalty_member_details_render.test.js
442:12 error Do not access a member directly from an await expression unicorn/no-await-expression-member
✖ 1 problem (1 error, 0 warnings)
error Command failed with exit code 1.
nano-staged
[~/t/main]$ git commit -m '.' [pp-js-deps][ruby-3.0.2@t]
yarn run v1.22.17
$ /Users/pavel/t/main/node_modules/.bin/nano-staged
Nano Staged v0.4.1
- Preparing pipeliner...
» Done backing up original repo state.
- Running tasks...
*.scss no staged files matching the pattern were found.
*.{js,jsx,ts,tsx} eslint --fix
*.{js,jsx,ts,tsx} stylelint --config '.stylelint_css_in_js.js' --syntax scss
- Restoring to its original state...
» Done restoring up to its original state.
- Removing patch files...
» Done removing up patch files.
eslint --fix:
/Users/pavel/t/main/app/javascript/__tests__/sites/reports/loyalty/loyalty_members_report/loyalty_member_details/loyalty_member_details_render.test.js
442:12 error Do not access a member directly from an await expression unicorn/no-await-expression-member
✖ 1 problem (1 error, 0 warnings)
Error
at Object.runTasks (file:///Users/pavel/t/main/node_modules/nano-staged/lib/pipeliner.js:118:21)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Object.backupUnstagedFiles (file:///Users/pavel/t/main/node_modules/nano-staged/lib/pipeliner.js:62:7)
at async Object.run (file:///Users/pavel/t/main/node_modules/nano-staged/lib/pipeliner.js:42:7)
at async run (file:///Users/pavel/t/main/node_modules/nano-staged/lib/run.js:58:5) {
tasks: '\x1B[31meslint --fix:\n' +
'\x1B[39m\x1B[0m\x1B[0m\n' +
'\x1B[0m\x1B[4m/Users/pavel/t/main/app/javascript/__tests__/sites/reports/loyalty/loyalty_members_report/loyalty_member_details/loyalty_member_details_render.test.js\x1B[24m\x1B[0m\n' +
'\x1B[0m \x1B[2m442:12\x1B[22m \x1B[31merror\x1B[39m Do not access a member directly from an await expression \x1B[2municorn/no-await-expression-member\x1B[22m\x1B[0m\n' +
'\x1B[0m\x1B[0m\n' +
'\x1B[0m\x1B[31m\x1B[1m✖ 1 problem (1 error, 0 warnings)\x1B[22m\x1B[39m\x1B[0m\n' +
'\x1B[0m\x1B[31m\x1B[1m\x1B[22m\x1B[39m\x1B[0m\n'
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
husky - pre-commit hook exited with code 1 (error)
I have a codebase with some customized svg icons and would like to automatically run *.svg
through svgo
, but not touch *.inkscape.svg
files. With lint-staged
I solved this with the pattern "**/!(*inkscape).svg"
.
pattern matches any string unless it contains inkscape
immediatly before .svg
foo.bar.svg
is not matched.
I wrote the following test file to simulate my use-case:
import { is } from 'uvu/assert'
import { test } from 'uvu'
import { globToRegex } from '../lib/glob-to-regex.js' //nano-staged@git
function match(glob, path, opts = {}) {
let regex = globToRegex(glob, opts)
return regex.regex.test(path)
}
test('mitsunee use-case', () => {
const pattern = '**/!(*foo).txt' // match all *.txt files other than *foo.txt
is(match(pattern, 'bar.foo.txt', { extended: true }), false, "Doesn't match *.foo.txt")
is(match(pattern, 'bar.baz.txt', { extended: true }), true, 'Does match *.baz.txt') // FAILS
is(match(pattern, 'bar.txt', { extended: true }), true, 'Does match *.txt') // FAILS
is(match(pattern, 'foo.bar.txt', { extended: true }), true, 'Does match foo*.txt') // FAILS
})
test.run()
I tried adjusting the pattern by (re)moving the single star and got the same result each time.
In addition I cloned the repository of micromatch
(which lint-staged
uses for glob pattern matching) and wrote the following tests to confirm my existing setup works as intended (all four tests passed):
'use strict';
require('mocha');
const { equal } = require('assert');
const { isMatch } = require('..'); //micromatch@git
describe('mitsunee use-case', () => {
const pattern = '**/!(*foo).txt';
it("Doesn't match *.foo.txt", () => {
equal(isMatch('bar.foo.txt', pattern), false);
});
it('Does match *.baz.txt', () => {
equal(isMatch('bar.baz.txt', pattern), true);
});
it('Does match *.txt', () => {
equal(isMatch('bar.txt', pattern), true);
});
it('Does match foo.*.txt', () => {
equal(isMatch('foo.bar.txt', pattern), true);
});
});
Sometimes, running eslint
on a monorepo workspace causes errors because the eslint
binary is being run from the root workspace, causing issues for instance when using the TypeScript plugin, which relies on the tsconfig.json
being on the workspace directory.
When executing any of the scripts specified in nano-staged
, we could use find-up
to determine where the nearest package.json
is located and use its directory as the cwd
.
No response
No response
I'm trying to migrate the project from lint-staged
to nano-staged
. Installed. Replaced configuration:
"nano-staged": {
"*.{js,ts,vue}": "eslint --cache --fix"
},
Run
$ npx nano-staged
Nano Staged v0.3.1
- Preparing pipeliner...
» Done backing up original repo state.
- Running tasks...
*.{js,ts,vue} eslint --cache --fix
- Restoring to its original state...
» Done restoring up to its original state.
- Removing patch files...
» Done removing up patch files.
eslint --cache --fix:
Error: spawn eslint ENOENT
This is repeated with any command
"nano-staged": {
"*.{js,ts,vue}": "npx eslint --cache --fix"
},
$ npx nano-staged
Nano Staged v0.3.1
- Preparing pipeliner...
» Done backing up original repo state.
- Running tasks...
*.{js,ts,vue} npx eslint --cache --fix
- Restoring to its original state...
» Done restoring up to its original state.
- Removing patch files...
» Done removing up patch files.
npx eslint --cache --fix:
Error: spawn npx ENOENT
"nano-staged": {
"*.{js,ts,vue}": "npm run lint"
},
$ npx nano-staged
Nano Staged v0.3.1
- Preparing pipeliner...
» Done backing up original repo state.
- Running tasks...
*.{js,ts,vue} npm run lint
- Restoring to its original state...
» Done restoring up to its original state.
- Removing patch files...
» Done removing up patch files.
npm run lint:
Error: spawn npm ENOENT
Heey,
thanks for this piece of magic, I was disappointed with lint-staged so I came here!
I'd like to have a way to suppress any progress message printed during the lint process, I just need to know if all's done or if some error occurred. The git client I use (fork) doesn't support interactive output very well, so it logs all the lines every time:
A --quiet
command line flag might do the trick for me.
No response
No response
Help improved the documentation :)
PD
PD
PD
npm
module.exports = {
'*.{ts,tsx}': [
'pnpm prettier --write',
() => 'pnpm tsc -p tsconfig.json',
'pnpm eslint --fix'
],
'*.css': ['pnpm prettier --write', 'pnpm stylelint --fix']
};
Leads to:
./node_modules/.bin/nano-staged --config ./.nano-staged.cjs
× Nano Staged config invalid
mkdir test
cd test
git init
pnpm add .nano-staged.cjs
nano .nano-staged.cjs # copy config from above
pnpm nano-staged -c .nano-staged.cjs
System:
OS: Linux 6.2 Fedora Linux 38 (Workstation Edition)
CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
Memory: 5.24 GB / 15.31 GB
Container: Yes
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.1.0 - ~/.asdf/installs/nodejs/20.1.0/bin/node
Yarn: 1.22.19 - ~/.asdf/shims/yarn
npm: 9.6.4 - ~/.asdf/plugins/nodejs/shims/npm
Browsers:
Chrome: 112.0.5615.165
Firefox: 112.0.2
pnpm
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.