Skip to content

Commit

Permalink
chore(refactor): kebab-case /shared/db
Browse files Browse the repository at this point in the history
  • Loading branch information
arcoraven committed Dec 10, 2024
1 parent 07c1493 commit e9ed959
Show file tree
Hide file tree
Showing 169 changed files with 297 additions and 152 deletions.
145 changes: 145 additions & 0 deletions renamer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import fs from "fs";
import path from "path";

// Convert camelCase to kebab-case
function toKebabCase(filename: string): string {
return filename.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
}

// Iteratively process files and directories
function processFilesAndFolders(rootDir: string) {
const queue: string[] = [rootDir];

while (queue.length > 0) {
const currentPath = queue.pop()!;
const entries = fs.readdirSync(currentPath, { withFileTypes: true });

for (const entry of entries) {
const fullPath = path.join(currentPath, entry.name);

// Handle directories first
if (entry.isDirectory()) {
const newName = toKebabCase(entry.name);
if (newName !== entry.name) {
const newPath = path.join(currentPath, newName);

if (fs.existsSync(newPath)) {
console.error(
`Conflict: ${newPath} already exists. Skipping ${fullPath}`,
);
continue;
}

console.log(`Renaming directory: ${fullPath} -> ${newPath}`);
fs.renameSync(fullPath, newPath);

// Update imports after renaming
updateImports(fullPath, newPath);

// Add the renamed directory back to the queue for further processing
queue.push(newPath);
} else {
queue.push(fullPath); // If no renaming, continue processing the directory
}
} else {
// Handle files
const newName = toKebabCase(entry.name);
if (newName !== entry.name) {
const newPath = path.join(currentPath, newName);

if (fs.existsSync(newPath)) {
console.error(
`Conflict: ${newPath} already exists. Skipping ${fullPath}`,
);
continue;
}

console.log(`Renaming file: ${fullPath} -> ${newPath}`);
fs.renameSync(fullPath, newPath);

// Update imports after renaming
updateImports(fullPath, newPath);
}
}
}
}
}

// Corrected `updateImports` function
function updateImports(oldPath: string, newPath: string) {
const projectRoot = path.resolve("./"); // Adjust project root if needed
const allFiles = getAllFiles(projectRoot, /\.(ts|js|tsx|jsx)$/);

const normalizedOldPath = normalizePath(oldPath);
const normalizedNewPath = normalizePath(newPath);

for (const file of allFiles) {
let content = fs.readFileSync(file, "utf-8");

const relativeOldPath = normalizePath(
formatRelativePath(file, normalizedOldPath),
);
const relativeNewPath = normalizePath(
formatRelativePath(file, normalizedNewPath),
);

const importRegex = new RegExp(
`(['"\`])(${escapeRegex(relativeOldPath)})(['"\`])`,
"g",
);

if (importRegex.test(content)) {
const updatedContent = content.replace(
importRegex,
`$1${relativeNewPath}$3`,
);
fs.writeFileSync(file, updatedContent, "utf-8");
console.log(`Updated imports in: ${file}`);
}
}
}

// Normalize file paths for consistent imports
function normalizePath(p: string): string {
return p.replace(/\\/g, "/").replace(/\.[tj]sx?$/, ""); // Ensure forward slashes and remove extensions
}

// Format paths as relative imports
function formatRelativePath(from: string, to: string): string {
const relativePath = path.relative(path.dirname(from), to);
return normalizePath(
relativePath.startsWith(".") ? relativePath : `./${relativePath}`,
); // Ensure ./ prefix
}

