feat(redirects): Generate _redirects on build
This commit is contained in:
45
tools/scripts/__snapshots__/createRedirects.test.js.snap
Normal file
45
tools/scripts/__snapshots__/createRedirects.test.js.snap
Normal file
@ -0,0 +1,45 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`createRedirects matches the snapshot 1`] = `
|
||||
"#api redirect
|
||||
|
||||
/internal/* https://api.example.com/internal/:splat
|
||||
|
||||
# auth redirects
|
||||
/signup /signin 301
|
||||
/email-signin /signin 301
|
||||
/login /signin 301
|
||||
/deprecated-signin /signin 301
|
||||
/logout /signout 301
|
||||
/passwordless-change /confirm-email 301
|
||||
|
||||
# certification redirects
|
||||
/:username/front-end-certification /certification/:username/legacy-front-end 301
|
||||
/:username/data-visualization-certification /certification/:username/legacy-data-visualization 301
|
||||
/:username/back-end-certification /certification/:username/legacy-back-end 301
|
||||
/:username/full-stack-certification /certification/:username/full-stack 301
|
||||
|
||||
# unsunscribe redirects
|
||||
/u/* https://api.example.com/u/:splat
|
||||
/unsunscribe/* https://api.example.com/unsunscribe/:splat
|
||||
/ue/* https://api.example.com/ue/:splat
|
||||
|
||||
# misc redirects
|
||||
/agile / 200
|
||||
/chat https://gitter.im/FreeCodeCamp/FreeCodeCamp 301
|
||||
/twitch https://twitch.tv/freecodecamp 301
|
||||
/nonprofits-form / 200
|
||||
/pmi-acp-agile-project-managers / 200
|
||||
/pmi-acp-agile-project-managers-form / 200
|
||||
/stories / 200
|
||||
/all-stories / 200
|
||||
/field-guide/* https://forum.example.com 301
|
||||
/learn-to-code /learn 200
|
||||
/map /learn 200
|
||||
/forum/* https://proxy.example.com 200
|
||||
/privacy https://home.example.com/forum/t/free-code-camp-privacy-policy/19545 301
|
||||
/nonprofit-project-instructions https://home.example.com/forum/t/how-free-code-camps-nonprofits-projects-work/19547 301
|
||||
/how-nonprofit-projects-work https://medium.freecodecamp.org/open-source-for-good-1a0ea9f32d5a 301
|
||||
|
||||
"
|
||||
`;
|
69
tools/scripts/createRedirects.js
Normal file
69
tools/scripts/createRedirects.js
Normal file
@ -0,0 +1,69 @@
|
||||
const apiPlaceholderRE = /#\{\{API\}\}/g;
|
||||
const homePlaceholderRE = /#\{\{HOME\}\}/g;
|
||||
const forumPlacehilderRE = /#\{\{FORUM\}\}/g;
|
||||
const forumProxyPlaceholderRE = /#\{\{FORUM_PROXY\}\}/g;
|
||||
|
||||
exports.createRedirects = function createRedirects(locations) {
|
||||
const { api, home, forum, forumProxy } = locations;
|
||||
|
||||
if (!(api && home && forum && forumProxy)) {
|
||||
throw new Error(`One or more locations are missing, all are required.
|
||||
|
||||
api: ${api}
|
||||
home: ${home}
|
||||
forum: ${forum}
|
||||
forumProxy: ${forumProxy}
|
||||
|
||||
`);
|
||||
}
|
||||
|
||||
return template
|
||||
.replace(apiPlaceholderRE, api)
|
||||
.replace(homePlaceholderRE, home)
|
||||
.replace(forumPlacehilderRE, forum)
|
||||
.replace(forumProxyPlaceholderRE, forumProxy);
|
||||
};
|
||||
|
||||
/* eslint-disable max-len */
|
||||
const template = `#api redirect
|
||||
|
||||
/internal/* #{{API}}/internal/:splat
|
||||
|
||||
# auth redirects
|
||||
/signup /signin 301
|
||||
/email-signin /signin 301
|
||||
/login /signin 301
|
||||
/deprecated-signin /signin 301
|
||||
/logout /signout 301
|
||||
/passwordless-change /confirm-email 301
|
||||
|
||||
# certification redirects
|
||||
/:username/front-end-certification /certification/:username/legacy-front-end 301
|
||||
/:username/data-visualization-certification /certification/:username/legacy-data-visualization 301
|
||||
/:username/back-end-certification /certification/:username/legacy-back-end 301
|
||||
/:username/full-stack-certification /certification/:username/full-stack 301
|
||||
|
||||
# unsunscribe redirects
|
||||
/u/* #{{API}}/u/:splat
|
||||
/unsunscribe/* #{{API}}/unsunscribe/:splat
|
||||
/ue/* #{{API}}/ue/:splat
|
||||
|
||||
# misc redirects
|
||||
/agile / 200
|
||||
/chat https://gitter.im/FreeCodeCamp/FreeCodeCamp 301
|
||||
/twitch https://twitch.tv/freecodecamp 301
|
||||
/nonprofits-form / 200
|
||||
/pmi-acp-agile-project-managers / 200
|
||||
/pmi-acp-agile-project-managers-form / 200
|
||||
/stories / 200
|
||||
/all-stories / 200
|
||||
/field-guide/* #{{FORUM}} 301
|
||||
/learn-to-code /learn 200
|
||||
/map /learn 200
|
||||
/forum/* #{{FORUM_PROXY}} 200
|
||||
/privacy #{{HOME}}/forum/t/free-code-camp-privacy-policy/19545 301
|
||||
/nonprofit-project-instructions #{{HOME}}/forum/t/how-free-code-camps-nonprofits-projects-work/19547 301
|
||||
/how-nonprofit-projects-work https://medium.freecodecamp.org/open-source-for-good-1a0ea9f32d5a 301
|
||||
|
||||
`;
|
||||
/* eslint-enable max-len */
|
72
tools/scripts/createRedirects.test.js
Normal file
72
tools/scripts/createRedirects.test.js
Normal file
@ -0,0 +1,72 @@
|
||||
/* global describe expect */
|
||||
|
||||
const { createRedirects } = require('./createRedirects');
|
||||
|
||||
const testLocations = {
|
||||
api: 'https://api.example.com',
|
||||
home: 'https://home.example.com',
|
||||
forum: 'https://forum.example.com',
|
||||
forumProxy: 'https://proxy.example.com'
|
||||
};
|
||||
|
||||
describe('createRedirects', () => {
|
||||
it('is a function', () => {
|
||||
expect(typeof createRedirects).toEqual('function');
|
||||
});
|
||||
|
||||
it('returns a string', () => {
|
||||
expect(typeof createRedirects(testLocations)).toEqual('string');
|
||||
});
|
||||
|
||||
it('replaces instances of `#{{...}}` with the locations provided', () => {
|
||||
expect.assertions(8);
|
||||
|
||||
const apiPlaceholderRE = /#\{\{API\}\}/;
|
||||
const homePlaceholderRE = /#\{\{HOME\}\}/;
|
||||
const forumPlacehilderRE = /#\{\{FORUM\}\}/;
|
||||
const forumProxyPlaceholderRE = /#\{\{FORUM_PROXY\}\}/;
|
||||
const redirects = createRedirects(testLocations);
|
||||
|
||||
const hasApiPlaceholder = apiPlaceholderRE.test(redirects);
|
||||
const hasHomePlaceholder = homePlaceholderRE.test(redirects);
|
||||
const hasForumPlaceholder = forumPlacehilderRE.test(redirects);
|
||||
const hasForumProxyPlaceholder = forumProxyPlaceholderRE.test(redirects);
|
||||
|
||||
expect(hasApiPlaceholder).toBe(false);
|
||||
expect(hasHomePlaceholder).toBe(false);
|
||||
expect(hasForumPlaceholder).toBe(false);
|
||||
expect(hasForumProxyPlaceholder).toBe(false);
|
||||
|
||||
const { api, home, forum, forumProxy } = testLocations;
|
||||
expect(redirects.includes(`${api}/internal/:splat`)).toBe(true);
|
||||
expect(
|
||||
redirects.includes(
|
||||
`${home}/forum/t/free-code-camp-privacy-policy/19545 301`
|
||||
)
|
||||
).toBe(true);
|
||||
expect(redirects.includes(`${forum} 301`)).toBe(true);
|
||||
expect(redirects.includes(`${forumProxy} 200`)).toBe(true);
|
||||
});
|
||||
|
||||
it('throws when any location is missing', () => {
|
||||
expect.assertions(4);
|
||||
|
||||
const api = 'api';
|
||||
const home = 'home';
|
||||
const forum = 'forum';
|
||||
const forumProxy = 'forumProxy';
|
||||
|
||||
const noApi = { forum, home, forumProxy };
|
||||
const noHome = { api, forum, forumProxy };
|
||||
const noForum = { api, home, forumProxy };
|
||||
const noProxy = { api, home, forum };
|
||||
|
||||
expect(() => createRedirects(noApi)).toThrow();
|
||||
expect(() => createRedirects(noHome)).toThrow();
|
||||
expect(() => createRedirects(noForum)).toThrow();
|
||||
expect(() => createRedirects(noProxy)).toThrow();
|
||||
});
|
||||
|
||||
it('matches the snapshot', () =>
|
||||
expect(createRedirects(testLocations)).toMatchSnapshot());
|
||||
});
|
@ -1,31 +1,54 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const debug = require('debug');
|
||||
|
||||
const envPath = path.resolve(__dirname, '../../.env');
|
||||
require('dotenv').config({ path: envPath });
|
||||
|
||||
const env = require('../../config/env');
|
||||
const { createRedirects } = require('./createRedirects');
|
||||
|
||||
const log = debug('fcc:tools:ensure-env');
|
||||
const {
|
||||
HOME_LOCATION: home,
|
||||
API_LOCATION: api,
|
||||
FORUM_LOCATION: forum,
|
||||
FORUM_PROXY_LOCATION: forumProxy
|
||||
} = process.env;
|
||||
|
||||
const apiPath = path.resolve(__dirname, '../../api-server');
|
||||
const clientPath = path.resolve(__dirname, '../../client');
|
||||
const clientStaticPath = path.resolve(clientPath, 'static');
|
||||
|
||||
const redirects = createRedirects({ api, home, forum, forumProxy });
|
||||
|
||||
fs.writeFile(`${clientStaticPath}/_redirects`, redirects, function(err) {
|
||||
if (err) {
|
||||
log('Error');
|
||||
console.error(err);
|
||||
}
|
||||
log('_redirects written');
|
||||
});
|
||||
|
||||
fs.access(`${apiPath}/server/rev-manifest.json`, function(err) {
|
||||
if (err) {
|
||||
console.log('\n\ncreating manifest\n\n');
|
||||
log('creating manifest');
|
||||
return fs.writeFileSync(`${apiPath}/server/rev-manifest.json`, '{}');
|
||||
}
|
||||
console.log('\n\nrev-manifest present\n\n');
|
||||
log('rev-manifest present');
|
||||
return null;
|
||||
});
|
||||
|
||||
fs.access(`${apiPath}/server/resources/pathMigration.json`, err => {
|
||||
if (err) {
|
||||
console.log('\n\ncreating pathMigration\n\n');
|
||||
log('creating pathMigration');
|
||||
return fs.writeFileSync(
|
||||
`${apiPath}/server/resources/pathMigration.json`,
|
||||
'{}'
|
||||
);
|
||||
}
|
||||
console.log('\n\npathMigration present\n\n');
|
||||
log('pathMigration present');
|
||||
return null;
|
||||
});
|
||||
|
||||
|
||||
fs.writeFileSync(`${clientPath}/config/env.json`, JSON.stringify(env));
|
||||
|
Reference in New Issue
Block a user