feat: 完善安装部署与数字溯源码
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
import { existsSync, readFileSync } from 'fs';
|
||||
import { dirname, resolve } from 'path';
|
||||
|
||||
export interface RuntimeConfigFile {
|
||||
appName?: string;
|
||||
port?: number;
|
||||
baseUrl?: string;
|
||||
adminOrigin?: string;
|
||||
clientOrigin?: string;
|
||||
databaseUrl?: string;
|
||||
redis?: { host?: string; port?: number; password?: string };
|
||||
jwt?: { secret?: string; expiresIn?: string };
|
||||
settingsEncryptionKey?: string;
|
||||
storage?: {
|
||||
driver?: 'local' | 'oss';
|
||||
localDir?: string;
|
||||
publicPrefix?: string;
|
||||
cdnBaseUrl?: string;
|
||||
oss?: {
|
||||
endpoint?: string;
|
||||
region?: string;
|
||||
bucket?: string;
|
||||
accessKeyId?: string;
|
||||
accessKeySecret?: string;
|
||||
};
|
||||
};
|
||||
backupDir?: string;
|
||||
blockchain?: {
|
||||
driver?: 'mock' | 'tianhe';
|
||||
tianhe?: { baseUrl?: string; appId?: string; appKey?: string; userId?: string; userKey?: string };
|
||||
};
|
||||
}
|
||||
|
||||
export function runtimeRoot(): string {
|
||||
return resolve(process.env.ARTVERIS_RUNTIME_DIR || process.env.RUNTIME_DIR || resolve(process.cwd(), 'runtime'));
|
||||
}
|
||||
|
||||
export function runtimeConfigPath(): string {
|
||||
return resolve(process.env.ARTVERIS_CONFIG_PATH || resolve(runtimeRoot(), 'artveris.config.json'));
|
||||
}
|
||||
|
||||
export function runtimeEnvPath(): string {
|
||||
return resolve(dirname(runtimeConfigPath()), '.env');
|
||||
}
|
||||
|
||||
export function loadRuntimeConfig(): RuntimeConfigFile {
|
||||
const path = runtimeConfigPath();
|
||||
if (!existsSync(path)) return {};
|
||||
try {
|
||||
return JSON.parse(readFileSync(path, 'utf8')) as RuntimeConfigFile;
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function loadEnvFile(path: string): void {
|
||||
if (!existsSync(path)) return;
|
||||
const text = readFileSync(path, 'utf8');
|
||||
for (const rawLine of text.split(/\r?\n/)) {
|
||||
const line = rawLine.trim();
|
||||
if (!line || line.startsWith('#')) continue;
|
||||
const eq = line.indexOf('=');
|
||||
if (eq <= 0) continue;
|
||||
const key = line.slice(0, eq).trim();
|
||||
if (process.env[key] !== undefined) continue;
|
||||
let value = line.slice(eq + 1).trim();
|
||||
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
||||
value = value.slice(1, -1);
|
||||
}
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
export function preloadRuntimeEnv(): void {
|
||||
loadEnvFile(resolve(process.cwd(), '.env'));
|
||||
loadEnvFile(resolve(process.cwd(), '../../.env'));
|
||||
loadEnvFile(runtimeEnvPath());
|
||||
}
|
||||
|
||||
export function hasRuntimeConfig(): boolean {
|
||||
return existsSync(runtimeConfigPath());
|
||||
}
|
||||
|
||||
export function isInstallMode(): boolean {
|
||||
preloadRuntimeEnv();
|
||||
if (process.env.ARTVERIS_INSTALL_MODE === '1') return true;
|
||||
if (process.env.ARTVERIS_INSTALL_MODE === '0') return false;
|
||||
if (process.env.DATABASE_URL) return false;
|
||||
return !hasRuntimeConfig();
|
||||
}
|
||||
Reference in New Issue
Block a user