Browse Source

Minor tidying up of the gatsby-transformer-versions-yaml plugin

main
Brian Vaughn 7 years ago
parent
commit
6c094ab192
  1. 20
      plugins/gatsby-transformer-versions-yaml/create-redirects.js
  2. 25
      plugins/gatsby-transformer-versions-yaml/gatsby-node.js

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

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

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

@ -7,26 +7,23 @@ const path = require('path');
// Reads versions.yml data into GraphQL. // Reads versions.yml data into GraphQL.
// This is used to generate redirect rules for older documentation versions. // This is used to generate redirect rules for older documentation versions.
exports.onPostBuild = async ({store}) => { exports.onPostBuild = async ({store}) => {
const path = resolve(__dirname, '../../content/versions.yml'); const versionsFile = resolve(__dirname, '../../content/versions.yml');
const file = readFileSync(path, 'utf8'); const file = readFileSync(versionsFile, 'utf8');
const versions = safeLoad(file); const versions = safeLoad(file);
// versions.yml structure is [{title: string, path: string, url: string}, ...] const {program} = store.getState();
const redirectsFilePath = path.join(
program.directory,
'public',
'_redirects',
);
// versions.yml structure is [{path: string, url: string, ...}, ...]
createRedirects( createRedirects(
versions.map(version => ({ versions.map(version => ({
fromPath: version.path, fromPath: version.path,
toPath: version.url, toPath: version.url,
})), })),
getPublicFolder(store), redirectsFilePath,
); );
}; };
function buildPrefixer(prefix, ...paths) {
return (...subpaths) => path.join(prefix, ...paths, ...subpaths);
}
function getPublicFolder(store) {
const {program} = store.getState();
return buildPrefixer(program.directory, `public`);
}

Loading…
Cancel
Save