Skip to content

Commit

Permalink
catch permission error on exit (#7)
Browse files Browse the repository at this point in the history
* catch permission error on exit

* fix tests add default command
  • Loading branch information
justinmchase authored Aug 6, 2023
1 parent 7b7a56c commit 335a746
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 11 deletions.
65 changes: 65 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions deps/cliffy.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { Command } from "https://deno.land/x/cliffy@v0.25.7/command/mod.ts";
export { EnumType } from "https://deno.land/x/cliffy@v0.25.7/command/types/enum.ts";
export { Command } from "https://deno.land/x/cliffy@v1.0.0-rc.3/command/mod.ts";
export { EnumType } from "https://deno.land/x/cliffy@v1.0.0-rc.3/command/types/enum.ts";
41 changes: 32 additions & 9 deletions src/grove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ export class Grove<TContext extends IContext> {
}

private build(context: TContext, command: Command, modes: IMode<TContext>[]) {
context.log.critical(
"grove_modes_empty",
"At least one mode must be configured",
);

for (const mode of modes) {
const options = mode.getOptions();
const subModes = mode.getModes();
Expand Down Expand Up @@ -75,8 +70,6 @@ export class Grove<TContext extends IContext> {
command.action((args: unknown) => this.run(args, context, mode));
}

// The first command is the default command
command.default(modes[0]?.name);
return command;
}

Expand All @@ -87,10 +80,38 @@ export class Grove<TContext extends IContext> {
.name("grove")
.action(function () {
this.showHelp();
Deno.exit(1);
try {
Deno.exit(1);
} catch (err) {
// In Deno Deploy the exit function is not allowed
// Just ignore the error in this case
// any other error throw
if (err.name !== "PermissionDenied") {
throw err;
}
}
});

this.build(context, command, this.config.modes);
if (!this.config.modes.length) {
context.log.critical(
"grove_modes_empty",
"At least one mode must be configured",
);
} else {
this.build(context, command, this.config.modes);
}
if (!args.length && this.config.modes.length) {
// In deno deploy you can't specify args so you must setup a default mode
// Cliffy supports default commands but it doesn't seem to work so for now we will hack it for now
// todo: use after https://github.com/c4spar/deno-cliffy/issues/655 is resolved
const defaultMode = this.config.modes[0].name;
context.log.info(
"grove_default_mode",
`no mode specified, using default mode ${defaultMode}`,
{ mode: defaultMode, args },
);
args = [defaultMode];
}
await command
.error((err) =>
context.log.error(
Expand All @@ -103,6 +124,8 @@ export class Grove<TContext extends IContext> {
.meta("deno", Deno.version.deno)
.meta("v8", Deno.version.v8)
.meta("typescript", Deno.version.typescript)
// todo: use after https://github.com/c4spar/deno-cliffy/issues/655 is resolved
// .default(this.config.modes[0].name)
.parse(args);
}

Expand Down

0 comments on commit 335a746

Please sign in to comment.