Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 82 additions & 55 deletions src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,92 +5,119 @@ import * as fuzzy from 'fuzzy';
import * as inquirer from 'inquirer';

import { FolderDetails } from '../misc';
import { apiEndpointTemplate, appendNewSetting,
initialSettingTemplate, slashCommandTemplate } from '../templates/boilerplate';
import {
apiEndpointTemplate,
appendNewSetting,
initialSettingTemplate,
slashCommandTemplate,
} from '../templates/boilerplate';

export default class Generate extends Command {
public static description = 'Adds boilerplate code for various functions';
public static flags = {
help: flags.help({char: 'h'}),
help: flags.help({ char: 'h' }),
options: flags.string({
char: 'o',
// tslint:disable-next-line:max-line-length
description: 'Choose the boilerplate needed a. Api Extension b. Slash Command Extension c. Settings Extension',
description:
'Choose boilerplate(s): a. Api Extension, b. Slash Command, c. Settings Extension',
options: ['a', 'b', 'c'],
multiple: true, // Allow multiple values
}),
};
public async run() {

public async run(): Promise<void> {
const { flags } = this.parse(Generate);
const fd = new FolderDetails(this);
try {
await fd.readInfoFile();
} catch (e) {
this.error(chalk.bold.red(e && e.message ? e.message : e));
}
let option = flags.options;

// Map flag options (a/b/c) to their full names
const optionMap: { [key: string]: string } = {
a: 'Api Extension',
b: 'Slash Command Extension',
c: 'Settings Extension',
};

let options = flags.options
? flags.options.map((opt) => optionMap[opt])
: [];

const categories = [
'Api Extension',
'Slash Command Extension',
'Settings Extension',
];
if (!option) {
inquirer.registerPrompt('checkbox-plus', require('inquirer-checkbox-plus-prompt'));
const result = await inquirer.prompt([{
type: 'checkbox-plus',
name: 'categories',
message: 'Choose the boilerplate needed',
pageSize: 10,
highlight: true,
searchable: true,
validate: (answer: Array<string>) => {
if (answer.length === 0) {
return chalk.bold.redBright('You must choose at least one option.');
}

return true;
},
// tslint:disable:promise-function-async
source: (answersSoFar: object, input: string) => {
input = input || '';

return new Promise((resolve) => {
const fuzzyResult = fuzzy.filter(input, categories);

const data = fuzzyResult.map((element) => {
return element.original;
});

resolve(data);
});
if (!options.length) {
inquirer.registerPrompt(
'checkbox-plus',
require('inquirer-checkbox-plus-prompt'),
);
const result = await inquirer.prompt([
{
type: 'checkbox-plus',
name: 'categories',
message: 'Choose the boilerplate needed',
pageSize: 10,
highlight: true,
searchable: true,
validate: (answer: Array<string>) => {
if (answer.length === 0) {
return chalk.bold.redBright(
'You must choose at least one option.',
);
}
return true;
},
source: async (answersSoFar: object, input: string) => {
const fuzzyResult = fuzzy.filter(input || '', categories);
return fuzzyResult.map((el) => el.original);
},
},
}] as any);
option = (result as any).categories[0];
}
switch (option) {
case 'Api Extension':
this.ApiExtensionBoilerplate(fd);
break;
case 'Slash Command Extension':
this.SlashCommandExtension(fd);
break;
case 'Settings Extension':
this.SettingExtension(fd);
break;
default:
break;
] as any);
options = (result as any).categories;
}

// Process each selected option
for (const option of options) {
switch (option) {
case 'Api Extension':
await this.ApiExtensionBoilerplate(fd);
break;
case 'Slash Command Extension':
await this.SlashCommandExtension(fd);
break;
case 'Settings Extension':
await this.SettingExtension(fd);
break;
default:
break;
}
}
}

private ApiExtensionBoilerplate = async (fd: FolderDetails): Promise<void> => {
const name = await cli.prompt(chalk.bold.greenBright('Name of endpoint class'));
const path = await cli.prompt(chalk.bold.greenBright('Path for endpoint'));
private ApiExtensionBoilerplate = async (
fd: FolderDetails,
): Promise<void> => {
const name = await cli.prompt(
chalk.bold.greenBright('Name of API endpoint class'),
);
const path = await cli.prompt(
chalk.bold.greenBright('Path for API endpoint'),
);
const toWrite = apiEndpointTemplate(name, path);
fd.generateEndpointClass(name, toWrite);
}

private SlashCommandExtension = async (fd: FolderDetails): Promise<void> => {
const name = await cli.prompt(chalk.bold.greenBright('Name of command class'));
private SlashCommandExtension = async (
fd: FolderDetails,
): Promise<void> => {
const name = await cli.prompt(
chalk.bold.greenBright('Name of slash command class'),
);
const toWrite = slashCommandTemplate(name);
fd.generateCommandClass(name, toWrite);
}
Expand Down