Skip to content

Commit

Permalink
Update to oclif 3
Browse files Browse the repository at this point in the history
  • Loading branch information
grant0417 committed Dec 20, 2023
1 parent db6e926 commit 46a9bea
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 720 deletions.
5 changes: 1 addition & 4 deletions integrations/oclif/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
"author": "Matt Schrage @mattschrage",
"bugs": "https://github.com/withfig/autocomplete-tools/issues",
"dependencies": {
"@oclif/command": "^1.8.36",
"@oclif/config": "^1.18.17",
"@oclif/core": "^3.10.0",
"prettier": "^3.1.0"
},
"devDependencies": {
"@oclif/dev-cli": "^1.26.10",
"@oclif/plugin-help": "^6.0.7",
"@types/node": "^20.9.2",
"@withfig/autocomplete-types": "workspace:^",
"globby": "^14.0.0",
Expand Down
34 changes: 13 additions & 21 deletions integrations/oclif/src/commands/generate-fig-spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Command } from "@oclif/command";
import { Command as ICommand } from "@oclif/config";
import * as fs from "fs";
import { Command } from "@oclif/core";
import * as fs from "node:fs";
import * as prettier from "prettier";

function getFigArgs(args: ICommand.Arg[]): Fig.Arg[] {
function getFigArgs(args: Command.Arg.Cached[]): Fig.Arg[] {
const figArgs: Fig.Arg[] = [];
for (const arg of args) {
if (arg.hidden) continue;
Expand All @@ -18,12 +17,12 @@ function getFigArgs(args: ICommand.Arg[]): Fig.Arg[] {
return figArgs;
}

function getFigOptions(options: [string, ICommand.Flag][]): Fig.Option[] {
function getFigOptions(options: Command.Flag.Cached[]): Fig.Option[] {
const figOptions: Fig.Option[] = [];
for (const [name, flag] of options) {
for (const flag of options) {
if (flag.hidden) continue;
figOptions.push({
name: flag.char ? [`-${flag.char}`, `--${name}`] : `--${name}`,
name: flag.char ? [`-${flag.char}`, `--${flag.name}`] : `--${flag.name}`,
...(flag.description && { description: flag.description }),
...(flag.type === "option" && {
args: {
Expand All @@ -32,24 +31,24 @@ function getFigOptions(options: [string, ICommand.Flag][]): Fig.Option[] {
},
}),
...(flag.required === true && { isRequired: true }),
...(flag.type === "boolean" && flag.allowNo && { exclusiveOn: [`--no-${name}`] }),
...(flag.type === "boolean" && flag.allowNo && { exclusiveOn: [`--no-${flag.name}`] }),
});
if (flag.type === "boolean" && flag.allowNo) {
figOptions.push({
name: `--no-${name}`,
name: `--no-${flag.name}`,
});
}
}
return figOptions;
}

function getFigSubcommands(commands: ICommand.Plugin[]): Fig.Subcommand[] {
function getFigSubcommands(commands: Command.Loadable[]): Fig.Subcommand[] {
const subcommands: Fig.Subcommand[] = [];
for (const command of commands) {
// skip this command or hidden commands
if (command.id === "generate-fig-spec" || command.hidden) continue;
const options: Fig.Option[] = getFigOptions(Object.entries(command.flags));
const args: Fig.Arg[] = getFigArgs(command.args);
const options: Fig.Option[] = getFigOptions(Object.values(command.flags));
const args: Fig.Arg[] = getFigArgs(Object.values(command.args));
subcommands.push({
name: command.aliases.length > 0 ? [command.id, ...command.aliases] : command.id,
...(command.description && { description: command.description }),
Expand All @@ -63,16 +62,8 @@ function getFigSubcommands(commands: ICommand.Plugin[]): Fig.Subcommand[] {
export class GenerateFigSpecCommand extends Command {
static description = "Generate a Fig completion spec";

// static flags = {
// help: flags.help({ char: "h" }),
// output: flags.string({
// char: "o",
// description: "Output filepath",
// }),
// };

async run() {
const { flags: parsedFlags } = this.parse(GenerateFigSpecCommand);
const { flags: parsedFlags } = await this.parse(GenerateFigSpecCommand);

const spec: Fig.Spec = {
name: this.config.name,
Expand All @@ -89,6 +80,7 @@ export class GenerateFigSpecCommand extends Command {
`,
{ parser: "typescript" }
);

if (parsedFlags.output) {
fs.writeFileSync(parsedFlags.output, template);
} else {
Expand Down
Loading

0 comments on commit 46a9bea

Please sign in to comment.