Skip to content

Commit

Permalink
fix: correct config file reading logic to allow environment configura…
Browse files Browse the repository at this point in the history
…tions, #171
  • Loading branch information
nshenderov committed Oct 22, 2024
1 parent 6112459 commit d18deb4
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions server/services/config.js
Original file line number Diff line number Diff line change
@@ -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`;
},
};
};

0 comments on commit d18deb4

Please sign in to comment.