-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: correct config file reading logic to allow environment configura…
…tions, #171
- Loading branch information
1 parent
6112459
commit d18deb4
Showing
1 changed file
with
34 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`; | ||
}, | ||
}; | ||
}; |