Skip to content

Commit

Permalink
refactor: give each module a namespace and add log module
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed May 11, 2021
1 parent bb4830d commit ee95ee5
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 248 deletions.
37 changes: 6 additions & 31 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,7 @@
export {
basename,
dirname,
extname,
fromFileUrl,
isAbsolute,
join,
normalize,
relative,
resolve,
SEP,
SEP_PATTERN,
toFileUrl,
toNamespacedPath,
} from "https://deno.land/std@0.93.0/path/mod.ts";
export * as path from "https://deno.land/std@0.93.0/path/mod.ts";
export * as io from "https://deno.land/std@0.93.0/io/mod.ts";
export * as fs from "https://deno.land/std@0.93.0/fs/mod.ts";
export * as log from "https://deno.land/std@0.93.0/log/mod.ts";
export * as flags from "https://deno.land/std@0.93.0/flags/mod.ts";
export { colors } from "https://deno.land/x/cliffy@v0.18.2/ansi/colors.ts";
export {
iter,
iterSync,
readAll,
readAllSync,
writeAll,
writeAllSync,
} from "https://deno.land/std@0.93.0/io/util.ts";
export { Buffer } from "https://deno.land/std@0.93.0/io/buffer.ts";
export { readLines } from "https://deno.land/std@0.93.0/io/bufio.ts";
export { default as escapeStr } from "https://esm.sh/shq@1.0.2";
export { parse as parseFlags } from "https://deno.land/std@0.93.0/flags/mod.ts";
export type {
ArgParsingOptions,
Args,
} from "https://deno.land/std@0.93.0/flags/mod.ts";
export { default as shq } from "https://esm.sh/shq@1.0.2";
102 changes: 24 additions & 78 deletions dzx.ts
Original file line number Diff line number Diff line change
@@ -1,88 +1,34 @@
/// <reference path="./types.d.ts" />

import { $, io, path } from "./mod.ts";
import { error } from "./src/_utils.ts";
import {
$,
basename,
Buffer,
cd,
dirname,
extname,
fromFileUrl,
isAbsolute,
iter,
iterSync,
join,
normalize,
parseFlags,
quote,
readAll,
readAllSync,
readLines,
relative,
resolve,
toFileUrl,
toNamespacedPath,
writeAll,
writeAllSync,
} from "./mod.ts";

// dzx
window.$ = $;
window.cd = cd;

// std/io
window.Buffer = Buffer;
window.iter = iter;
window.iterSync = iterSync;
window.quote = quote;
window.readAll = readAll;
window.readAllSync = readAllSync;
window.readLines = readLines;
window.writeAll = writeAll;
window.writeAllSync = writeAllSync;

// std/path
window.basename = basename;
window.dirname = dirname;
window.extname = extname;
window.fromFileUrl = fromFileUrl;
window.isAbsolute = isAbsolute;
window.join = join;
window.normalize = normalize;
window.relative = relative;
window.resolve = resolve;
window.toFileUrl = toFileUrl;
window.toNamespacedPath = toNamespacedPath;

window.parseFlags = parseFlags;

const script: string | undefined = Deno.args[0];

