Skip to content

Commit bcb02fd

Browse files
authored
refactor(runtime): make $.mainModule, $.args & $.startTime readonly (#32)
1 parent 3292575 commit bcb02fd

File tree

4 files changed

+43
-23
lines changed

4 files changed

+43
-23
lines changed

src/cli/lib/bootstrap.ts

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { $ } from "../../runtime/mod.ts";
22

3+
const startTime = Date.now();
4+
35
export interface BootstrapOptions {
46
code?: string;
5-
startTime?: number;
67
mainModule?: string;
78
args?: Array<string> | string;
89
base64?: boolean;
@@ -14,19 +15,32 @@ export function base64Module(code: string) {
1415
}
1516

1617
export function stringifyArgs(args: Array<string>) {
17-
return args?.length
18-
? `$.args = JSON.parse(decodeURIComponent("${
18+
if (!args?.length) {
19+
return "";
20+
}
21+
let code = args?.length
22+
? `const args = JSON.parse(decodeURIComponent("${
1923
encodeURIComponent(JSON.stringify(args))
20-
}"));`
21-
: "";
24+
}"));\n`
25+
: "const args = [];\n";
26+
code += `Object.defineProperty($, "args", { get: () => args });`;
27+
return code;
28+
}
29+
30+
export function stringifyMainModule(mainModule: string) {
31+
return `Object.defineProperty($, "mainModule", { get: () => "${mainModule}" });`;
32+
}
33+
34+
export function stringifyStartTime(startTime: number) {
35+
return `Object.defineProperty($, "startTime", { get: () => ${startTime} });`;
2236
}
2337

2438
export function bootstrap(options: BootstrapOptions): string {
2539
const code = [
2640
`import "${new URL("../../../mod.ts", import.meta.url)}";`,
2741
"{",
28-
options.startTime ? `$.startTime = ${options.startTime};` : "",
29-
options.mainModule ? `$.mainModule = "${options.mainModule}";` : "",
42+
stringifyStartTime(startTime),
43+
options.mainModule ? stringifyMainModule(options.mainModule) : "",
3044
options.verbose !== undefined ? `$.verbose = ${options.verbose};` : "",
3145
typeof options.args === "string"
3246
? options.args
@@ -78,14 +92,26 @@ export interface ImportModuleOptions {
7892
}
7993

8094
export async function importModule(options: ImportModuleOptions) {
81-
$.mainModule = options.mainModule;
82-
if (options.args) {
83-
$.args = options.args;
84-
}
95+
const mainModule = options.mainModule;
96+
Object.defineProperty($, "mainModule", {
97+
get: () => mainModule,
98+
});
99+
100+
const args = options.args ? [...options.args] : [];
101+
Object.defineProperty($, "args", {
102+
get: () => args,
103+
});
104+
85105
if (typeof options.verbose !== "undefined") {
86106
$.verbose = options.verbose;
87107
}
108+
109+
Object.defineProperty($, "startTime", {
110+
get: (): number => startTime,
111+
});
112+
88113
await import($.mainModule);
114+
89115
if ($.verbose) {
90116
console.log($.bold("time: %ss"), Math.round($.time) / 1000);
91117
}

src/cli/lib/worker.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@ export interface SpawnWorkerOptions extends Omit<BootstrapOptions, "base64"> {
1717

1818
export function spawnWorker({
1919
args,
20-
startTime,
2120
mainModule,
2221
perms,
2322
verbose,
2423
}: SpawnWorkerOptions): void {
2524
new Worker(
2625
bootstrapModule({
2726
args,
28-
startTime,
2927
mainModule,
3028
verbose,
3129
base64: true,

src/cli/mod.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { VERSION } from "../../version.ts";
2-
import { $, path } from "../runtime/mod.ts";
2+
import { path } from "../runtime/mod.ts";
33
import { bundleCommand } from "./bundle.ts";
44
import { compileCommand } from "./compile.ts";
55
import {
@@ -107,7 +107,6 @@ export function dzx() {
107107
mainModule,
108108
args,
109109
verbose,
110-
startTime: $.startTime,
111110
});
112111
} else {
113112
await importModule({ mainModule, args, verbose });

src/runtime/mod.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ export { ProcessError } from "./process_error.ts";
1919
export { ProcessOutput } from "./process_output.ts";
2020

2121
export type $ = typeof exec & typeof colors & {
22-
shell: string;
23-
prefix: string;
24-
mainModule: string;
22+
get mainModule(): string;
23+
get args(): Array<string>;
2524
get verbose(): number;
2625
set verbose(value: boolean | number);
26+
get startTime(): number;
27+
shell: string;
28+
prefix: string;
2729
stdout: NonNullable<Deno.RunOptions["stdout"]>;
2830
stderr: NonNullable<Deno.RunOptions["stderr"]>;
29-
args: Array<string>;
3031
quote: typeof shq;
3132
throwErrors: boolean;
32-
startTime: number;
3333
time: number;
3434
};
3535

@@ -43,13 +43,10 @@ Object.setPrototypeOf($, Object.getPrototypeOf(colors));
4343
$._stack = [];
4444
$.shell = "/bin/bash";
4545
$.prefix = "set -euo pipefail;";
46-
$.mainModule = Deno.mainModule;
4746
$.stdout = "piped";
4847
$.stderr = "piped";
49-
$.args = [];
5048
$.quote = shq;
5149
$.throwErrors = false;
52-
$.startTime = Date.now();
5350

5451
let _verbose = 1;
5552
Object.defineProperty($, "verbose", {

0 commit comments

Comments
 (0)