1 | const { readFile } = require('fs')
|
2 | const { promisify } = require('util')
|
3 |
|
4 | const pathExists = require('path-exists')
|
5 | const { parse: loadToml } = require('toml')
|
6 |
|
7 | const { splitResults } = require('./results')
|
8 |
|
9 | const pReadFile = promisify(readFile)
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const parseConfigRedirects = async function (netlifyConfigPath) {
|
15 | if (!(await pathExists(netlifyConfigPath))) {
|
16 | return splitResults([])
|
17 | }
|
18 |
|
19 | const results = await parseConfig(netlifyConfigPath)
|
20 | return splitResults(results)
|
21 | }
|
22 |
|
23 |
|
24 | const parseConfig = async function (configPath) {
|
25 | try {
|
26 | const configString = await pReadFile(configPath, 'utf8')
|
27 | const config = loadToml(configString)
|
28 |
|
29 | const { redirects = [] } = JSON.parse(JSON.stringify(config))
|
30 | if (!Array.isArray(redirects)) {
|
31 | throw new TypeError(`"redirects" must be an array`)
|
32 | }
|
33 | return redirects
|
34 | } catch (error) {
|
35 | return [new Error(`Could not parse configuration file: ${error}`)]
|
36 | }
|
37 | }
|
38 |
|
39 | module.exports = { parseConfigRedirects }
|