From c35b27cef8dfba89ca1216ea4f0baadb775e3222 Mon Sep 17 00:00:00 2001 From: nshenderov Date: Tue, 22 Oct 2024 19:20:04 +0300 Subject: [PATCH] fix: additional changes to (0fae3ee) to handle moduleDetection:force, closes #172 --- server/src/services/config.js | 48 ++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/server/src/services/config.js b/server/src/services/config.js index 415246e..35f4d1a 100644 --- a/server/src/services/config.js +++ b/server/src/services/config.js @@ -3,29 +3,41 @@ const fs = require('fs'); module.exports = ({ strapi }) => { - return { - getConfig() { - const appDir = process.cwd(); - const isTsProject = fs.existsSync(`${appDir}/dist`); + const readConfigFile = () => { + const appDir = process.cwd(); + const isTSProject = fs.existsSync(`${appDir}/dist`); + const envName = process.env.NODE_ENV; - const cfgDir = isTsProject ? `${appDir}/dist/config` : `${appDir}/config`; - const cfgFileName = 'ckeditor.js'; - const envName = process.env.NODE_ENV; + const cfgDir = isTSProject ? `${appDir}/dist/config` : `${appDir}/config`; + const cfgFileName = 'ckeditor.js'; - const envFilePath = `${cfgDir}/env/${envName}/${cfgFileName}`; - const baseFilePath = `${cfgDir}/${cfgFileName}`; - let fileToRead; + const envFilePath = `${cfgDir}/env/${envName}/${cfgFileName}`; + const baseFilePath = `${cfgDir}/${cfgFileName}`; + + if (fs.existsSync(envFilePath)) { + return fs.readFileSync(envFilePath, 'utf8'); + } else if (fs.existsSync(baseFilePath)) { + return fs.readFileSync(baseFilePath, 'utf8'); + } else { + return null; + } + }; - if (fs.existsSync(envFilePath)) { - fileToRead = envFilePath; - } else if (fs.existsSync(baseFilePath)) { - fileToRead = baseFilePath; + const trimConfig = (str) => { + for (const func of ['const CKEConfig', 'function CKEConfig']) { + const idx = str.indexOf(func); + if (idx >= 0) { + return str.substring(idx) + `\nglobalThis.SH_CKE_CONFIG = CKEConfig()`; } + } + }; + + return { + getConfig() { + const configFileContent = readConfigFile(); + const config = configFileContent && trimConfig(configFileContent); - return fileToRead ? - fs.readFileSync(fileToRead, 'utf8') + - `\nglobalThis.SH_CKE_CONFIG = CKEConfig()` - : `globalThis.SH_CKE_CONFIG = null`; + return config || `globalThis.SH_CKE_CONFIG = null`; }, }; };