Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into JOTSR-main
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Mar 18, 2024
2 parents 31c9d73 + 89475a8 commit 766634a
Show file tree
Hide file tree
Showing 9 changed files with 9 additions and 112 deletions.
28 changes: 0 additions & 28 deletions _utils/distance.ts

This file was deleted.

9 changes: 0 additions & 9 deletions command/_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,6 @@ export class DefaultCommandNotFoundError extends CommandError {
}
}

export class CommandExecutableNotFoundError extends CommandError {
constructor(name: string) {
super(
`Command executable not found: ${name}`,
);
Object.setPrototypeOf(this, CommandExecutableNotFoundError.prototype);
}
}

export class UnknownCompletionCommandError extends CommandError {
constructor(name: string, commands: Array<Command>) {
super(
Expand Down
39 changes: 1 addition & 38 deletions command/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
} from "./_utils.ts";
import { bold, brightBlue, red } from "@std/fmt/colors";
import {
CommandExecutableNotFoundError,
CommandNotFoundError,
DefaultCommandNotFoundError,
DuplicateCommandAliasError,
Expand Down Expand Up @@ -151,7 +150,6 @@ export class Command<
private completions = new Map<string, Completion>();
private cmd: Command<any> = this;
private argsDefinition?: string;
private isExecutable = false;
private throwOnError = false;
private _allowEmpty = false;
private _stopEarly = false;
Expand Down Expand Up @@ -792,12 +790,6 @@ export class Command<
return this;
}

/** Make command executable. */
public executable(): this {
this.cmd.isExecutable = true;
return this;
}

/**
* Set command arguments.
*
Expand Down Expand Up @@ -1717,10 +1709,7 @@ export class Command<
this.registerDefaults();
this.rawArgs = ctx.unknown.slice();

if (this.isExecutable) {
await this.executeExecutable(ctx.unknown);
return { options: {}, args: [], cmd: this, literal: [] };
} else if (this._useRawArgs) {
if (this._useRawArgs) {
await this.parseEnvVars(ctx, this.envVars);
return await this.execute(ctx.env, ctx.unknown);
}
Expand Down Expand Up @@ -1965,32 +1954,6 @@ export class Command<
await this.globalActionHandler?.(options, ...args);
}

/**
* Execute external sub-command.
* @param args Raw command line arguments.
*/
protected async executeExecutable(args: string[]) {
const command = this.getPath().replace(/\s+/g, "-");

await Deno.permissions.request({ name: "run", command });

try {
const cmd: Deno.Command = new Deno.Command(command, {
args,
});
const output: Deno.CommandOutput = await cmd.output();

if (!output.success) {
Deno.exit(output.code);
}
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
throw new CommandExecutableNotFoundError(command);
}
throw error;
}
}

/** Parse raw command line arguments. */
protected parseOptions(
ctx: ParseContext,
Expand Down
1 change: 1 addition & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@std/io": "jsr:@std/io@0.217",
"@std/path": "jsr:@std/path@0.217",
"@std/testing": "jsr:@std/testing@0.217",
"@std/text": "jsr:@std/text@0.217",
"conditional_type_checks": "npm:conditional-type-checks@1.0.6",
"sinon": "npm:sinon@13.0.2"
}
Expand Down
10 changes: 0 additions & 10 deletions examples/command/git_style_executables.ts

This file was deleted.

7 changes: 0 additions & 7 deletions examples/command/sub_commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,3 @@ await new Command()
}),
)
.parse(Deno.args);

// Command implemented using separate executable file (description is passed as second parameter to `.command()`)
await new Command()
.command("start <service>", "Start named service.").executable()
.command("stop [service]", "Stop named service, or all if no name supplied.")
.executable()
.parse(Deno.args);
17 changes: 2 additions & 15 deletions flags/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FlagOptions } from "./types.ts";
import { distance } from "../_utils/distance.ts";
import { closestString } from "@std/text/closest_string";

/** Convert param case string to camel case. */
export function paramCaseToCamelCase(str: string): string {
Expand Down Expand Up @@ -63,7 +63,7 @@ export function didYouMean(
type: string,
types: Array<string>,
): string {
const match: string | undefined = closest(type, types);
const match: string | undefined = closestString(type, types);
return match ? `${message} "${match}"?` : "";
}

Expand Down Expand Up @@ -123,19 +123,6 @@ function matchWildCardOption(
return option;
}

function closest(str: string, arr: string[]): string | undefined {
let minDistance = Infinity;
let minIndex = 0;
for (let i = 0; i < arr.length; i++) {
const dist = distance(str, arr[i]);
if (dist < minDistance) {
minDistance = dist;
minIndex = i;
}
}
return arr[minIndex];
}

export function getDefaultValue(option: FlagOptions): unknown {
return typeof option.default === "function"
? option.default()
Expand Down
4 changes: 2 additions & 2 deletions prompt/_generic_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
} from "./_generic_input.ts";
import { WidenType } from "./_utils.ts";
import { bold, brightBlue, dim, stripColor, yellow } from "@std/fmt/colors";
import { levenshteinDistance } from "@std/text/levenshtein_distance";
import { Figures, getFiguresByKeys } from "./_figures.ts";
import { distance } from "../_utils/distance.ts";

type UnsupportedInputOptions = "suggestions" | "list";

Expand Down Expand Up @@ -805,7 +805,7 @@ function matchOptions<
if (matchOption(searchInput, option)) {
matched.push({
option,
distance: distance(option.name, searchInput),
distance: levenshteinDistance(option.name, searchInput),
children: [],
});
}
Expand Down
6 changes: 3 additions & 3 deletions prompt/_generic_suggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
GenericInputPromptSettings,
} from "./_generic_input.ts";
import { bold, brightBlue, dim, stripColor, underline } from "@std/fmt/colors";
import { levenshteinDistance } from "@std/text/levenshtein_distance";
import { dirname, join, normalize } from "@std/path";
import { Figures, getFiguresByKeys } from "./_figures.ts";
import { distance } from "../_utils/distance.ts";

/** Generic input prompt options. */
export interface GenericSuggestionsOptions<TValue, TRawValue>
Expand Down Expand Up @@ -238,8 +238,8 @@ export abstract class GenericSuggestions<TValue, TRawValue>
.startsWith(input.toLowerCase())
)
.sort((a: string | number, b: string | number) =>
distance((a || a).toString(), input) -
distance((b || b).toString(), input)
levenshteinDistance((a || a).toString(), input) -
levenshteinDistance((b || b).toString(), input)
);
}

Expand Down

0 comments on commit 766634a

Please sign in to comment.