-
Notifications
You must be signed in to change notification settings - Fork 54
/
index.js
88 lines (67 loc) · 2.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const fs = require('fs')
const path = require('path')
const { promisify } = require('util')
const writeFileAsync = promisify(fs.writeFile)
const resolveConfig = require('@netlify/config')
const { getConfigPath } = require('@netlify/config')
const { parseRedirectsFormat, parseNetlifyConfig } = require('netlify-redirect-parser')
const configorama = require('configorama')
async function parseFile(parser, name, filePath) {
const result = await parser(filePath)
if (result.errors.length) {
console.error(`Warnings while parsing ${name} file:`)
result.errors.forEach(err => console.error(` ${err.lineNum}: ${err.line} -- ${err.reason}`))
}
return result.success
}
async function getAll(projectPath) {
const rulesUnique = new Set()
const configPath = await getConfigPath(undefined, projectPath)
const redirectsFilePath = path.resolve(projectPath, '_redirects')
if (fs.existsSync(redirectsFilePath)) {
const fileName = redirectsFilePath
.split(path.sep)
.pop();
(await parseFile(parseRedirectsFormat, fileName, redirectsFilePath))
.forEach(r => rulesUnique.add(JSON.stringify(r)))
}
if (fs.existsSync(configPath)) {
const fileName = configPath
.split(path.sep)
.pop();
(await parseFile(parseNetlifyConfig, fileName, configPath))
.forEach(r => rulesUnique.add(JSON.stringify(r)))
}
return Array.from(rulesUnique).map(item => JSON.parse(item))
}
async function remove(rule = {}, projectPath) {
const rules = (await getAll(projectPath)).filter(
r =>
!(
r.from === rule.from &&
r.to === rule.to &&
r.status === rule.status &&
r.force === rule.force &&
r.query === rule.query &&
JSON.stringify(r.conditions) === JSON.stringify(rule.conditions) &&
JSON.stringify(r.headers) === JSON.stringify(rule.headers) &&
r.signed === rule.signed
),
)
const configPath = await getConfigPath(undefined, projectPath)
const config = await resolveConfig(configPath, { cwd: projectPath })
config.redirects = rules
const TOMLConfig = configorama.format.toml.dump(config)
return writeFileAsync(path.resolve(projectPath, 'netlify.toml'), TOMLConfig, { flag: 'w' })
}
async function add(rule = {}, projectPath) {
const rulesUnique = new Set((await getAll(projectPath)).map(r => JSON.stringify(r)))
rulesUnique.add(JSON.stringify(rule))
const rules = Array.from(rulesUnique).map(item => JSON.parse(item))
const configPath = await getConfigPath(undefined, projectPath)
const config = await resolveConfig(configPath, { cwd: projectPath })
config.redirects = rules
const TOMLConfig = configorama.format.toml.dump(config)
return writeFileAsync(path.resolve(projectPath, 'netlify.toml'), TOMLConfig, { flag: 'w' })
}
module.exports = { getAll, remove, add }