-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cea5842
commit 475ea1e
Showing
3 changed files
with
53 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |