File Type: ts
Generated Description:
This TypeScript script removes duplicate package entries from a package.json
file. Specifically, it identifies packages listed in both dependencies
and devDependencies
and removes them from the dependencies
section, leaving them only in devDependencies
. It then overwrites the original package.json
file with the updated dependencies. The script handles cases where either dependencies
or devDependencies
are missing and exits gracefully with a status code of 0 even if errors occur.
-
packageJsonPath
: Determines the path to thepackage.json
file usingprocess.cwd()
to get the current working directory. -
packageJson
: Reads and parses thepackage.json
file into aPackageJson
object (presumably a type definition fromtype-fest
). It uses error handling to throw if neitherdependencies
nordevDependencies
are found. -
dependencies
anddevDependencies
: Extracts thedependencies
anddevDependencies
objects from the parsedpackageJson
. Includes error handling for missing dependency sections. -
picked
: This is the core logic. It uses a series of chained array methods:Object.entries
: Converts thedependencies
object into an array of key-value pairs..filter
: Filters out entries where the key also exists indevDependencies
..map
: (Redundant in this case, could be removed) Creates a new array with the same key-value pairs..reduce
: Creates a new object ({} as Record<string, string>
) containing only the unique dependencies (those not present indevDependencies
). It includes a check for null or undefined keys and values and avoids overwriting existing entries.
-
if (require.main === module)
: This check ensures the code inside theasync
function only executes when the script is run directly (not imported as a module). -
async
function: This function handles the file writing. It updatespackageJson.dependencies
with thepicked
object, writes the modified JSON back to the file usingfs.writeFileSync
, and includes atry...catch...finally
block for robust error handling.
- Error Handling: The script uses
try...catch
blocks to handle potential errors during file reading and writing, and it throws custom errors if dependencies are missing, ensuring graceful failure and informative error messages. - Immutability (Partial): While it modifies the
packageJson
object directly, the core logic inpicked
uses functional programming techniques (filter
,reduce
,map
) to build a new object based on the originaldependencies
without directly mutating it. - Type Safety (Partial): Uses TypeScript for type checking, leveraging
type-fest
for better type definitions. This improves code maintainability and helps prevent errors. - Functional Programming Paradigm: The use of
filter
,map
, andreduce
demonstrates the functional approach to data manipulation.
- Maintaining Clean
package.json
Files: This script helps automate the process of removing redundant dependencies, leading to cleaner and more manageable project files. - Pre-Deployment Script: This could be included as part of a pre-deployment workflow to ensure the production
package.json
contains only necessary dependencies. - Improving Build Performance: Redundant dependencies might cause unnecessary build time or consume more space during packaging. Removing duplicates can potentially optimize build performance.
- Code Consistency: Enforcing a single location for each dependency improves code consistency and maintainability.
This script is a valuable tool for maintaining clean and efficient Node.js projects. However, it’s important to note that manually reviewing the resulting package.json
is always recommended before deploying to production.
Description generated on 3/21/2025, 11:38:57 PM