Skip to content

Commit ee95ee5

Browse files
committed
refactor: give each module a namespace and add log module
1 parent bb4830d commit ee95ee5

File tree

6 files changed

+64
-248
lines changed

6 files changed

+64
-248
lines changed

deps.ts

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,7 @@
1-
export {
2-
basename,
3-
dirname,
4-
extname,
5-
fromFileUrl,
6-
isAbsolute,
7-
join,
8-
normalize,
9-
relative,
10-
resolve,
11-
SEP,
12-
SEP_PATTERN,
13-
toFileUrl,
14-
toNamespacedPath,
15-
} from "https://deno.land/[email protected]/path/mod.ts";
1+
export * as path from "https://deno.land/[email protected]/path/mod.ts";
2+
export * as io from "https://deno.land/[email protected]/io/mod.ts";
3+
export * as fs from "https://deno.land/[email protected]/fs/mod.ts";
4+
export * as log from "https://deno.land/[email protected]/log/mod.ts";
5+
export * as flags from "https://deno.land/[email protected]/flags/mod.ts";
166
export { colors } from "https://deno.land/x/[email protected]/ansi/colors.ts";
17-
export {
18-
iter,
19-
iterSync,
20-
readAll,
21-
readAllSync,
22-
writeAll,
23-
writeAllSync,
24-
} from "https://deno.land/[email protected]/io/util.ts";
25-
export { Buffer } from "https://deno.land/[email protected]/io/buffer.ts";
26-
export { readLines } from "https://deno.land/[email protected]/io/bufio.ts";
27-
export { default as escapeStr } from "https://esm.sh/[email protected]";
28-
export { parse as parseFlags } from "https://deno.land/[email protected]/flags/mod.ts";
29-
export type {
30-
ArgParsingOptions,
31-
Args,
32-
} from "https://deno.land/[email protected]/flags/mod.ts";
7+
export { default as shq } from "https://esm.sh/[email protected]";

dzx.ts

Lines changed: 24 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,34 @@
11
/// <reference path="./types.d.ts" />
2-
2+
import { $, io, path } from "./mod.ts";
33
import { error } from "./src/_utils.ts";
4-
import {
5-
$,
6-
basename,
7-
Buffer,
8-
cd,
9-
dirname,
10-
extname,
11-
fromFileUrl,
12-
isAbsolute,
13-
iter,
14-
iterSync,
15-
join,
16-
normalize,
17-
parseFlags,
18-
quote,
19-
readAll,
20-
readAllSync,
21-
readLines,
22-
relative,
23-
resolve,
24-
toFileUrl,
25-
toNamespacedPath,
26-
writeAll,
27-
writeAllSync,
28-
} from "./mod.ts";
29-
30-
// dzx
31-
window.$ = $;
32-
window.cd = cd;
33-
34-
// std/io
35-
window.Buffer = Buffer;
36-
window.iter = iter;
37-
window.iterSync = iterSync;
38-
window.quote = quote;
39-
window.readAll = readAll;
40-
window.readAllSync = readAllSync;
41-
window.readLines = readLines;
42-
window.writeAll = writeAll;
43-
window.writeAllSync = writeAllSync;
44-
45-
// std/path
46-
window.basename = basename;
47-
window.dirname = dirname;
48-
window.extname = extname;
49-
window.fromFileUrl = fromFileUrl;
50-
window.isAbsolute = isAbsolute;
51-
window.join = join;
52-
window.normalize = normalize;
53-
window.relative = relative;
54-
window.resolve = resolve;
55-
window.toFileUrl = toFileUrl;
56-
window.toNamespacedPath = toNamespacedPath;
57-
58-
window.parseFlags = parseFlags;
59-
60-
const script: string | undefined = Deno.args[0];
614

