2019-03-12 22:07:57 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2019 Balena Ltd.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2023-09-06 14:39:31 +00:00
|
|
|
import { Parser } from '@oclif/core';
|
2017-12-21 17:40:13 +00:00
|
|
|
import * as ent from 'ent';
|
2019-03-12 22:07:57 +00:00
|
|
|
import * as _ from 'lodash';
|
|
|
|
|
2024-08-22 16:03:37 +00:00
|
|
|
import { getManualSortCompareFunction } from '../../src/utils/helpers';
|
|
|
|
import { capitanoizeOclifUsage } from '../../src/utils/oclif-utils';
|
2024-03-14 12:11:53 +00:00
|
|
|
import type { Category, Document, OclifCommand } from './doc-types';
|
2019-04-02 11:26:21 +00:00
|
|
|
|
2024-09-30 17:56:48 +00:00
|
|
|
function renderOclifCommand(command: Category['commands'][0]): string[] {
|
|
|
|
const result = [`## ${ent.encode(command.name || '')}`];
|
2024-10-08 17:46:19 +00:00
|
|
|
if (command.aliases?.length) {
|
|
|
|
result.push('### Aliases');
|
|
|
|
result.push(command.aliases.map((alias) => `- \`${alias}\``).join('\n'));
|
|
|
|
result.push(
|
|
|
|
`\nTo use one of the aliases, replace \`${command.name}\` with the alias.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
result.push('### Description');
|
2019-04-02 11:26:21 +00:00
|
|
|
const description = (command.description || '')
|
|
|
|
.split('\n')
|
|
|
|
.slice(1) // remove the first line, which oclif uses as help header
|
|
|
|
.join('\n')
|
|
|
|
.trim();
|
|
|
|
result.push(description);
|
|
|
|
|
|
|
|
if (!_.isEmpty(command.examples)) {
|
2020-06-15 22:53:07 +00:00
|
|
|
result.push('Examples:', command.examples!.map((v) => `\t${v}`).join('\n'));
|
2019-04-02 11:26:21 +00:00
|
|
|
}
|
2017-12-21 17:40:13 +00:00
|
|
|
|
2019-04-02 11:26:21 +00:00
|
|
|
if (!_.isEmpty(command.args)) {
|
|
|
|
result.push('### Arguments');
|
2023-09-06 14:39:31 +00:00
|
|
|
for (const [name, arg] of Object.entries(command.args!)) {
|
|
|
|
result.push(`#### ${name.toUpperCase()}`, arg.description || '');
|
2019-04-02 11:26:21 +00:00
|
|
|
}
|
2017-12-21 17:40:13 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 11:26:21 +00:00
|
|
|
if (!_.isEmpty(command.flags)) {
|
|
|
|
result.push('### Options');
|
|
|
|
for (const [name, flag] of Object.entries(command.flags!)) {
|
|
|
|
if (name === 'help') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
flag.name = name;
|
2023-09-06 14:39:31 +00:00
|
|
|
const flagUsage = Parser.flagUsages([flag])
|
2019-04-02 11:26:21 +00:00
|
|
|
.map(([usage, _description]) => usage)
|
|
|
|
.join()
|
|
|
|
.trim();
|
|
|
|
result.push(`#### ${flagUsage}`);
|
|
|
|
result.push(flag.description || '');
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 17:40:13 +00:00
|
|
|
return result;
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2017-12-21 17:40:13 +00:00
|
|
|
|
2019-04-02 11:26:21 +00:00
|
|
|
function renderCategory(category: Category): string[] {
|
|
|
|
const result = [`# ${category.title}`];
|
2019-03-12 22:07:57 +00:00
|
|
|
for (const command of category.commands) {
|
2020-09-04 14:34:34 +00:00
|
|
|
result.push(...renderOclifCommand(command));
|
2017-12-21 17:40:13 +00:00
|
|
|
}
|
|
|
|
return result;
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2017-12-21 17:40:13 +00:00
|
|
|
|
2019-04-02 11:26:21 +00:00
|
|
|
function getAnchor(cmdSignature: string): string {
|
|
|
|
return `#${_.trim(cmdSignature.replace(/\W+/g, '-'), '-').toLowerCase()}`;
|
2017-12-21 17:40:13 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 11:26:21 +00:00
|
|
|
function renderToc(categories: Category[]): string[] {
|
|
|
|
const result = [`# CLI Command Reference`];
|
2017-12-21 17:40:13 +00:00
|
|
|
|
2019-03-12 22:07:57 +00:00
|
|
|
for (const category of categories) {
|
2019-04-02 11:26:21 +00:00
|
|
|
result.push(`- ${category.title}`);
|
|
|
|
result.push(
|
|
|
|
category.commands
|
2020-06-15 22:53:07 +00:00
|
|
|
.map((command) => {
|
2024-09-30 17:56:48 +00:00
|
|
|
const signature = capitanoizeOclifUsage(command.name);
|
2019-04-02 11:26:21 +00:00
|
|
|
return `\t- [${ent.encode(signature)}](${getAnchor(signature)})`;
|
|
|
|
})
|
|
|
|
.join('\n'),
|
|
|
|
);
|
2017-12-21 17:40:13 +00:00
|
|
|
}
|
|
|
|
return result;
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2017-12-21 17:40:13 +00:00
|
|
|
|
2019-04-02 11:26:21 +00:00
|
|
|
const manualCategorySorting: { [category: string]: string[] } = {
|
|
|
|
'Environment Variables': ['envs', 'env rm', 'env add', 'env rename'],
|
2019-10-31 01:46:14 +00:00
|
|
|
OS: [
|
|
|
|
'os versions',
|
|
|
|
'os download',
|
|
|
|
'os build config',
|
|
|
|
'os configure',
|
|
|
|
'os initialize',
|
|
|
|
],
|
2019-04-02 11:26:21 +00:00
|
|
|
};
|
2017-12-21 17:40:13 +00:00
|
|
|
|
2019-04-02 11:26:21 +00:00
|
|
|
function sortCommands(doc: Document): void {
|
2019-03-12 22:07:57 +00:00
|
|
|
for (const category of doc.categories) {
|
2019-04-02 11:26:21 +00:00
|
|
|
if (category.title in manualCategorySorting) {
|
|
|
|
category.commands = category.commands.sort(
|
2020-09-04 14:34:34 +00:00
|
|
|
getManualSortCompareFunction<OclifCommand, string>(
|
2019-04-02 11:26:21 +00:00
|
|
|
manualCategorySorting[category.title],
|
2020-09-04 14:34:34 +00:00
|
|
|
(cmd: OclifCommand, x: string) =>
|
|
|
|
(cmd.usage || '').toString().replace(/\W+/g, ' ').includes(x),
|
2019-04-02 11:26:21 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2017-12-21 17:40:13 +00:00
|
|
|
}
|
2019-04-02 11:26:21 +00:00
|
|
|
}
|
2017-12-21 17:40:13 +00:00
|
|
|
|
2019-04-02 11:26:21 +00:00
|
|
|
export function render(doc: Document) {
|
|
|
|
sortCommands(doc);
|
|
|
|
const result = [
|
|
|
|
`# ${doc.title}`,
|
|
|
|
doc.introduction,
|
|
|
|
...renderToc(doc.categories),
|
|
|
|
];
|
|
|
|
for (const category of doc.categories) {
|
|
|
|
result.push(...renderCategory(category));
|
|
|
|
}
|
|
|
|
return result.join('\n\n');
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|