Skip to content

Commit 02cb655

Browse files
committed
feat: add $.startTime and $.time
1 parent 95e6f3c commit 02cb655

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

src/cli/mod.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { io, path } from "../../mod.ts";
1+
import { io, path } from "../runtime/mod.ts";
22
import { bundleCommand } from "./bundle.ts";
33
import { compileCommand } from "./compile.ts";
44
import { Command, ValidationError } from "./deps.ts";
55

66
export function dzx() {
7-
const start = Date.now();
87
return new Command<void>()
98
.version("0.2.0")
109
.name("dzx")
@@ -64,42 +63,41 @@ export function dzx() {
6463
_args?: Array<string>,
6564
) => {
6665
if (script) {
67-
script = addProtocool(script);
68-
$.mainModule = script;
66+
$.mainModule = addProtocool(script);
6967
if (worker) {
70-
spawnWorker(script, perms);
68+
spawnWorker(perms);
7169
} else {
72-
await import(script);
70+
await import($.mainModule);
7371
}
7472
} else if (Deno.isatty(Deno.stdin.rid)) {
7573
throw new ValidationError(`Missing argument(s): script`);
7674
} else {
7775
await importFromStdin();
7876
}
7977
if ($.verbose) {
80-
const end = Date.now();
81-
console.log($.bold("time: %ss"), Math.round(end - start) / 1000);
78+
console.log($.bold("time: %ss"), Math.round($.time) / 1000);
8279
}
8380
},
8481
)
8582
.command("bundle", bundleCommand())
8683
.command("compile", compileCommand());
8784

88-
function spawnWorker(script: string, perms: Permissions): void {
85+
function spawnWorker(perms: Permissions): void {
8986
new Worker(
9087
`data:application/typescript,${
9188
encodeURIComponent(`
9289
import "${new URL("./src/runtime/mod.ts", Deno.mainModule)}";
93-
$.mainModule = "${script}";
94-
await import("${script}");
90+
$.mainModule = "${$.mainModule}";
91+
$.startTime = ${$.startTime};
92+
await import("${$.mainModule}");
9593
if ($.verbose) {
9694
const end = Date.now();
97-
console.log($.bold("time: %ss"), Math.round(end - ${start}) / 1000);
95+
console.log($.bold("time: %ss"), Math.round($.time) / 1000);
9896
}
9997
self.close();`)
10098
}`,
10199
{
102-
name: script,
100+
name: $.mainModule,
103101
type: "module",
104102
deno: {
105103
namespace: true,

src/runtime/mod.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export type $ = typeof exec & typeof colors & {
99
verbose: boolean;
1010
quote: typeof shq;
1111
throwErrors: boolean;
12+
startTime: number;
13+
time: number;
1214
};
1315

1416
export const $: $ = exec as $;
@@ -21,6 +23,10 @@ $.mainModule = "";
2123
$.verbose = false;
2224
$.quote = shq;
2325
$.throwErrors = false;
26+
$.startTime = Date.now();
27+
Object.defineProperty($, "time", {
28+
get: () => Date.now() - $.startTime,
29+
});
2430

2531
// dzx
2632
self.$ = $;

0 commit comments

Comments
 (0)