summaryrefslogtreecommitdiff
path: root/std/node/process.ts
diff options
context:
space:
mode:
authorBenjamin Lupton <b@lupton.cc>2020-06-24 06:00:47 +1000
committerGitHub <noreply@github.com>2020-06-23 16:00:47 -0400
commitd16337cc9c59732fe81655482e08b72d844472e6 (patch)
tree9e343edba1b42aee590ee5a2955f9c123d6d9cd0 /std/node/process.ts
parent63db3e933e90cdf43580c4c670eace5cd92d5a43 (diff)
fix(std/node): global process should usable (#6392)
Diffstat (limited to 'std/node/process.ts')
-rw-r--r--std/node/process.ts83
1 files changed, 68 insertions, 15 deletions
diff --git a/std/node/process.ts b/std/node/process.ts
index cad72a00a..12cd1b4a9 100644
--- a/std/node/process.ts
+++ b/std/node/process.ts
@@ -1,33 +1,82 @@
import { notImplemented } from "./_utils.ts";
-function on(_event: string, _callback: Function): void {
- // TODO(rsp): to be implemented
- notImplemented();
-}
+/** https://nodejs.org/api/process.html#process_process_arch */
+export const arch = Deno.build.arch;
+
+/** https://nodejs.org/api/process.html#process_process_chdir_directory */
+export const chdir = Deno.chdir;
+
+/** https://nodejs.org/api/process.html#process_process_cwd */
+export const cwd = Deno.cwd;
+
+/** https://nodejs.org/api/process.html#process_process_exit_code */
+export const exit = Deno.exit;
+
+/** https://nodejs.org/api/process.html#process_process_pid */
+export const pid = Deno.pid;
+
+/** https://nodejs.org/api/process.html#process_process_platform */
+export const platform = Deno.build.os === "windows" ? "win32" : Deno.build.os;
+
+/** https://nodejs.org/api/process.html#process_process_version */
+export const version = `v${Deno.version.deno}`;
+
+/** https://nodejs.org/api/process.html#process_process_versions */
+export const versions = {
+ node: Deno.version.deno,
+ ...Deno.version,
+};
+/** https://nodejs.org/api/process.html#process_process */
+// @deprecated exported only for backwards compatibility with old deno versions
export const process = {
- version: `v${Deno.version.deno}`,
- versions: {
- node: Deno.version.deno,
- ...Deno.version,
+ arch,
+ chdir,
+ cwd,
+ exit,
+ pid,
+ platform,
+ version,
+ versions,
+
+ /** https://nodejs.org/api/process.html#process_process_events */
+ // node --input-type=module -e "import {on} from 'process'; console.log(on)"
+ // on is not exported by node, it is only available within process
+ on(_event: string, _callback: Function): void {
+ // TODO(rsp): to be implemented
+ notImplemented();
},
- platform: Deno.build.os === "windows" ? "win32" : Deno.build.os,
- arch: Deno.build.arch,
- pid: Deno.pid,
- cwd: Deno.cwd,
- chdir: Deno.chdir,
- exit: Deno.exit,
- on,
+
+ /** https://nodejs.org/api/process.html#process_process_env */
get env(): { [index: string]: string } {
// using getter to avoid --allow-env unless it's used
return Deno.env.toObject();
},
+
+ /** https://nodejs.org/api/process.html#process_process_argv */
get argv(): string[] {
// Deno.execPath() also requires --allow-env
return [Deno.execPath(), ...Deno.args];
},
};
+// define the type for configuring the env and argv promises
+// as well as for the global.process declaration
+type Process = typeof process;
+
+/** requires the use of await for compatibility with deno */
+export const env = new Promise<Process["env"]>((resolve) =>
+ resolve(process.env)
+);
+
+/** requires the use of await for compatibility with deno */
+export const argv = new Promise<Process["argv"]>((resolve) =>
+ resolve(process.argv)
+);
+
+/** use this for access to `process.env` and `process.argv` without the need for await */
+export default process;
+
Object.defineProperty(process, Symbol.toStringTag, {
enumerable: false,
writable: true,
@@ -41,3 +90,7 @@ Object.defineProperty(globalThis, "process", {
writable: true,
configurable: true,
});
+
+declare global {
+ const process: Process;
+}