Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/arlac77/npm-package-walker

Walks down the local npm package dependency tree and calls a visitor function for each package
https://github.com/arlac77/npm-package-walker

dependency-tree npm

Last synced: 27 days ago
JSON representation

Walks down the local npm package dependency tree and calls a visitor function for each package

Awesome Lists containing this project

README

        

[![Maintainability](undefined)](undefined)

# npm-package-walker

Walks down the local npm package dependency tree and calls a visitor function
for each package.

```js
import { packageWalker } from "npm-package-walker";

async function collectPackageNames() {
const names = new Set();

await packageWalker(
async (pkg, base, modulePath) => {
names.add(pkg.name);
return true;
},
process.cwd(),
["dependencies"]
);

return names;
}

collectPackageNames().then(names => console.log(names));
```

# API

### Table of Contents

* [defaultDependencyTypes](#defaultdependencytypes)
* [packageVisitor](#packagevisitor)
* [Parameters](#parameters)
* [packageWalker](#packagewalker)
* [Parameters](#parameters-1)

## defaultDependencyTypes

Dependency types used by default

* dependencies
* devDependencies
* optionalDependencies
* peerDependencies

## packageVisitor

Type: [Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)

### Parameters

* `package` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** package.json content
* `directory` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** package base dir
* `packagePath` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>** how deep in the dependency tree are we (starting with 0 for the root package)

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)>** true to continue traversing dependencies of this package

## packageWalker

Walks the local package dependency tree and calls a visitor function.
The visitor function recives the decoded package.json, its directory, and the nesting level starting with 0 for the base package.
Descending the dependency tree continues until the visitor function returns false or no more dependencies
are declared in a package.

### Parameters

* `visitor` **[packageVisitor](#packagevisitor)** async to be called for each package
* `base` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** directory where to start crawling package.json (optional, default `process.cwd()`)
* `dependencyTypes` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>** dig into dependency dev and/or prod (optional, default `defaultDependencyTypes`)

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)>** when resolving to true further dig into the dependencies

# install

With [npm](http://npmjs.org) do:

```shell
npm install npm-package-walker
```

# license

BSD-2-Clause