mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-21 00:07:01 +08:00
fix(server): prelude should load both local and remote config file (#7852)
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import 'reflect-metadata';
|
import 'reflect-metadata';
|
||||||
|
|
||||||
import { cpSync } from 'node:fs';
|
import { cpSync } from 'node:fs';
|
||||||
import { join } from 'node:path';
|
import { join, parse } from 'node:path';
|
||||||
import { fileURLToPath, pathToFileURL } from 'node:url';
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||||||
|
|
||||||
import { config } from 'dotenv';
|
import { config } from 'dotenv';
|
||||||
@@ -12,20 +12,28 @@ import {
|
|||||||
} from './fundamentals/config';
|
} from './fundamentals/config';
|
||||||
import { enablePlugin } from './plugins';
|
import { enablePlugin } from './plugins';
|
||||||
|
|
||||||
const configDir = join(fileURLToPath(import.meta.url), '../config');
|
const PROJECT_CONFIG_PATH = join(fileURLToPath(import.meta.url), '../config');
|
||||||
async function loadRemote(remoteDir: string, file: string) {
|
async function loadRemote(remoteDir: string, file: string) {
|
||||||
const filePath = join(configDir, file);
|
let fileToLoad = join(PROJECT_CONFIG_PATH, file);
|
||||||
if (configDir !== remoteDir) {
|
|
||||||
cpSync(join(remoteDir, file), filePath, {
|
if (PROJECT_CONFIG_PATH !== remoteDir) {
|
||||||
|
const remoteFile = join(remoteDir, file);
|
||||||
|
const remoteFileAtLocal = join(
|
||||||
|
PROJECT_CONFIG_PATH,
|
||||||
|
parse(file).name + '.remote.js'
|
||||||
|
);
|
||||||
|
cpSync(remoteFile, remoteFileAtLocal, {
|
||||||
force: true,
|
force: true,
|
||||||
});
|
});
|
||||||
|
fileToLoad = remoteFileAtLocal;
|
||||||
}
|
}
|
||||||
|
|
||||||
await import(pathToFileURL(filePath).href);
|
await import(pathToFileURL(fileToLoad).href);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function load() {
|
async function load() {
|
||||||
const AFFiNE_CONFIG_PATH = process.env.AFFINE_CONFIG_PATH ?? configDir;
|
const AFFiNE_CONFIG_PATH =
|
||||||
|
process.env.AFFINE_CONFIG_PATH ?? PROJECT_CONFIG_PATH;
|
||||||
// Initializing AFFiNE config
|
// Initializing AFFiNE config
|
||||||
//
|
//
|
||||||
// 1. load dotenv file to `process.env`
|
// 1. load dotenv file to `process.env`
|
||||||
@@ -44,15 +52,22 @@ async function load() {
|
|||||||
// TODO(@forehalo):
|
// TODO(@forehalo):
|
||||||
// Modules may contribute to ENV_MAP, figure out a good way to involve them instead of hardcoding in `./config/affine.env`
|
// Modules may contribute to ENV_MAP, figure out a good way to involve them instead of hardcoding in `./config/affine.env`
|
||||||
// 3. load env => config map to `globalThis.AFFiNE.ENV_MAP
|
// 3. load env => config map to `globalThis.AFFiNE.ENV_MAP
|
||||||
|
// load local env map as well in case there are new env added
|
||||||
|
await loadRemote(PROJECT_CONFIG_PATH, 'affine.env.js');
|
||||||
|
const projectEnvMap = AFFiNE.ENV_MAP;
|
||||||
await loadRemote(AFFiNE_CONFIG_PATH, 'affine.env.js');
|
await loadRemote(AFFiNE_CONFIG_PATH, 'affine.env.js');
|
||||||
|
const customEnvMap = AFFiNE.ENV_MAP;
|
||||||
|
AFFiNE.ENV_MAP = { ...projectEnvMap, ...customEnvMap };
|
||||||
|
|
||||||
// 4. load `config/affine` to patch custom configs
|
// 4. load `config/affine` to patch custom configs
|
||||||
|
// load local config as well in case there are new default configurations added
|
||||||
|
await loadRemote(PROJECT_CONFIG_PATH, 'affine.js');
|
||||||
await loadRemote(AFFiNE_CONFIG_PATH, 'affine.js');
|
await loadRemote(AFFiNE_CONFIG_PATH, 'affine.js');
|
||||||
|
|
||||||
// 5. load `config/affine.self` to patch custom configs
|
// 5. load `config/affine.self` to patch custom configs
|
||||||
// This is the file only take effect in [AFFiNE Cloud]
|
// This is the file only take effect in [AFFiNE Cloud]
|
||||||
if (!AFFiNE.isSelfhosted) {
|
if (!AFFiNE.isSelfhosted) {
|
||||||
await loadRemote(AFFiNE_CONFIG_PATH, 'affine.self.js');
|
await loadRemote(PROJECT_CONFIG_PATH, 'affine.self.js');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6. apply `process.env` map overriding to `globalThis.AFFiNE`
|
// 6. apply `process.env` map overriding to `globalThis.AFFiNE`
|
||||||
|
|||||||
Reference in New Issue
Block a user