Skip to content

Commit f894e30

Browse files
committed
feat: node vm执行index.js脚本
1 parent 2b5d8fd commit f894e30

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

common/scripts/check.js

+23-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
*/
44
import {temExe} from "#common/utils/temExe.js";
55
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'
69
import path from "path";
710
import {parseFilePath} from "#common/utils/parseFilePath.js";
811
const args = process.argv.slice(2);
@@ -34,7 +37,23 @@ switch (args[0]) {
3437
}
3538
break;
3639
}
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()

common/template/template.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {showLogs} from "#common/utils/showLogs.js";
21

32
/**
43
* @题目

0 commit comments

Comments
 (0)