Alexander Mills
Alexander Mills

Reputation: 100210

dynamically add folder to ignore when using npm publish

I have a static .npmignore file with

foo
bar
baz

in it. When I publish to NPM, the contents in those 3 dirs will be ignored.

My question is, is there a way to dynamically add a folder to ignore when using npm publish at the command line?

Something like:

npm publish --ignore=.r2g

here we can ignore a folder called .r2g

Here are the npm-publish docs

Upvotes: 4

Views: 1370

Answers (1)

dral
dral

Reputation: 176

First, a static solution:

You can include a .npmignore file per directory that you want to exclude. As the doc states:

Like git, npm looks for .npmignore and .gitignore files in all subdirectories of your package, not only the root directory.

I created an example of this in https://github.com/dral/npmignore-example (and the corresponding package npmignore-example).

file structure index.js .npmignore bar/index.js baz/index.js foo/index.js /.npmignore

the root .npmignore removes only the baz directory. the nested foo/.npmignore removes all content (from this point on).

The installed package includes only

index.js
bar/index.js

In order to do so dynamically you can use a script that adds and then removes a simple .npmignore file in the selected folder(s).

echo '*' > .r2g/.npmignore && npm publish && rm .r2g/.npmignore

Then if you need to automate this, consider using prepublish and postpublish scripts that take into account an env variable so you can use something like NPM_IGNORE=.r2g npm publish.

Hope this helps.

Upvotes: 5

Related Questions