Skip to content

Commit

Permalink
refactor(command)!: remove support for executable sub-commands (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar authored Mar 18, 2024
1 parent 181e055 commit 89475a8
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 64 deletions.
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 @@ -13,7 +13,6 @@ import {
} from "./_utils.ts";
import { bold, brightBlue, red } from "./deps.ts";
import {
CommandExecutableNotFoundError,
CommandNotFoundError,
DefaultCommandNotFoundError,
DuplicateCommandAliasError,
Expand Down Expand Up @@ -152,7 +151,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 @@ -793,12 +791,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 @@ -1718,10 +1710,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 @@ -1966,32 +1955,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
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);

0 comments on commit 89475a8

Please sign in to comment.