62-
try {
63-
if (!script) {
64-
if (!Deno.isatty(Deno.stdin.rid)) {
65-
const data = new TextDecoder().decode(await readAll(Deno.stdin));
66-
if (data) {
67-
await import(
68-
`data:application/typescript,${encodeURIComponent(data)}`
69-
);
5+
if (import.meta.main) {
6+
const script: string | undefined = Deno.args[0];
7+
try {
8+
if (!script) {
9+
if (!Deno.isatty(Deno.stdin.rid)) {
10+
const data = new TextDecoder().decode(await io.readAll(Deno.stdin));
11+
if (data) {
12+
await import(
13+
`data:application/typescript,${encodeURIComponent(data)}`
14+
);
15+
} else {
16+
error(`usage: dzx <script>`, 2);
17+
}
7018
} else {
71-
error(`usage: dzx <script>`, 2);
19+
error(`usage: dzx <script>`);
7220
}
21+
} else if (
22+
script.startsWith("http://") || script.startsWith("https://") ||
23+
script.startsWith("file://")
24+
) {
25+
await import(script);
26+
} else if (script) {
27+
await import("file://" + path.join($.cwd, script));
7328
} else {
7429
error(`usage: dzx <script>`);
7530
}
76-
} else if (
77-
script.startsWith("http://") || script.startsWith("https://") ||
78-
script.startsWith("file://")
79-
) {
80-
await import(script);
81-
} else if (script) {
82-
await import("file://" + join($.cwd, script));
83-
} else {
84-
error(`usage: dzx <script>`);
31+
} catch (err) {
32+
error(err);
8533
}
86-
} catch (err) {
87-
error(err);
8834
}

mod.ts

Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,13 @@
1-
import {
2-
basename,
3-
Buffer,
4-
colors,
5-
dirname,
6-
escapeStr,
7-
extname,
8-
fromFileUrl,
9-
isAbsolute,
10-
iter,
11-
iterSync,
12-
join,
13-
normalize,
14-
readAll,
15-
readAllSync,
16-
readLines,
17-
relative,
18-
resolve,
19-
toFileUrl,
20-
toNamespacedPath,
21-
writeAll,
22-
writeAllSync,
23-
} from "./deps.ts";
1+
import { colors, flags, fs, io, log, path, shq } from "./deps.ts";
242
import { cd } from "./src/cd.ts";
253
import { exec } from "./src/exec.ts";
264
import { quote } from "./src/quote.ts";
27-
import { parseFlags } from "./src/parse_flags.ts";
285

296
export type $ = typeof exec & typeof colors & {
307
verbose: boolean;
318
cwd: string;
329
shell: string;
33-
quote: typeof escapeStr;
10+
quote: typeof shq;
3411
throwErors: boolean;
3512
};
3613

@@ -42,30 +19,19 @@ $._stack = [];
4219
$.shell = "/bin/sh";
4320
$.verbose = false;
4421
$.cwd = Deno.cwd();
45-
$.quote = escapeStr;
22+
$.quote = shq;
4623
$.throwErors = false;
4724

48-
export {
49-
basename,
50-
Buffer,
51-
cd,
52-
dirname,
53-
extname,
54-
fromFileUrl,
55-
isAbsolute,
56-
iter,
57-
iterSync,
58-
join,
59-
normalize,
60-
parseFlags,
61-
quote,
62-
readAll,
63-
readAllSync,
64-
readLines,
65-
relative,
66-
resolve,
67-
toFileUrl,
68-
toNamespacedPath,
69-
writeAll,
70-
writeAllSync,
71-
};
25+
// dzx
26+
window.$ = $;
27+
window.cd = cd;
28+
window.quote = quote;
29+
30+
// x
31+
window.path = path;
32+
window.io = io;
33+
window.fs = fs;
34+
window.log = log;
35+
window.flags = flags;
36+
37+
export { cd, flags, fs, io, log, path, quote };

src/exec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { iter } from "../deps.ts";
21
import { ProcessError } from "./process_error.ts";
32
import { ProcessOutput } from "./process_output.ts";
43
import { quote } from "./quote.ts";
@@ -51,7 +50,7 @@ async function read(
5150
reader: Deno.Reader,
5251
...results: Array<Array<string>>
5352
) {
54-
for await (const chunk of iter(reader)) {
53+
for await (const chunk of io.iter(reader)) {
5554
const str = new TextDecoder().decode(chunk);
5655
for (const result of results) {
5756
result.push(str);

src/parse_flags.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

types.d.ts

Lines changed: 17 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,38 @@
11
import type {
22
$,
33
cd as _cd,
4-
parseFlags as _parseFlags,
4+
flags as _flags,
5+
fs as _fs,
6+
io as _io,
7+
log as _log,
8+
path as _path,
59
quote as _quote,
610
} from "./mod.ts";
7-
import type {
8-
ArgParsingOptions as _ArgParsingOptions,
9-
Args as _Args,
10-
basename as _basename,
11-
Buffer as _Buffer,
12-
dirname as _dirname,
13-
extname as _extname,
14-
fromFileUrl as _fromFileUrl,
15-
isAbsolute as _isAbsolute,
16-
iter as _iter,
17-
iterSync as _iterSync,
18-
join as _join,
19-
normalize as _normalize,
20-
readAll as _readAll,
21-
readAllSync as _readAllSync,
22-
readLines as _readLines,
23-
relative as _relative,
24-
resolve as _resolve,
25-
toFileUrl as _toFileUrl,
26-
toNamespacedPath as _toNamespacedPath,
27-
writeAll as _writeAll,
28-
writeAllSync as _writeAllSync,
29-
} from "./deps.ts";
3011

3112
declare global {
3213
// dzx
3314
const $: $;
3415
const cd: typeof _cd;
3516
const quote: typeof _quote;
3617

37-
// std/io
38-
const Buffer: typeof _Buffer;
39-
const iter: typeof _iter;
40-
const iterSync: typeof _iterSync;
41-
const readAll: typeof _readAll;
42-
const readAllSync: typeof _readAllSync;
43-
const readLines: typeof _readLines;
44-
const writeAll: typeof _writeAll;
45-
const writeAllSync: typeof _writeAllSync;
46-
47-
// std/path
48-
const basename: typeof _basename;
49-
const dirname: typeof _dirname;
50-
const extname: typeof _extname;
51-
const fromFileUrl: typeof _fromFileUrl;
52-
const isAbsolute: typeof _isAbsolute;
53-
const join: typeof _join;
54-
const normalize: typeof _normalize;
55-
const relative: typeof _relative;
56-
const resolve: typeof _resolve;
57-
const toFileUrl: typeof _toFileUrl;
58-
const toNamespacedPath: typeof _toNamespacedPath;
59-
60-
// std/flags
61-
const parseFlags: typeof _parseFlags;
62-
type ArgParsingOptions = _ArgParsingOptions;
63-
type Args = _Args;
18+
// x
19+
const path: typeof _path;
20+
const io: typeof _io;
21+
const fs: typeof _fs;
22+
const log: typeof _log;
23+
const flags: typeof _flags;
6424

6525
interface Window {
6626
// dzx
6727
$: $;
6828
cd: typeof _cd;
6929
quote: typeof _quote;
7030

71-
// std/io
72-
Buffer: typeof _Buffer;
73-
iter: typeof _iter;
74-
iterSync: typeof _iterSync;
75-
readAll: typeof _readAll;
76-
readAllSync: typeof _readAllSync;
77-
readLines: typeof _readLines;
78-
writeAll: typeof _writeAll;
79-
writeAllSync: typeof _writeAllSync;
80-
81-
// std/path
82-
basename: typeof _basename;
83-
dirname: typeof _dirname;
84-
extname: typeof _extname;
85-
fromFileUrl: typeof _fromFileUrl;
86-
isAbsolute: typeof _isAbsolute;
87-
join: typeof _join;
88-
normalize: typeof _normalize;
89-
relative: typeof _relative;
90-
resolve: typeof _resolve;
91-
toFileUrl: typeof _toFileUrl;
92-
toNamespacedPath: typeof _toNamespacedPath;
93-
94-
// std/flags
95-
parseFlags: typeof _parseFlags;
31+
// x
32+
path: typeof _path;
33+
io: typeof _io;
34+
fs: typeof _fs;
35+
log: typeof _log;
36+
flags: typeof _flags;
9637
}
9738
}

0 commit comments

Comments
 (0)