Skip to content

Commit

Permalink
Minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dbritto-dev committed Jul 6, 2024
1 parent cea5842 commit 475ea1e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 25 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 "<regex>"
npx npm-ri "<dependecy-name-regex>"
```

### Example
Expand All @@ -19,11 +19,17 @@ npx npm-ri "@radix-ui/.*"
## With custom version

```sh
npx npm-ri "<regex>" -v <version>
npx npm-ri "<regex>" --dependency-version <dependency-version>
```

### Example
or

```sh
npx npm-ri "@tanstack/.*" -v beta
npx npm-ri "<regex>" -dv <dependency-version>
```

### Example

```sh
npx npm-ri "@tanstack/.*" -dv beta
```
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
56 changes: 39 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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>",
"regex that will be use to find packages",
(value) => new RegExp(value)
"<dependency-name-regex>",
"regex that will be use to find dependencies",
(value) => {
return new RegExp(value);
}
)
.option(
"-v, --version <version>",
"version that will be use to update packages",
"-dv, --dependency-version <dependency-version>",
"version that will be use to update dependencies",
"latest"
)
.option("--dry-run", "do not actually perform updates", false)
.action(run);

program.parse();

0 comments on commit 475ea1e

Please sign in to comment.