Skip to content

Commit aaf3aef

Browse files
committed
refactor: move loadConfig into util
1 parent 10ad27d commit aaf3aef

File tree

2 files changed

+56
-45
lines changed

2 files changed

+56
-45
lines changed

lib/prepare.js

+2-45
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
const path = require('path')
22
const fs = require('fs-extra')
33
const globby = require('globby')
4-
const yamlParser = require('js-yaml')
5-
const tomlParser = require('toml')
64
const createMarkdown = require('./markdown')
5+
const loadConfig = require('./util/loadConfig')
76
const tempPath = path.resolve(__dirname, 'app/.temp')
87
const {
98
inferTitle,
@@ -76,21 +75,7 @@ if (!Object.assign) Object.assign = require('object-assign')`
7675

7776
async function resolveOptions (sourceDir) {
7877
const vuepressDir = path.resolve(sourceDir, '.vuepress')
79-
const configPath = path.resolve(vuepressDir, 'config.js')
80-
const configYmlPath = path.resolve(vuepressDir, 'config.yml')
81-
const configTomlPath = path.resolve(vuepressDir, 'config.toml')
82-
83-
delete require.cache[configPath]
84-
85-
// resolve siteConfig
86-
let siteConfig = {}
87-
if (fs.existsSync(configYmlPath)) {
88-
siteConfig = await parseConfig(configYmlPath)
89-
} else if (fs.existsSync(configTomlPath)) {
90-
siteConfig = await parseConfig(configTomlPath)
91-
} else if (fs.existsSync(configPath)) {
92-
siteConfig = require(configPath)
93-
}
78+
const siteConfig = loadConfig(vuepressDir)
9479

9580
// normalize head tag urls for base
9681
const base = siteConfig.base || '/'
@@ -361,31 +346,3 @@ function sort (arr) {
361346
return 0
362347
})
363348
}
364-
365-
async function parseConfig (file) {
366-
const content = await fs.readFile(file, 'utf-8')
367-
const [extension] = /.\w+$/.exec(file)
368-
let data
369-
370-
switch (extension) {
371-
case '.yml':
372-
case '.yaml':
373-
data = yamlParser.safeLoad(content)
374-
break
375-
376-
case '.toml':
377-
data = tomlParser.parse(content)
378-
// reformat to match config since TOML does not allow different data type
379-
// https://github.com/toml-lang/toml#array
380-
const format = []
381-
Object.keys(data.head).forEach(meta => {
382-
data.head[meta].forEach(values => {
383-
format.push([meta, values])
384-
})
385-
})
386-
data.head = format
387-
break
388-
}
389-
390-
return data || {}
391-
}

lib/util/loadConfig.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const fs = require('fs-extra')
2+
const path = require('path')
3+
const yamlParser = require('js-yaml')
4+
const tomlParser = require('toml')
5+
6+
module.exports = function loadConfig (vuepressDir, bustCache = false) {
7+
const configPath = path.resolve(vuepressDir, 'config.js')
8+
const configYmlPath = path.resolve(vuepressDir, 'config.yml')
9+
const configTomlPath = path.resolve(vuepressDir, 'config.toml')
10+
11+
if (bustCache) {
12+
delete require.cache[configPath]
13+
}
14+
15+
// resolve siteConfig
16+
let siteConfig = {}
17+
if (fs.existsSync(configYmlPath)) {
18+
siteConfig = parseConfig(configYmlPath)
19+
} else if (fs.existsSync(configTomlPath)) {
20+
siteConfig = parseConfig(configTomlPath)
21+
} else if (fs.existsSync(configPath)) {
22+
siteConfig = require(configPath)
23+
}
24+
25+
return siteConfig
26+
}
27+
28+
function parseConfig (file) {
29+
const content = fs.readFileSync(file, 'utf-8')
30+
const [extension] = /.\w+$/.exec(file)
31+
let data
32+
33+
switch (extension) {
34+
case '.yml':
35+
case '.yaml':
36+
data = yamlParser.safeLoad(content)
37+
break
38+
39+
case '.toml':
40+
data = tomlParser.parse(content)
41+
// reformat to match config since TOML does not allow different data type
42+
// https://github.com/toml-lang/toml#array
43+
const format = []
44+
Object.keys(data.head).forEach(meta => {
45+
data.head[meta].forEach(values => {
46+
format.push([meta, values])
47+
})
48+
})
49+
data.head = format
50+
break
51+
}
52+
53+
return data || {}
54+
}

0 commit comments

Comments
 (0)