Skip to content

Commit 866793c

Browse files
committed
feat: add quote method to global space and remove cd from $ symbol
1 parent 7e5473c commit 866793c

File tree

5 files changed

+25
-15
lines changed

5 files changed

+25
-15
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ await Promise.all([
3838
]);
3939

4040
const name = "foo bar";
41-
await $`mkdir /tmp/${name}`;
41+
await $`mkdir /tmp/${name}`; // <-- string will be safly quoted to: /tmp/'foo bar'
4242
```
4343

4444
## Content
@@ -107,6 +107,10 @@ Set the current shel.
107107

108108
Set the current working directory.
109109

110+
### `$.quote`
111+
112+
Parser method that is used to safely quote strings. Used by: ``$`command` ``
113+
110114
### ``$`command` ``
111115

112116
```ts
@@ -152,7 +156,7 @@ try {
152156

153157
### `cd()`
154158

155-
Set the current working directory. Also available on the global `$` symbol.
159+
Set the current working directory. If path does not exist, an error is thrown.
156160

157161
### `$.[style]()`
158162

@@ -162,6 +166,11 @@ dzx has chainable color methods that are available on the global `$` symbol.
162166
console.log($.blue.bold("Hello world!"));
163167
```
164168

169+
### ``quote`string` ``
170+
171+
The quote methods quotes safly a string. by default the `shq` package is used.
172+
Can be overidden with `$.quote`.
173+
165174
## Remote usage
166175

167176
```typescript

dzx.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import { join, readAll } from "./deps.ts";
44
import { ProcessError } from "./src/process_error.ts";
5-
import { $ } from "./mod.ts";
5+
import { $, cd, quote } from "./mod.ts";
66

77
window.$ = $;
8-
window.cd = $.cd;
8+
window.cd = cd;
9+
window.quote = quote;
910

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

mod.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { colors } from "./deps.ts";
1+
import { colors, escapeStr } from "./deps.ts";
22
import { cd } from "./src/cd.ts";
33
import { exec } from "./src/exec.ts";
44
import { quote } from "./src/quote.ts";
@@ -7,19 +7,19 @@ export type $ = typeof exec & typeof colors & {
77
verbose: boolean;
88
cwd: string;
99
shell: string;
10-
cd: typeof cd;
11-
quote: typeof quote;
10+
quote: typeof escapeStr;
1211
};
1312

1413
export const $: $ = exec as $;
1514

15+
export { quote };
16+
1617
Object.setPrototypeOf($, Object.getPrototypeOf(colors));
1718

1819
$._stack = [];
1920
$.shell = "/bin/sh";
2021
$.verbose = false;
2122
$.cwd = Deno.cwd();
22-
$.cd = cd;
23-
$.quote = quote;
23+
$.quote = escapeStr;
2424

2525
export { cd };

src/quote.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { escapeStr } from "../deps.ts";
2-
31
export function quote(
42
pieces: TemplateStringsArray,
53
...args: Array<string | number>
@@ -8,7 +6,7 @@ export function quote(
86
let i = 0;
97
for (; i < args.length; i++) {
108
if (typeof args[i] === "string") {
11-
parsed += escapeStr(args[i] as string) + pieces[i + 1];
9+
parsed += $.quote(args[i] as string) + pieces[i + 1];
1210
} else {
1311
parsed += args[i] + pieces[i + 1];
1412
}

types.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import type { $ } from "./mod.ts";
1+
import type { $, cd as _cd, quote as _quote } from "./mod.ts";
22

33
declare global {
44
const $: $;
5-
const cd: typeof $.cd;
5+
const cd: typeof _cd;
6+
const quote: typeof _quote;
67

78
interface Window {
89
$: $;
9-
cd: typeof $.cd;
10+
cd: typeof _cd;
11+
quote: typeof _quote;
1012
}
1113
}

0 commit comments

Comments
 (0)