Skip to content

Commit

Permalink
feat(cli): dry out get table alias function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
joewagner committed Aug 23, 2023
1 parent c4dcb26 commit d167b90
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 33 deletions.
18 changes: 3 additions & 15 deletions packages/cli/src/commands/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const builder: CommandBuilder<Record<string, unknown>, Options> = (
await init();
const { privateKey, providerUrl } = argv;
const chain = getChainName(argv.chain);
let { name } = argv;
const name = await getTableNameWithAlias(argv.aliases, argv.name);

try {
const signer = await getWalletWithProvider({
Expand All @@ -45,10 +45,6 @@ export const builder: CommandBuilder<Record<string, unknown>, Options> = (
providerUrl,
});

// Check if the passed `name` is a table alias
if (argv.aliases != null)
name = await getTableNameWithAlias(argv.aliases, name);

const reg = new Registry({ signer });
const res = await reg.getController(name);

Expand Down Expand Up @@ -76,7 +72,7 @@ export const builder: CommandBuilder<Record<string, unknown>, Options> = (
await init();
const { controller, privateKey, providerUrl } = argv;
const chain = getChainName(argv.chain);
let { name } = argv;
const name = await getTableNameWithAlias(argv.aliases, argv.name);

try {
const signer = await getWalletWithProvider({
Expand All @@ -85,10 +81,6 @@ export const builder: CommandBuilder<Record<string, unknown>, Options> = (
providerUrl,
});

// Check if the passed `name` is a table alias
if (argv.aliases != null)
name = await getTableNameWithAlias(argv.aliases, name);

const reg = new Registry({ signer });
const res = await reg.setController({ tableName: name, controller });

Expand All @@ -113,7 +105,7 @@ export const builder: CommandBuilder<Record<string, unknown>, Options> = (
await init();
const { privateKey, providerUrl } = argv;
const chain = getChainName(argv.chain);
let { name } = argv;
const name = await getTableNameWithAlias(argv.aliases, argv.name);

try {
const signer = await getWalletWithProvider({
Expand All @@ -122,10 +114,6 @@ export const builder: CommandBuilder<Record<string, unknown>, Options> = (
providerUrl,
});

// Check if the passed `name` is a table alias
if (argv.aliases != null)
name = await getTableNameWithAlias(argv.aliases, name);

const reg = new Registry({ signer });
const res = await reg.lockController(name);

Expand Down
5 changes: 1 addition & 4 deletions packages/cli/src/commands/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ export const builder: CommandBuilder<Record<string, unknown>, Options> = (
export const handler = async (argv: Arguments<Options>): Promise<void> => {
await init();
try {
let { name } = argv;
let name = await getTableNameWithAlias(argv.aliases, argv.name);

// Check if the passed `name` is a table alias
if (argv.aliases != null)
name = await getTableNameWithAlias(argv.aliases, name);
// Check if the passed `name` uses ENS
// Note: duplicative `setupCommand` calls will occur with ENS, but this is
// required to properly parse the chainId from the table name
Expand Down
5 changes: 1 addition & 4 deletions packages/cli/src/commands/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ export const builder: CommandBuilder<Record<string, unknown>, Options> = (
export const handler = async (argv: Arguments<Options>): Promise<void> => {
try {
await init();
let { name } = argv;
let name = await getTableNameWithAlias(argv.aliases, argv.name);

// Check if the passed `name` is a table alias
if (argv.aliases != null)
name = await getTableNameWithAlias(argv.aliases, name);
// Check if the passed `name` uses ENS
// Note: duplicative `setupCommand` calls will occur with ENS, but this is
// required to properly parse the chainId from the table name
Expand Down
6 changes: 1 addition & 5 deletions packages/cli/src/commands/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ export const handler = async (argv: Arguments<Options>): Promise<void> => {
try {
await init();
const { receiver, chain } = argv;
let { name } = argv;

// Check if the passed `name` is a table alias
if (argv.aliases != null)
name = await getTableNameWithAlias(argv.aliases, name);
const name = await getTableNameWithAlias(argv.aliases, argv.name);

const tableDetails = await globalThis.sqlparser.validateTableName(name);
const chainId = tableDetails.chainId;
Expand Down
9 changes: 5 additions & 4 deletions packages/cli/src/commands/write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ export const handler = async (argv: Arguments<Options>): Promise<void> => {
normalized.statements.map(async function (stmt) {
// re-normalize so we can be sure we've isolated each statement and it's tableId
const norm = (await normalize(stmt)) as NormalizedStatement;
let name = norm.tables[0];
// Check if the passed `name` is a table alias
if (argv.aliases != null)
name = await getTableNameWithAlias(argv.aliases, name);
const name = await getTableNameWithAlias(
argv.aliases,
norm.tables[0]
);

const { tableId } = await globalThis.sqlparser.validateTableName(
name
);
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,11 @@ export function jsonFileAliases(filepath: string): AliasesNameMap {
}

export async function getTableNameWithAlias(
filepath: string,
filepath: unknown,
name: string
): Promise<string> {
if (typeof filepath !== "string" || filepath.trim() === "") return name;

const nameMap = await jsonFileAliases(filepath).read();
const uuName = nameMap[name];
return uuName ?? name;
Expand Down

0 comments on commit d167b90

Please sign in to comment.