Merge pull request #1673 from balena-io/support-oclif-secondary

Add support for primary/secondary oclif commands
This commit is contained in:
srlowe 2020-03-19 16:12:51 +01:00 committed by GitHub
commit c2f0f9a894
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 77 additions and 34 deletions

View File

@ -14,10 +14,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Command, flags } from '@oclif/command';
import { flags } from '@oclif/command';
import * as SDK from 'balena-sdk';
import { stripIndent } from 'common-tags';
import * as _ from 'lodash';
import Command from '../../command';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, getVisuals } from '../../utils/lazy';

View File

@ -15,10 +15,11 @@
* limitations under the License.
*/
import { Command, flags } from '@oclif/command';
import { flags } from '@oclif/command';
import * as BalenaSdk from 'balena-sdk';
import { stripIndent } from 'common-tags';
import * as _ from 'lodash';
import Command from '../../command';
import { ExpectedError } from '../../errors';
import * as cf from '../../utils/common-flags';

View File

@ -14,8 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Command, flags } from '@oclif/command';
import { flags } from '@oclif/command';
import { stripIndent } from 'common-tags';
import Command from '../../command';
import * as cf from '../../utils/common-flags';
import * as ec from '../../utils/env-common';

View File

@ -15,8 +15,9 @@
* limitations under the License.
*/
import { Command, flags } from '@oclif/command';
import { flags } from '@oclif/command';
import { stripIndent } from 'common-tags';
import Command from '../../command';
import * as ec from '../../utils/env-common';
import { getBalenaSdk } from '../../utils/lazy';

View File

@ -14,10 +14,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Command, flags } from '@oclif/command';
import { flags } from '@oclif/command';
import * as SDK from 'balena-sdk';
import { stripIndent } from 'common-tags';
import * as _ from 'lodash';
import Command from '../command';
import { ExpectedError } from '../errors';
import * as cf from '../utils/common-flags';

View File

@ -15,12 +15,13 @@
* limitations under the License.
*/
import { Command, flags } from '@oclif/command';
import { flags } from '@oclif/command';
import BalenaSdk = require('balena-sdk');
import Bluebird = require('bluebird');
import { stripIndent } from 'common-tags';
import * as _ from 'lodash';
import * as path from 'path';
import Command from '../../command';
import { ExpectedError } from '../../errors';
import * as cf from '../../utils/common-flags';

View File

@ -15,8 +15,9 @@
* limitations under the License.
*/
import { Command, flags } from '@oclif/command';
import { flags } from '@oclif/command';
import { stripIndent } from 'common-tags';
import Command from '../command';
interface FlagsDef {
all?: boolean;

View File

@ -77,31 +77,35 @@ general = (params, options, done) ->
commands = _.reject capitano.state.commands, (command) ->
return command.hidden or command.isWildcard()
groupedCommands = _.groupBy commands, (command) ->
capitanoCommands = _.groupBy commands, (command) ->
if command.primary
return 'primary'
return 'secondary'
print parse(groupedCommands.primary).sort(getManualSortCompareFunction(
manuallySortedPrimaryCommands,
([signature, description], manualItem) ->
signature == manualItem or signature.startsWith("#{manualItem} ")
))
getOclifHelpLinePairs()
.then (oclifHelpLinePairs) ->
if options.verbose
console.log('\nAdditional commands:\n')
secondaryCommandPromise = getOclifHelpLinePairs()
.then (oclifHelpLinePairs) ->
print parse(groupedCommands.secondary).concat(oclifHelpLinePairs).sort()
else
console.log('\nRun `balena help --verbose` to list additional commands')
secondaryCommandPromise = Promise.resolve()
primaryHelpLinePairs =
parse(capitanoCommands.primary).concat(oclifHelpLinePairs.primary).sort(getManualSortCompareFunction(
manuallySortedPrimaryCommands,
([signature, description], manualItem) ->
signature == manualItem or signature.startsWith("#{manualItem} ")
))
secondaryHelpLinePairs = parse(capitanoCommands.secondary).concat(oclifHelpLinePairs.secondary).sort()
print primaryHelpLinePairs
if options.verbose
console.log('\nAdditional commands:\n')
print secondaryHelpLinePairs
else
console.log('\nRun `balena help --verbose` to list additional commands')
secondaryCommandPromise
.then ->
if not _.isEmpty(capitano.state.globalOptions)
console.log('\nGlobal Options:\n')
print parse(capitano.state.globalOptions).sort()
done()
.catch(done)

View File

@ -15,32 +15,38 @@
* limitations under the License.
*/
import { Command } from '@oclif/command';
import * as Bluebird from 'bluebird';
import * as _ from 'lodash';
import * as path from 'path';
import Command from '../command';
import { capitanoizeOclifUsage } from '../utils/oclif-utils';
export async function getOclifHelpLinePairs(): Promise<
Array<[string, string]>
> {
export async function getOclifHelpLinePairs() {
const { convertedCommands } = await import('../preparser');
const cmdClasses: Array<Promise<typeof Command>> = [];
const primary: Array<[string, string]> = [];
const secondary: Array<[string, string]> = [];
for (const convertedCmd of convertedCommands) {
const [topic, cmd] = convertedCmd.split(':');
const pathComponents = ['..', 'actions-oclif', topic];
if (cmd) {
pathComponents.push(cmd);
}
// note that `import(path)` returns a promise
cmdClasses.push(import(path.join(...pathComponents)));
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));
}
}
return Bluebird.map(cmdClasses, getCmdUsageDescriptionLinePair);
return { primary, secondary };
}
function getCmdUsageDescriptionLinePair(cmdModule: any): [string, string] {
const cmd: typeof Command = cmdModule.default;
function getCmdUsageDescriptionLinePair(cmd: typeof Command): [string, string] {
const usage = capitanoizeOclifUsage(cmd.usage);
let description = '';
// note: [^] matches any characters (including line breaks), achieving the

26
lib/command.ts Normal file
View File

@ -0,0 +1,26 @@
/**
* @license
* Copyright 2020 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 Command from '@oclif/command';
export default abstract class extends Command {
/**
* When set to true, command will be listed in `help`,
* otherwise listed in `help --verbose` with secondary commands.
*/
public static primary = false;
}