1 | const fs = require('fs-extra')
|
2 | const getPathnameProjectConfigFile = require('./get-pathname-project-config-file')
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | module.exports = async (key) => {
|
16 | const pathnameKootJS = getPathnameProjectConfigFile()
|
17 |
|
18 | try {
|
19 | const config = require(pathnameKootJS)
|
20 | return config[key]
|
21 | } catch (e) { }
|
22 |
|
23 | const content = await fs.readFile(pathnameKootJS, 'utf-8')
|
24 |
|
25 | {
|
26 | const regex = new RegExp(`${key}[ ]*=[ ]*['"](.+?)['"]`, "gm")
|
27 | const matches = regex.exec(content)
|
28 | if (Array.isArray(matches) && matches.length > 1)
|
29 | return matches[1]
|
30 | }
|
31 |
|
32 | {
|
33 | const regex = new RegExp(`${key}[ ]*=[ ]*{(.+?)}`, "gm")
|
34 | const matches = regex.exec(content)
|
35 | if (Array.isArray(matches) && matches.length > 1) {
|
36 | try {
|
37 | return JSON.parse(`{${matches[1]}}`)
|
38 | } catch (e) {
|
39 | const c = matches[1]
|
40 | .replace(/([: ])require\(['"](.+?)['"]\)\.(\w+?)([}, ])/g, '$1"$2:$3"$4')
|
41 | .replace(/([: ])require\(['"](.+?)['"]\)\.(\w+?)$/g, '$1"$2:$3"')
|
42 | .replace(/([: ])require\(['"](.+?)['"]\)([}, ])/g, '$1"$2"$3')
|
43 | .replace(/([: ])require\(['"](.+?)['"]\)$/g, '$1"$2"')
|
44 | return JSON.parse(`{${c}}`)
|
45 | }
|
46 | }
|
47 | }
|
48 |
|
49 | return undefined
|
50 | }
|