2019-04-02 11:26:21 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as _ from 'lodash';
|
2019-08-28 18:51:56 +00:00
|
|
|
import * as path from 'path';
|
2020-03-19 08:45:57 +00:00
|
|
|
import Command from '../command';
|
2019-04-02 11:26:21 +00:00
|
|
|
|
2019-11-09 01:56:11 +00:00
|
|
|
import { capitanoizeOclifUsage } from '../utils/oclif-utils';
|
|
|
|
|
2020-03-19 08:45:57 +00:00
|
|
|
export async function getOclifHelpLinePairs() {
|
2019-08-28 18:51:56 +00:00
|
|
|
const { convertedCommands } = await import('../preparser');
|
2020-03-19 08:45:57 +00:00
|
|
|
const primary: Array<[string, string]> = [];
|
|
|
|
const secondary: Array<[string, string]> = [];
|
|
|
|
|
2019-08-28 18:51:56 +00:00
|
|
|
for (const convertedCmd of convertedCommands) {
|
|
|
|
const [topic, cmd] = convertedCmd.split(':');
|
|
|
|
const pathComponents = ['..', 'actions-oclif', topic];
|
|
|
|
if (cmd) {
|
|
|
|
pathComponents.push(cmd);
|
|
|
|
}
|
2020-03-19 08:45:57 +00:00
|
|
|
|
|
|
|
const cmdModule = await import(path.join(...pathComponents));
|
|
|
|
const command: typeof Command = cmdModule.default;
|
|
|
|
|
|
|
|
if (command.primary) {
|
|
|
|
primary.push(getCmdUsageDescriptionLinePair(command));
|
|
|
|
} else {
|
|
|
|
secondary.push(getCmdUsageDescriptionLinePair(command));
|
|
|
|
}
|
2019-08-28 18:51:56 +00:00
|
|
|
}
|
2020-03-19 08:45:57 +00:00
|
|
|
|
|
|
|
return { primary, secondary };
|
2019-04-02 11:26:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 08:45:57 +00:00
|
|
|
function getCmdUsageDescriptionLinePair(cmd: typeof Command): [string, string] {
|
2019-11-09 01:56:11 +00:00
|
|
|
const usage = capitanoizeOclifUsage(cmd.usage);
|
2019-04-02 11:26:21 +00:00
|
|
|
let description = '';
|
2019-06-05 15:12:16 +00:00
|
|
|
// note: [^] matches any characters (including line breaks), achieving the
|
|
|
|
// same effect as the 's' regex flag which is only supported by Node 9+
|
|
|
|
const matches = /\s*([^]+?)\n[^]*/.exec(cmd.description || '');
|
2019-04-02 11:26:21 +00:00
|
|
|
if (matches && matches.length > 1) {
|
|
|
|
description = _.lowerFirst(_.trimEnd(matches[1], '.'));
|
|
|
|
}
|
|
|
|
return [usage, description];
|
|
|
|
}
|