From ff9910e52acd0c1c4778c4709932e1badb939bf0 Mon Sep 17 00:00:00 2001 From: Benjamin Fischer <61995275+c4spar@users.noreply.github.com> Date: Mon, 11 Jul 2022 20:09:36 +0200 Subject: [PATCH] refactor(runtime): don't inject globals by default (#72) --- globals.ts | 5 +++ src/_utils.ts | 5 ++- src/cli/lib/bootstrap.ts | 5 ++- src/runtime/cd.ts | 3 +- src/runtime/exec.ts | 6 --- src/runtime/globals.ts | 5 +++ src/runtime/mod.ts | 80 ++-------------------------------------- src/runtime/process.ts | 7 +++- src/runtime/quote.ts | 4 +- src/runtime/shell.ts | 44 ++++++++++++++++++++++ 10 files changed, 74 insertions(+), 90 deletions(-) create mode 100644 globals.ts create mode 100644 src/runtime/globals.ts create mode 100644 src/runtime/shell.ts diff --git a/globals.ts b/globals.ts new file mode 100644 index 0000000..58cff36 --- /dev/null +++ b/globals.ts @@ -0,0 +1,5 @@ +/// + +import { initGlobals } from "./src/runtime/globals.ts"; + +initGlobals(); diff --git a/src/_utils.ts b/src/_utils.ts index 388ec40..903ccaa 100644 --- a/src/_utils.ts +++ b/src/_utils.ts @@ -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) { @@ -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 { diff --git a/src/cli/lib/bootstrap.ts b/src/cli/lib/bootstrap.ts index 8203d75..78d17bb 100644 --- a/src/cli/lib/bootstrap.ts +++ b/src/cli/lib/bootstrap.ts @@ -1,3 +1,4 @@ +import { initGlobals } from "../../runtime/globals.ts"; import { $ } from "../../runtime/mod.ts"; const startTime = Date.now(); @@ -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) : "", @@ -89,6 +90,8 @@ export interface ImportModuleOptions { } export async function importModule(options: ImportModuleOptions) { + initGlobals(); + const mainModule = options.mainModule; Object.defineProperty($, "mainModule", { get: () => mainModule, diff --git a/src/runtime/cd.ts b/src/runtime/cd.ts index 2137ac3..895f813 100644 --- a/src/runtime/cd.ts +++ b/src/runtime/cd.ts @@ -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(); diff --git a/src/runtime/exec.ts b/src/runtime/exec.ts index 67f2671..71ba6e9 100644 --- a/src/runtime/exec.ts +++ b/src/runtime/exec.ts @@ -1,5 +1,3 @@ -/// - import { Process } from "./process.ts"; import { ProcessError } from "./process_error.ts"; import { ProcessOutput } from "./process_output.ts"; @@ -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, }); diff --git a/src/runtime/globals.ts b/src/runtime/globals.ts new file mode 100644 index 0000000..3ac59e8 --- /dev/null +++ b/src/runtime/globals.ts @@ -0,0 +1,5 @@ +import * as globals from "./mod.ts"; + +export function initGlobals() { + Object.assign(self, globals); +} diff --git a/src/runtime/mod.ts b/src/runtime/mod.ts index 91d047d..c2abeba 100644 --- a/src/runtime/mod.ts +++ b/src/runtime/mod.ts @@ -1,78 +1,6 @@ -/// - -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; - get verbose(): number; - set verbose(value: boolean | number); - get startTime(): number; - shell: string; - prefix: string; - stdout: NonNullable; - stderr: NonNullable; - 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 }; diff --git a/src/runtime/process.ts b/src/runtime/process.ts index 8a60c4e..d715002 100644 --- a/src/runtime/process.ts +++ b/src/runtime/process.ts @@ -1,9 +1,8 @@ -/// - 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 @@ -37,6 +36,10 @@ export class Process implements Promise { retries: 0, }); Error.captureStackTrace(this.#baseError, errorContext); + + if ($.verbose) { + console.log($.brightBlue("$ %s"), cmd); + } } get #process(): Deno.Process { diff --git a/src/runtime/quote.ts b/src/runtime/quote.ts index 1beb8d4..0b69baa 100644 --- a/src/runtime/quote.ts +++ b/src/runtime/quote.ts @@ -1,4 +1,4 @@ -/// +import { shq } from "./deps.ts"; export function quote( pieces: TemplateStringsArray, @@ -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]; } diff --git a/src/runtime/shell.ts b/src/runtime/shell.ts new file mode 100644 index 0000000..d6f1570 --- /dev/null +++ b/src/runtime/shell.ts @@ -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; + get verbose(): number; + set verbose(value: boolean | number); + get startTime(): number; + shell: string; + prefix: string; + stdin: NonNullable; + stdout: NonNullable; + stderr: NonNullable; + 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, +});