Skip to content

Commit

Permalink
feat: add quote method to global space and remove cd from $ symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed May 8, 2021
1 parent 7e5473c commit 866793c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ await Promise.all([
]);

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

## Content
Expand Down Expand Up @@ -107,6 +107,10 @@ Set the current shel.

Set the current working directory.

### `$.quote`

Parser method that is used to safely quote strings. Used by: ``$`command` ``

### ``$`command` ``

```ts
Expand Down Expand Up @@ -152,7 +156,7 @@ try {

### `cd()`

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

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

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

### ``quote`string` ``

The quote methods quotes safly a string. by default the `shq` package is used.
Can be overidden with `$.quote`.

## Remote usage

```typescript
Expand Down
5 changes: 3 additions & 2 deletions dzx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import { join, readAll } from "./deps.ts";
import { ProcessError } from "./src/process_error.ts";
import { $ } from "./mod.ts";
import { $, cd, quote } from "./mod.ts";

window.$ = $;
window.cd = $.cd;
window.cd = cd;
window.quote = quote;

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

Expand Down
10 changes: 5 additions & 5 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { colors } from "./deps.ts";
import { colors, escapeStr } from "./deps.ts";
import { cd } from "./src/cd.ts";
import { exec } from "./src/exec.ts";
import { quote } from "./src/quote.ts";
Expand All @@ -7,19 +7,19 @@ export type $ = typeof exec & typeof colors & {
verbose: boolean;
cwd: string;
shell: string;
cd: typeof cd;
quote: typeof quote;
quote: typeof escapeStr;
};

export const $: $ = exec as $;

export { quote };

Object.setPrototypeOf($, Object.getPrototypeOf(colors));

$._stack = [];
$.shell = "/bin/sh";
$.verbose = false;
$.cwd = Deno.cwd();
$.cd = cd;
$.quote = quote;
$.quote = escapeStr;

export { cd };
4 changes: 1 addition & 3 deletions src/quote.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { escapeStr } from "../deps.ts";

export function quote(
pieces: TemplateStringsArray,
...args: Array<string | number>
Expand All @@ -8,7 +6,7 @@ export function quote(
let i = 0;
for (; i < args.length; i++) {
if (typeof args[i] === "string") {
parsed += escapeStr(args[i] as string) + pieces[i + 1];
parsed += $.quote(args[i] as string) + pieces[i + 1];
} else {
parsed += args[i] + pieces[i + 1];
}
Expand Down
8 changes: 5 additions & 3 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { $ } from "./mod.ts";
import type { $, cd as _cd, quote as _quote } from "./mod.ts";

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

interface Window {
$: $;
cd: typeof $.cd;
cd: typeof _cd;
quote: typeof _quote;
}
}

0 comments on commit 866793c

Please sign in to comment.