Skip to content

Commit

Permalink
Add exit
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Mar 11, 2024
1 parent 10a8941 commit 8b40c8f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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";
Expand All @@ -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.
2 changes: 1 addition & 1 deletion deno.json
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" }
}
1 change: 1 addition & 0 deletions mod.ts
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";
44 changes: 44 additions & 0 deletions utils/args.ts
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 [];
}
}

0 comments on commit 8b40c8f

Please sign in to comment.