Skip to content

Commit

Permalink
refactor(runtime): don't inject globals by default (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar authored Jul 11, 2022
1 parent 3be3584 commit ff9910e
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 90 deletions.
5 changes: 5 additions & 0 deletions globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference path="./types.d.ts" />

import { initGlobals } from "./src/runtime/globals.ts";

initGlobals();
5 changes: 3 additions & 2 deletions src/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { path } from "./runtime/deps.ts";
import { colors, path } from "./runtime/deps.ts";
import { $ } from "./runtime/shell.ts";

export function error(message: string | Error, exitCode = 1): Error {
if ($.throwErrors) {
Expand All @@ -11,7 +12,7 @@ export function error(message: string | Error, exitCode = 1): Error {
}

function getErrorMessage(message: string) {
return $.red(`${$.bold("error:")} ${message}`);
return colors.red(`${colors.bold("error:")} ${message}`);
}

export function addProtocol(script: string): string {
Expand Down
5 changes: 4 additions & 1 deletion src/cli/lib/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { initGlobals } from "../../runtime/globals.ts";
import { $ } from "../../runtime/mod.ts";

const startTime = Date.now();
Expand Down Expand Up @@ -36,7 +37,7 @@ export function stringifyStartTime(startTime: number) {

export function bootstrap(options: BootstrapOptions): string {
const code = [
`import "${new URL("../../../mod.ts", import.meta.url)}";`,
`import "${new URL("../../../globals.ts", import.meta.url)}";`,
"{",
stringifyStartTime(startTime),
options.mainModule ? stringifyMainModule(options.mainModule) : "",
Expand Down Expand Up @@ -89,6 +90,8 @@ export interface ImportModuleOptions {
}

export async function importModule(options: ImportModuleOptions) {
initGlobals();

const mainModule = options.mainModule;
Object.defineProperty($, "mainModule", {
get: () => mainModule,
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/cd.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { error } from "../_utils.ts";
import { path } from "./mod.ts";
import { path } from "./deps.ts";
import { $ } from "./shell.ts";

const cwd = Deno.cwd();

Expand Down
6 changes: 0 additions & 6 deletions src/runtime/exec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference path="../../types.d.ts" />

import { Process } from "./process.ts";
import { ProcessError } from "./process_error.ts";
import { ProcessOutput } from "./process_output.ts";
Expand All @@ -16,10 +14,6 @@ export function exec(
) => (a instanceof ProcessOutput ? a.stdout.replace(/\n$/, "") : a)),
);

if ($.verbose) {
console.log($.brightBlue("$ %s"), cmd);
}

return new Process(cmd, {
errorContext: exec,
});
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as globals from "./mod.ts";

export function initGlobals() {
Object.assign(self, globals);
}
80 changes: 4 additions & 76 deletions src/runtime/mod.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,6 @@
/// <reference path="../../types.d.ts" />

import {
async,
colors,
flags,
fs,
io,
log,
path,
shq,
streams,
} from "./deps.ts";
import { cd } from "./cd.ts";
import { exec, statusOnly, stderrOnly, stdoutOnly } from "./exec.ts";
import { quote } from "./quote.ts";

export { async, flags, fs, io, log, path, streams } from "./deps.ts";
export { $, $e, $o, $s } from "./shell.ts";
export { cd } from "./cd.ts";
export { quote } from "./quote.ts";
export { ProcessError } from "./process_error.ts";
export { ProcessOutput } from "./process_output.ts";

export type $ = typeof exec & typeof colors & {
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"]>;
quote: typeof shq;
throwErrors: boolean;
time: number;
};

export const $: $ = exec as $;
export const $s: typeof statusOnly = statusOnly;
export const $o: typeof stdoutOnly = stdoutOnly;
export const $e: typeof stderrOnly = stderrOnly;

Object.setPrototypeOf($, Object.getPrototypeOf(colors));

$._stack = [];
$.shell = "/bin/bash";
$.prefix = "set -euo pipefail;";
$.stdout = "piped";
$.stderr = "piped";
$.quote = shq;
$.throwErrors = false;

let _verbose = 1;
Object.defineProperty($, "verbose", {
get: (): number => _verbose,
set: (verbose: boolean | number) => _verbose = Number(verbose),
});

Object.defineProperty($, "time", {
get: () => Date.now() - $.startTime,
});

// dzx
self.$ = $;
self.$s = $s;
self.$o = $o;
self.$e = $e;
self.cd = cd;
self.quote = quote;

// x
self.async = async;
self.path = path;
self.io = io;
self.streams = streams;
self.fs = fs;
self.log = log;
self.flags = flags;

export { async, cd, flags, fs, io, log, path, quote, streams };
7 changes: 5 additions & 2 deletions src/runtime/process.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/// <reference path="../../types.d.ts" />

import { Deferred, deferred } from "./deps.ts";
import { readLines } from "./lib/readline.ts";
import { ProcessError } from "./process_error.ts";
import { ProcessOutput } from "./process_output.ts";
import { $ } from "./shell.ts";

export interface ProcessOptions {
// deno-lint-ignore ban-types
Expand Down Expand Up @@ -37,6 +36,10 @@ export class Process implements Promise<ProcessOutput> {
retries: 0,
});
Error.captureStackTrace(this.#baseError, errorContext);

if ($.verbose) {
console.log($.brightBlue("$ %s"), cmd);
}
}

get #process(): Deno.Process {
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/quote.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference path="../../types.d.ts" />
import { shq } from "./deps.ts";

export function quote(
pieces: TemplateStringsArray,
Expand All @@ -8,7 +8,7 @@ export function quote(
let i = 0;
for (; i < args.length; i++) {
if (typeof args[i] === "string") {
parsed += $.quote(args[i] as string) + pieces[i + 1];
parsed += shq(args[i] as string) + pieces[i + 1];
} else {
parsed += args[i] + pieces[i + 1];
}
Expand Down
44 changes: 44 additions & 0 deletions src/runtime/shell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { colors, shq } from "./deps.ts";
import { exec, statusOnly, stderrOnly, stdoutOnly } from "./exec.ts";

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

export const $: $ = exec as $;
export const $s: typeof statusOnly = statusOnly;
export const $o: typeof stdoutOnly = stdoutOnly;
export const $e: typeof stderrOnly = stderrOnly;

Object.setPrototypeOf($, Object.getPrototypeOf(colors));

$._stack = [];
$.shell = "/bin/bash";
$.prefix = "set -euo pipefail;";
$.stdin = "inherit";
$.stdout = "piped";
$.stderr = "piped";
$.quote = shq;
$.throwErrors = false;

let _verbose = 1;
Object.defineProperty($, "verbose", {
get: (): number => _verbose,
set: (verbose: boolean | number) => _verbose = Number(verbose),
});

Object.defineProperty($, "time", {
get: () => Date.now() - $.startTime,
});

0 comments on commit ff9910e

Please sign in to comment.