-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
101 lines (84 loc) · 2.02 KB
/
Copy pathindex.js
File metadata and controls
101 lines (84 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
const { spawnSync } = require("child_process")
const p = require("path")
const f = require("fs")
const args = process.argv.slice(2)
const file = args[0]
if (!file) {
console.error("file required!")
process.exit(1)
}
if (!f.existsSync(file)) {
console.error("file not found!")
process.exit(1)
}
const t = args.includes("-time")
const b = args.includes("-build")
const d = args.includes("-build-and-delete")
const i = args.indexOf("-input")
if (i > -1 && !args[i + 1]) {
console.error("input value missing!")
process.exit(1)
}
const inputData =
i > -1 ? f.readFileSync(args[i + 1]) : null
const start = t ? process.hrtime.bigint() : 0
const ext = p.extname(file)
function run(cmd, cmdArgs) {
const res = spawnSync(cmd, cmdArgs, {
stdio: inputData
? ["pipe", "inherit", "inherit"]
: "inherit",
input: inputData
})
if (res.error) throw res.error
if (res.status !== 0) process.exit(res.status)
}
try {
switch (ext) {
case ".py":
run("python", [file])
break
case ".js":
run("node", [file])
break
case ".ts":
run("ts-node", [file])
break
case ".go":
if (b || d) {
const out = "__run_tmp__"
run("go", ["build", "-o", out, file])
run(`./${out}`, [])
if (d && f.existsSync(out)) {
f.unlinkSync(out)
}
} else {
run("go", ["run", file])
}
break
case ".sh":
run("bash", [file])
break
case ".cs":
run("dotnet", ["run", "--project", file])
break
default:
console.error("not executable file type!")
process.exit(1)
}
} catch (err) {
console.error("\n실행 중 오류 발생!")
process.exit(1)
}
if (t) {
const elapsed = Number(process.hrtime.bigint() - start) / 1e9
const msg = `걸린 시간: ${elapsed.toFixed(3)}초`
const W = 32
const L = Math.floor((W - msg.length) / 2)
const R = W - msg.length - L
console.log(`
${"=".repeat(40)}
|${" ".repeat(L) + msg + " ".repeat(R + 1)}|
${"=".repeat(40)}`)
}