diff --git a/README.md b/README.md index e021a2a..32de52d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # @cross/utils -**Work in progress** A collection of useful routines to simplify cross runtime -(Node, Deno and Bun) development. +A collection of useful routines to simplify cross runtime (Node, Deno and Bun) +development. Available for Node, Deno Bun and Browser at [jsr.io/@cross/utils](https://jsr.io/@cross/utils), and works seamlessly with @@ -25,7 +25,9 @@ bunx jsr add @cross/utils ## Methods -### Exit +### exit + +Terminates the current process with a provided exit code. ```js import { exit } from "@cross/utils"; @@ -36,3 +38,24 @@ exit(); // Exit with error code 0 console.log("Will not show"); ``` + +**Parameters** + +- `code` (number, optional): The exit code for the process. Defaults to 0 + (success). + +### args + +Extracts command-line arguments in a cross-runtime compatible manner. + +```js +import { args } from "@cross/utils"; + +console.log("These are the arguments", args()); // Default behavior +console.log("All arguments (including executable and script)", args(true)); +``` + +**Parameters** + +- `all` (boolean, optional): When true, includes the executable path and script + name at the beginning of the returned array. Defaults to false. diff --git a/deno.json b/deno.json index 140c0bc..1b17898 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@cross/utils", - "version": "0.0.1", + "version": "0.0.2", "exports": "./mod.ts", "imports": { "@cross/runtime": "jsr:@cross/runtime@^0.0.16" } } diff --git a/mod.ts b/mod.ts index cfbc5e6..15e6491 100644 --- a/mod.ts +++ b/mod.ts @@ -1 +1,2 @@ export { exit } from "./utils/exit.ts"; +export { args } from "./utils/args.ts"; diff --git a/utils/args.ts b/utils/args.ts new file mode 100644 index 0000000..7a81b57 --- /dev/null +++ b/utils/args.ts @@ -0,0 +1,44 @@ +import { CurrentRuntime, Runtime } from "@cross/runtime"; + +/** + * Retrieves command-line arguments in a cross-runtime compatible manner. + * + * @param {boolean} all A boolean indicating if all arguments should be passed, including script name and executable. + * + * @returns {string[]} An array containing the command-line arguments. + * @example + * // import { args } from "@cross/utils"; + * + * const args = args(); + * console.log("Arguments:", args); + */ +export function args(all: boolean = false): string[] { + if (CurrentRuntime === Runtime.Deno) { + if (all) { + // Construct 'fake' executable and script name based on Deno.execPath() and Deno.mainModule + // @ts-ignore Cross Runtime + const executable = Deno.execPath(); + // @ts-ignore Cross Runtime + const script = Deno.mainModule; + // @ts-ignore Cross Runtime + return [executable, script, ...Deno.args]; + } else { + // @ts-ignore Cross Runtime + return Deno.args; + } + } else if ( + CurrentRuntime === Runtime.Node || CurrentRuntime === Runtime.Bun + ) { + // @ts-ignore Cross Runtime + if (all) { + // @ts-ignore Cross Runtime + return process.argv; + } else { + // @ts-ignore Cross Runtime + return process.argv.slice(2); // Skip the first two arguments (executable and script name) + } + } else { + console.error("Cannot determine runtime. Argument retrieval unavailable."); + return []; + } +}