Skip to content

Commit ff9910e

Browse files
authored
refactor(runtime): don't inject globals by default (#72)
1 parent 3be3584 commit ff9910e

File tree

10 files changed

+74
-90
lines changed

10 files changed

+74
-90
lines changed

globals.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// <reference path="./types.d.ts" />
2+
3+
import { initGlobals } from "./src/runtime/globals.ts";
4+
5+
initGlobals();

src/_utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { path } from "./runtime/deps.ts";
1+
import { colors, path } from "./runtime/deps.ts";
2+
import { $ } from "./runtime/shell.ts";
23

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

1314
function getErrorMessage(message: string) {
14-
return $.red(`${$.bold("error:")} ${message}`);
15+
return colors.red(`${colors.bold("error:")} ${message}`);
1516
}
1617

1718
export function addProtocol(script: string): string {

src/cli/lib/bootstrap.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { initGlobals } from "../../runtime/globals.ts";
12
import { $ } from "../../runtime/mod.ts";
23

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

3738
export function bootstrap(options: BootstrapOptions): string {
3839
const code = [
39-
`import "${new URL("../../../mod.ts", import.meta.url)}";`,
40+
`import "${new URL("../../../globals.ts", import.meta.url)}";`,
4041
"{",
4142
stringifyStartTime(startTime),
4243
options.mainModule ? stringifyMainModule(options.mainModule) : "",
@@ -89,6 +90,8 @@ export interface ImportModuleOptions {
8990
}
9091

9192
export async function importModule(options: ImportModuleOptions) {
93+
initGlobals();
94+
9295
const mainModule = options.mainModule;
9396
Object.defineProperty($, "mainModule", {
9497
get: () => mainModule,

src/runtime/cd.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { error } from "../_utils.ts";
2-
import { path } from "./mod.ts";
2+
import { path } from "./deps.ts";
3+
import { $ } from "./shell.ts";
34

45
const cwd = Deno.cwd();
56

src/runtime/exec.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/// <reference path="../../types.d.ts" />
2-
31
import { Process } from "./process.ts";
42
import { ProcessError } from "./process_error.ts";
53
import { ProcessOutput } from "./process_output.ts";
@@ -16,10 +14,6 @@ export function exec(
1614
) => (a instanceof ProcessOutput ? a.stdout.replace(/\n$/, "") : a)),
1715
);
1816

19-
if ($.verbose) {
20-
console.log($.brightBlue("$ %s"), cmd);
21-
}
22-
2317
return new Process(cmd, {
2418
errorContext: exec,
2519
});

src/runtime/globals.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as globals from "./mod.ts";
2+
3+
export function initGlobals() {
4+
Object.assign(self, globals);
5+
}

src/runtime/mod.ts

Lines changed: 4 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,6 @@
1-
/// <reference path="../../types.d.ts" />
2-
3-
import {
4-
async,
5-
colors,
6-
flags,
7-
fs,
8-
io,
9-
log,
10-
path,
11-
shq,
12-
streams,
13-
} from "./deps.ts";
14-
import { cd } from "./cd.ts";
15-
import { exec, statusOnly, stderrOnly, stdoutOnly } from "./exec.ts";
16-
import { quote } from "./quote.ts";
17-
1+
export { async, flags, fs, io, log, path, streams } from "./deps.ts";
2+
export { $, $e, $o, $s } from "./shell.ts";
3+
export { cd } from "./cd.ts";
4+
export { quote } from "./quote.ts";
185
export { ProcessError } from "./process_error.ts";
196
export { ProcessOutput } from "./process_output.ts";
20-
21-
export type $ = typeof exec & typeof colors & {
22-
get mainModule(): string;
23-
get args(): Array<string>;
24-
get verbose(): number;
25-
set verbose(value: boolean | number);
26-
get startTime(): number;
27-
shell: string;
28-
prefix: string;
29-
stdout: NonNullable<Deno.RunOptions["stdout"]>;
30-
stderr: NonNullable<Deno.RunOptions["stderr"]>;
31-
quote: typeof shq;
32-
throwErrors: boolean;
33-
time: number;
34-
};
35-
36-
export const $: $ = exec as $;
37-
export const $s: typeof statusOnly = statusOnly;
38-
export const $o: typeof stdoutOnly = stdoutOnly;
39-
export const $e: typeof stderrOnly = stderrOnly;
40-
41-
Object.setPrototypeOf($, Object.getPrototypeOf(colors));
42-
43-
$._stack = [];
44-
$.shell = "/bin/bash";
45-
$.prefix = "set -euo pipefail;";
46-
$.stdout = "piped";
47-
$.stderr = "piped";
48-
$.quote = shq;
49-
$.throwErrors = false;
50-
51-
let _verbose = 1;
52-
Object.defineProperty($, "verbose", {
53-
get: (): number => _verbose,
54-
set: (verbose: boolean | number) => _verbose = Number(verbose),
55-
});
56-
57-
Object.defineProperty($, "time", {
58-
get: () => Date.now() - $.startTime,
59-
});
60-
61-
// dzx
62-
self.$ = $;
63-
self.$s = $s;
64-
self.$o = $o;
65-
self.$e = $e;
66-
self.cd = cd;
67-
self.quote = quote;
68-
69-
// x
70-
self.async = async;
71-
self.path = path;
72-
self.io = io;
73-
self.streams = streams;
74-
self.fs = fs;
75-
self.log = log;
76-
self.flags = flags;
77-
78-
export { async, cd, flags, fs, io, log, path, quote, streams };

src/runtime/process.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
/// <reference path="../../types.d.ts" />
2-
31
import { Deferred, deferred } from "./deps.ts";
42
import { readLines } from "./lib/readline.ts";
53
import { ProcessError } from "./process_error.ts";
64
import { ProcessOutput } from "./process_output.ts";
5+
import { $ } from "./shell.ts";
76

87
export interface ProcessOptions {
98
// deno-lint-ignore ban-types
@@ -37,6 +36,10 @@ export class Process implements Promise<ProcessOutput> {
3736
retries: 0,
3837
});
3938
Error.captureStackTrace(this.#baseError, errorContext);
39+
40+
if ($.verbose) {
41+
console.log($.brightBlue("$ %s"), cmd);
42+
}
4043
}
4144

4245
get #process(): Deno.Process {

src/runtime/quote.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// <reference path="../../types.d.ts" />
1+
import { shq } from "./deps.ts";
22

33
export function quote(
44
pieces: TemplateStringsArray,
@@ -8,7 +8,7 @@ export function quote(
88
let i = 0;
99
for (; i < args.length; i++) {
1010
if (typeof args[i] === "string") {
11-
parsed += $.quote(args[i] as string) + pieces[i + 1];
11+
parsed += shq(args[i] as string) + pieces[i + 1];
1212
} else {
1313
parsed += args[i] + pieces[i + 1];
1414
}

src/runtime/shell.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { colors, shq } from "./deps.ts";
2+
import { exec, statusOnly, stderrOnly, stdoutOnly } from "./exec.ts";
3+
4+
export type $ = typeof exec & typeof colors & {
5+
get mainModule(): string;
6+
get args(): Array<string>;
7+
get verbose(): number;
8+
set verbose(value: boolean | number);
9+
get startTime(): number;
10+
shell: string;
11+
prefix: string;
12+
stdin: NonNullable<Deno.RunOptions["stdin"]>;
13+
stdout: NonNullable<Deno.RunOptions["stdout"]>;
14+
stderr: NonNullable<Deno.RunOptions["stderr"]>;
15+
quote: typeof shq;
16+
throwErrors: boolean;
17+
time: number;
18+
};
19+
20+
export const $: $ = exec as $;
21+
export const $s: typeof statusOnly = statusOnly;
22+
export const $o: typeof stdoutOnly = stdoutOnly;
23+
export const $e: typeof stderrOnly = stderrOnly;
24+
25+
Object.setPrototypeOf($, Object.getPrototypeOf(colors));
26+
27+
$._stack = [];
28+
$.shell = "/bin/bash";
29+
$.prefix = "set -euo pipefail;";
30+
$.stdin = "inherit";
31+
$.stdout = "piped";
32+
$.stderr = "piped";
33+
$.quote = shq;
34+
$.throwErrors = false;
35+
36+
let _verbose = 1;
37+
Object.defineProperty($, "verbose", {
38+
get: (): number => _verbose,
39+
set: (verbose: boolean | number) => _verbose = Number(verbose),
40+
});
41+
42+
Object.defineProperty($, "time", {
43+
get: () => Date.now() - $.startTime,
44+
});

0 commit comments

Comments
 (0)