Skip to content

Commit

Permalink
feat(help): support array command description (#214)
Browse files Browse the repository at this point in the history
* fix(help): parameter format

* feat(help): support array command description
  • Loading branch information
fXy-during authored Apr 10, 2020
1 parent 09c0403 commit b7549da
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
23 changes: 18 additions & 5 deletions packages/feflow-cli/src/core/devkit/commandOptions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const getOptionFromCommand = (optionsDescription: any): Object[] => {
if(Array.isArray(optionsDescription)) return optionsDescription;
const getCommandLine = (optionsDescription: any, description: any, cmd: any): Object[] => {
if (Array.isArray(optionsDescription)) return optionsDescription;

const options: Object[] = [];

Expand All @@ -12,7 +12,20 @@ const getOptionFromCommand = (optionsDescription: any): Object[] => {
options.push(optionDescritionItem);
});

return options;
return [
{
header: `fef ${cmd}`,
content: description,
},
{
header: 'Usage',
content: `$ fef ${cmd} [options]`,
},
{
header: 'Options',
optionList: options,
},
];
};

const getOptionItem = (optionItemConfig: any, option: any): object => {
Expand All @@ -28,9 +41,9 @@ const getOptionItem = (optionItemConfig: any, option: any): object => {
}

optionDescritionItem = optionItemConfig;
optionDescritionItem.type = String;
optionDescritionItem.type = typeof optionItemConfig.type === 'function' ? optionItemConfig.type : String;
}
return optionDescritionItem;
};

export default getOptionFromCommand;
export default getCommandLine;
6 changes: 3 additions & 3 deletions packages/feflow-cli/src/core/devkit/loadDevkits.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import Config from './config';
import getOptionFromCommand from "./commandOptions";
import getCommandLine from "./commandOptions";

const registerDevkitCommand = (command: any, commandConfig: any, directoryPath: any, ctx: any) => {
const builder = commandConfig.builder;
Expand All @@ -9,9 +9,9 @@ const registerDevkitCommand = (command: any, commandConfig: any, directoryPath:
const pkgPath = path.join(directoryPath, 'node_modules', packageName);
try {
const devkitConfig = config.loadDevkitConfig(pkgPath);
const { implementation, description, optionsDescription = {} } = devkitConfig.builders[command];
const { implementation, description, optionsDescription, usage = {} } = devkitConfig.builders[command];

const options = getOptionFromCommand(optionsDescription);
const options = getCommandLine(optionsDescription || usage, description, command);
if (Array.isArray(implementation)) {
ctx.commander.register(command, description, async () => {
for (let i = 0; i < implementation.length; i ++) {
Expand Down
28 changes: 7 additions & 21 deletions packages/feflow-cli/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Table from 'easy-table';
import spawn from 'cross-spawn';
import loadPlugins from './plugin/loadPlugins';
import loadDevkits from './devkit/loadDevkits';
import getOptionFromCommand from './devkit/commandOptions';
import getCommandLine from './devkit/commandOptions';
import { FEFLOW_ROOT } from '../shared/constant';
import { safeDump, parseYaml } from '../shared/yaml';
import packageJson from '../shared/packageJson';
Expand Down Expand Up @@ -367,38 +367,24 @@ export default class Feflow {
}

async showCommandOptionDescription(cmd: any, ctx: any): Promise<any> {
let cmdDescription;

let optionDescrition: any = {
header: 'Options',
optionList: [],
};

const registriedCommand = ctx.commander.get(cmd);
let commandLine: object[] = [];

if (registriedCommand && registriedCommand.options) {
cmdDescription = registriedCommand.desc;
optionDescrition.optionList = getOptionFromCommand(registriedCommand.options);
commandLine = getCommandLine(registriedCommand.options, registriedCommand.desc, cmd);
}

if (cmd === "help") {
return registriedCommand.call(this, ctx)
}
if(optionDescrition.optionList.length == 0) {
if(commandLine.length == 0) {
ctx.logger.warn(`Current command dosen't have help message`);
return;
}

const sections = [];
sections.push({
header: `fef ${cmd}`,
content: cmdDescription
})
sections.push({
header: 'Usage',
content: `$ fef ${cmd} [options]`
})
sections.push(optionDescrition);
let sections = [];

sections.push(...commandLine);
const usage = commandLineUsage(sections);

console.log(usage);
Expand Down

0 comments on commit b7549da

Please sign in to comment.