Skip to content

Commit

Permalink
feat: add support for peerDependencies and bundleDependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
jase88 committed Oct 23, 2023
1 parent 2332213 commit 486512c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

filter-npm-deps is a convenient Node.js CLI script designed to filter a given list of dependencies from your `package.json`.

It supports filtering dependencies from all three commonly used dependency fields: _dependencies_, _devDependencies_, and _optionalDependencies_.
It supports filtering dependencies from the commonly used dependency fields:
- [dependencies](https://docs.npmjs.com/cli/v10/configuring-npm/package-json#dependencies)
- [devDependencies](https://docs.npmjs.com/cli/v10/configuring-npm/package-json#devdependencies)
- [optionalDependencies](https://docs.npmjs.com/cli/v10/configuring-npm/package-json#optionaldependencies)
- [peerDependencies](https://docs.npmjs.com/cli/v10/configuring-npm/package-json#peerdependencies)
- [bundleDependencies](https://docs.npmjs.com/cli/v10/configuring-npm/package-json#bundledependencies)

## 🎯 Motivation

Expand Down Expand Up @@ -77,4 +82,4 @@ For example with npm:
While filter-npm-deps serves as a simple solution for filtering dependencies, there are other tools available that you might find useful:

- [jq](https://jqlang.github.io/jq/manual/) is a powerful command-line tool for processing JSON data. It can be used to filter for nested properties, but it requires explicit handling of all three dependency fields.
- [install-subset](https://github.com/tabrindle/install-subset) offers a different approach by enabling you to define specific subsets of npm dependencies that should be installed. This allows you to have more fine-grained control over your project's dependencies.
- [install-subset](https://github.com/tabrindle/install-subset) offers a different approach by enabling you to define specific subsets of npm dependencies that should be installed.
21 changes: 12 additions & 9 deletions filter-npm-deps.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ if (!existsSync(packageJsonFileName)) {
);
}

let packageJSON = {};
let packageJSON

try {
packageJSON = JSON.parse(readFileSync(packageJsonFileName, 'utf8'));
Expand All @@ -52,19 +52,22 @@ try {
);
}

const { optionalDependencies, devDependencies, dependencies } = packageJSON;
const filterDeps = (object) =>
Object.fromEntries(
Object.entries(object ?? {}).filter(([key]) =>
dependenciesToKeep.includes(key),
),
);

const newPackageJSON = {
...packageJSON,
optionalDependencies: filterDeps(optionalDependencies),
devDependencies: filterDeps(devDependencies),
dependencies: filterDeps(dependencies),
};
for(const dependencyField of ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']) {
if(packageJSON[dependencyField]) {
packageJSON[dependencyField] = filterDeps(packageJSON[dependencyField])
}
}

if(packageJSON.bundleDependencies) {
packageJSON.bundleDependencies = packageJSON.bundleDependencies.filter(dependency => dependenciesToKeep.includes(dependency))
}


writeFileSync(packageJsonFileName, JSON.stringify(newPackageJSON), 'utf8');
writeFileSync(packageJsonFileName, JSON.stringify(packageJSON), 'utf8');
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "filter-npm-deps",
"version": "0.2.0",
"version": "0.3.0",
"description": "A convenient Node.js CLI script to filter and only keep a list of dependencies from your package.json",
"engines": {
"node": ">=18"
Expand Down

0 comments on commit 486512c

Please sign in to comment.