Skip to content

Commit

Permalink
Returned package-based cloning, improved cloning, removed unnecessary…
Browse files Browse the repository at this point in the history
… steps
  • Loading branch information
Sukairo-02 committed Jul 9, 2024
1 parent 7e7dc50 commit c9ffbe1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 51 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@drizzle-team/brocli",
"type": "module",
"author": "Drizzle Team",
"version": "0.8.0",
"version": "0.8.1",
"description": "Typed CLI command runner",
"license": "Apache-2.0",
"sideEffects": false,
Expand All @@ -26,8 +26,10 @@
"devDependencies": {
"@arethetypeswrong/cli": "^0.15.3",
"@originjs/vite-plugin-commonjs": "^1.0.3",
"@types/clone": "^2.1.4",
"@types/node": "^20.12.13",
"@types/shell-quote": "^1.7.5",
"clone": "^2.1.2",
"dprint": "^0.46.2",
"shell-quote": "^1.8.1",
"tsup": "^8.1.0",
Expand Down
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions src/command-core.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import clone from 'clone';
import { parse as parseQuotes } from 'shell-quote';
import { BroCliError } from './brocli-error';
import { defaultTheme } from './help-themes';
Expand All @@ -9,7 +10,7 @@ import {
type ProcessedOptions,
type TypeOf,
} from './option-builder';
import { clone, isInt } from './util';
import { isInt } from './util';

// Type area
export type HelpHandler = (calledFor: Command | Command[]) => any;
Expand Down Expand Up @@ -374,7 +375,10 @@ 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);
if (cmd.subcommands) {
cmd.subcommands = clone(cmd.subcommands);
assignParent(cmd, cmd.subcommands);
}

return cmd;
};
Expand Down Expand Up @@ -652,15 +656,11 @@ export const getCommandNameRecursive = (command: Command): string =>
command.parent ? `${getCommandNameRecursive(command.parent)} ${command.name}` : command.name;

const validateCommands = (commands: Command[], parent?: Command) => {
const cloned = parent ? commands : clone(commands);

const storedNames: Record<string, [string, ...string[]]> = {};

for (const cmd of cloned) {
for (const cmd of commands) {
const storageVals = Object.values(storedNames);

cmd.parent = parent;

for (const storage of storageVals) {
const nameOccupier = storage.find((e) => e === cmd.name);

Expand Down Expand Up @@ -696,7 +696,7 @@ const validateCommands = (commands: Command[], parent?: Command) => {
if (cmd.subcommands) cmd.subcommands = validateCommands(cmd.subcommands, cmd) as [Command, ...Command[]];
}

return cloned;
return commands;
};

const removeByIndex = <T>(arr: T[], idx: number): T[] => [...arr.slice(0, idx), ...arr.slice(idx + 1, arr.length)];
Expand Down
42 changes: 0 additions & 42 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,3 @@
export function isInt(value: number) {
return value === Math.floor(value);
}

/**
* Warning: do not use on classes - breaks instanceof, this
*/
export const clone = <T>(data: T, parent?: any): T => {
switch (typeof data) {
case 'object': {
if (data === null) return data;
if (Array.isArray(data)) {
return data.map((d) => clone(d)) as T;
}

const origData = Object.entries(data);

let hasParent = false;
const res: Record<string, any> = {};
for (const [key, value] of origData) {
if (key === 'parent') {
hasParent = true;
continue;
}

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

if (hasParent) res['parent'] = parent;

return res as T;
}

case 'function': {
return data;
}

case 'undefined': {
return data;
}

default:
return JSON.parse(JSON.stringify(data));
}
};

0 comments on commit c9ffbe1

Please sign in to comment.