mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-26 01:06:23 +02:00
feat(desktop): add resolve-env for runtime placeholder replacement
This commit is contained in:
parent
4b117ac09e
commit
caed67eb3f
1 changed files with 69 additions and 0 deletions
69
surfsense_desktop/src/resolve-env.ts
Normal file
69
surfsense_desktop/src/resolve-env.ts
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
// TODO: Placeholders are gone after the first run. Self-hosted users
|
||||||
|
// cannot change their backend URL without reinstalling.
|
||||||
|
|
||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
|
const DEFAULTS: Record<string, string> = {
|
||||||
|
__NEXT_PUBLIC_FASTAPI_BACKEND_URL__: 'http://localhost:8000',
|
||||||
|
__NEXT_PUBLIC_FASTAPI_BACKEND_AUTH_TYPE__: 'LOCAL',
|
||||||
|
__NEXT_PUBLIC_ETL_SERVICE__: 'DOCLING',
|
||||||
|
__NEXT_PUBLIC_ELECTRIC_URL__: 'http://localhost:5133',
|
||||||
|
__NEXT_PUBLIC_ELECTRIC_AUTH_MODE__: 'insecure',
|
||||||
|
__NEXT_PUBLIC_DEPLOYMENT_MODE__: 'self-hosted',
|
||||||
|
};
|
||||||
|
|
||||||
|
function walk(dir: string, replacements: [string, string][]) {
|
||||||
|
let entries: fs.Dirent[];
|
||||||
|
try {
|
||||||
|
entries = fs.readdirSync(dir, { withFileTypes: true });
|
||||||
|
} catch {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (const entry of entries) {
|
||||||
|
const full = path.join(dir, entry.name);
|
||||||
|
if (entry.isDirectory()) {
|
||||||
|
walk(full, replacements);
|
||||||
|
} else if (entry.name.endsWith('.js')) {
|
||||||
|
let content = fs.readFileSync(full, 'utf8');
|
||||||
|
let changed = false;
|
||||||
|
for (const [placeholder, value] of replacements) {
|
||||||
|
if (content.includes(placeholder)) {
|
||||||
|
content = content.replaceAll(placeholder, value);
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (changed) {
|
||||||
|
fs.writeFileSync(full, content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveEnv(standalonePath: string, overrides?: Record<string, string>) {
|
||||||
|
const replacements: [string, string][] = Object.entries(DEFAULTS).map(
|
||||||
|
([placeholder, defaultValue]) => {
|
||||||
|
const envKey = placeholder.replace(/^__|__$/g, '');
|
||||||
|
const value = overrides?.[envKey] ?? process.env[envKey] ?? defaultValue;
|
||||||
|
return [placeholder, value];
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log('[resolve-env] Replacing placeholders in standalone build:');
|
||||||
|
for (const [placeholder, value] of replacements) {
|
||||||
|
console.log(` ${placeholder} -> ${value}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
walk(path.join(standalonePath, '.next'), replacements);
|
||||||
|
|
||||||
|
const serverJs = path.join(standalonePath, 'server.js');
|
||||||
|
if (fs.existsSync(serverJs)) {
|
||||||
|
let content = fs.readFileSync(serverJs, 'utf8');
|
||||||
|
for (const [placeholder, value] of replacements) {
|
||||||
|
if (content.includes(placeholder)) {
|
||||||
|
content = content.replaceAll(placeholder, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fs.writeFileSync(serverJs, content);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue