diff --git a/mod.test.ts b/mod.test.ts index 6374394..02958ad 100644 --- a/mod.test.ts +++ b/mod.test.ts @@ -982,11 +982,25 @@ async function isDir(path: string) { Deno.test("$.commandExists", async () => { assertEquals(await $.commandExists("some-fake-command"), false); assertEquals(await $.commandExists("deno"), true); + + const $new = build$({ + commandBuilder: new CommandBuilder().registerCommand("some-fake-command", () => { + return Promise.resolve({ code: 0, kind: "continue" }); + }), + }); + assertEquals(await $new.commandExists("some-fake-command"), true); }); Deno.test("$.commandExistsSync", () => { assertEquals($.commandExistsSync("some-fake-command"), false); assertEquals($.commandExistsSync("deno"), true); + + const $new = build$({ + commandBuilder: new CommandBuilder().registerCommand("some-fake-command", () => { + return Promise.resolve({ code: 0, kind: "continue" }); + }), + }); + assertEquals($new.commandExistsSync("some-fake-command"), true); }); Deno.test("$.stripAnsi", () => { diff --git a/mod.ts b/mod.ts index d36165f..66fc3d6 100644 --- a/mod.ts +++ b/mod.ts @@ -1,4 +1,4 @@ -import { CommandBuilder, CommandResult, escapeArg } from "./src/command.ts"; +import { CommandBuilder, CommandResult, escapeArg, getRegisteredCommandNamesSymbol } from "./src/command.ts"; import { Box, Delay, @@ -160,10 +160,10 @@ export interface $Type { * ``` */ escapeArg(arg: string): string; - /** Strip ANSI escape codes from a string */ + /** Strips ANSI escape codes from a string */ stripAnsi(text: string): string; /** - * De-indent (a.k.a. dedent/outdent) template literal strings + * De-indents (a.k.a. dedent/outdent) template literal strings * * Re-export of https://deno.land/x/outdent * @@ -193,26 +193,14 @@ export interface $Type { /** 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') - * } - * ``` + * Determines if the provided command exists resolving to `true` if the command + * will be resolved by the shell of the current `$` or false otherwise. */ commandExists(commandName: string): Promise; - /** Gets if the provided command exists synchronously */ + /** + * Determines if the provided command exists resolving to `true` if the command + * will be resolved by the shell of the current `$` or false otherwise. + */ commandExistsSync(commandName: string): boolean; /** Re-export of deno_std's `fs` module. */ fs: typeof fs; @@ -549,12 +537,6 @@ 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 `$`. */ @@ -655,6 +637,18 @@ function build$FromState(state: $State) { state.indentLevel.value--; } }, + commandExists(commandName: string) { + if (state.commandBuilder.getValue()[getRegisteredCommandNamesSymbol]().includes(commandName)) { + return Promise.resolve(true); + } + return helperObject.which(commandName).then((c) => c != null); + }, + commandExistsSync(commandName: string) { + if (state.commandBuilder.getValue()[getRegisteredCommandNamesSymbol]().includes(commandName)) { + return true; + } + return helperObject.whichSync(commandName) != null; + }, maybeConfirm, confirm, maybeSelect, diff --git a/src/command.ts b/src/command.ts index cc32d0e..14fc536 100644 --- a/src/command.ts +++ b/src/command.ts @@ -56,6 +56,9 @@ const builtInCommands = { pwd: pwdCommand, }; +/** @internal */ +export const getRegisteredCommandNamesSymbol = Symbol(); + /** * Underlying builder API for executing commands. * @@ -391,6 +394,11 @@ export class CommandBuilder implements PromiseLike { async json(): Promise { return (await this.quiet("stdout")).stdoutJson; } + + /** @internal */ + [getRegisteredCommandNamesSymbol]() { + return Object.keys(this.#state.commands); + } } export async function parseAndSpawnCommand(state: CommandBuilderState) { diff --git a/src/request.ts b/src/request.ts index da9e930..b283fe3 100644 --- a/src/request.ts +++ b/src/request.ts @@ -20,6 +20,7 @@ interface RequestBuilderState { timeout: number | undefined; } +/** @internal */ export const withProgressBarFactorySymbol = Symbol(); /**