|
|
4 years ago | |
|---|---|---|
| .. | ||
| bin | 4 years ago | |
| node_modules/glob | 4 years ago | |
| LICENSE | 4 years ago | |
| README.md | 4 years ago | |
| index.js | 4 years ago | |
| package.json | 4 years ago | |
README.md
npm-packlist
Get a list of the files to add from a folder into an npm package.
These can be handed to tar like so to make an npm package tarball:
const packlist = require('npm-packlist')
const tar = require('tar')
const packageDir = '/path/to/package'
const packageTarball = '/path/to/package.tgz'
packlist({ path: packageDir })
.then(files => tar.create({
prefix: 'package/',
cwd: packageDir,
file: packageTarball,
gzip: true
}, files))
.then(_ => {
// tarball has been created, continue with your day
})
This uses the following rules:
-
If a
package.jsonfile is found, and it has afileslist, then ignore everything that isn't infiles. Always include the readme, license, notice, changes, changelog, and history files, if they exist, and the package.json file itself. -
If there's no
package.jsonfile (or it has nofileslist), and there is a.npmignorefile, then ignore all the files in the.npmignorefile. -
If there's no
package.jsonwith afileslist, and there's no.npmignorefile, but there is a.gitignorefile, then ignore all the files in the.gitignorefile. -
Everything in the root
node_modulesis ignored, unless it's a bundled dependency. If it IS a bundled dependency, and it's a symbolic link, then the target of the link is included, not the symlink itself. -
Unless they're explicitly included (by being in a
fileslist, or a!negatedrule in a relevant.npmignoreor.gitignore), always ignore certain common cruft files:- .npmignore and .gitignore files (their effect is in the package already, there's no need to include them in the package)
- editor junk like
.*.swp,._*and.*.origfiles .npmrcfiles (these may contain private configs)- The
node_modules/.binfolder - Waf and gyp cruft like
/build/config.gypiand.lock-wscript - Darwin's
.DS_Storefiles because wtf are those even npm-debug.logfiles at the root of a project
You can explicitly re-include any of these with a
fileslist inpackage.jsonor a negated ignore file rule.
Only the package.json file in the very root of the project is ever
inspected for a files list. Below the top level of the root package,
package.json is treated as just another file, and no package-specific
semantics are applied.
Interaction between package.json and .npmignore rules
For simplicity, it is best to use either a files list in package.json
or a .npmignore file, and not both. If you only use one of these
methods, you can skip this documentation section.
The files list in package.json is used to direct the exploration of the
tree. In other words, that's all the walker will ever look at when
exploring that level.
In some cases this can lead to a .npmignore file being ignored. If a
directory is listed in files, then any rules in a root or nested
.npmignore files will be honored.
For example, with this package.json:
{
"files": [ "dir" ]
}
a .npmignore file at dir/.npmignore (and any subsequent
sub-directories) will be honored. However, a .npmignore at the root
level will be skipped.
Conversely, with this package.json:
{
"files": ["dir/subdir"]
}
a .npmignore file at dir/.npmignore will not be honored.
Any specific file matched by a glob or filename in the package.json files
list will be included, and cannot be excluded by any .npmignore files in
nested directories, or by a .npmignore file in the root package
directory, unless that root .npmignore file is also in the files list.
The previous (v1) implementation used in npm 6 and below treated
package.json as a special sort of "reverse ignore" file. That is, it was
parsed and handled as if it was a .npmignore file with ! prepended to
all of the globs in the files list. In order to include children of a
directory listed in files, they would also have /** appended to them.
This is tricky to explain, but is a significant improvement over the previous (v1) implementation used in npm 6 and below, with the following beneficial properties:
- If you have
{"files":["lib"]}inpackage.json, then the walker will still ignore files such aslib/.DS_Storeandlib/.foo.swp. The previous implementation would include these files, as they'd be matched by the computed!lib/**ignore rule. - If you have
{"files":["lib/a.js","lib/b.js"]}inpackage.json, and alib/.npmignorecontaininga.js, then the walker will still include the two files indicated inpackage.json, and ignore thelib/.npmignorefile. The previous implementation would mark these files for inclusion, but then exclude them when it came to the nested.npmignorefile. (Ignore file semantics dictate that a "closer" ignore file always takes precedence.) - A file in
lib/pkg-template/package.jsonwill be included, and itsfileslist will not have any bearing on other files being included or skipped. When treatingpackage.jsonas just Yet Another ignore file, this was not the case, leading to difficulty for modules that aim to initialize a project.
In general, this walk should work as a reasonable developer would expect. Matching human expectation is tricky business, and if you find cases where it violates those expectations, please let us know.
API
Same API as ignore-walk, just hard-coded file list and rule sets.
The Walker and WalkerSync classes take a bundled argument, which
is a list of package names to include from node_modules. When calling
the top-level packlist() and packlist.sync() functions, this
module calls into npm-bundled directly.