Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(runtime): move std modules to a separate sub-folder #91

Merged
merged 1 commit into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions dzx.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference path="./types.d.ts" />

import { dzx } from "./src/cli/mod.ts";

if (import.meta.main) {
Expand Down
4 changes: 2 additions & 2 deletions globals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <reference path="./types.d.ts" />
/// <reference path="./src/cli/globals.d.ts" />

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

initGlobals();
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./src/runtime/mod.ts";
export * from "./src/std/mod.ts";
1 change: 1 addition & 0 deletions shell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./src/runtime/mod.ts";
30 changes: 0 additions & 30 deletions src/_utils.ts

This file was deleted.

14 changes: 10 additions & 4 deletions src/cli/bundle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Command, copy, DenoBundleOptions, ValidationError } from "./deps.ts";
import { createError } from "../_utils.ts";
import { path } from "../runtime/mod.ts";
import { createError } from "../runtime/lib/error.ts";
import {
basename,
Command,
copy,
DenoBundleOptions,
join,
ValidationError,
} from "./deps.ts";
import { bootstrapModule } from "./lib/bootstrap.ts";

export function bundleCommand() {
Expand Down Expand Up @@ -55,7 +61,7 @@ export async function preBundle(
});

const tmpDir = await Deno.makeTempDir();
const tmpFile = path.join(tmpDir, path.basename(script));
const tmpFile = join(tmpDir, basename(script));
const data = new TextEncoder().encode(bundleContent);
await Deno.writeFile(tmpFile, data, { create: true });

Expand Down
2 changes: 1 addition & 1 deletion src/cli/compile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createError } from "../runtime/lib/error.ts";
import { preBundle } from "./bundle.ts";
import { createError } from "../_utils.ts";
import { Command, copy, parse, ValidationError } from "./deps.ts";

export function compileCommand() {
Expand Down
11 changes: 10 additions & 1 deletion src/cli/deps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
/** std */
export { copy } from "https://deno.land/std@0.140.0/streams/conversion.ts";
export {
copy,
readAll,
} from "https://deno.land/std@0.140.0/streams/conversion.ts";
export { parse } from "https://deno.land/std@0.130.0/flags/mod.ts";
export {
basename,
extname,
isAbsolute,
join,
} from "https://deno.land/std@0.130.0/path/mod.ts";

/** 3rd party */
export { Command } from "https://deno.land/x/cliffy@v0.24.2/command/command.ts";
Expand Down
18 changes: 9 additions & 9 deletions types.d.ts → src/cli/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,42 @@ import type {
$ as _$,
$e as _$e,
$o as _$o,
async as _async,
cd as _cd,
flags as _flags,
fs as _fs,
io as _io,
log as _log,
path as _path,
quote as _quote,
streams as _streams,
} from "./src/runtime/mod.ts";
} from "../runtime/mod.ts";

import type {
Args as _Args,
async as _async,
CopyOptions as _CopyOptions,
DebouncedFunction as _DebouncedFunction,
Deferred as _Deferred,
DelayOptions as _DelayOptions,
ExpandGlobOptions as _ExpandGlobOptions,
flags as _flags,
FormatInputPathObject as _FormatInputPathObject,
FormatterFunction as _FormatterFunction,
fs as _fs,
GlobOptions as _GlobOptions,
GlobToRegExpOptions as _GlobToRegExpOptions,
HandlerOptions as _HandlerOptions,
io as _io,
IOReadableStreamFromReaderOptions as _IOReadableStreamFromReaderOptions,
IOWritableStreamFromWriterOptions as _IOWritableStreamFromWriterOptions,
LevelName as _LevelName,
log as _log,
LogConfig as _LogConfig,
LogMode as _LogMode,
ParsedPath as _ParsedPath,
ParseOptions as _ParseOptions,
path as _path,
ReadableStreamFromReaderOptions as _ReadableStreamFromReaderOptions,
ReadLineResult as _ReadLineResult,
streams as _streams,
WalkEntry as _WalkEntry,
WalkOptions as _WalkOptions,
WritableStreamFromWriterOptions as _WritableStreamFromWriterOptions,
} from "./src/runtime/deps.ts";
} from "../std/mod.ts";

