Skip to content

Commit

Permalink
refactor(runtime): make $.mainModule, $.args & $.startTime read…
Browse files Browse the repository at this point in the history
…only (#32)
  • Loading branch information
c4spar authored Apr 27, 2022
1 parent 3292575 commit bcb02fd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 23 deletions.
48 changes: 37 additions & 11 deletions src/cli/lib/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { $ } from "../../runtime/mod.ts";

const startTime = Date.now();

export interface BootstrapOptions {
code?: string;
startTime?: number;
mainModule?: string;
args?: Array<string> | string;
base64?: boolean;
Expand All @@ -14,19 +15,32 @@ export function base64Module(code: string) {
}

export function stringifyArgs(args: Array<string>) {
return args?.length
? `$.args = JSON.parse(decodeURIComponent("${
if (!args?.length) {
return "";
}
let code = args?.length
? `const args = JSON.parse(decodeURIComponent("${
encodeURIComponent(JSON.stringify(args))
}"));`
: "";
}"));\n`
: "const args = [];\n";
code += `Object.defineProperty($, "args", { get: () => args });`;
return code;
}

export function stringifyMainModule(mainModule: string) {
return `Object.defineProperty($, "mainModule", { get: () => "${mainModule}" });`;
}

export function stringifyStartTime(startTime: number) {
return `Object.defineProperty($, "startTime", { get: () => ${startTime} });`;
}

export function bootstrap(options: BootstrapOptions): string {
const code = [
`import "${new URL("../../../mod.ts", import.meta.url)}";`,
"{",
options.startTime ? `$.startTime = ${options.startTime};` : "",
options.mainModule ? `$.mainModule = "${options.mainModule}";` : "",
stringifyStartTime(startTime),
options.mainModule ? stringifyMainModule(options.mainModule) : "",
options.verbose !== undefined ? `$.verbose = ${options.verbose};` : "",
typeof options.args === "string"
? options.args
Expand Down Expand Up @@ -78,14 +92,26 @@ export interface ImportModuleOptions {
}

export async function importModule(options: ImportModuleOptions) {
$.mainModule = options.mainModule;
if (options.args) {
$.args = options.args;
}
const mainModule = options.mainModule;
Object.defineProperty($, "mainModule", {
get: () => mainModule,
});

const args = options.args ? [...options.args] : [];
Object.defineProperty($, "args", {
get: () => args,
});

if (typeof options.verbose !== "undefined") {
$.verbose = options.verbose;
}

Object.defineProperty($, "startTime", {
get: (): number => startTime,
});

await import($.mainModule);

if ($.verbose) {
console.log($.bold("time: %ss"), Math.round($.time) / 1000);
}
Expand Down
2 changes: 0 additions & 2 deletions src/cli/lib/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ export interface SpawnWorkerOptions extends Omit<BootstrapOptions, "base64"> {

export function spawnWorker({
args,
startTime,
mainModule,
perms,
verbose,
}: SpawnWorkerOptions): void {
new Worker(
bootstrapModule({
args,
startTime,
mainModule,
verbose,
base64: true,
Expand Down
3 changes: 1 addition & 2 deletions src/cli/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { VERSION } from "../../version.ts";
import { $, path } from "../runtime/mod.ts";
import { path } from "../runtime/mod.ts";
import { bundleCommand } from "./bundle.ts";
import { compileCommand } from "./compile.ts";
import {
Expand Down Expand Up @@ -107,7 +107,6 @@ export function dzx() {
mainModule,
args,
verbose,
startTime: $.startTime,
});
} else {
await importModule({ mainModule, args, verbose });
Expand Down
13 changes: 5 additions & 8 deletions src/runtime/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ export { ProcessError } from "./process_error.ts";
export { ProcessOutput } from "./process_output.ts";

export type $ = typeof exec & typeof colors & {
shell: string;
prefix: string;
mainModule: string;
get mainModule(): string;
get args(): Array<string>;
get verbose(): number;
set verbose(value: boolean | number);
get startTime(): number;
shell: string;
prefix: string;
stdout: NonNullable<Deno.RunOptions["stdout"]>;
stderr: NonNullable<Deno.RunOptions["stderr"]>;
args: Array<string>;
quote: typeof shq;
throwErrors: boolean;
startTime: number;
time: number;
};

Expand All @@ -43,13 +43,10 @@ Object.setPrototypeOf($, Object.getPrototypeOf(colors));
$._stack = [];
$.shell = "/bin/bash";
$.prefix = "set -euo pipefail;";
$.mainModule = Deno.mainModule;
$.stdout = "piped";
$.stderr = "piped";
$.args = [];
$.quote = shq;
$.throwErrors = false;
$.startTime = Date.now();

let _verbose = 1;
Object.defineProperty($, "verbose", {
Expand Down

0 comments on commit bcb02fd

Please sign in to comment.