3
3
import fs from 'node:fs'
4
4
import path , { dirname , resolve } from 'node:path'
5
5
import process from 'node:process'
6
- import { exec } from 'node:child_process' // 确保导入 exec
6
+ import { spawn } from 'node:child_process' // 确保导入 exec
7
7
import { fileURLToPath } from 'node:url'
8
8
import { Command } from 'commander'
9
9
@@ -54,17 +54,7 @@ if (process.argv.length === 2) {
54
54
const __dirname = dirname ( __filename )
55
55
const fullPath = resolve ( __dirname , '../dist/index.mjs' )
56
56
57
- exec ( `node ${ fullPath } ` , ( error , stdout , stderr ) => {
58
- if ( error ) {
59
- console . error ( `Error executing script: ${ error . message } ` )
60
- return
61
- }
62
- if ( stderr ) {
63
- console . error ( `Script error: ${ stderr } ` )
64
- return
65
- }
66
- console . log ( stdout )
67
- } )
57
+ runNodeScript ( fullPath )
68
58
}
69
59
else {
70
60
program . parse ( process . argv )
@@ -91,3 +81,18 @@ function addToGitignore() {
91
81
console . log ( '.gitignore does not exist.' )
92
82
}
93
83
}
84
+
85
+ function runNodeScript ( fullPath ) {
86
+ return new Promise ( ( resolve , reject ) => {
87
+ const child = spawn ( 'node' , [ fullPath ] , { stdio : 'inherit' } )
88
+
89
+ child . on ( 'close' , ( code ) => {
90
+ if ( code === 0 ) {
91
+ resolve ( )
92
+ }
93
+ else {
94
+ reject ( new Error ( `Node.js script exited with code ${ code } ` ) )
95
+ }
96
+ } )
97
+ } )
98
+ }
0 commit comments