Skip to content

Commit

Permalink
Improved parent assignation to subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
Sukairo-02 committed Jul 9, 2024
1 parent bb1bc07 commit 7e7dc50
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/command-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,12 @@ const validateOptions = <TOptionConfig extends Record<string, GenericBuilderInte
return Object.fromEntries(entries) as ProcessedOptions<any>;
};

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<string, GenericBuilderInternals> | undefined,
TOptsData = TOpts extends Record<string, GenericBuilderInternals> ? TypeOf<TOpts> : undefined,
Expand Down Expand Up @@ -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;
};

Expand Down
18 changes: 9 additions & 9 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ export const clone = <T>(data: T, parent?: any): T => {
}

const origData = Object.entries(data);
let hasParent = false
const res: Record<string, any> = {}

let hasParent = false;
const res: Record<string, any> = {};
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': {
Expand Down

0 comments on commit 7e7dc50

Please sign in to comment.