-
Notifications
You must be signed in to change notification settings - Fork 0
/
bunsh
executable file
·60 lines (53 loc) · 1.19 KB
/
bunsh
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
#!/usr/bin/env bun
import { $ } from 'bun'
const bunBuiltinsCommands = [
'cd',
'ls',
'rm',
'echo',
'pwd',
'bun',
'cat',
'touch',
'mkdir',
'which',
'mv',
'exit',
'true',
'false',
'yes',
'seq',
'dirname',
'basename',
]
const customFunctions : {[key: string]: (...args: any[]) => any} = {
myFunction: (...args: any[]) => {
console.log(`Args: ${args.join(' ')}`)
},
export: (...args: any[]) => {
const [envName, value] = args[0].split('=');
$.env({[envName]: value});
},
}
$.env({A: 'test'})
const newLineConsole = () => console.write('$ ')
newLineConsole()
for await (const line of console) {
const commandAndArgs = line.split(' ')
const cmd = commandAndArgs[0]
const args = commandAndArgs.slice(1)
try {
if (typeof customFunctions[cmd] === 'function') {
customFunctions[cmd].call({}, ...args)
} else {
if (cmd === 'cd') {
await $.cwd(args.join(' '))
} else {
await $`${cmd} ${{raw: args.join(' ')}}`
}
}
} catch (err) {
console.error(err)
}
newLineConsole()
}