How to configure prettier to check all files with a specific extension - node.js

I have installed prettier via
yarn add prettier
I would like prettier to only format typescript code (I am developing AWS CDK project, there is no src folder by convention & there could be typescript files here and there). In other words, I would like prettier to check all files in my project that has extension *.ts.
I checked its documentation for configuration. But there is no such option to specify file extension.
How can I run prettier for only *ts files then? Is it even possible? If not, what could be the workaround?

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.

For *.ts files:
npx prettier 'src/**/*.ts' --write
If you want target other file extensions:
npx prettier 'src/**/*.{js,ts,mjs,cjs,json}' --write

Related

Why is my glob in my npm script not working?

I am trying to create a regex glob for an NPM script. This is what I have:
"format": "prettier-eslint --write \"{,!(node_modules|cypressTests)/**/}*.{js,json,vue}\""
And this currently formats all .js, .json, and .vue files that are NOT in the node_modules folder or in the cypressTests folder.
The below is my problem:
The cypressTests folder ALSO contains a node_modules folder that I do not want to format. How can I exclude ./cypressTests/node_modules just like I am currently doing for the folder ./node_modules?
I tried like this and this does not work. It then excludes pretty much everything in the entire project for some reason:
"format": "prettier-eslint --write \"{,!(node_modules|cypressTests/node_modules)/**/}*.{js,json,vue}\""
Prettier has an easier solution to ignore files. You can make a .prettierignore file (which uses .gitignore syntax) to ignore files when prettifying. The file should be placed in the root directory of your project. I believe the syntax inside the file in your case would be:
**/node_modules
Which is actually what prettier ignores by default (among some other source-control folder exclusions)-- so unless you already have a .prettierignore file, all node_modules folders should already be excluded from prettification.
The command would then simply be:
"format": "prettier-eslint --write \"**/*.{js,json,vue}\""
or if you want your .prettierignore file to be somewhere else:
"format": "prettier-eslint --ignore-path <path-to-ignore-file> --write \"**/*.{js,json,vue}\""
If this solution doesn't work for you and you need to use the glob method, I wasn't quite able to figure that out, but this website was handy for testing globs.

Does eslint automatically exclude node_modules/?

I don't have an .eslintignore file and .eslintrc has no ignore patterns. When I run the following: ./node_modules/.bin/eslint **/*.js, it appears that files in the node_modules folder are ignored.
The ESLint documentation makes no mention of any default excluded folders or files. What is the real situation?
node_modules is ignored by default
It is in the doc: https://eslint.org/docs/latest/user-guide/configuring/
relevant part:
In addition to any patterns in the .eslintignore file, ESLint always
follows a couple of implicit ignore rules even if the --no-ignore flag
is passed. The implicit rules are as follows:
node_modules/ is ignored.
dot-files (except for .eslintrc.*), as well as dot-folders and their contents, are ignored.

Can I tell babel to replace an output directory?

The Situation
I'm working on a project in Node.js and using babel to transpile my code. My package.json has a build command defined like this:
"scripts": {
"build": "yarn run babel src -d lib",
},
The Problem
This transpiles fine, taking the content of src and outputing the result to lib, but there are two issues:
lib will contain old files from past transpiles even if they no longer have a matching file in src.
Babel will not rename files with a changed case if my OS is case insensitive. For example, if I had transpiled a file named src/Foo.js and later renamed it to src/foo.js then future transpiles will still be named lib/Foo.js
The Question
Can I tell babel to wipe away the contents of the lib directory before transpiling or do I need to just insert a rm into the build script?
Babel does not have functionality to do this. It is very common to use a rimraf or some other means to delete the directory before running Babel. rm directly is certainly also an option, but that does get more complicated if you want to support Windows too, hence the rimraf usage.
Babel CLI has a flag to remove the output directory: --delete-dir-on-start
Couldn't find any online documentation for it, but it's listed in babel --help:
--delete-dir-on-start Delete the out directory before compilation
Was added in this PR back in 2017.

.npmignore - Ignore all *.ts files but not *.d.ts

I am looking for a good way to ignore all .ts files in a project when I publish to NPM, I can do that by adding the following to my .npmignore file:
*.ts
but wait..actually I want to keep all the .d.ts files in my project, when I publish...
What can I add to my .npmignore file that will keep all .d.ts files but ignore all .ts files?
I assumed I have to use some form of regex to ignore all files that end with .ts that do not end with .d.ts
so the JS regex for this might look like:
*.\.[^d]\.ts
what the above regex should mean is match anything that ends with .ts that does not end with .d.ts
but of course we are stuck with what I believe to be less powerful regexes used by the OS etc.
In the docs for .npmignore it states:
.npmignore files follow the same pattern rules as .gitignore files:
In which case you can negate the .d.ts files in .npmignore by using !. For example in your .npmignore you simply:
# ignore the .ts files
*.ts
# include the .d.ts files
!*.d.ts

eslint ignore project .eslintrc

I'm working on a project that has an .eslintrc file checked in to source control at the root of the project.
The problem is that it has custom dependencies and when I use a tool to run my linting (syntastic with Vim) it fails. The dependencies are custom and I wish to skip running them in my editor.
I want to use another .eslintrc file, rather than the one which is in the root of the project.
How can I ignore <projectroot>/.eslintrc and instead use /custom/path/.eslintrc?
A glance at the ESLint docs tells us that you want to pass the --no-eslintrc option to make it ignore the .eslintrc in the project root and --config /custom/path/.eslintrc to make it use that file instead.
The Syntastic README says you can configure the arguments given to eslint like so:
let g:syntastic_javascript_eslint_args = "--no-eslintrc --config /custom/path/.eslintrc"

Resources