@reliverse/rse
Version:
@reliverse/rse is your all-in-one companion for bootstrapping and improving any kind of projects (especially web apps built with frameworks like Next.js) — whether you're kicking off something new or upgrading an existing app. It is also a little AI-power
83 lines (82 loc) • 2.45 kB
JavaScript
import path from "@reliverse/pathkit";
import fs from "@reliverse/relifso";
import { relinka } from "@reliverse/relinka";
import { defineArgs, defineCommand, runCmd } from "@reliverse/rempts";
import { execaCommand } from "execa";
import MagicString from "magic-string";
import { getAuthGenerateCmd } from "../../cmds.js";
import { configPath, schemaPath } from "./consts.js";
const notice = `/**
* THIS FILE IS AUTO-GENERATED - DO NOT EDIT DIRECTLY
*
* To modify the schema, edit src/lib/auth.ts instead,
* then run 'bun db:auth' to regenerate this file.
*
* Any direct changes to this file will be overwritten.
*/
`;
export default defineCommand({
meta: {
name: "auth",
version: "1.1.2"
},
args: defineArgs({
config: {
type: "string",
default: configPath,
required: true
},
schema: {
type: "string",
default: schemaPath,
required: true
}
}),
async run({ args }) {
const configExists = await fs.exists(args.config);
if (!configExists) {
relinka("error", "Config file does not exist; tried:", args.config);
process.exit(1);
}
const schemaExists = await fs.exists(args.schema);
if (!schemaExists) {
relinka("error", "Schema file does not exist; tried:", args.schema);
process.exit(1);
}
await runCmd(await getAuthGenerateCmd(), [
`--config ${configPath} --output ${schemaPath}`
]);
const filePath = path.resolve(schemaPath);
const originalContent = await fs.readFile(filePath, "utf8");
const s = new MagicString(originalContent);
s.prepend(notice);
s.replace(
/export const (\w+) = pgTable/g,
(_match, tableName) => {
return `export const ${tableName}Table = pgTable`;
}
);
const tableNames = [];
const tableMatches = originalContent.matchAll(
/export const (\w+) = pgTable/g
);
for (const match of tableMatches) {
if (match[1]) {
tableNames.push(match[1]);
}
}
console.log("\u221A Ensured better-auth tables:", tableNames);
for (const tableName of tableNames) {
s.replace(
new RegExp(`\\(\\)\\s*=>\\s*${tableName}\\s*\\.`, "g"),
(match) => {
return match.replace(tableName, `${tableName}Table`);
}
);
}
await fs.writeFile(filePath, s.toString(), "utf8");
await execaCommand("bun biome check --write .", {
stdio: "inherit"
});
}
});