diff --git a/README.md b/README.md index 9aee643..386cd4d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/filter-npm-deps.mjs b/filter-npm-deps.mjs index ff97a07..f775914 100644 --- a/filter-npm-deps.mjs +++ b/filter-npm-deps.mjs @@ -41,7 +41,7 @@ if (!existsSync(packageJsonFileName)) { ); } -let packageJSON = {}; +let packageJSON try { packageJSON = JSON.parse(readFileSync(packageJsonFileName, 'utf8')); @@ -52,7 +52,6 @@ try { ); } -const { optionalDependencies, devDependencies, dependencies } = packageJSON; const filterDeps = (object) => Object.fromEntries( Object.entries(object ?? {}).filter(([key]) => @@ -60,11 +59,15 @@ const filterDeps = (object) => ), ); -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'); diff --git a/package.json b/package.json index 80fb7f9..f366eff 100644 --- a/package.json +++ b/package.json @@ -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"