Skip to content

Commit

Permalink
feat: add $.args
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed May 14, 2021
1 parent b3fa670 commit b1cdd29
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ script.
- **flags:** Deno's `std/flags` module.

```ts
console.log($.blue.bold("Hello world!"));
const args: flags.Args = flags.parse($.args);
```

## Variables
Expand All @@ -240,6 +240,8 @@ script.
`"piped"`, `"null"` or `number`. Will be reverted to default after all async
ops are done. Default: `"piped"`
- **$.throwErrors:** Throw errors instead of calling `Deno.exit`.
- **$.args:** Equivalent to `Deno.args`, but without the script name as first
argument.
- **$.startTime:** The execution start time in ms.
- **$.time:** The time left since execution start (now() - $.startTime).
- **$.quote:** Parser method that is used to safely quote strings. Used by:
Expand Down
6 changes: 5 additions & 1 deletion src/cli/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ export function dzx() {
async (
{ worker, ...perms },
script?: string,
_args?: Array<string>,
args: Array<string> = [],
) => {
$.args = args;
if (script) {
$.mainModule = addProtocool(script);
if (worker) {
Expand Down Expand Up @@ -90,6 +91,9 @@ export function dzx() {
import "${new URL("./src/runtime/mod.ts", Deno.mainModule)}";
$.mainModule = "${$.mainModule}";
$.startTime = ${$.startTime};
$.args = JSON.parse(decodeURIComponent("${
encodeURIComponent(JSON.stringify($.args))
}"));
await import("${$.mainModule}");
if ($.verbose) {
const end = Date.now();
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ export type $ = typeof exec & typeof colors & {
verbose: boolean;
stdout: NonNullable<Deno.RunOptions["stdout"]>;
stderr: NonNullable<Deno.RunOptions["stderr"]>;
args: Array<string>;
quote: typeof shq;
throwErrors: boolean;
startTime: number;
time: number;
quote: typeof shq;
};

export const $: $ = exec as $;
Expand All @@ -25,6 +26,7 @@ $.mainModule = "";
$.verbose = false;
$.stdout = "piped";
$.stderr = "piped";
$.args = [];
$.quote = shq;
$.throwErrors = false;
$.startTime = Date.now();
Expand Down

0 comments on commit b1cdd29

Please sign in to comment.