@@ -2,13 +2,16 @@ const {appendFile, exists, readFile, writeFile} = require('fs-extra');
2
2
3
3
const HEADER_COMMENT = `## Created with gatsby-transformer-versions-yaml` ;
4
4
5
- module . exports = async function writeRedirectsFile ( redirects , publicFolder ) {
5
+ // Patterned after the 'gatsby-plugin-netlify' plug-in:
6
+ // https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-netlify/src/create-redirects.js
7
+ module . exports = async function writeRedirectsFile (
8
+ redirects ,
9
+ redirectsFilePath ,
10
+ ) {
6
11
if ( ! redirects . length ) {
7
12
return null ;
8
13
}
9
14
10
- const FILE_PATH = publicFolder ( `_redirects` ) ;
11
-
12
15
// Map redirect data to the format Netlify expects
13
16
// https://www.netlify.com/docs/redirects/
14
17
redirects = redirects . map ( redirect => {
@@ -20,8 +23,7 @@ module.exports = async function writeRedirectsFile(redirects, publicFolder) {
20
23
...rest
21
24
} = redirect ;
22
25
23
- // The order of the first 3 parameters is significant.
24
- // The order for rest params (key-value pairs) is arbitrary.
26
+ // The order of these parameters is significant.
25
27
const pieces = [
26
28
fromPath ,
27
29
toPath ,
@@ -49,9 +51,9 @@ module.exports = async function writeRedirectsFile(redirects, publicFolder) {
49
51
// Websites may also have statically defined redirects
50
52
// In that case we should append to them (not overwrite)
51
53
// Make sure we aren't just looking at previous build results though
52
- const fileExists = await exists ( FILE_PATH ) ;
54
+ const fileExists = await exists ( redirectsFilePath ) ;
53
55
if ( fileExists ) {
54
- const fileContents = await readFile ( FILE_PATH ) ;
56
+ const fileContents = await readFile ( redirectsFilePath ) ;
55
57
if ( fileContents . indexOf ( HEADER_COMMENT ) < 0 ) {
56
58
appendToFile = true ;
57
59
}
@@ -60,6 +62,6 @@ module.exports = async function writeRedirectsFile(redirects, publicFolder) {
60
62
const data = `${ HEADER_COMMENT } \n\n${ redirects . join ( `\n` ) } ` ;
61
63
62
64
return appendToFile
63
- ? appendFile ( FILE_PATH , `\n\n${ data } ` )
64
- : writeFile ( FILE_PATH , data ) ;
65
+ ? appendFile ( redirectsFilePath , `\n\n${ data } ` )
66
+ : writeFile ( redirectsFilePath , data ) ;
65
67
} ;
0 commit comments