try {
if (!script) {
if (!Deno.isatty(Deno.stdin.rid)) {
const data = new TextDecoder().decode(await readAll(Deno.stdin));
if (data) {
await import(
`data:application/typescript,${encodeURIComponent(data)}`
);
if (import.meta.main) {
const script: string | undefined = Deno.args[0];
try {
if (!script) {
if (!Deno.isatty(Deno.stdin.rid)) {
const data = new TextDecoder().decode(await io.readAll(Deno.stdin));
if (data) {
await import(
`data:application/typescript,${encodeURIComponent(data)}`
);
} else {
error(`usage: dzx <script>`, 2);
}
} else {
error(`usage: dzx <script>`, 2);
error(`usage: dzx <script>`);
}
} else if (
script.startsWith("http://") || script.startsWith("https://") ||
script.startsWith("file://")
) {
await import(script);
} else if (script) {
await import("file://" + path.join($.cwd, script));
} else {
error(`usage: dzx <script>`);
}
} else if (
script.startsWith("http://") || script.startsWith("https://") ||
script.startsWith("file://")
) {
await import(script);
} else if (script) {
await import("file://" + join($.cwd, script));
} else {
error(`usage: dzx <script>`);
} catch (err) {
error(err);
}
} catch (err) {
error(err);
}
66 changes: 16 additions & 50 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,13 @@
import {
basename,
Buffer,
colors,
dirname,
escapeStr,
extname,
fromFileUrl,
isAbsolute,
iter,
iterSync,
join,
normalize,
readAll,
readAllSync,
readLines,
relative,
resolve,
toFileUrl,
toNamespacedPath,
writeAll,
writeAllSync,
} from "./deps.ts";
import { colors, flags, fs, io, log, path, shq } from "./deps.ts";
import { cd } from "./src/cd.ts";
import { exec } from "./src/exec.ts";
import { quote } from "./src/quote.ts";
import { parseFlags } from "./src/parse_flags.ts";

export type $ = typeof exec & typeof colors & {
verbose: boolean;
cwd: string;
shell: string;
quote: typeof escapeStr;
quote: typeof shq;
throwErors: boolean;
};

Expand All @@ -42,30 +19,19 @@ $._stack = [];
$.shell = "/bin/sh";
$.verbose = false;
$.cwd = Deno.cwd();
$.quote = escapeStr;
$.quote = shq;
$.throwErors = false;

export {
basename,
Buffer,
cd,
dirname,
extname,
fromFileUrl,
isAbsolute,
iter,
iterSync,
join,
normalize,
parseFlags,
quote,
readAll,
readAllSync,
readLines,
relative,
resolve,
toFileUrl,
toNamespacedPath,
writeAll,
writeAllSync,
};
// dzx
window.$ = $;
window.cd = cd;
window.quote = quote;

// x
window.path = path;
window.io = io;
window.fs = fs;
window.log = log;
window.flags = flags;

export { cd, flags, fs, io, log, path, quote };
3 changes: 1 addition & 2 deletions src/exec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { iter } from "../deps.ts";
import { ProcessError } from "./process_error.ts";
import { ProcessOutput } from "./process_output.ts";
import { quote } from "./quote.ts";
Expand Down Expand Up @@ -51,7 +50,7 @@ async function read(
reader: Deno.Reader,
...results: Array<Array<string>>
) {
for await (const chunk of iter(reader)) {
for await (const chunk of io.iter(reader)) {
const str = new TextDecoder().decode(chunk);
for (const result of results) {
result.push(str);
Expand Down
11 changes: 0 additions & 11 deletions src/parse_flags.ts

This file was deleted.

93 changes: 17 additions & 76 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -1,97 +1,38 @@
import type {
$,
cd as _cd,
parseFlags as _parseFlags,
flags as _flags,
fs as _fs,
io as _io,
log as _log,
path as _path,
quote as _quote,
} from "./mod.ts";
import type {
ArgParsingOptions as _ArgParsingOptions,
Args as _Args,
basename as _basename,
Buffer as _Buffer,
dirname as _dirname,
extname as _extname,
fromFileUrl as _fromFileUrl,
isAbsolute as _isAbsolute,
iter as _iter,
iterSync as _iterSync,
join as _join,
normalize as _normalize,
readAll as _readAll,
readAllSync as _readAllSync,
readLines as _readLines,
relative as _relative,
resolve as _resolve,
toFileUrl as _toFileUrl,
toNamespacedPath as _toNamespacedPath,
writeAll as _writeAll,
writeAllSync as _writeAllSync,
} from "./deps.ts";

declare global {
// dzx
const $: $;
const cd: typeof _cd;
const quote: typeof _quote;

// std/io
const Buffer: typeof _Buffer;
const iter: typeof _iter;
const iterSync: typeof _iterSync;
const readAll: typeof _readAll;
const readAllSync: typeof _readAllSync;
const readLines: typeof _readLines;
const writeAll: typeof _writeAll;
const writeAllSync: typeof _writeAllSync;

// std/path
const basename: typeof _basename;
const dirname: typeof _dirname;
const extname: typeof _extname;
const fromFileUrl: typeof _fromFileUrl;
const isAbsolute: typeof _isAbsolute;
const join: typeof _join;
const normalize: typeof _normalize;
const relative: typeof _relative;
const resolve: typeof _resolve;
const toFileUrl: typeof _toFileUrl;
const toNamespacedPath: typeof _toNamespacedPath;

// std/flags
const parseFlags: typeof _parseFlags;
type ArgParsingOptions = _ArgParsingOptions;
type Args = _Args;
// x
const path: typeof _path;
const io: typeof _io;
const fs: typeof _fs;
const log: typeof _log;
const flags: typeof _flags;

interface Window {
// dzx
$: $;
cd: typeof _cd;
quote: typeof _quote;

// std/io
Buffer: typeof _Buffer;
iter: typeof _iter;
iterSync: typeof _iterSync;
readAll: typeof _readAll;
readAllSync: typeof _readAllSync;
readLines: typeof _readLines;
writeAll: typeof _writeAll;
writeAllSync: typeof _writeAllSync;

// std/path
basename: typeof _basename;
dirname: typeof _dirname;
extname: typeof _extname;
fromFileUrl: typeof _fromFileUrl;
isAbsolute: typeof _isAbsolute;
join: typeof _join;
normalize: typeof _normalize;
relative: typeof _relative;
resolve: typeof _resolve;
toFileUrl: typeof _toFileUrl;
toNamespacedPath: typeof _toNamespacedPath;

// std/flags
parseFlags: typeof _parseFlags;
// x
path: typeof _path;
io: typeof _io;
fs: typeof _fs;
log: typeof _log;
flags: typeof _flags;
}
}

0 comments on commit ee95ee5

Please sign in to comment.