Skip to content

Commit 7e7dc50

Browse files
committed
Improved parent assignation to subcommands
1 parent bb1bc07 commit 7e7dc50

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/command-core.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,12 @@ const validateOptions = <TOptionConfig extends Record<string, GenericBuilderInte
311311
return Object.fromEntries(entries) as ProcessedOptions<any>;
312312
};
313313

314+
const assignParent = (parent: Command, subcommands: Command[]) =>
315+
subcommands.forEach((e) => {
316+
e.parent = parent;
317+
if (e.subcommands) assignParent(e, e.subcommands);
318+
});
319+
314320
export const command = <
315321
TOpts extends Record<string, GenericBuilderInternals> | undefined,
316322
TOptsData = TOpts extends Record<string, GenericBuilderInternals> ? TypeOf<TOpts> : undefined,
@@ -368,6 +374,8 @@ export const command = <
368374
if (idx !== i) throw new BroCliError(`Can't define command '${cmd.name}' - duplicate alias '${n}'!`);
369375
});
370376

377+
if (cmd.subcommands) assignParent(cmd, cmd.subcommands);
378+
371379
return cmd;
372380
};
373381

src/util.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ export const clone = <T>(data: T, parent?: any): T => {
1414
}
1515

1616
const origData = Object.entries(data);
17-
18-
let hasParent = false
19-
const res: Record<string, any> = {}
17+
18+
let hasParent = false;
19+
const res: Record<string, any> = {};
2020
for (const [key, value] of origData) {
21-
if(key === 'parent') {
22-
hasParent = true
23-
continue
24-
}
21+
if (key === 'parent') {
22+
hasParent = true;
23+
continue;
24+
}
2525

2626
res[key] = clone(value, res);
2727
}
2828

29-
if(hasParent) res['parent'] = parent
29+
if (hasParent) res['parent'] = parent;
3030

31-
return res as T
31+
return res as T;
3232
}
3333

3434
case 'function': {

0 commit comments

Comments
 (0)