For *.ts files:
npx prettier 'src/**/*.ts' --write
If you want target other file extensions:
npx prettier 'src/**/*.{js,ts,mjs,cjs,json}' --write
Answer from Tiago Bértolo on Stack OverflowFor *.ts files:
npx prettier 'src/**/*.ts' --write
If you want target other file extensions:
npx prettier 'src/**/*.{js,ts,mjs,cjs,json}' --write
To exclude files from formatting, create a .prettierignore file at the root of your project.
And to format only the *.ts files you should ignore everything but the *.ts files.
Example
# Ignore everything recursively
*
# But not the .ts files
!*.ts
# Check subdirectories too
!*/
In the code above, the * means to ignore everything including the subfolders, and the next line !*.ts tells the prettier to reverse the previous ignoring of the .ts files. The last line !*/ means to check the subdirectories too, but with the previous rule, it's only looking for the .ts files.
Check the prettier and gitignore docs for more information.
You can do that using lint-staged:
Linting makes more sense when running before committing your code. By doing that you can ensure no errors are going into repository and enforce code style. But running a lint process on a whole project is slow and linting results can be irrelevant. Ultimately you only want to lint files that will be committed.
This project contains a script that will run arbitrary npm and shell tasks with a list of staged files as an argument, filtered by a specified glob pattern.
Install lint-staged and husky, which is required for pre-commit hooks, with this command:
npm install --save-dev lint-staged husky
Change your package.json as follows:
{
"scripts": {
"precommit": "lint-staged"
},
"lint-staged": {
"*.js": [
"prettier --write",
"git add"
]
}
}
I found that just running:
prettier --write $(git diff --name-only --diff-filter d | grep '\.js$' | xargs)
was quite enough for my needs, just made an alias and used that.
Use the extension called ”Format Files”. Here are the steps:
- Download the extension called ”Format Files” on VSCode.
- Select and open the folder with files to format on VSCode.
- Press Ctrl+Shift+P to open command palette.
- Enter "Start Format Files: Workspace" and select this option.
Source: https://marketplace.visualstudio.com/items?itemName=jbockle.jbockle-format-files
This works for me
Install prettier:
npm init
npm i -D prettier --save-dev
Add following script in package.json:
"pretty": "prettier --write \"./**/*.{js,jsx,mjs,cjs,ts,tsx,json}\""
In this case only, i need to format my .js .jsx and .json files.
Run script:
npm run pretty
I've been trying to use as neovim as vanilla possible to expand my horizon and one of the coolest and unknown features out here must be the filter (:!) command. Basically, you can (optionally) redirect part of your buffer as stdin to a shell command which writes it back into your buffer!
As an example, this one way how you could format your code using prettier:
:%!npx prettier --stdin-filepath %
Explanation:
The
%before the!is the range, ie, which part of your buffer do you want to redirect to the command?%means everything, but you could also say for example:.!npx prettier ...where the.would be just the current line, or make a visual selection and write:'<,'>!npx prettier ....The
!is called filter by the vim docs, it calls a shell commandNext comes the command itself, the first part is pretty self explanatory. I don't have prettier installed globally so I use
npx. You have to pass the--stdin-filepathoption, otherwise it won't read your buffer, but the file content (which can differ from your buffer if, for example, you haven't saved your buffer yet). The second%means, in the context of the shell command, "the current file name". (And another cool trick: you can expand it using <c-a>)
That's it, you can use this with any shell command! The beauty of the unix philosophy...
Of course you could also map this to some keybinding.
If you know other cool and hidden tricks of vim, please share them in the comments!