-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
create-redirects.js
72 lines (59 loc) · 1.9 KB
/
create-redirects.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
import { HEADER_COMMENT } from "./constants"
import { appendFile, exists, readFile, writeFile } from "fs-extra"
export default async function writeRedirectsFile(
pluginData,
redirects,
rewrites
) {
const { publicFolder } = pluginData
if (!redirects.length && !rewrites.length) return null
const FILE_PATH = publicFolder(`_redirects`)
// Map redirect data to the format Netlify expects
// https://www.netlify.com/docs/redirects/
redirects = redirects.map(redirect => {
const {
fromPath,
isPermanent,
redirectInBrowser, // eslint-disable-line no-unused-vars
toPath,
...rest
} = redirect
// The order of the first 3 parameters is significant.
// The order for rest params (key-value pairs) is arbitrary.
const pieces = [
fromPath,
toPath,
isPermanent ? 301 : 302, // Status
]
for (let key in rest) {
const value = rest[key]
if (typeof value === `string` && value.indexOf(` `) >= 0) {
console.warn(
`Invalid redirect value "${value}" specified for key "${key}". ` +
`Values should not contain spaces.`
)
} else {
pieces.push(`${key}=${value}`)
}
}
return pieces.join(` `)
})
rewrites = rewrites.map(
({ fromPath, toPath }) => `${fromPath} ${toPath} 200`
)
let appendToFile = false
// Websites may also have statically defined redirects
// In that case we should append to them (not overwrite)
// Make sure we aren't just looking at previous build results though
const fileExists = await exists(FILE_PATH)
if (fileExists) {
const fileContents = await readFile(FILE_PATH)
if (fileContents.indexOf(HEADER_COMMENT) < 0) {
appendToFile = true
}
}
const data = `${HEADER_COMMENT}\n\n${[...redirects, ...rewrites].join(`\n`)}`
return appendToFile
? appendFile(FILE_PATH, `\n\n${data}`)
: writeFile(FILE_PATH, data)
}