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

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

Related

How to configure prettier to check all files with a specific extension

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

Node and standard glob patterns

How would I use standard glob patterns in a .gitignore file to ignore the node_modules and scss directories and all .json files in all my project sub-directoies?
Check out this boilerplate, it should be a good starting point. Also, add the following.
# SCSS directory or files into it
scss
scss/**/*.scss
# JSON files in sub-directors
**/*.json
To minimize the answer of #federico-dondi, adding the following lines to .gitconfig should do the trick:
node_modules/
scss/
**/*.json
Hope This helps!

gitignore all node_modules directories and subdirectories

If I put this in .gitignore:
node_modules
it doesn't appear to ignore subdirectories that contain a node_modules folder.
So my guess is that this will work:
*/node_modules/
does anyone know what the right syntax is for ignore files to ignore matching subfolders?
You can ignore sub-directories indeed, and the syntax you proposed is correct, here is a working example of a .gitignore i'm using :
# Bundler
Gemfile.lock
bin/*
.bundle/*
*/test/

npm module missing files after publish

For reference, the repo is https://github.com/microsoftly/luis-response-builder.
The node module files are generated with tsc and output to the dist folder. I have a prepublishOnly step that removes the dist folder, runs tsc, then runs the test against the transpiled js. The tests pass when I publish just fine.
The problem is, when I install the project anywhere else, the dist folder contains only the file with the path dist/src/index.js.
I cannot for the life of me figure out why the file is missing when installed but not when published.
Quoting from npm-publish Documentation:
All files in the package directory are included if no local .gitignore or .npmignore file exists. If both files exist and a file is ignored by .gitignore but not by .npmignore then it will be included.
Your repository's .gitignore file contains the following:
node_modules
dist
*.env
yarn-error.log
Since dist is being ignored, it's not committed with npm publish, as per the documentation.
Check out the package.json documentation about files.
Since you haven't included the files key, it will only include the file specified in main (along with some other default files).
The files value is an array so you can include multiple files and/or folders.
eg:
files: [
"dist",
"config/somefile.js"
]

Does NPM ignore files listed in .gitignore?

I have a file that is generated by npm install command (using preinstall task). I don't want to add it in the git repository, nor in the NPM project.
Supposing the file name is foo.json, I added it in .gitignore file as foo.json.
Is this enough to avoid uploading it on NPM registry?
I know I can add .npmignore file that will surely ignore the file, but I won't add it if .gitignore already does this.
If a project has both an .npmignore and .gitignore file, npm will only use the .npmignore file.
From the documentation:
Use a .npmignore file to keep stuff out of your package. If there's no .npmignore file, but there is a .gitignore file, then npm will ignore the stuff matched by the .gitignore file. If you want to include something that is excluded by your .gitignore file, you can create an empty .npmignore file to override it.
In simpler terms, npm prefers the .npmignore file if it is there, but will fall back to the .gitignore file.
In many cases, both Git and npm can ignore the same files, so it makes sense to just use a .gitignore file on its own. If there's ever a discrepancy (i.e. npm and Git need to ignore different files), then you need to maintain separate .gitignore and .npmignore files.
More information on what to put in .npmignore files: Should I .npmignore my tests?
For anyone reading this trying to ignore a file/dir from git but wish to include it in npm publish and have tried using an empty .npmignore file with no luck. This works.
In your .gitignore file, add the file/dir you wish to exclude **/build for example and in your .npmignore file make sure you specify the same file/dir but with the ! prefix so for the build example you would include !**/build

Resources