Skip to content

Commit

Permalink
feat: $.commandExists should take into account registered commands …
Browse files Browse the repository at this point in the history
…on the `$` (#74)
  • Loading branch information
dsherret authored Jan 7, 2023
1 parent ee8ffd3 commit 860ecf8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 27 deletions.
14 changes: 14 additions & 0 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
48 changes: 21 additions & 27 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CommandBuilder, CommandResult, escapeArg } from "./src/command.ts";
import { CommandBuilder, CommandResult, escapeArg, getRegisteredCommandNamesSymbol } from "./src/command.ts";
import {
Box,
Delay,
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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<boolean>;
/** 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;
Expand Down Expand Up @@ -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 `$`. */
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ const builtInCommands = {
pwd: pwdCommand,
};

/** @internal */
export const getRegisteredCommandNamesSymbol = Symbol();

/**
* Underlying builder API for executing commands.
*
Expand Down Expand Up @@ -391,6 +394,11 @@ export class CommandBuilder implements PromiseLike<CommandResult> {
async json<TResult = any>(): Promise<TResult> {
return (await this.quiet("stdout")).stdoutJson;
}

/** @internal */
[getRegisteredCommandNamesSymbol]() {
return Object.keys(this.#state.commands);
}
}

export async function parseAndSpawnCommand(state: CommandBuilderState) {
Expand Down
1 change: 1 addition & 0 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface RequestBuilderState {
timeout: number | undefined;
}

/** @internal */
export const withProgressBarFactorySymbol = Symbol();

/**
Expand Down

0 comments on commit 860ecf8

Please sign in to comment.