1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-02 17:01:14 -05:00

fix(std/node): global process should usable (#6392)

This commit is contained in:
Benjamin Lupton 2020-06-24 06:00:47 +10:00 committed by GitHub
parent 63db3e933e
commit d16337cc9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 94 additions and 19 deletions

View file

@ -21,7 +21,7 @@
import { notImplemented } from "./_utils.ts";
import { validateIntegerRange } from "./util.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";

View file

@ -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;
}

View file

@ -1,9 +1,29 @@
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
// (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({
name: "process.cwd and process.chdir success",
fn() {
@ -82,8 +102,9 @@ Deno.test({
Deno.test({
name: "process.argv",
fn() {
async fn() {
assert(Array.isArray(process.argv));
assert(Array.isArray(await argv));
assert(
process.argv[0].match(/[^/\\]*deno[^/\\]*$/),
"deno included in the file name of argv[0]"
@ -94,7 +115,8 @@ Deno.test({
Deno.test({
name: "process.env",
fn() {
async fn() {
assertEquals(typeof process.env.PATH, "string");
assertEquals(typeof (await env).PATH, "string");
},
});