// Escape special regex characters in paths
function escapeRegex(string: string): string {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

// Get all project files iteratively
function getAllFiles(rootDir: string, extensions: RegExp): string[] {
const result: string[] = [];
const stack: string[] = [rootDir];

while (stack.length > 0) {
const currentDir = stack.pop()!;
const entries = fs.readdirSync(currentDir, { withFileTypes: true });

for (const entry of entries) {
const fullPath = path.join(currentDir, entry.name);

if (entry.isDirectory() && !entry.name.startsWith(".")) {
stack.push(fullPath);
} else if (extensions.test(entry.name)) {
result.push(fullPath);
}
}
}

return result;
}

// Run the script
const projectRoot = path.resolve("./src/shared/db"); // Change as needed
processFilesAndFolders(projectRoot);
6 changes: 3 additions & 3 deletions src/server/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import type { FastifyInstance } from "fastify";
import type { FastifyRequest } from "fastify/types/request";
import jsonwebtoken, { type JwtPayload } from "jsonwebtoken";
import { validate as uuidValidate } from "uuid";
import { getPermissions } from "../../shared/db/permissions/getPermissions";
import { createToken } from "../../shared/db/tokens/createToken";
import { revokeToken } from "../../shared/db/tokens/revokeToken";
import { getPermissions } from "../../shared/db/permissions/get-permissions";
import { createToken } from "../../shared/db/tokens/create-token";
import { revokeToken } from "../../shared/db/tokens/revoke-token";
import { WebhooksEventTypes } from "../../shared/schemas/webhooks";
import { THIRDWEB_DASHBOARD_ISSUER, handleSiwe } from "../../shared/utils/auth";
import { getAccessToken } from "../../shared/utils/cache/accessToken";
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/admin/nonces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
lastUsedNonceKey,
recycledNoncesKey,
sentNoncesKey,
} from "../../../shared/db/wallets/walletNonce";
} from "../../../shared/db/wallets/wallet-nonce";
import { getChain } from "../../../shared/utils/chain";
import { redis } from "../../../shared/utils/redis/redis";
import { thirdwebClient } from "../../../shared/utils/sdk";
Expand Down
4 changes: 2 additions & 2 deletions src/server/routes/auth/access-tokens/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { buildJWT } from "@thirdweb-dev/auth";
import { LocalWallet } from "@thirdweb-dev/wallets";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { updateConfiguration } from "../../../../shared/db/configuration/updateConfiguration";
import { createToken } from "../../../../shared/db/tokens/createToken";
import { updateConfiguration } from "../../../../shared/db/configuration/update-configuration";
import { createToken } from "../../../../shared/db/tokens/create-token";
import { accessTokenCache } from "../../../../shared/utils/cache/accessToken";
import { getConfig } from "../../../../shared/utils/cache/getConfig";
import { env } from "../../../../shared/utils/env";
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/auth/access-tokens/getAll.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { getAccessTokens } from "../../../../shared/db/tokens/getAccessTokens";
import { getAccessTokens } from "../../../../shared/db/tokens/get-access-tokens";
import { AddressSchema } from "../../../schemas/address";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";

Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/auth/access-tokens/revoke.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { revokeToken } from "../../../../shared/db/tokens/revokeToken";
import { revokeToken } from "../../../../shared/db/tokens/revoke-token";
import { accessTokenCache } from "../../../../shared/utils/cache/accessToken";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";

Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/auth/access-tokens/update.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { updateToken } from "../../../../shared/db/tokens/updateToken";
import { updateToken } from "../../../../shared/db/tokens/update-token";
import { accessTokenCache } from "../../../../shared/utils/cache/accessToken";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";

Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/auth/permissions/grant.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { updatePermissions } from "../../../../shared/db/permissions/updatePermissions";
import { updatePermissions } from "../../../../shared/db/permissions/update-permissions";
import { AddressSchema } from "../../../schemas/address";
import { permissionsSchema } from "../../../../shared/schemas/auth";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/auth/permissions/revoke.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { deletePermissions } from "../../../../shared/db/permissions/deletePermissions";
import { deletePermissions } from "../../../../shared/db/permissions/delete-permissions";
import { AddressSchema } from "../../../schemas/address";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";

Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/backend-wallet/getAll.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { getAllWallets } from "../../../shared/db/wallets/getAllWallets";
import { getAllWallets } from "../../../shared/db/wallets/get-all-wallets";
import {
standardResponseSchema,
walletDetailsSchema,
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/backend-wallet/getNonce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import type { Address } from "thirdweb";
import { inspectNonce } from "../../../shared/db/wallets/walletNonce";
import { inspectNonce } from "../../../shared/db/wallets/wallet-nonce";
import { standardResponseSchema } from "../../schemas/sharedApiSchemas";
import { walletWithAddressParamSchema } from "../../schemas/wallet";
import { getChainIdFromChain } from "../../utils/chain";
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/backend-wallet/getTransactionsByNonce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { TransactionDB } from "../../../shared/db/transactions/db";
import { getNonceMap } from "../../../shared/db/wallets/nonceMap";
import { getNonceMap } from "../../../shared/db/wallets/nonce-map";
import { normalizeAddress } from "../../../shared/utils/primitiveTypes";
import type { AnyTransaction } from "../../../shared/utils/transaction/types";
import { standardResponseSchema } from "../../schemas/sharedApiSchemas";
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/backend-wallet/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type Static, Type } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import type { Address } from "thirdweb";
import { deleteWalletDetails } from "../../../shared/db/wallets/deleteWalletDetails";
import { deleteWalletDetails } from "../../../shared/db/wallets/delete-wallet-details";
import { AddressSchema } from "../../schemas/address";
import { standardResponseSchema } from "../../schemas/sharedApiSchemas";

Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/backend-wallet/reset-nonces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
deleteNoncesForBackendWallets,
getUsedBackendWallets,
syncLatestNonceFromOnchain,
} from "../../../shared/db/wallets/walletNonce";
} from "../../../shared/db/wallets/wallet-nonce";
import { AddressSchema } from "../../schemas/address";
import { standardResponseSchema } from "../../schemas/sharedApiSchemas";

Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/backend-wallet/signMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { arbitrumSepolia } from "thirdweb/chains";
import {
getWalletDetails,
isSmartBackendWallet,
} from "../../../shared/db/wallets/getWalletDetails";
} from "../../../shared/db/wallets/get-wallet-details";
import { walletDetailsToAccount } from "../../../shared/utils/account";
import { getChain } from "../../../shared/utils/chain";
import { createCustomError } from "../../middleware/error";
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/backend-wallet/update.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type Static, Type } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { updateWalletDetails } from "../../../shared/db/wallets/updateWalletDetails";
import { updateWalletDetails } from "../../../shared/db/wallets/update-wallet-details";
import { AddressSchema } from "../../schemas/address";
import { standardResponseSchema } from "../../schemas/sharedApiSchemas";

Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/configuration/auth/update.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type Static, Type } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { updateConfiguration } from "../../../../shared/db/configuration/updateConfiguration";
import { updateConfiguration } from "../../../../shared/db/configuration/update-configuration";
import { getConfig } from "../../../../shared/utils/cache/getConfig";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";
import { responseBodySchema } from "./get";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { updateConfiguration } from "../../../../shared/db/configuration/updateConfiguration";
import { updateConfiguration } from "../../../../shared/db/configuration/update-configuration";
import { getConfig } from "../../../../shared/utils/cache/getConfig";
import { WeiAmountStringSchema } from "../../../schemas/number";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/configuration/cache/update.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type Static, Type } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { updateConfiguration } from "../../../../shared/db/configuration/updateConfiguration";
import { updateConfiguration } from "../../../../shared/db/configuration/update-configuration";
import { getConfig } from "../../../../shared/utils/cache/getConfig";
import { clearCacheCron } from "../../../../shared/utils/cron/clearCacheCron";
import { isValidCron } from "../../../../shared/utils/cron/isValidCron";
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/configuration/chains/update.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { updateConfiguration } from "../../../../shared/db/configuration/updateConfiguration";
import { updateConfiguration } from "../../../../shared/db/configuration/update-configuration";
import { getConfig } from "../../../../shared/utils/cache/getConfig";
import { sdkCache } from "../../../../shared/utils/cache/getSdk";
import { chainResponseSchema } from "../../../schemas/chain";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { updateConfiguration } from "../../../../shared/db/configuration/updateConfiguration";
import { updateConfiguration } from "../../../../shared/db/configuration/update-configuration";
import { getConfig } from "../../../../shared/utils/cache/getConfig";
import { createCustomError } from "../../../middleware/error";
import {
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/configuration/cors/add.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type Static, Type } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { updateConfiguration } from "../../../../shared/db/configuration/updateConfiguration";
import { updateConfiguration } from "../../../../shared/db/configuration/update-configuration";
import { getConfig } from "../../../../shared/utils/cache/getConfig";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";
import { mandatoryAllowedCorsUrls } from "../../../utils/cors-urls";
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/configuration/cors/remove.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type Static, Type } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { updateConfiguration } from "../../../../shared/db/configuration/updateConfiguration";
import { updateConfiguration } from "../../../../shared/db/configuration/update-configuration";
import { getConfig } from "../../../../shared/utils/cache/getConfig";
import { createCustomError } from "../../../middleware/error";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/configuration/cors/set.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { updateConfiguration } from "../../../../shared/db/configuration/updateConfiguration";
import { updateConfiguration } from "../../../../shared/db/configuration/update-configuration";
import { getConfig } from "../../../../shared/utils/cache/getConfig";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";
import { mandatoryAllowedCorsUrls } from "../../../utils/cors-urls";
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/configuration/ip/set.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type Static, Type } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { updateConfiguration } from "../../../../shared/db/configuration/updateConfiguration";
import { updateConfiguration } from "../../../../shared/db/configuration/update-configuration";
import { getConfig } from "../../../../shared/utils/cache/getConfig";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";
import { responseBodySchema } from "./get";
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/configuration/transactions/update.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { updateConfiguration } from "../../../../shared/db/configuration/updateConfiguration";
import { updateConfiguration } from "../../../../shared/db/configuration/update-configuration";
import { getConfig } from "../../../../shared/utils/cache/getConfig";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";

Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/configuration/wallets/update.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { updateConfiguration } from "../../../../shared/db/configuration/updateConfiguration";
import { updateConfiguration } from "../../../../shared/db/configuration/update-configuration";
import { WalletType } from "../../../../shared/schemas/wallet";
import { getConfig } from "../../../../shared/utils/cache/getConfig";
import { createCustomError } from "../../../middleware/error";
Expand Down
4 changes: 2 additions & 2 deletions src/server/routes/contract/events/getContractEventLogs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Type, type Static } from "@sinclair/typebox";
import type { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { getContractEventLogsByBlockAndTopics } from "../../../../shared/db/contractEventLogs/getContractEventLogs";
import { isContractSubscribed } from "../../../../shared/db/contractSubscriptions/getContractSubscriptions";
import { getContractEventLogsByBlockAndTopics } from "../../../../shared/db/contractEventLogs/get-contract-event-logs";

Check failure on line 4 in src/server/routes/contract/events/getContractEventLogs.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module '../../../../shared/db/contractEventLogs/get-contract-event-logs' or its corresponding type declarations.
import { isContractSubscribed } from "../../../../shared/db/contractSubscriptions/get-contract-subscriptions";

Check failure on line 5 in src/server/routes/contract/events/getContractEventLogs.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module '../../../../shared/db/contractSubscriptions/get-contract-subscriptions' or its corresponding type declarations.
import { createCustomError } from "../../../middleware/error";
import { AddressSchema, TransactionHashSchema } from "../../../schemas/address";
import {
Expand Down
Loading

0 comments on commit e9ed959

Please sign in to comment.