Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5d3f91b380 |
50
bin/shrimp
50
bin/shrimp
|
|
@ -33,10 +33,10 @@ function showVersion() {
|
|||
console.log('🦐 v0.0.1')
|
||||
}
|
||||
|
||||
async function evalCode(code: string, imports: string[]) {
|
||||
async function evalCode(code: string, imports: string[], globals?: Record<string, any>) {
|
||||
const importStatement = imports.length > 0 ? `import ${imports.join(' ')}` : ''
|
||||
if (importStatement) code = `${importStatement}; ${code}`
|
||||
return await runCode(code)
|
||||
return await runCode(code, globals)
|
||||
}
|
||||
|
||||
async function main() {
|
||||
|
|
@ -47,11 +47,29 @@ async function main() {
|
|||
return
|
||||
}
|
||||
|
||||
// Parse -I flags for imports (supports both "-I math" and "-Imath")
|
||||
// cli command and arg
|
||||
let command = ''
|
||||
let cmdArg = ''
|
||||
|
||||
// parse -I flags for imports. supports both "-I math" and "-Imath"
|
||||
const imports: string[] = []
|
||||
|
||||
while (args.length > 0) {
|
||||
const arg = args[0]
|
||||
// shrimp -E args -- arg1 arg2
|
||||
let grabArgs = false
|
||||
let scriptArgs = []
|
||||
|
||||
for (const arg of args) {
|
||||
// everything after -- goes into "args" for this script
|
||||
if (arg === '--') {
|
||||
grabArgs = true
|
||||
continue
|
||||
}
|
||||
|
||||
// grab all args after --
|
||||
if (grabArgs) {
|
||||
scriptArgs.push(arg)
|
||||
continue
|
||||
}
|
||||
|
||||
if (arg === '-I') {
|
||||
// "-I math" format
|
||||
|
|
@ -70,8 +88,12 @@ async function main() {
|
|||
}
|
||||
imports.push(moduleName)
|
||||
args = args.slice(1)
|
||||
}
|
||||
|
||||
if (!command) {
|
||||
command = arg
|
||||
} else {
|
||||
break
|
||||
cmdArg = arg
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -80,8 +102,6 @@ async function main() {
|
|||
return
|
||||
}
|
||||
|
||||
const command = args[0]
|
||||
|
||||
if (['help', '-help', '--help', '-h'].includes(command)) {
|
||||
showHelp()
|
||||
return
|
||||
|
|
@ -101,29 +121,29 @@ async function main() {
|
|||
}
|
||||
|
||||
if (['eval', '-eval', '--eval', '-e'].includes(command)) {
|
||||
const code = args[1]
|
||||
const code = cmdArg
|
||||
if (!code) {
|
||||
console.log(`${colors.bright}usage: shrimp eval <code>${colors.reset}`)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
await evalCode(code, imports)
|
||||
await evalCode(code, imports, { args: scriptArgs })
|
||||
return
|
||||
}
|
||||
|
||||
if (['print', '-print', '--print', '-E'].includes(command)) {
|
||||
const code = args[1]
|
||||
const code = cmdArg
|
||||
if (!code) {
|
||||
console.log(`${colors.bright}usage: shrimp print <code>${colors.reset}`)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
console.log(await evalCode(code, imports))
|
||||
console.log(await evalCode(code, imports, { args: scriptArgs }))
|
||||
return
|
||||
}
|
||||
|
||||
if (['bytecode', '-bytecode', '--bytecode', '-b'].includes(command)) {
|
||||
const file = args[1]
|
||||
const file = cmdArg
|
||||
if (!file) {
|
||||
console.log(`${colors.bright}usage: shrimp bytecode <file>${colors.reset}`)
|
||||
process.exit(1)
|
||||
|
|
@ -133,7 +153,7 @@ async function main() {
|
|||
}
|
||||
|
||||
if (['parse', '-parse', '--parse', '-p'].includes(command)) {
|
||||
const file = args[1]
|
||||
const file = cmdArg
|
||||
if (!file) {
|
||||
console.log(`${colors.bright}usage: shrimp parse <file>${colors.reset}`)
|
||||
process.exit(1)
|
||||
|
|
@ -144,7 +164,7 @@ async function main() {
|
|||
}
|
||||
|
||||
if (['run', '-run', '--run', '-r'].includes(command)) {
|
||||
const file = args[1]
|
||||
const file = cmdArg
|
||||
if (!file) {
|
||||
console.log(`${colors.bright}usage: shrimp run <file>${colors.reset}`)
|
||||
process.exit(1)
|
||||
|
|
|
|||
|
|
@ -50,7 +50,6 @@ export const globals = {
|
|||
const only = new Set(onlyArray)
|
||||
const wantsOnly = only.size > 0
|
||||
|
||||
|
||||
for (const ident of idents) {
|
||||
const module = this.get(ident)
|
||||
|
||||
|
|
@ -66,7 +65,8 @@ export const globals = {
|
|||
},
|
||||
|
||||
// env
|
||||
args: Bun.argv.slice(1),
|
||||
argv: Bun.argv.slice(1), // everything
|
||||
args: Bun.argv.slice(3), // only args specifically passed to this script/program
|
||||
exit: (num: number) => process.exit(num ?? 0),
|
||||
|
||||
// type predicates
|
||||
|
|
|
|||
|
|
@ -87,8 +87,12 @@ describe('environment', () => {
|
|||
await expect(`type args`).toEvaluateTo('array', globals)
|
||||
})
|
||||
|
||||
test('', async () => {
|
||||
await expect(`list.first args | str.ends-with? 'shrimp.test.ts'`).toEvaluateTo(true)
|
||||
test('args is just stuff passed to the script', async () => {
|
||||
await expect(`list.empty? args`).toEvaluateTo(true)
|
||||
})
|
||||
|
||||
test('argv is all the classic argv info (minus bun)', async () => {
|
||||
await expect(`list.first argv | str.ends-with? 'shrimp.test.ts'`).toEvaluateTo(true)
|
||||
})
|
||||
})
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user