feat(project): init
This commit is contained in:
51
scripts/load-env.mjs
Normal file
51
scripts/load-env.mjs
Normal file
@@ -0,0 +1,51 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
export function loadEnvFiles({
|
||||
cwd = process.cwd(),
|
||||
files = [".env", ".env.local"],
|
||||
override = false,
|
||||
} = {}) {
|
||||
const originalKeys = new Set(Object.keys(process.env));
|
||||
|
||||
for (const fileName of files) {
|
||||
const filePath = path.join(cwd, fileName);
|
||||
|
||||
try {
|
||||
const source = readFileSync(filePath, "utf8");
|
||||
applyEnvSource(source, { override, originalKeys });
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function applyEnvSource(source, { override, originalKeys }) {
|
||||
for (const line of source.split(/\r?\n/)) {
|
||||
const trimmedLine = line.trim();
|
||||
|
||||
if (!trimmedLine || trimmedLine.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const separatorIndex = trimmedLine.indexOf("=");
|
||||
|
||||
if (separatorIndex === -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const key = trimmedLine.slice(0, separatorIndex).trim();
|
||||
const rawValue = trimmedLine.slice(separatorIndex + 1).trim();
|
||||
const value = rawValue.replace(/^['"]|['"]$/g, "");
|
||||
|
||||
if (!key) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!override && originalKeys.has(key)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
12
scripts/reset-admin.mjs
Normal file
12
scripts/reset-admin.mjs
Normal file
@@ -0,0 +1,12 @@
|
||||
import { loadEnvFiles } from "./load-env.mjs";
|
||||
|
||||
loadEnvFiles({ override: true });
|
||||
|
||||
const { appConfig } = await import("../lib/config.js");
|
||||
const { syncAdminUserFromConfig } = await import("../lib/db.js");
|
||||
|
||||
syncAdminUserFromConfig();
|
||||
|
||||
console.log(
|
||||
`[image-prompt-studio] synced admin user "${appConfig.adminUsername}" from environment`,
|
||||
);
|
||||
27
scripts/start-standalone.mjs
Normal file
27
scripts/start-standalone.mjs
Normal file
@@ -0,0 +1,27 @@
|
||||
import { spawn } from "node:child_process";
|
||||
import path from "node:path";
|
||||
import { loadEnvFiles } from "./load-env.mjs";
|
||||
|
||||
loadEnvFiles();
|
||||
|
||||
const serverEntry = path.join(
|
||||
process.cwd(),
|
||||
".next",
|
||||
"standalone",
|
||||
"server.js",
|
||||
);
|
||||
|
||||
const child = spawn(process.execPath, [serverEntry], {
|
||||
cwd: process.cwd(),
|
||||
env: process.env,
|
||||
stdio: "inherit",
|
||||
});
|
||||
|
||||
child.on("exit", (code, signal) => {
|
||||
if (signal) {
|
||||
process.kill(process.pid, signal);
|
||||
return;
|
||||
}
|
||||
|
||||
process.exit(code ?? 0);
|
||||
});
|
||||
Reference in New Issue
Block a user