From 402e48c8f89e2e42b35c73a14ee6f4a5884f0e1a Mon Sep 17 00:00:00 2001 From: Scott Lowe Date: Thu, 2 Jul 2020 15:56:32 +0200 Subject: [PATCH] Fix cross-framework nested commands Change-type: patch Signed-off-by: Scott Lowe --- lib/preparser.ts | 3 ++- lib/utils/helpers.ts | 24 +++++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/preparser.ts b/lib/preparser.ts index a6d4d018..e2f975f7 100644 --- a/lib/preparser.ts +++ b/lib/preparser.ts @@ -150,6 +150,7 @@ export const convertedCommands = [ 'api-key:generate', 'device', 'device:identify', + 'device:init', 'device:move', 'device:os-update', 'device:public-url', @@ -195,7 +196,7 @@ export const convertedCommands = [ * oclif "topics" (colon-separated subcommands like `env:add`) * @param argvSlice process.argv.slice(2) */ -function isOclifCommand(argvSlice: string[]): [boolean, boolean] { +export function isOclifCommand(argvSlice: string[]): [boolean, boolean] { // Look for commands that have been transitioned to oclif // const { convertedCommands } = require('oclif/utils/command'); const arg0 = argvSlice.length > 0 ? argvSlice[0] : ''; diff --git a/lib/utils/helpers.ts b/lib/utils/helpers.ts index eddc649a..d5bf58c6 100644 --- a/lib/utils/helpers.ts +++ b/lib/utils/helpers.ts @@ -25,6 +25,7 @@ import type { Device, PineOptionsFor } from 'balena-sdk'; import { ExpectedError } from '../errors'; import { getBalenaSdk, getChalk, getVisuals } from './lazy'; import { promisify } from 'util'; +import { isOclifCommand } from '../preparser'; export function getGroupDefaults(group: { options: Array<{ name: string; default?: string }>; @@ -96,9 +97,26 @@ export async function sudo( } export function runCommand(command: string): Promise { - const capitano = require('capitano') as typeof import('capitano'); - const capitanoRunAsync = promisify(capitano.run); - return capitanoRunAsync(command); + let commandArgs = command.split(' '); + const [isOclif, isOclifTopic] = isOclifCommand(commandArgs); + if (isOclif) { + if (isOclifTopic) { + commandArgs = [ + commandArgs[0] + ':' + commandArgs[1], + ...commandArgs.slice(2), + ]; + } + const { run } = require('@oclif/command'); + return run(commandArgs); + } else { + const capitano = require('capitano') as typeof import('capitano'); + // Need to require app-capitano to register capitano commands, + // in case calling command is oclif + require('../app-capitano'); + + const capitanoRunAsync = promisify(capitano.run); + return capitanoRunAsync(command); + } } export async function getManifest(