feat(redirects): Generate _redirects on build

This commit is contained in:
Bouncey
2018-10-08 00:00:50 +01:00
committed by Stuart Taylor
parent deff9a92a9
commit cfbb973c8b
7 changed files with 3354 additions and 12 deletions

3137
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,7 @@
"scripts": {
"bootstrap": "lerna bootstrap && lerna run build --scope @freecodecamp/curriculum",
"develop": "npm-run-all -s ensure-env start-develop",
"ensure-env": "node ./tools/scripts/ensure-env.js",
"ensure-env": "cross-env DEBUG=fcc:* node ./tools/scripts/ensure-env.js",
"lint": "echo 'Warning: TODO - Define Linting.'",
"pretest-ci": "npm-run-all -s lint bootstrap",
"seed": "node tools/scripts/seed/seedChallenges",
@ -13,12 +13,15 @@
"test:client": "cd ./client && npm test && cd ../",
"test:curriculum": "echo 'Warning: TODO - Define Testing.'",
"test:server": "echo 'Warning: TODO - Define Testing.'",
"test:tools": "cd ./tools/challenge-md-parser && npm test && cd ../scripts/seed && npm test && cd ../../../",
"test:tools": "jest ./tools",
"start-develop": "node ./tools/scripts/start-develop.js"
},
"devDependencies": {
"cross-env": "^5.2.0",
"debug": "^4.0.1",
"dotenv": "^6.0.0",
"eslint-config-freecodecamp": "^1.1.1",
"jest": "^23.6.0",
"lerna": "^3.4.0",
"npm-run-all": "^4.1.3",
"tree-kill": "^1.2.0"

View File

@ -22,5 +22,6 @@ IMAGE_BASE_URL='https://s3.amazonaws.com/freecodecamp/images/'
HOME_LOCATION='http://localhost:8000'
API_LOCATION='http://localhost:3000'
FORUM_LOCATION='https://forum.localhost'
FORUM_PROXY_LOCATION='https://proxy.localhost'
LOCALE=english

View 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
"
`;

View 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 */

View 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());
});

View File

@ -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));