UNPKG

698 BJavaScriptView Raw
1const fs = require('fs-extra')
2const getPathnameProjectConfigFile = require('./get-pathname-project-config-file')
3
4/**
5 * 从 koot.js 中读取对应配置
6 * @param {String} key
7 * @returns {*}
8 */
9module.exports = async (key) => {
10 const pathnameKootJS = getPathnameProjectConfigFile()
11
12 try {
13 const config = require(pathnameKootJS)
14 return config[key]
15 } catch (e) { }
16
17 const content = await fs.readFile(pathnameKootJS, 'utf-8')
18 const regex = new RegExp(`${key}[ ]*=[ ]*['"](.+?)['"]`, "gm")
19 const matches = regex.exec(content)
20
21 if (Array.isArray(matches) && matches.length > 1)
22 return matches[1]
23
24 return undefined
25}