From 46b695cf2219e3f713c5d6ea2a5a4c32016fd068 Mon Sep 17 00:00:00 2001 From: Scott Lowe Date: Thu, 19 Mar 2020 09:45:57 +0100 Subject: [PATCH] Add support for primary/secondary oclif commands Change-type: minor Signed-off-by: Scott Lowe --- lib/actions-oclif/devices/supported.ts | 3 ++- lib/actions-oclif/env/add.ts | 3 ++- lib/actions-oclif/env/rename.ts | 3 ++- lib/actions-oclif/env/rm.ts | 3 ++- lib/actions-oclif/envs.ts | 3 ++- lib/actions-oclif/os/configure.ts | 3 ++- lib/actions-oclif/version.ts | 3 ++- lib/actions/help.coffee | 36 ++++++++++++++------------ lib/actions/help_ts.ts | 28 ++++++++++++-------- lib/command.ts | 26 +++++++++++++++++++ 10 files changed, 77 insertions(+), 34 deletions(-) create mode 100644 lib/command.ts diff --git a/lib/actions-oclif/devices/supported.ts b/lib/actions-oclif/devices/supported.ts index 07857dcb..767745a9 100644 --- a/lib/actions-oclif/devices/supported.ts +++ b/lib/actions-oclif/devices/supported.ts @@ -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'; diff --git a/lib/actions-oclif/env/add.ts b/lib/actions-oclif/env/add.ts index c9a40593..1e19c827 100644 --- a/lib/actions-oclif/env/add.ts +++ b/lib/actions-oclif/env/add.ts @@ -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'; diff --git a/lib/actions-oclif/env/rename.ts b/lib/actions-oclif/env/rename.ts index e94d6ba1..13078c18 100644 --- a/lib/actions-oclif/env/rename.ts +++ b/lib/actions-oclif/env/rename.ts @@ -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'; diff --git a/lib/actions-oclif/env/rm.ts b/lib/actions-oclif/env/rm.ts index 2bcc6c94..6a142e7d 100644 --- a/lib/actions-oclif/env/rm.ts +++ b/lib/actions-oclif/env/rm.ts @@ -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'; diff --git a/lib/actions-oclif/envs.ts b/lib/actions-oclif/envs.ts index 29121314..44d60586 100644 --- a/lib/actions-oclif/envs.ts +++ b/lib/actions-oclif/envs.ts @@ -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'; diff --git a/lib/actions-oclif/os/configure.ts b/lib/actions-oclif/os/configure.ts index 03e674aa..4339f82c 100644 --- a/lib/actions-oclif/os/configure.ts +++ b/lib/actions-oclif/os/configure.ts @@ -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'; diff --git a/lib/actions-oclif/version.ts b/lib/actions-oclif/version.ts index 603665bc..3e5b9b1f 100644 --- a/lib/actions-oclif/version.ts +++ b/lib/actions-oclif/version.ts @@ -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; diff --git a/lib/actions/help.coffee b/lib/actions/help.coffee index 8d6c3a1f..16d16d1e 100644 --- a/lib/actions/help.coffee +++ b/lib/actions/help.coffee @@ -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) diff --git a/lib/actions/help_ts.ts b/lib/actions/help_ts.ts index b9423bd0..c8c94677 100644 --- a/lib/actions/help_ts.ts +++ b/lib/actions/help_ts.ts @@ -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> = []; + 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 diff --git a/lib/command.ts b/lib/command.ts new file mode 100644 index 00000000..8ef08f4d --- /dev/null +++ b/lib/command.ts @@ -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; +}