From d8e53ac4e7a21b6034159a7615c58325e9728641 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Wed, 30 Aug 2023 09:27:10 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9EFix=20arguments=20passed=20to=20Nod?= =?UTF-8?q?eJS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The process argument handling is different from Deno and NodeJS. The following script in Deno: ``` shellsession $ deno script.ts hello world ``` will set `Deno.args` to `["hello", "world"]`. By contrast, the following in NodeJS: ``` shellsession $ node script.mjs hello world ``` will set `process.argv` to `["/path/to/node","/abs/path/to/script.mjs", "hello", "world"]` The NodeJS way is similar to the argv of a `Bash` script, but the Deno way is more practical. This normalizes Node to behave like Deno, only passing the user arguments to the script. We can explore how to get the script name and the executable name later. --- deno.json | 2 +- lib/main.ts | 2 +- test/main/node.mjs | 5 ++++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/deno.json b/deno.json index 7f15e9987..03923ed9f 100644 --- a/deno.json +++ b/deno.json @@ -2,7 +2,7 @@ "lock": false, "tasks": { "test": "deno test --allow-run=deno", - "test:node": "deno task build:npm 0.0.0 && node test/main/node.mjs", + "test:node": "deno task build:npm 0.0.0 && node test/main/node.mjs hello world", "build:npm": "deno run -A tasks/build-npm.ts" }, "lint": { diff --git a/lib/main.ts b/lib/main.ts index b95a09d09..7c024b126 100644 --- a/lib/main.ts +++ b/lib/main.ts @@ -86,7 +86,7 @@ export async function main( //@ts-expect-error type-checked by Deno, run on Node process.on("SIGINT", interrupt); //@ts-expect-error type-checked by Deno, run on Node - yield* body(global.process.argv.slice()); + yield* body(global.process.argv.slice(2)); } finally { //@ts-expect-error this runs on Node process.off("SIGINT", interrupt); diff --git a/test/main/node.mjs b/test/main/node.mjs index 18de7ca20..64767e7da 100644 --- a/test/main/node.mjs +++ b/test/main/node.mjs @@ -1,5 +1,8 @@ import { main } from "../../build/npm/esm/mod.js"; -await main(function* () { +await main(function* ([hello, world]) { + if (hello !== "hello" && world !== "world") { + throw new Error("arguments were not properly passed to main operation"); + } console.log("hello world"); });