-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
72 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { exit } from "./utils/exit.ts"; | ||
export { args } from "./utils/args.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 []; | ||
} | ||
} |