From d18deb4734aa00d2012cf2545b22eac980276c06 Mon Sep 17 00:00:00 2001 From: nshenderov Date: Tue, 22 Oct 2024 19:29:53 +0300 Subject: [PATCH] fix: correct config file reading logic to allow environment configurations, #171 --- server/services/config.js | 50 ++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/server/services/config.js b/server/services/config.js index 6b13386..35f4d1a 100644 --- a/server/services/config.js +++ b/server/services/config.js @@ -1,25 +1,43 @@ -"use strict"; +'use strict'; -const fs = require("fs"); +const fs = require('fs'); module.exports = ({ strapi }) => { + 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 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; + } + }; + + 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 appDir = process.cwd(); - - const fileName = "ckeditor"; - - for (const ext of ["js", "ts"]) { - const filePath = `${appDir}/config/${fileName}.${ext}`; - if (fs.existsSync(filePath)) { - return ( - fs.readFileSync(filePath, "utf8") + - `\nglobalThis.SH_CKE_CONFIG = CKEConfig()` - ); - } - } + const configFileContent = readConfigFile(); + const config = configFileContent && trimConfig(configFileContent); - return `globalThis.SH_CKE_CONFIG = null`; + return config || `globalThis.SH_CKE_CONFIG = null`; }, }; };