diff --git a/src/command-core.ts b/src/command-core.ts index a858806..982e2ea 100755 --- a/src/command-core.ts +++ b/src/command-core.ts @@ -311,6 +311,12 @@ const validateOptions = ; }; +const assignParent = (parent: Command, subcommands: Command[]) => + subcommands.forEach((e) => { + e.parent = parent; + if (e.subcommands) assignParent(e, e.subcommands); + }); + export const command = < TOpts extends Record | undefined, TOptsData = TOpts extends Record ? TypeOf : undefined, @@ -368,6 +374,8 @@ export const command = < if (idx !== i) throw new BroCliError(`Can't define command '${cmd.name}' - duplicate alias '${n}'!`); }); + if (cmd.subcommands) assignParent(cmd, cmd.subcommands); + return cmd; }; diff --git a/src/util.ts b/src/util.ts index 855396b..1453617 100644 --- a/src/util.ts +++ b/src/util.ts @@ -14,21 +14,21 @@ export const clone = (data: T, parent?: any): T => { } const origData = Object.entries(data); - - let hasParent = false - const res: Record = {} + + let hasParent = false; + const res: Record = {}; for (const [key, value] of origData) { - if(key === 'parent') { - hasParent = true - continue - } + if (key === 'parent') { + hasParent = true; + continue; + } res[key] = clone(value, res); } - if(hasParent) res['parent'] = parent + if (hasParent) res['parent'] = parent; - return res as T + return res as T; } case 'function': {