-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Shell.svelte
100 lines (89 loc) · 2.34 KB
/
Shell.svelte
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
<script>
import { Command } from '@tauri-apps/api/shell'
const windows = navigator.userAgent.includes('Windows')
let cmd = windows ? 'cmd' : 'sh'
let args = windows ? ['/C'] : ['-c']
export let onMessage
let script = 'echo "hello world"'
let cwd = null
let env = 'SOMETHING=value ANOTHER=2'
let encoding = ''
let stdin = ''
let child
function _getEnv() {
return env.split(' ').reduce((env, clause) => {
let [key, value] = clause.split('=')
return {
...env,
[key]: value
}
}, {})
}
function spawn() {
child = null
const command = new Command(cmd, [...args, script], {
cwd: cwd || null,
env: _getEnv(),
encoding,
})
command.on('close', (data) => {
onMessage(
`command finished with code ${data.code} and signal ${data.signal}`
)
child = null
})
command.on('error', (error) => onMessage(`command error: "${error}"`))
command.stdout.on('data', (line) => onMessage(`command stdout: "${line}"`))
command.stderr.on('data', (line) => onMessage(`command stderr: "${line}"`))
command
.spawn()
.then((c) => {
child = c
})
.catch(onMessage)
}
function kill() {
child
.kill()
.then(() => onMessage('killed child process'))
.catch(onMessage)
}
function writeToStdin() {
child.write(stdin).catch(onMessage)
}
</script>
<div class="flex flex-col childre:grow gap-1">
<div class="flex items-center gap-1">
Script:
<input class="grow input" bind:value={script} />
</div>
<div class="flex items-center gap-1">
Encoding:
<input class="grow input" bind:value={encoding} />
</div>
<div class="flex items-center gap-1">
Working directory:
<input
class="grow input"
bind:value={cwd}
placeholder="Working directory"
/>
</div>
<div class="flex items-center gap-1">
Arguments:
<input
class="grow input"
bind:value={env}
placeholder="Environment variables"
/>
</div>
<div class="flex children:grow gap-1">
<button class="btn" on:click={spawn}>Run</button>
<button class="btn" on:click={kill}>Kill</button>
</div>
{#if child}
<br />
<input class="input" placeholder="write to stdin" bind:value={stdin} />
<button class="btn" on:click={writeToStdin}>Write</button>
{/if}
</div>