File tree 2 files changed +23
-5
lines changed
2 files changed +23
-5
lines changed Original file line number Diff line number Diff line change 3
3
*/
4
4
import { temExe } from "#common/utils/temExe.js" ;
5
5
import { readStore } from "#common/utils/store.js" ;
6
+ import { showLogs } from "#common/utils/showLogs.js" ;
7
+ import fs from 'fs' ;
8
+ import vm from 'vm'
6
9
import path from "path" ;
7
10
import { parseFilePath } from "#common/utils/parseFilePath.js" ;
8
11
const args = process . argv . slice ( 2 ) ;
@@ -34,7 +37,23 @@ switch (args[0]) {
34
37
}
35
38
break ;
36
39
}
37
- const filePath = parseFilePath ( `./src/${ name } /index.js` ) ;
38
- temExe ( 'node {0}' , filePath )
39
- . then ( res => console . log ( `执行结果:\n${ res } ` ) )
40
- . catch ( e => console . log ( "执行报错: " , e ) ) ;
40
+
41
+ function executeScript ( filePath , content ) {
42
+ const fileContent = fs . readFileSync ( filePath , 'utf-8' ) ;
43
+
44
+ const script = new vm . Script ( fileContent ) ;
45
+ return script . runInContext ( content ) ;
46
+
47
+ }
48
+ async function main ( ) {
49
+ const src = parseFilePath ( `${ process . cwd ( ) } /src/${ name } /index.js` ) ;
50
+
51
+ try {
52
+ await executeScript ( src , vm . createContext ( {
53
+ showLogs
54
+ } ) )
55
+ } catch ( error ) {
56
+ console . log ( '执行失败' , error )
57
+ }
58
+ }
59
+ main ( )
Original file line number Diff line number Diff line change 1
- import { showLogs } from "#common/utils/showLogs.js" ;
2
1
3
2
/**
4
3
* @题目
You can’t perform that action at this time.
0 commit comments