1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-11 18:17:48 -05:00
denoland-deno/std/node/process.ts
Benjamin Lupton 16038b8f82
fix(std/node/process): env, argv exports (#6455)
The promise approach still required permissions to be specified
at initialisation, rather than at request.

Using a Proxy instance solves this permission issue.

The Proxy instance approach also eliminates the need for the
await. Achieving direct compatibility with Node.js.

/ref pr #6392
/ref commit d16337cc9c
2020-06-25 07:18:01 -04:00

101 lines
3.1 KiB
TypeScript

import { notImplemented } from "./_utils.ts";
/** 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 `import { process } from 'process'` for backwards compatibility with old deno versions
export const process = {
arch,
chdir,
cwd,
exit,
pid,
platform,
version,
versions,
/** https://nodejs.org/api/process.html#process_process_events */
// on is not exported by node, it is only available within process:
// node --input-type=module -e "import { on } from 'process'; console.log(on)"
on(_event: string, _callback: Function): void {
// TODO(rsp): to be implemented
notImplemented();
},
/** https://nodejs.org/api/process.html#process_process_argv */
get argv(): string[] {
// Getter delegates --allow-env and --allow-read until request
// Getter also allows the export Proxy instance to function as intended
return [Deno.execPath(), ...Deno.args];
},
/** https://nodejs.org/api/process.html#process_process_env */
get env(): { [index: string]: string } {
// Getter delegates --allow-env and --allow-read until request
// Getter also allows the export Proxy instance to function as intended
return Deno.env.toObject();
},
};
/**
* https://nodejs.org/api/process.html#process_process_argv
* @example `import { argv } from './std/node/process.ts'; console.log(argv)`
*/
// Proxy delegates --allow-env and --allow-read to request time, even for exports
export const argv = new Proxy(process.argv, {});
/**
* https://nodejs.org/api/process.html#process_process_env
* @example `import { env } from './std/node/process.ts'; console.log(env)`
*/
// Proxy delegates --allow-env and --allow-read to request time, even for exports
export const env = new Proxy(process.env, {});
// import process from './std/node/process.ts'
export default process;
// Define the type for the global declration
type Process = typeof process;
Object.defineProperty(process, Symbol.toStringTag, {
enumerable: false,
writable: true,
configurable: false,
value: "process",
});
Object.defineProperty(globalThis, "process", {
value: process,
enumerable: false,
writable: true,
configurable: true,
});
declare global {
const process: Process;
}