As Mike 'Pomax' Kamermans mentioned in comment the fact that .npmignore
and .gitignore
use the same syntax can be leveraged:
git ls-files -co --exclude-per-directory=.npmignore
The command above lists exactly files that are not npm-ignored according to .npmignore
file. (On top of that npm automatically ignores some other entries like node_modules
.)
Git ls-files
command generally lists combinations of files in working directory and index.
-c
option says show cached files
-o
show 'other', i.e. untracked files
--exclude-per-directory=.npmignore
use .npmignore
as name of files of ignore entries
EDIT:
Since the approach above has bunch of exceptions - files that will never or always included regardless of content of the .npmignore
- I find it unreliable. Following command is heavyweight but reliable:
file_name=$(npm pack) && tar -ztf $file_name && rm $file_name
It packages the project, lists package files and at the end removes created package.