declare global {
/**
Expand Down
7 changes: 7 additions & 0 deletions src/cli/globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as std from "../std/mod.ts";
import * as runtime from "../runtime/mod.ts";

export function initGlobals() {
Object.assign(self, std);
Object.assign(self, runtime);
}
4 changes: 2 additions & 2 deletions src/cli/lib/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { initGlobals } from "../../runtime/globals.ts";
import { initGlobals } from "../globals.ts";
import { $ } from "../../runtime/mod.ts";

const startTime = Date.now();
Expand Down Expand Up @@ -66,7 +66,7 @@ export type BootstrapModuleOptions = BootstrapOptions;
export function bootstrapScript(code: string) {
return base64Module(`
/// <reference path="${new URL(
"../../../types.d.ts",
"../globals.d.ts",
import.meta.url,
)}" />
{\n${code}\n}
Expand Down
6 changes: 3 additions & 3 deletions src/cli/lib/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { tokens } from "https://deno.land/x/rusty_markdown@v0.4.1/mod.ts";
import { addProtocol } from "../../_utils.ts";
import { tokens } from "../deps.ts";
import { addProtocol } from "./url.ts";

export async function getMarkdownModule(url: string) {
let mdContent;
Expand Down Expand Up @@ -40,7 +40,7 @@ export async function getMarkdownModule(url: string) {
return `data:application/typescript,${
encodeURIComponent(`
/// <reference path="${new URL(
"../../../types.d.ts",
"../globals.d.ts",
import.meta.url,
)}" />
{\n${code}\n}
Expand Down
4 changes: 2 additions & 2 deletions src/cli/lib/stream.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ValidationError } from "../deps.ts";
import { readAll, ValidationError } from "../deps.ts";
import { bootstrapScript } from "./bootstrap.ts";

export async function getModuleFromStdin(): Promise<string> {
const code = new TextDecoder().decode(await streams.readAll(Deno.stdin));
const code = new TextDecoder().decode(await readAll(Deno.stdin));
if (!code) {
throw new ValidationError(`Failed to read from stdin.`, { exitCode: 2 });
}
Expand Down
15 changes: 15 additions & 0 deletions src/cli/lib/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { isAbsolute, join } from "../deps.ts";

export function addProtocol(script: string): string {
const hasProtocol: boolean = script.startsWith("http://") ||
script.startsWith("https://") ||
script.startsWith("file://");

if (!hasProtocol) {
const filePath = isAbsolute(script) ? script : join(Deno.cwd(), script);

script = "file://" + filePath;
}

return script;
}
7 changes: 3 additions & 4 deletions src/cli/mod.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { VERSION } from "../../version.ts";
import { path } from "../runtime/mod.ts";
import { bundleCommand } from "./bundle.ts";
import { compileCommand } from "./compile.ts";
import { Command, DenoLandProvider, UpgradeCommand } from "./deps.ts";
import { addProtocol } from "../_utils.ts";
import { Command, DenoLandProvider, extname, UpgradeCommand } from "./deps.ts";
import { evalCommand } from "./eval.ts";
import { importModule } from "./lib/bootstrap.ts";
import { getModuleFromStdin } from "./lib/stream.ts";
import { getMarkdownModule } from "./lib/markdown.ts";
import { spawnWorker } from "./lib/worker.ts";
import { repl, replCommand } from "./repl.ts";
import { addProtocol } from "./lib/url.ts";

export function dzx() {
return new Command()
Expand Down Expand Up @@ -103,7 +102,7 @@ export function dzx() {

let mainModule: string;
if (script) {
const scriptExt = path.extname(script);
const scriptExt = extname(script);
mainModule = [".md", ".markdown"].includes(scriptExt)
? await getMarkdownModule(script)
: addProtocol(script);
Expand Down
2 changes: 1 addition & 1 deletion src/cli/repl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { VERSION } from "../../version.ts";
import { createError } from "../_utils.ts";
import { createError } from "../runtime/lib/error.ts";
import { Command } from "./deps.ts";
import { bootstrap } from "./lib/bootstrap.ts";
import { generateFlags } from "./lib/flags.ts";
Expand Down
20 changes: 9 additions & 11 deletions src/runtime/cd.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { createError, DzxErrorOptions } from "../_utils.ts";
import { colors, path } from "./deps.ts";
import { bold, brightBlue, brightYellow, join, sep, white } from "./deps.ts";
import { createError, DzxErrorOptions } from "./lib/error.ts";
import { $ } from "./shell.ts";

const cwd = Deno.cwd();

export function cd(dir: string) {
if ($.verbose) {
console.log($.brightBlue("$ %s"), `cd ${dir}`);
console.log(brightBlue("$ %s"), `cd ${dir}`);
}
let realPath = dir;

try {
if (dir[0] === "~") {
realPath = path.join(homedir() as string, dir.slice(1));
} else if (dir[0] !== path.sep) {
realPath = path.join(cwd, dir);
realPath = join(homedir() as string, dir.slice(1));
} else if (dir[0] !== sep) {
realPath = join(cwd, dir);
}

Deno.chdir(realPath);
Expand All @@ -35,11 +35,9 @@ export function cd(dir: string) {

function fmtError(message: string, opts: DzxErrorOptions) {
return createError(
`cd: ${message}: ${dir}\n${
colors.bold(
colors.white(`Directory:`),
)
} ${colors.brightYellow(realPath)}`,
`cd: ${message}: ${dir}\n${bold(white(`Directory:`))} ${
brightYellow(realPath)
}`,
opts,
);
}
Expand Down
53 changes: 10 additions & 43 deletions src/runtime/deps.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,17 @@
export * as async from "https://deno.land/std@0.140.0/async/mod.ts";
export {
type DebouncedFunction,
type Deferred,
deferred,
type DelayOptions,
} from "https://deno.land/std@0.140.0/async/mod.ts";
export * as path from "https://deno.land/std@0.140.0/path/mod.ts";
export type {
FormatInputPathObject,
GlobOptions,
GlobToRegExpOptions,
ParsedPath,
} from "https://deno.land/std@0.140.0/path/mod.ts";
export * as io from "https://deno.land/std@0.140.0/io/mod.ts";
export { BufReader } from "https://deno.land/std@0.140.0/io/buffer.ts";
export type {
ReadableStreamFromReaderOptions as IOReadableStreamFromReaderOptions,
ReadLineResult,
WritableStreamFromWriterOptions as IOWritableStreamFromWriterOptions,
} from "https://deno.land/std@0.140.0/io/mod.ts";
export * as streams from "https://deno.land/std@0.140.0/streams/mod.ts";
export { writeAll } from "https://deno.land/std@0.140.0/streams/mod.ts";
export type {
ReadableStreamFromReaderOptions,
WritableStreamFromWriterOptions,
} from "https://deno.land/std@0.140.0/streams/mod.ts";
export * as fs from "https://deno.land/std@0.140.0/fs/mod.ts";
export type {
CopyOptions,
ExpandGlobOptions,
WalkEntry,
WalkOptions,
} from "https://deno.land/std@0.140.0/fs/mod.ts";
export * as log from "https://deno.land/std@0.140.0/log/mod.ts";
export type {
FormatterFunction,
HandlerOptions,
LevelName,
LogConfig,
LogMode,
} from "https://deno.land/std@0.140.0/log/mod.ts";
export * as flags from "https://deno.land/std@0.140.0/flags/mod.ts";
export type {
Args,
ParseOptions,
} from "https://deno.land/std@0.140.0/flags/mod.ts";
export { concat } from "https://deno.land/std@0.140.0/bytes/mod.ts";
export {
bold,
brightBlue,
brightYellow,
red,
white,
} from "https://deno.land/std@0.140.0/fmt/colors.ts";
export { BufReader } from "https://deno.land/std@0.140.0/io/buffer.ts";
export { join, sep } from "https://deno.land/std@0.140.0/path/mod.ts";
export { colors } from "https://deno.land/x/cliffy@v0.24.2/ansi/colors.ts";
export { default as shq } from "https://esm.sh/shq@1.0.2";
export { concat } from "https://deno.land/std@0.140.0/bytes/mod.ts";
5 changes: 0 additions & 5 deletions src/runtime/globals.ts

This file was deleted.

19 changes: 19 additions & 0 deletions src/runtime/lib/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { red } from "../deps.ts";

export interface DzxErrorOptions {
// deno-lint-ignore ban-types
context?: Function;
}

export function createError(
message: string | Error,
{ context }: DzxErrorOptions = {},
): Error {
const err = message instanceof Error ? message : new Error(red(message));

if (context) {
Error.captureStackTrace(err, context);
}

return err;
}
1 change: 0 additions & 1 deletion src/runtime/mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { async, flags, fs, io, log, path, streams } from "./deps.ts";
export { $, $e, $o } from "./shell.ts";
export { cd } from "./cd.ts";
export { quote } from "./quote.ts";
Expand Down
Loading