11cb0ef41Sopenharmony_ci# @npmcli/installed-package-contents 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciGet the list of files installed in a package in node_modules, including 41cb0ef41Sopenharmony_cibundled dependencies. 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciThis is useful if you want to remove a package node from the tree _without_ 71cb0ef41Sopenharmony_ciremoving its child nodes, for example to extract a new version of the 81cb0ef41Sopenharmony_cidependency into place safely. 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ciIt's sort of the reflection of [npm-packlist](http://npm.im/npm-packlist), 111cb0ef41Sopenharmony_cibut for listing out the _installed_ files rather than the files that _will_ 121cb0ef41Sopenharmony_cibe installed. This is of course a much simpler operation, because we don't 131cb0ef41Sopenharmony_cihave to handle ignore files or package.json `files` lists. 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ci## USAGE 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci```js 181cb0ef41Sopenharmony_ci// programmatic usage 191cb0ef41Sopenharmony_ciconst pkgContents = require('@npmcli/installed-package-contents') 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_cipkgContents({ path: 'node_modules/foo', depth: 1 }).then(files => { 221cb0ef41Sopenharmony_ci // files is an array of items that need to be passed to 231cb0ef41Sopenharmony_ci // rimraf or moved out of the way to make the folder empty 241cb0ef41Sopenharmony_ci // if foo bundled dependencies, those will be included. 251cb0ef41Sopenharmony_ci // It will not traverse into child directories, because we set 261cb0ef41Sopenharmony_ci // depth:1 in the options. 271cb0ef41Sopenharmony_ci // If the folder doesn't exist, this returns an empty array. 281cb0ef41Sopenharmony_ci}) 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_cipkgContents({ path: 'node_modules/foo', depth: Infinity }).then(files => { 311cb0ef41Sopenharmony_ci // setting depth:Infinity tells it to keep walking forever 321cb0ef41Sopenharmony_ci // until it hits something that isn't a directory, so we'll 331cb0ef41Sopenharmony_ci // just get the list of all files, but not their containing 341cb0ef41Sopenharmony_ci // directories. 351cb0ef41Sopenharmony_ci}) 361cb0ef41Sopenharmony_ci``` 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ciAs a CLI: 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci```bash 411cb0ef41Sopenharmony_ci$ installed-package-contents node_modules/bundle-some -d1 421cb0ef41Sopenharmony_cinode_modules/.bin/some 431cb0ef41Sopenharmony_cinode_modules/bundle-some/package.json 441cb0ef41Sopenharmony_cinode_modules/bundle-some/node_modules/@scope/baz 451cb0ef41Sopenharmony_cinode_modules/bundle-some/node_modules/.bin/foo 461cb0ef41Sopenharmony_cinode_modules/bundle-some/node_modules/foo 471cb0ef41Sopenharmony_ci``` 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ciCLI options: 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci``` 521cb0ef41Sopenharmony_ciUsage: 531cb0ef41Sopenharmony_ci installed-package-contents <path> [-d<n> --depth=<n>] 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ciLists the files installed for a package specified by <path>. 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ciOptions: 581cb0ef41Sopenharmony_ci -d<n> --depth=<n> Provide a numeric value ("Infinity" is allowed) 591cb0ef41Sopenharmony_ci to specify how deep in the file tree to traverse. 601cb0ef41Sopenharmony_ci Default=1 611cb0ef41Sopenharmony_ci -h --help Show this usage information 621cb0ef41Sopenharmony_ci``` 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci## OPTIONS 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci* `depth` Number, default `1`. How deep to traverse through folders to get 671cb0ef41Sopenharmony_ci contents. Typically you'd want to set this to either `1` (to get the 681cb0ef41Sopenharmony_ci surface files and folders) or `Infinity` (to get all files), but any 691cb0ef41Sopenharmony_ci other positive number is supported as well. If set to `0` or a 701cb0ef41Sopenharmony_ci negative number, returns the path provided and (if it is a package) its 711cb0ef41Sopenharmony_ci set of linked bins. 721cb0ef41Sopenharmony_ci* `path` Required. Path to the package in `node_modules` where traversal 731cb0ef41Sopenharmony_ci should begin. 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci## RETURN VALUE 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ciA Promise that resolves to an array of fully-resolved files and folders 781cb0ef41Sopenharmony_cimatching the criteria. This includes all bundled dependencies in 791cb0ef41Sopenharmony_ci`node_modules`, and any linked executables in `node_modules/.bin` that the 801cb0ef41Sopenharmony_cipackage caused to be installed. 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ciAn empty or missing package folder will return an empty array. Empty 831cb0ef41Sopenharmony_cidirectories _within_ package contents are listed, even if the `depth` 841cb0ef41Sopenharmony_ciargument would cause them to be traversed into. 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci## CAVEAT 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ciIf using this module to generate a list of files that should be recursively 891cb0ef41Sopenharmony_ciremoved to clear away the package, note that this will leave empty 901cb0ef41Sopenharmony_cidirectories behind in certain cases: 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci- If all child packages are bundled dependencies, then the 931cb0ef41Sopenharmony_ci `node_modules` folder will remain. 941cb0ef41Sopenharmony_ci- If all child packages within a given scope were bundled dependencies, 951cb0ef41Sopenharmony_ci then the `node_modules/@scope` folder will remain. 961cb0ef41Sopenharmony_ci- If all linked bin scripts were removed, then an empty `node_modules/.bin` 971cb0ef41Sopenharmony_ci folder will remain. 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ciIn the interest of speed and algorithmic complexity, this module does _not_ 1001cb0ef41Sopenharmony_cido a subsequent readdir to see if it would remove all directory entries, 1011cb0ef41Sopenharmony_cithough it would be easier to look at if it returned `node_modules` or 1021cb0ef41Sopenharmony_ci`.bin` in that case rather than the contents. However, if the intent is to 1031cb0ef41Sopenharmony_cipass these arguments to `rimraf`, it hardly makes sense to do _two_ 1041cb0ef41Sopenharmony_ci`readdir` calls just so that we can have the luxury of having to make a 1051cb0ef41Sopenharmony_cithird. 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ciSince the primary use case is to delete a package's contents so that they 1081cb0ef41Sopenharmony_cican be re-filled with a new version of that package, this caveat does not 1091cb0ef41Sopenharmony_cipose a problem. Empty directories are already ignored by both npm and git. 110