Skip to content

Commit 475ea1e

Browse files
committed
Minor updates
1 parent cea5842 commit 475ea1e

File tree

3 files changed

+53
-25
lines changed

3 files changed

+53
-25
lines changed

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# npm-ri
22

3-
npm install using regular expression
3+
npm install using regular expression to update installed packages
44

55
# Usage
66

7-
## Basic (by default version is "latest")
7+
## Basic (by default package version is "latest")
88

99
```sh
10-
npx npm-ri "<regex>"
10+
npx npm-ri "<dependecy-name-regex>"
1111
```
1212

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

2121
```sh
22-
npx npm-ri "<regex>" -v <version>
22+
npx npm-ri "<regex>" --dependency-version <dependency-version>
2323
```
2424

25-
### Example
25+
or
2626

2727
```sh
28-
npx npm-ri "@tanstack/.*" -v beta
28+
npx npm-ri "<regex>" -dv <dependency-version>
29+
```
30+
31+
### Example
32+
33+
```sh
34+
npx npm-ri "@tanstack/.*" -dv beta
2935
```

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"type": "module",
33
"name": "npm-ri",
44
"private": false,
5-
"description": "npm install packages using regular expression",
6-
"version": "0.0.3",
5+
"description": "npm install packages using regular expressions to update installed dependencies",
6+
"version": "0.0.4",
77
"author": "Danilo Britto",
88
"repository": {
99
"type": "git",

src/index.ts

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,74 @@
11
#! /usr/bin/env node
22

3+
import { cwd } from "node:process";
34
import { createRequire } from "node:module";
45
import { spawn } from "node:child_process";
56
import { program } from "commander";
67

7-
function run(regex: RegExp, { version }: { version: string }) {
8-
const require = createRequire(`${process.cwd()}/`);
8+
function run(
9+
dependencyNameRegex: RegExp,
10+
{ dependencyVersion, dryRun }: { dependencyVersion: string; dryRun: boolean }
11+
) {
12+
const require = createRequire(`${cwd()}/`);
913
const pkg = require("./package.json");
1014

1115
const dependencies = Object.keys(pkg.dependencies || {})
1216
.concat(Object.keys(pkg.devDependencies || {}))
13-
.filter((dependency) => regex.test(dependency))
14-
.map((dependency) => `${dependency}@${version}`);
17+
.filter((dependency) => dependencyNameRegex.test(dependency))
18+
.map((dependency) => `${dependency}@${dependencyVersion}`);
1519

1620
if (dependencies.length > 0) {
21+
if (dryRun) {
22+
console.info(
23+
`The next packages might be updated:\n${dependencies.join("\n")}`
24+
);
25+
26+
return;
27+
}
28+
1729
const npmInstall = spawn("npm", ["install"].concat(dependencies));
1830

1931
npmInstall.on("close", (code: number) => {
2032
if (code === 0) {
2133
console.info(
22-
`The next packages were updated to their latest version:\n${dependencies.join(
23-
"\n"
24-
)}`
34+
`The next packages were updated:\n${dependencies.join("\n")}`
2535
);
26-
} else {
27-
console.error("Something went wrong");
36+
37+
return;
2838
}
2939

30-
process.exit(code);
40+
throw new Error("Something went wrong");
3141
});
42+
3243
return;
3344
}
3445

35-
console.log(`No packages found with the next regular expression: ${regex}`);
36-
process.exit(1);
46+
throw new Error(
47+
`No packages found with the next regular expressions: ${dependencyNameRegex}`
48+
);
3749
}
3850

51+
program
52+
.name("npm-ri")
53+
.description(
54+
"npm install using regular expressions to update installed dependencies"
55+
)
56+
.version("0.0.4");
57+
3958
program
4059
.argument(
41-
"<regex>",
42-
"regex that will be use to find packages",
43-
(value) => new RegExp(value)
60+
"<dependency-name-regex>",
61+
"regex that will be use to find dependencies",
62+
(value) => {
63+
return new RegExp(value);
64+
}
4465
)
4566
.option(
46-
"-v, --version <version>",
47-
"version that will be use to update packages",
67+
"-dv, --dependency-version <dependency-version>",
68+
"version that will be use to update dependencies",
4869
"latest"
4970
)
71+
.option("--dry-run", "do not actually perform updates", false)
5072
.action(run);
5173

5274
program.parse();

0 commit comments

Comments
 (0)