-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbin.js
executable file
·78 lines (67 loc) · 1.69 KB
/
bin.js
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
#!/usr/bin/env node
import { readFileSync } from 'fs'
import { fileURLToPath } from 'url'
import path from 'path'
import { trim, debugDecompileToBasm } from './dist/index.js'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const args = process.argv.slice(2)
const options = { asm: false, decompile: false, version: false }
// Parse options
const fileArgs = args.filter(arg => {
if (arg === '--version') {
options.version = true
return false
}
if (arg === '--asm') {
options.asm = true
return false
}
if (arg === '--decompile') {
options.decompile = true
return false
}
return true
})
// Check if we're receiving data from STDIN
const isStdin = !process.stdin.isTTY
if (options.version) {
const pkg = readFileSync(path.join(__dirname, 'package.json'), 'utf8')
console.log(JSON.parse(pkg).version)
process.exit(0)
}
else if (isStdin) {
let data = ''
process.stdin.setEncoding('utf8')
process.stdin.on('data', chunk => {
data += chunk
})
process.stdin.on('end', () => {
go(data)
})
} else {
// Handle file input
if (fileArgs.length === 0) {
console.error('Usage: trim [--asm] [--decompile] <filepath> or pipe content via STDIN')
process.exit(1)
}
try {
go(readFileSync(fileArgs[0], 'utf8'), options)
} catch (error) {
console.error(`Error reading file: ${error.message}`)
process.exit(1)
}
}
function go(source) {
if (options.decompile) {
console.log(debugDecompileToBasm(source).basm)
} else {
const bytecode = trim.compile(source)
if (options.asm) {
console.log(debugDecompileToBasm(bytecode).basm)
}
else {
console.log(bytecode)
}
}
}