|
| 1 | +import { WebContainerProcess } from "@webcontainer/api"; |
| 2 | + |
| 3 | +export class ProcessWrap { |
| 4 | + /** @internal */ |
| 5 | + private _webcontainerProcess!: WebContainerProcess; |
| 6 | + |
| 7 | + /** @internal */ |
| 8 | + private _isReady: Promise<void>; |
| 9 | + |
| 10 | + /** @internal */ |
| 11 | + private _output: string = ""; |
| 12 | + |
| 13 | + /** @internal */ |
| 14 | + private _listeners: (() => void)[] = []; |
| 15 | + |
| 16 | + /** @internal */ |
| 17 | + private _writer?: ReturnType<WebContainerProcess["input"]["getWriter"]>; |
| 18 | + |
| 19 | + /** |
| 20 | + * Wait for process to exit. |
| 21 | + */ |
| 22 | + isDone: Promise<void>; |
| 23 | + |
| 24 | + constructor(promise: Promise<WebContainerProcess>) { |
| 25 | + let setDone: () => void = () => undefined; |
| 26 | + this.isDone = new Promise((resolve) => (setDone = resolve)); |
| 27 | + |
| 28 | + this._isReady = promise.then((webcontainerProcess) => { |
| 29 | + this._webcontainerProcess = webcontainerProcess; |
| 30 | + this._writer = webcontainerProcess.input.getWriter(); |
| 31 | + |
| 32 | + webcontainerProcess.exit.then(() => setDone()); |
| 33 | + |
| 34 | + this._webcontainerProcess.output.pipeTo( |
| 35 | + new WritableStream({ |
| 36 | + write: (data) => { |
| 37 | + this._output += data; |
| 38 | + this._listeners.forEach((fn) => fn()); |
| 39 | + }, |
| 40 | + }), |
| 41 | + ); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + then<TResult1 = string, TResult2 = never>( |
| 46 | + onfulfilled?: ((value: string) => TResult1 | PromiseLike<TResult1>) | null, |
| 47 | + onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null, |
| 48 | + ): Promise<TResult1 | TResult2> { |
| 49 | + return this.isDone |
| 50 | + .then(() => this._output.trim()) |
| 51 | + .then(onfulfilled, onrejected); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Write command into the process. |
| 56 | + */ |
| 57 | + write = async (text: string) => { |
| 58 | + await this._isReady; |
| 59 | + |
| 60 | + this.resetCapturedText(); |
| 61 | + |
| 62 | + if (!this._writer) { |
| 63 | + throw new Error("Process setup failed, writer not initialized"); |
| 64 | + } |
| 65 | + |
| 66 | + return this._writer.write(text); |
| 67 | + }; |
| 68 | + |
| 69 | + /** |
| 70 | + * Reset captured output, so that `waitForText` does not match previous captured outputs. |
| 71 | + */ |
| 72 | + resetCapturedText = () => { |
| 73 | + this._output = ""; |
| 74 | + }; |
| 75 | + |
| 76 | + /** |
| 77 | + * Wait for process to output expected text. |
| 78 | + */ |
| 79 | + waitForText = async (expected: string, timeoutMs = 10_000) => { |
| 80 | + const error = new Error("Timeout"); |
| 81 | + |
| 82 | + if ("captureStackTrace" in Error) { |
| 83 | + Error.captureStackTrace(error, this.waitForText); |
| 84 | + } |
| 85 | + |
| 86 | + await this._isReady; |
| 87 | + |
| 88 | + return new Promise<void>((resolve, reject) => { |
| 89 | + if (this._output.includes(expected)) { |
| 90 | + resolve(); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + const timeout = setTimeout(() => { |
| 95 | + error.message = `Timeout when waiting for error "${expected}".\nReceived:\n${this._output}`; |
| 96 | + reject(error); |
| 97 | + }, timeoutMs); |
| 98 | + |
| 99 | + const listener = () => { |
| 100 | + if (this._output.includes(expected)) { |
| 101 | + clearTimeout(timeout); |
| 102 | + this._listeners.splice(this._listeners.indexOf(listener), 1); |
| 103 | + |
| 104 | + resolve(); |
| 105 | + } |
| 106 | + }; |
| 107 | + |
| 108 | + this._listeners.push(listener); |
| 109 | + }); |
| 110 | + }; |
| 111 | + |
| 112 | + /** |
| 113 | + * Exit the process. |
| 114 | + */ |
| 115 | + exit = async () => { |
| 116 | + await this._isReady; |
| 117 | + |
| 118 | + // @ts-ignore -- internal check |
| 119 | + if (this._webcontainerProcess._process != null) { |
| 120 | + this._webcontainerProcess.kill(); |
| 121 | + } |
| 122 | + |
| 123 | + this._listeners.splice(0); |
| 124 | + |
| 125 | + return this.isDone; |
| 126 | + }; |
| 127 | +} |
0 commit comments