mirror of
https://github.com/denoland/deno.git
synced 2024-12-11 10:07:54 -05:00
fix(std/node): global process should usable (#6392)
This commit is contained in:
parent
63db3e933e
commit
d16337cc9c
3 changed files with 94 additions and 19 deletions
|
@ -21,7 +21,7 @@
|
||||||
import { notImplemented } from "./_utils.ts";
|
import { notImplemented } from "./_utils.ts";
|
||||||
import { validateIntegerRange } from "./util.ts";
|
import { validateIntegerRange } from "./util.ts";
|
||||||
import { EOL as fsEOL } from "../fs/eol.ts";
|
import { EOL as fsEOL } from "../fs/eol.ts";
|
||||||
import { process } from "./process.ts";
|
import process from "./process.ts";
|
||||||
|
|
||||||
const SEE_GITHUB_ISSUE = "See https://github.com/denoland/deno/issues/3802";
|
const SEE_GITHUB_ISSUE = "See https://github.com/denoland/deno/issues/3802";
|
||||||
|
|
||||||
|
|
|
@ -1,33 +1,82 @@
|
||||||
import { notImplemented } from "./_utils.ts";
|
import { notImplemented } from "./_utils.ts";
|
||||||
|
|
||||||
function on(_event: string, _callback: Function): void {
|
/** https://nodejs.org/api/process.html#process_process_arch */
|
||||||
// TODO(rsp): to be implemented
|
export const arch = Deno.build.arch;
|
||||||
notImplemented();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/** 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 = {
|
export const process = {
|
||||||
version: `v${Deno.version.deno}`,
|
arch,
|
||||||
versions: {
|
chdir,
|
||||||
node: Deno.version.deno,
|
cwd,
|
||||||
...Deno.version,
|
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,
|
/** https://nodejs.org/api/process.html#process_process_env */
|
||||||
pid: Deno.pid,
|
|
||||||
cwd: Deno.cwd,
|
|
||||||
chdir: Deno.chdir,
|
|
||||||
exit: Deno.exit,
|
|
||||||
on,
|
|
||||||
get env(): { [index: string]: string } {
|
get env(): { [index: string]: string } {
|
||||||
// using getter to avoid --allow-env unless it's used
|
// using getter to avoid --allow-env unless it's used
|
||||||
return Deno.env.toObject();
|
return Deno.env.toObject();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/** https://nodejs.org/api/process.html#process_process_argv */
|
||||||
get argv(): string[] {
|
get argv(): string[] {
|
||||||
// Deno.execPath() also requires --allow-env
|
// Deno.execPath() also requires --allow-env
|
||||||
return [Deno.execPath(), ...Deno.args];
|
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, {
|
Object.defineProperty(process, Symbol.toStringTag, {
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
writable: true,
|
writable: true,
|
||||||
|
@ -41,3 +90,7 @@ Object.defineProperty(globalThis, "process", {
|
||||||
writable: true,
|
writable: true,
|
||||||
configurable: true,
|
configurable: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
const process: Process;
|
||||||
|
}
|
||||||
|
|
|
@ -1,9 +1,29 @@
|
||||||
import { assert, assertThrows, assertEquals } from "../testing/asserts.ts";
|
import { assert, assertThrows, assertEquals } from "../testing/asserts.ts";
|
||||||
import { process } from "./process.ts";
|
import * as all from "./process.ts";
|
||||||
|
import { env, argv } from "./process.ts";
|
||||||
|
|
||||||
// NOTE: Deno.execPath() (and thus process.argv) currently requires --allow-env
|
// NOTE: Deno.execPath() (and thus process.argv) currently requires --allow-env
|
||||||
// (Also Deno.env.toObject() (and process.env) requires --allow-env but it's more obvious)
|
// (Also Deno.env.toObject() (and process.env) requires --allow-env but it's more obvious)
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "process exports are as they should be",
|
||||||
|
fn() {
|
||||||
|
// * should be the same as process, default, and globalThis.process
|
||||||
|
// without the export aliases, and with properties that are not standalone
|
||||||
|
const allKeys = new Set<string>(Object.keys(all));
|
||||||
|
// without { process } for deno b/c
|
||||||
|
allKeys.delete("process");
|
||||||
|
// without esm default
|
||||||
|
allKeys.delete("default");
|
||||||
|
// with on, which is not exported via *
|
||||||
|
allKeys.add("on");
|
||||||
|
const allStr = Array.from(allKeys).sort().join(" ");
|
||||||
|
assertEquals(Object.keys(all.default).sort().join(" "), allStr);
|
||||||
|
assertEquals(Object.keys(all.process).sort().join(" "), allStr);
|
||||||
|
assertEquals(Object.keys(process).sort().join(" "), allStr);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "process.cwd and process.chdir success",
|
name: "process.cwd and process.chdir success",
|
||||||
fn() {
|
fn() {
|
||||||
|
@ -82,8 +102,9 @@ Deno.test({
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "process.argv",
|
name: "process.argv",
|
||||||
fn() {
|
async fn() {
|
||||||
assert(Array.isArray(process.argv));
|
assert(Array.isArray(process.argv));
|
||||||
|
assert(Array.isArray(await argv));
|
||||||
assert(
|
assert(
|
||||||
process.argv[0].match(/[^/\\]*deno[^/\\]*$/),
|
process.argv[0].match(/[^/\\]*deno[^/\\]*$/),
|
||||||
"deno included in the file name of argv[0]"
|
"deno included in the file name of argv[0]"
|
||||||
|
@ -94,7 +115,8 @@ Deno.test({
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "process.env",
|
name: "process.env",
|
||||||
fn() {
|
async fn() {
|
||||||
assertEquals(typeof process.env.PATH, "string");
|
assertEquals(typeof process.env.PATH, "string");
|
||||||
|
assertEquals(typeof (await env).PATH, "string");
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue