1
1
#!/usr/bin/env node
2
- import { confirm , select } from '@inquirer/prompts' ;
3
- import { execSync } from 'node:child_process' ;
4
- import { readdirSync , lstatSync } from 'node:fs' ;
5
- import { join } from 'node:path' ;
6
- import { rimraf } from 'rimraf' ;
7
-
8
- const currentPath = process . cwd ( ) ;
9
- const packagesDir = join ( currentPath , 'packages' ) ;
2
+ import { confirm , select } from '@inquirer/prompts'
3
+ import { execSync } from 'node:child_process'
4
+ import { readdirSync , lstatSync } from 'node:fs'
5
+ import { join } from 'node:path'
6
+ import { rimraf } from 'rimraf'
7
+
8
+ const currentPath = process . cwd ( )
9
+ const packagesDir = join ( currentPath , 'packages' )
10
10
const header = `
11
11
==========================
12
12
🤖 Package update Wizard 🧙
13
13
==========================
14
14
`
15
15
16
16
async function updatePackages ( dir , target , updateFiles ) {
17
- console . log ( `${ updateFiles ? 'Updating' : 'Checking' } packages for ${ target } updates in ${ dir } ...` ) ;
18
- const command = `npx npm-check-updates ${ updateFiles ? '-u' : '' } --target ${ target } ` ;
19
- execSync ( command , { stdio : 'inherit' , cwd : dir } ) ;
17
+ console . log ( `${ updateFiles ? 'Updating' : 'Checking' } packages for ${ target } updates in ${ dir } ...` )
18
+ const command = `npx npm-check-updates ${ updateFiles ? '-u' : '' } --target ${ target } `
19
+ execSync ( command , { stdio : 'inherit' , cwd : dir } )
20
20
}
21
21
22
22
function removeDependencies ( dir ) {
23
- console . log ( `Removing root dependencies in ${ dir } ...` ) ;
24
- const rootNodeModulesPath = join ( dir , 'node_modules' ) ;
25
- rimraf . sync ( rootNodeModulesPath ) ;
23
+ console . log ( `Removing root dependencies in ${ dir } ...` )
24
+ const rootNodeModulesPath = join ( dir , 'node_modules' )
25
+ rimraf . sync ( rootNodeModulesPath )
26
26
27
- const packagesDir = join ( dir , 'packages' ) ;
27
+ const packagesDir = join ( dir , 'packages' )
28
28
29
29
readdirSync ( packagesDir ) . forEach ( packageDir => {
30
- const fullPath = join ( packagesDir , packageDir ) ;
30
+ const fullPath = join ( packagesDir , packageDir )
31
31
32
32
if ( lstatSync ( fullPath ) . isDirectory ( ) ) {
33
- console . log ( `Removing dependencies in ${ packageDir } ...` ) ;
34
- const nodeModulesPath = join ( fullPath , 'node_modules' ) ;
35
- rimraf . sync ( nodeModulesPath ) ;
33
+ console . log ( `Removing dependencies in ${ packageDir } ...` )
34
+ const nodeModulesPath = join ( fullPath , 'node_modules' )
35
+ rimraf . sync ( nodeModulesPath )
36
36
}
37
- } ) ;
37
+ } )
38
38
}
39
39
40
-
41
-
42
40
function installDependencies ( dir ) {
43
- console . log ( `Installing dependencies in ${ dir } ...` ) ;
44
- execSync ( 'pnpm pnpm.install.workaround' , { stdio : 'inherit' , cwd : dir } ) ;
41
+ console . log ( `Installing dependencies in ${ dir } ...` )
42
+ execSync ( 'pnpm pnpm.install.workaround' , { stdio : 'inherit' , cwd : dir } )
45
43
}
46
44
47
-
48
45
function isPnpmInstalled ( ) {
49
46
try {
50
- execSync ( 'pnpm --version' , { stdio : 'ignore' } ) ;
51
- return true ;
47
+ execSync ( 'pnpm --version' , { stdio : 'ignore' } )
48
+ return true
52
49
} catch {
53
- return false ;
50
+ return false
54
51
}
55
52
}
56
53
57
54
async function main ( ) {
58
- console . log ( header ) ;
55
+ console . log ( header )
59
56
60
57
const target = await select ( {
61
58
message : 'Which version target would you like to update to?' ,
@@ -64,50 +61,50 @@ async function main() {
64
61
{ name : 'Latest' , value : 'latest' }
65
62
] ,
66
63
default : 'minor'
67
- } ) ;
64
+ } )
68
65
69
66
const updateFiles = await confirm ( {
70
67
message : 'Do you want to update the package.json files?' ,
71
68
default : true
72
- } ) ;
69
+ } )
73
70
74
- console . log ( `${ updateFiles ? 'Updating' : 'Checking' } root 'package.json' for ${ target } updates...` ) ;
75
- await updatePackages ( currentPath , target , updateFiles ) ;
71
+ console . log ( `${ updateFiles ? 'Updating' : 'Checking' } root 'package.json' for ${ target } updates...` )
72
+ await updatePackages ( currentPath , target , updateFiles )
76
73
77
74
readdirSync ( packagesDir ) . forEach ( async ( packageDir ) => {
78
- const fullPath = join ( packagesDir , packageDir ) ;
75
+ const fullPath = join ( packagesDir , packageDir )
79
76
if ( lstatSync ( fullPath ) . isDirectory ( ) ) {
80
- await updatePackages ( fullPath , target , updateFiles ) ;
77
+ await updatePackages ( fullPath , target , updateFiles )
81
78
}
82
- } ) ;
79
+ } )
83
80
84
81
if ( updateFiles ) {
85
82
const removeNodeModules = await confirm ( {
86
83
message : 'Do you want to remove all "node_modules" and reinstall dependencies?' ,
87
84
default : true
88
- } ) ;
85
+ } )
89
86
90
87
if ( removeNodeModules ) {
91
- removeDependencies ( currentPath ) ;
88
+ removeDependencies ( currentPath )
92
89
const usePnpm = await confirm ( {
93
90
message : 'Would you like reinstall the dependencies?' ,
94
91
default : true
95
- } ) ;
92
+ } )
96
93
97
94
if ( usePnpm ) {
98
95
if ( isPnpmInstalled ( ) ) {
99
- installDependencies ( currentPath ) ;
96
+ installDependencies ( currentPath )
100
97
} else {
101
- console . error ( 'pnpm is not installed. Please install pnpm and try again.' ) ;
98
+ console . error ( 'pnpm is not installed. Please install pnpm and try again.' )
102
99
}
103
100
}
104
101
}
105
102
}
106
103
107
- console . log ( `All packages ${ updateFiles ? 'updated' : 'checked' } !` ) ;
104
+ console . log ( `All packages ${ updateFiles ? 'updated' : 'checked' } !` )
108
105
}
109
106
110
107
main ( ) . catch ( ( error ) => {
111
- console . error ( 'An unexpected error occurred:' , error ) ;
112
- process . exit ( 1 ) ;
113
- } ) ;
108
+ console . error ( 'An unexpected error occurred:' , error )
109
+ process . exit ( 1 )
110
+ } )
0 commit comments