From 475ea1e8eb86ece1a0d5eac1d89e42ed5feb8fa0 Mon Sep 17 00:00:00 2001 From: Danilo Britto Date: Fri, 5 Jul 2024 21:26:44 -0500 Subject: [PATCH] Minor updates --- README.md | 18 +++++++++++------ package.json | 4 ++-- src/index.ts | 56 ++++++++++++++++++++++++++++++++++++---------------- 3 files changed, 53 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index cad0287..54ec5d1 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # npm-ri -npm install using regular expression +npm install using regular expression to update installed packages # Usage -## Basic (by default version is "latest") +## Basic (by default package version is "latest") ```sh -npx npm-ri "" +npx npm-ri "" ``` ### Example @@ -19,11 +19,17 @@ npx npm-ri "@radix-ui/.*" ## With custom version ```sh -npx npm-ri "" -v +npx npm-ri "" --dependency-version ``` -### Example +or ```sh -npx npm-ri "@tanstack/.*" -v beta +npx npm-ri "" -dv +``` + +### Example + +```sh +npx npm-ri "@tanstack/.*" -dv beta ``` diff --git a/package.json b/package.json index 5b835b9..bc2a9fc 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,8 @@ "type": "module", "name": "npm-ri", "private": false, - "description": "npm install packages using regular expression", - "version": "0.0.3", + "description": "npm install packages using regular expressions to update installed dependencies", + "version": "0.0.4", "author": "Danilo Britto", "repository": { "type": "git", diff --git a/src/index.ts b/src/index.ts index e91c439..4cca0a9 100755 --- a/src/index.ts +++ b/src/index.ts @@ -1,52 +1,74 @@ #! /usr/bin/env node +import { cwd } from "node:process"; import { createRequire } from "node:module"; import { spawn } from "node:child_process"; import { program } from "commander"; -function run(regex: RegExp, { version }: { version: string }) { - const require = createRequire(`${process.cwd()}/`); +function run( + dependencyNameRegex: RegExp, + { dependencyVersion, dryRun }: { dependencyVersion: string; dryRun: boolean } +) { + const require = createRequire(`${cwd()}/`); const pkg = require("./package.json"); const dependencies = Object.keys(pkg.dependencies || {}) .concat(Object.keys(pkg.devDependencies || {})) - .filter((dependency) => regex.test(dependency)) - .map((dependency) => `${dependency}@${version}`); + .filter((dependency) => dependencyNameRegex.test(dependency)) + .map((dependency) => `${dependency}@${dependencyVersion}`); if (dependencies.length > 0) { + if (dryRun) { + console.info( + `The next packages might be updated:\n${dependencies.join("\n")}` + ); + + return; + } + const npmInstall = spawn("npm", ["install"].concat(dependencies)); npmInstall.on("close", (code: number) => { if (code === 0) { console.info( - `The next packages were updated to their latest version:\n${dependencies.join( - "\n" - )}` + `The next packages were updated:\n${dependencies.join("\n")}` ); - } else { - console.error("Something went wrong"); + + return; } - process.exit(code); + throw new Error("Something went wrong"); }); + return; } - console.log(`No packages found with the next regular expression: ${regex}`); - process.exit(1); + throw new Error( + `No packages found with the next regular expressions: ${dependencyNameRegex}` + ); } +program + .name("npm-ri") + .description( + "npm install using regular expressions to update installed dependencies" + ) + .version("0.0.4"); + program .argument( - "", - "regex that will be use to find packages", - (value) => new RegExp(value) + "", + "regex that will be use to find dependencies", + (value) => { + return new RegExp(value); + } ) .option( - "-v, --version ", - "version that will be use to update packages", + "-dv, --dependency-version ", + "version that will be use to update dependencies", "latest" ) + .option("--dry-run", "do not actually perform updates", false) .action(run); program.parse();