-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbin.js
executable file
·100 lines (81 loc) · 2.28 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env -S node --no-warnings --experimental-vm-modules
import { parseArgs } from 'node:util'
import Agregore from './index.js'
const HELP_TEXT = `
agregore run <script> [...opts]
agregore eval "Some code" [...opts]
--no-https: Disable loading scripts from HTTPS
--no-http: Disable loading scripts from HTTP
--no-ipfs: Disable loading scripts from IPFS
--no-hyper: Disable loading scripts from hypercore-protocol
--root: The root folder to persist data to (defaults to current folder)
--autoclose: Used with 'run' to automatically close after the module gets evaluated
--help: Show this text
`
const args = parseArgs({
options: {
help: {
type: 'boolean',
short: 'h'
},
autoclose: {
type: 'boolean',
short: 'a'
},
'no-http': { type: 'boolean' },
'no-https': { type: 'boolean' },
'no-ipfs': { type: 'boolean' },
'no-hyper': { type: 'boolean' },
'no-gemini': { type: 'boolean' }
},
strict: true,
allowPositionals: true
})
// console.log(args)
const firstCommand = args.positionals[0]
if (!firstCommand || args.values.help) {
console.log(HELP_TEXT)
} else if (firstCommand === 'run') {
doRun()
} else if (firstCommand === 'eval') {
doEval()
} else {
console.log(`Unknown command: ${firstCommand}.\n\n${HELP_TEXT}`)
}
async function init () {
const opts = {}
if (args.values['no-http']) opts.http = false
if (args.values['no-https']) opts.https = false
if (args.values['no-ipfs']) opts.ipfs = false
if (args.values['no-hyper']) opts.hyper = false
if (args.values['no-gemini']) opts.gemini = false
if (args.values.root) opts.root = args.values.root
const agregore = await new Agregore(opts)
await agregore.init()
return agregore
}
async function doRun () {
const script = args.positionals[1]
if (!script) {
console.error('Must specify script to execute')
return
}
const agregore = await init()
await agregore.import(script)
if (args.values.autoclose) {
await agregore.close()
}
}
async function doEval () {
const toEval = args.positionals[1]
if (!toEval) {
console.error('Must specify code to evaluate')
return
}
const agregore = await init()
const result = await agregore.eval(toEval)
if (result !== undefined) {
console.log(result)
}
await agregore.close()
}