Fix cross-framework nested commands

Change-type: patch
Signed-off-by: Scott Lowe <scott@balena.io>
This commit is contained in:
Scott Lowe 2020-07-02 15:56:32 +02:00
parent 6f6cb42725
commit 402e48c8f8
2 changed files with 23 additions and 4 deletions

View File

@ -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] : '';

View File

@ -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<T>(command: string): Promise<T> {
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(