-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtee.ts
38 lines (35 loc) · 1.17 KB
/
tee.ts
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
/// <reference path="../userspace/index.d.ts" />
/**
* Redirects stdin to stdout and to all files passed as parameters
* @usage <file1> <file2> ... <fileN>
*/
if (typeof webcontainer !== 'function')
throw new Error('Missing webcontainer runtime');
webcontainer(async process => {
const encoder = new TextEncoder();
let rids = new Set((await Promise.all([...new Set(process.argv.slice(1))].map(path =>
process
.openWrite(path, 'override', true)
.catch(err =>
process.write(
2,
encoder.encode(`${process.argv[0]}: ${path}: ${((err instanceof Error && err.message) || err)}\n`)
).then(() => null)
)
))).filter((rid: number | null): rid is number => rid !== null));
rids.add(1);
let buffer: Uint8Array | null;
while ((buffer = await process.read(0)) !== null)
await Promise.all(
[...rids].map(r =>
process.write(r, buffer!)
.catch(err => {
rids.delete(r);
process.write(
2,
encoder.encode(`${process.argv[0]}: ${process.getResourceURI(r)!}: ${((err instanceof Error && err.message) || err)}\n`)
)
})
)
);
});