-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcat.ts
36 lines (33 loc) · 1.02 KB
/
cat.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
/// <reference path="../userspace/index.d.ts" />
/**
* Prints one or more file sequentially into stdout
* @usage <file1> <file2> ... <fileN>
*/
if (typeof webcontainer !== 'function')
throw new Error('Missing webcontainer runtime');
webcontainer(async process => {
let pendingWrite = Promise.resolve();
const encoder = new TextEncoder();
const paths = process.argv.slice(1);
if (paths.length === 0)
paths.push('-');
for (const path of paths) {
let rid = null;
try {
rid = path === '-' ? 0 : await process.openRead(path);
let buffer;
while ((buffer = await process.read(rid)) !== null) {
await pendingWrite;
pendingWrite = process.write(1, buffer);
}
await pendingWrite;
} catch (err) {
await pendingWrite.catch(() => null);
await process.write(2, encoder.encode(`${path}: ${((err instanceof Error && err.message) || err)}\n`));
} finally {
pendingWrite = Promise.resolve();
if (rid !== null)
await process.close(rid);
}
}
});