Skip to content

Commit 6c094ab

Browse files
committed
Minor tidying up of the gatsby-transformer-versions-yaml plugin
1 parent d184d02 commit 6c094ab

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

plugins/gatsby-transformer-versions-yaml/create-redirects.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ const {appendFile, exists, readFile, writeFile} = require('fs-extra');
22

33
const HEADER_COMMENT = `## Created with gatsby-transformer-versions-yaml`;
44

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+
) {
611
if (!redirects.length) {
712
return null;
813
}
914

10-
const FILE_PATH = publicFolder(`_redirects`);
11-
1215
// Map redirect data to the format Netlify expects
1316
// https://www.netlify.com/docs/redirects/
1417
redirects = redirects.map(redirect => {
@@ -20,8 +23,7 @@ module.exports = async function writeRedirectsFile(redirects, publicFolder) {
2023
...rest
2124
} = redirect;
2225

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.
2527
const pieces = [
2628
fromPath,
2729
toPath,
@@ -49,9 +51,9 @@ module.exports = async function writeRedirectsFile(redirects, publicFolder) {
4951
// Websites may also have statically defined redirects
5052
// In that case we should append to them (not overwrite)
5153
// 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);
5355
if (fileExists) {
54-
const fileContents = await readFile(FILE_PATH);
56+
const fileContents = await readFile(redirectsFilePath);
5557
if (fileContents.indexOf(HEADER_COMMENT) < 0) {
5658
appendToFile = true;
5759
}
@@ -60,6 +62,6 @@ module.exports = async function writeRedirectsFile(redirects, publicFolder) {
6062
const data = `${HEADER_COMMENT}\n\n${redirects.join(`\n`)}`;
6163

6264
return appendToFile
63-
? appendFile(FILE_PATH, `\n\n${data}`)
64-
: writeFile(FILE_PATH, data);
65+
? appendFile(redirectsFilePath, `\n\n${data}`)
66+
: writeFile(redirectsFilePath, data);
6567
};

plugins/gatsby-transformer-versions-yaml/gatsby-node.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,23 @@ const path = require('path');
77
// Reads versions.yml data into GraphQL.
88
// This is used to generate redirect rules for older documentation versions.
99
exports.onPostBuild = async ({store}) => {
10-
const path = resolve(__dirname, '../../content/versions.yml');
11-
const file = readFileSync(path, 'utf8');
10+
const versionsFile = resolve(__dirname, '../../content/versions.yml');
11+
const file = readFileSync(versionsFile, 'utf8');
1212
const versions = safeLoad(file);
1313

14-
// versions.yml structure is [{title: string, path: string, url: string}, ...]
14+
const {program} = store.getState();
15+
const redirectsFilePath = path.join(
16+
program.directory,
17+
'public',
18+
'_redirects',
19+
);
20+
21+
// versions.yml structure is [{path: string, url: string, ...}, ...]
1522
createRedirects(
1623
versions.map(version => ({
1724
fromPath: version.path,
1825
toPath: version.url,
1926
})),
20-
getPublicFolder(store),
27+
redirectsFilePath,
2128
);
2229
};
23-
24-
function buildPrefixer(prefix, ...paths) {
25-
return (...subpaths) => path.join(prefix, ...paths, ...subpaths);
26-
}
27-
28-
function getPublicFolder(store) {
29-
const {program} = store.getState();
30-
31-
return buildPrefixer(program.directory, `public`);
32-
}

0 commit comments

Comments
 (0)