99 lines
1.9 KiB
TypeScript
99 lines
1.9 KiB
TypeScript
/* eslint-disable unicorn/no-process-exit */
|
|
/**
|
|
* @file Script utilities
|
|
*/
|
|
|
|
import {constants, writeFile} from "node:fs/promises";
|
|
import {dirname, join} from "node:path";
|
|
import {fileURLToPath} from "node:url";
|
|
|
|
import {execa} from "execa";
|
|
|
|
import {getSchema, getStatus} from "#/supabase/supabase";
|
|
|
|
/**
|
|
* Project root directory
|
|
*/
|
|
export const root = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
|
|
/**
|
|
* Frontend environment file
|
|
*/
|
|
const frontendEnv = join(root, ".env");
|
|
|
|
/**
|
|
* Environment file flags
|
|
*/
|
|
const envFlags = constants.O_CREAT | constants.O_EXCL | constants.O_WRONLY;
|
|
|
|
/**
|
|
* Write environment files
|
|
*/
|
|
export const writeEnvs = async () => {
|
|
// Get the status
|
|
const status = await getStatus();
|
|
|
|
// Create the environment files
|
|
try {
|
|
await writeFile(
|
|
frontendEnv,
|
|
`VITE_HCAPTCHA_SITE_KEY = "" # Required!
|
|
VITE_SUPABASE_URL = ${JSON.stringify(status.apiUrl)}
|
|
VITE_SUPABASE_ANON_KEY = ${JSON.stringify(status.anonKey)}
|
|
VITE_SENTRY_DSN = "" # Required!`,
|
|
{
|
|
flag: envFlags,
|
|
},
|
|
);
|
|
} catch (error) {
|
|
if ((error as any)?.code !== "EEXIST") {
|
|
throw error;
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Frontend schema file
|
|
*/
|
|
const frontendSchema = join(root, "src", "lib", "schema.ts");
|
|
|
|
/**
|
|
* Write Supabase schema files
|
|
*/
|
|
export const writeSupabaseSchema = async () => {
|
|
// Generate the content
|
|
const content = `/**
|
|
* @file Supabase schema
|
|
*
|
|
* This file is automatically generated by npm run supabase:schema. DO NOT EDIT THIS FILE!
|
|
*/
|
|
|
|
${await getSchema()}`;
|
|
|
|
// Write the files
|
|
await writeFile(frontendSchema, content);
|
|
|
|
const {all, exitCode, failed} = await execa(
|
|
"eslint",
|
|
[
|
|
frontendSchema,
|
|
"--fix",
|
|
"--debug",
|
|
],
|
|
{
|
|
all: true,
|
|
cwd: root,
|
|
preferLocal: true,
|
|
reject: false,
|
|
},
|
|
);
|
|
|
|
if (failed) {
|
|
console.error(
|
|
`Formatting Supabase schema failed (Exit code ${exitCode}): ${all}`,
|
|
);
|
|
|
|
process.exit(1);
|
|
}
|
|
};
|