'use strict';
(({ getEnv, getArgv, getCwd, exit, writeStdout, writeStderr, readStdin }) => {
class ProcessWritableStream {
#writeFunc;
#isTTY;
constructor(writeFunc, isTTY = false) {
this.#writeFunc = writeFunc;
this.#isTTY = isTTY;
}
write(chunk, encoding, callback) {
if (typeof encoding === 'function') {
callback = encoding;
encoding = 'utf8';
}
try {
let str;
if (typeof chunk === 'string') {
str = chunk;
} else if (chunk instanceof Uint8Array) {
this.#writeFunc(chunk);
if (callback) callback();
return true;
} else if (chunk && typeof chunk === 'object' && chunk.buffer instanceof ArrayBuffer) {
const uint8 = new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength);
this.#writeFunc(uint8);
if (callback) callback();
return true;
} else {
str = String(chunk);
}
this.#writeFunc(str);
if (callback) callback();
return true;
} catch (e) {
if (callback) callback(e);
return false;
}
}
end(chunk, encoding, callback) {
if (typeof chunk === 'function') {
callback = chunk;
chunk = undefined;
encoding = 'utf8';
} else if (typeof encoding === 'function') {
callback = encoding;
encoding = 'utf8';
}
if (chunk !== undefined) {
this.write(chunk, encoding);
}
if (callback) callback();
}
get isTTY() {
return this.#isTTY;
}
setEncoding() { return this; }
pause() { return this; }
resume() { return this; }
destroy() { return this; }
}
class ProcessReadableStream {
#isTTY;
#hasRead;
#data;
constructor(isTTY = false) {
this.#isTTY = isTTY;
this.#hasRead = false;
this.#data = null;
}
read(size) {
if (!this.#hasRead) {
this.#data = readStdin();
this.#hasRead = true;
}
return this.#data;
}
setEncoding() { return this; }
pause() { return this; }
resume() { return this; }
destroy() { return this; }
get isTTY() {
return this.#isTTY;
}
}
const process = {
get env() {
if (!this._env) {
this._env = getEnv();
}
return this._env;
},
get argv() {
if (!this._argv) {
this._argv = getArgv();
}
return this._argv;
},
cwd() {
return getCwd();
},
exit(code) {
exit(code || 0);
},
get stdout() {
if (!this._stdout) {
this._stdout = new ProcessWritableStream(writeStdout, false);
}
return this._stdout;
},
get stderr() {
if (!this._stderr) {
this._stderr = new ProcessWritableStream(writeStderr, false);
}
return this._stderr;
},
get stdin() {
if (!this._stdin) {
this._stdin = new ProcessReadableStream(false);
}
return this._stdin;
},
};
globalThis.process = process;
});