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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add $.commandExists, $.dedent, and $.stripAnsi #71

Merged
merged 17 commits into from Jan 7, 2023
Merged
34 changes: 34 additions & 0 deletions README.md
Expand Up @@ -441,6 +441,13 @@ Getting path to an executable based on a command name:
console.log(await $.which("deno")); // outputs the path to deno executable
```

Check if a command exists:

```ts
console.log(await $.commandExists("deno"));
console.log($.commandExistsSync("deno"));
andrewbrey marked this conversation as resolved.
Show resolved Hide resolved
```

Attempting to do an action until it succeeds or hits the maximum number of retries:

```ts
Expand All @@ -454,6 +461,26 @@ await $.withRetries({
});
```

"Dedent" or remove leading whitespace from a string:

```ts
console.log($.dedent`
This line will appear without any indentation.
* This list will appear with 2 spaces more than previous line.
* As will this line.

Empty lines (like the one above) will not affect the common indentation.
`);
```

```
This line will appear without any indentation.
* This list will appear with 2 spaces more than previous line.
* As will this line.

Empty lines (like the one above) will not affect the common indentation.
```

Re-export of deno_std's path:

```ts
Expand All @@ -468,6 +495,13 @@ for await (const file of $.fs.expandGlob("**/*.ts")) {
}
```

Remove ansi escape sequences from a string:

```ts
$.stripAnsi("\u001B[4mHello World\u001B[0m");
//=> 'Hello World'
```

## Making requests

Dax ships with a slightly less verbose wrapper around `fetch` that will throw by default on non-`2xx` status codes (this is configurable per status code).
Expand Down
34 changes: 34 additions & 0 deletions mod.test.ts
Expand Up @@ -978,3 +978,37 @@ async function isDir(path: string) {
const info = await lstat(path);
return info?.isDirectory ? true : false;
}

Deno.test("$.commandExists", async () => {
assertEquals(await $.commandExists("some-fake-command"), false);
assertEquals(await $.commandExists("deno"), true);
});

Deno.test("$.commandExistsSync", () => {
assertEquals($.commandExistsSync("some-fake-command"), false);
assertEquals($.commandExistsSync("deno"), true);
});

Deno.test("$.stripAnsi", () => {
assertEquals($.stripAnsi("\u001B[4mHello World\u001B[0m"), "Hello World");
assertEquals($.stripAnsi("no ansi escapes here"), "no ansi escapes here");
});

Deno.test("$.dedent", () => {
const actual = $.dedent`
This line will appear without any indentation.
* This list will appear with 2 spaces more than previous line.
* As will this line.

Empty lines (like the one above) will not affect the common indentation.
`;

const expected = `
This line will appear without any indentation.
* This list will appear with 2 spaces more than previous line.
* As will this line.

Empty lines (like the one above) will not affect the common indentation.`.trim();

assertEquals(actual, expected);
});
60 changes: 57 additions & 3 deletions mod.ts
Expand Up @@ -25,7 +25,8 @@ import {
select,
SelectOptions,
} from "./src/console/mod.ts";
import { colors, fs, path, which, whichSync } from "./src/deps.ts";
import { colors, fs, outdent, path, which, whichSync } from "./src/deps.ts";
import { wasmInstance } from "./src/lib/mod.ts";
import { RequestBuilder, withProgressBarFactorySymbol } from "./src/request.ts";

export { CommandBuilder, CommandResult } from "./src/command.ts";
Expand Down Expand Up @@ -158,6 +159,27 @@ export interface $Type {
* ```
*/
escapeArg(arg: string): string;
/** Strip ANSI escape codes from a string */
stripAnsi(text: string): string;
/**
* De-indent (a.k.a. dedent/outdent) template literal strings
*
* Re-export of https://deno.land/x/outdent
*
* Removes the leading whitespace from each line,
* allowing you to break the string into multiple
* lines with indentation. If lines have an uneven
* amount of indentation, then only the common
* whitespace is removed.
*
* The opening and closing lines (which contain
* the ` marks) must be on their own line. The
* opening line must be empty, and the closing
* line may contain whitespace. The opening and
* closing line will be removed from the output,
* so that only the content in between remains.
*/
dedent: typeof outdent;
/**
* Gets if the provided path exists asynchronously.
*
Expand All @@ -169,6 +191,28 @@ export interface $Type {
exists(path: string): Promise<boolean>;
/** Gets if the provided path exists synchronously. */
existsSync(path: string): boolean;
/**
* Using `$.which`, determine if the provided command exists
* resolving to `true` if `$.which` finds the specified
* command and to `false` otherwise.
*
* The following are equivalent:
*
* ```ts
* // use $.which directly
* if(typeof (await $.which('deno')) !== 'undefined') {
* console.log('deno found');
* }
*
* // use $.commandExists
* if(await $.commandExists('deno')) {
* console.log('deno found')
* }
* ```
*/
commandExists(commandName: string): Promise<boolean>;
/** Gets if the provided command exists synchronously */
commandExistsSync(commandName: string): boolean;
/** Re-export of deno_std's `fs` module. */
fs: typeof fs;
/** Re-export of deno_std's `path` module. */
Expand Down Expand Up @@ -477,12 +521,16 @@ const helperObject = {
path,
cd,
escapeArg,
existsSync(path: string) {
return fs.existsSync(path);
stripAnsi(text: string) {
return wasmInstance.strip_ansi_codes(text);
},
dedent: outdent,
exists(path: string) {
return fs.exists(path);
},
existsSync(path: string) {
return fs.existsSync(path);
},
sleep,
which(commandName: string) {
if (commandName.toUpperCase() === "DENO") {
Expand All @@ -498,6 +546,12 @@ const helperObject = {
return whichSync(commandName);
}
},
commandExists(commandName: string) {
return this.which(commandName).then((c) => typeof c !== "undefined");
},
commandExistsSync(commandName: string) {
return typeof this.whichSync(commandName) !== "undefined";
},
};

/** Options for creating a custom `$`. */
Expand Down
1 change: 1 addition & 0 deletions src/deps.ts
Expand Up @@ -6,4 +6,5 @@ export * as path from "https://deno.land/std@0.170.0/path/mod.ts";
export { readAll } from "https://deno.land/std@0.170.0/streams/read_all.ts";
export { writeAllSync } from "https://deno.land/std@0.170.0/streams/write_all.ts";
export { default as localDataDir } from "https://deno.land/x/dir@1.5.1/data_local_dir/mod.ts";
export { outdent } from "https://deno.land/x/outdent@v0.8.0/src/index.ts";
export { RealEnvironment as DenoWhichRealEnvironment, which, whichSync } from "https://deno.land/x/which@0.2.1/mod.ts";