@@ -5,33 +5,69 @@ import { createRequire } from "node:module";
5
5
import { spawn } from "node:child_process" ;
6
6
import { program } from "commander" ;
7
7
8
+ function printDependenciesToUpdate (
9
+ title : string ,
10
+ dependenciesToUpdate : [ string , { current : string ; next : string } ] [ ]
11
+ ) {
12
+ console . info (
13
+ `${ title } :\n${ dependenciesToUpdate
14
+ . map (
15
+ ( [ name , { current : currentVersion , next : nextVersion } ] ) =>
16
+ `${ name } @${ currentVersion } -> ${ name } @${ nextVersion } `
17
+ )
18
+ . join ( "\n" ) } `
19
+ ) ;
20
+ }
21
+
8
22
function run (
9
23
dependencyNameRegex : RegExp ,
10
- { dependencyVersion, dryRun } : { dependencyVersion : string ; dryRun : boolean }
24
+ {
25
+ dependencyVersion : nextDependencyVersion ,
26
+ dryRun,
27
+ } : { dependencyVersion : string ; dryRun : boolean }
11
28
) {
12
29
const require = createRequire ( `${ cwd ( ) } /` ) ;
13
30
const pkg = require ( "./package.json" ) ;
14
31
15
- const dependencies = Object . keys ( pkg . dependencies || { } )
16
- . concat ( Object . keys ( pkg . devDependencies || { } ) )
17
- . filter ( ( dependency ) => dependencyNameRegex . test ( dependency ) )
18
- . map ( ( dependency ) => `${ dependency } @${ dependencyVersion } ` ) ;
32
+ const dependenciesToUpdate = Object . entries (
33
+ Object . assign ( { } , pkg . dependencies || { } , pkg . devDependencies || { } )
34
+ )
35
+ . filter ( ( [ dependencyName ] ) => dependencyNameRegex . test ( dependencyName ) )
36
+ . map (
37
+ ( [ dependencyName , currentDependencyVersion ] ) =>
38
+ [
39
+ dependencyName ,
40
+ {
41
+ current : currentDependencyVersion as string ,
42
+ next : nextDependencyVersion as string ,
43
+ } ,
44
+ ] as [ string , { current : string ; next : string } ]
45
+ ) ;
19
46
20
- if ( dependencies . length > 0 ) {
47
+ if ( dependenciesToUpdate . length > 0 ) {
21
48
if ( dryRun ) {
22
- console . info (
23
- `The next packages might be updated:\n${ dependencies . join ( "\n" ) } `
49
+ printDependenciesToUpdate (
50
+ "The next packages might be updated" ,
51
+ dependenciesToUpdate
24
52
) ;
25
53
26
54
return ;
27
55
}
28
56
29
- const npmInstall = spawn ( "npm" , [ "install" ] . concat ( dependencies ) ) ;
57
+ const npmInstall = spawn (
58
+ "npm" ,
59
+ [ "install" ] . concat (
60
+ dependenciesToUpdate . map (
61
+ ( [ name , { next : nextVersion } ] ) => `${ name } @${ nextVersion } `
62
+ )
63
+ )
64
+ ) ;
30
65
31
66
npmInstall . on ( "close" , ( code : number ) => {
32
67
if ( code === 0 ) {
33
- console . info (
34
- `The next packages were updated:\n${ dependencies . join ( "\n" ) } `
68
+ printDependenciesToUpdate (
69
+ "The next packages were updated" ,
70
+ dependenciesToUpdate
35
71
) ;
36
72
37
73
return ;
@@ -53,7 +89,7 @@ program
53
89
. description (
54
90
"npm install using regular expressions to update installed dependencies"
55
91
)
56
- . version ( "0.0.4 " ) ;
92
+ . version ( "0.0.5 " ) ;
57
93
58
94
program
59
95
. argument (
0 commit comments