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
+
40
42
function installDependencies ( dir ) {
41
- console . log ( `Installing dependencies in ${ dir } ...` )
42
- execSync ( 'pnpm pnpm.install.workaround' , { stdio : 'inherit' , cwd : dir } )
43
+ console . log ( `Installing dependencies in ${ dir } ...` ) ;
44
+ execSync ( 'pnpm pnpm.install.workaround' , { stdio : 'inherit' , cwd : dir } ) ;
43
45
}
44
46
47
+
45
48
function isPnpmInstalled ( ) {
46
49
try {
47
- execSync ( 'pnpm --version' , { stdio : 'ignore' } )
48
- return true
50
+ execSync ( 'pnpm --version' , { stdio : 'ignore' } ) ;
51
+ return true ;
49
52
} catch {
50
- return false
53
+ return false ;
51
54
}
52
55
}
53
56
54
57
async function main ( ) {
55
- console . log ( header )
58
+ console . log ( header ) ;
56
59
57
60
const target = await select ( {
58
61
message : 'Which version target would you like to update to?' ,
@@ -61,50 +64,50 @@ async function main() {
61
64
{ name : 'Latest' , value : 'latest' }
62
65
] ,
63
66
default : 'minor'
64
- } )
67
+ } ) ;
65
68
66
69
const updateFiles = await confirm ( {
67
70
message : 'Do you want to update the package.json files?' ,
68
71
default : true
69
- } )
72
+ } ) ;
70
73
71
- console . log ( `${ updateFiles ? 'Updating' : 'Checking' } root 'package.json' for ${ target } updates...` )
72
- await updatePackages ( currentPath , target , updateFiles )
74
+ console . log ( `${ updateFiles ? 'Updating' : 'Checking' } root 'package.json' for ${ target } updates...` ) ;
75
+ await updatePackages ( currentPath , target , updateFiles ) ;
73
76
74
77
readdirSync ( packagesDir ) . forEach ( async ( packageDir ) => {
75
- const fullPath = join ( packagesDir , packageDir )
78
+ const fullPath = join ( packagesDir , packageDir ) ;
76
79
if ( lstatSync ( fullPath ) . isDirectory ( ) ) {
77
- await updatePackages ( fullPath , target , updateFiles )
80
+ await updatePackages ( fullPath , target , updateFiles ) ;
78
81
}
79
- } )
82
+ } ) ;
80
83
81
84
if ( updateFiles ) {
82
85
const removeNodeModules = await confirm ( {
83
86
message : 'Do you want to remove all "node_modules" and reinstall dependencies?' ,
84
87
default : true
85
- } )
88
+ } ) ;
86
89
87
90
if ( removeNodeModules ) {
88
- removeDependencies ( currentPath )
91
+ removeDependencies ( currentPath ) ;
89
92
const usePnpm = await confirm ( {
90
93
message : 'Would you like reinstall the dependencies?' ,
91
94
default : true
92
- } )
95
+ } ) ;
93
96
94
97
if ( usePnpm ) {
95
98
if ( isPnpmInstalled ( ) ) {
96
- installDependencies ( currentPath )
99
+ installDependencies ( currentPath ) ;
97
100
} else {
98
- console . error ( 'pnpm is not installed. Please install pnpm and try again.' )
101
+ console . error ( 'pnpm is not installed. Please install pnpm and try again.' ) ;
99
102
}
100
103
}
101
104
}
102
105
}
103
106
104
- console . log ( `All packages ${ updateFiles ? 'updated' : 'checked' } !` )
107
+ console . log ( `All packages ${ updateFiles ? 'updated' : 'checked' } !` ) ;
105
108
}
106
109
107
110
main ( ) . catch ( ( error ) => {
108
- console . error ( 'An unexpected error occurred:' , error )
109
- process . exit ( 1 )
110
- } )
111
+ console . error ( 'An unexpected error occurred:' , error ) ;
112
+ process . exit ( 1 ) ;
113
+ } ) ;
0 commit comments