fix: Fix _redirects file
parent
9a4e13954a
commit
f9ff43d128
|
@ -36,9 +36,11 @@ exports[`createRedirects matches the snapshot 1`] = `
|
|||
/field-guide/* /forum 301
|
||||
/learn-to-code /learn 200
|
||||
/map /learn 200
|
||||
/news https://news.example.com
|
||||
/news/* https://news.example.com/:splat
|
||||
/forum/* https://forum.example.com/:splat
|
||||
/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
|
||||
/privacy https://forum.example.com/t/free-code-camp-privacy-policy/19545 301
|
||||
/nonprofit-project-instructions https://forum.example.com/t/how-free-code-camps-nonprofits-projects-work/19547 301
|
||||
/how-nonprofit-projects-work https://medium.freecodecamp.org/open-source-for-good-1a0ea9f32d5a 301
|
||||
|
||||
"
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
const apiPlaceholderRE = /#\{\{API\}\}/g;
|
||||
const homePlaceholderRE = /#\{\{HOME\}\}/g;
|
||||
const newsPlaceholderRE = /#\{\{NEWS\}\}/g;
|
||||
const forumPlacehilderRE = /#\{\{FORUM\}\}/g;
|
||||
|
||||
exports.createRedirects = function createRedirects(locations) {
|
||||
const { api, home, forum } = locations;
|
||||
const { api, news, forum } = locations;
|
||||
|
||||
if (!(api && home && forum )) {
|
||||
if (!(api && news && forum)) {
|
||||
throw new Error(`One or more locations are missing, all are required.
|
||||
|
||||
api: ${api}
|
||||
home: ${home}
|
||||
news: ${news}
|
||||
forum: ${forum}
|
||||
|
||||
`);
|
||||
|
@ -17,7 +17,7 @@ exports.createRedirects = function createRedirects(locations) {
|
|||
|
||||
return template
|
||||
.replace(apiPlaceholderRE, api)
|
||||
.replace(homePlaceholderRE, home)
|
||||
.replace(newsPlaceholderRE, news)
|
||||
.replace(forumPlacehilderRE, forum);
|
||||
};
|
||||
|
||||
|
@ -57,9 +57,11 @@ const template = `#api redirect
|
|||
/field-guide/* /forum 301
|
||||
/learn-to-code /learn 200
|
||||
/map /learn 200
|
||||
/news #{{NEWS}}
|
||||
/news/* #{{NEWS}}/:splat
|
||||
/forum/* #{{FORUM}}/:splat
|
||||
/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
|
||||
/privacy #{{FORUM}}/t/free-code-camp-privacy-policy/19545 301
|
||||
/nonprofit-project-instructions #{{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
|
||||
|
||||
`;
|
||||
|
|
|
@ -4,7 +4,7 @@ const { createRedirects } = require('./createRedirects');
|
|||
|
||||
const testLocations = {
|
||||
api: 'https://api.example.com',
|
||||
home: 'https://home.example.com',
|
||||
news: 'https://news.example.com',
|
||||
forum: 'https://forum.example.com',
|
||||
};
|
||||
|
||||
|
@ -18,29 +18,26 @@ describe('createRedirects', () => {
|
|||
});
|
||||
|
||||
it('replaces instances of `#{{...}}` with the locations provided', () => {
|
||||
expect.assertions(7);
|
||||
expect.assertions(6);
|
||||
|
||||
const apiPlaceholderRE = /#\{\{API\}\}/;
|
||||
const homePlaceholderRE = /#\{\{HOME\}\}/;
|
||||
const forumPlacehilderRE = /#\{\{FORUM\}\}/;
|
||||
const forumProxyPlaceholderRE = /#\{\{FORUM_PROXY\}\}/;
|
||||
const newsPlaceholderRE = /#\{\{NEWS\}\}/;
|
||||
const forumPlaceholderRE = /#\{\{FORUM\}\}/;
|
||||
const redirects = createRedirects(testLocations);
|
||||
|
||||
const hasApiPlaceholder = apiPlaceholderRE.test(redirects);
|
||||
const hasHomePlaceholder = homePlaceholderRE.test(redirects);
|
||||
const hasForumPlaceholder = forumPlacehilderRE.test(redirects);
|
||||
const hasForumProxyPlaceholder = forumProxyPlaceholderRE.test(redirects);
|
||||
const hasNewsPlaceholder = newsPlaceholderRE.test(redirects);
|
||||
const hasForumPlaceholder = forumPlaceholderRE.test(redirects);
|
||||
|
||||
expect(hasApiPlaceholder).toBe(false);
|
||||
expect(hasHomePlaceholder).toBe(false);
|
||||
expect(hasNewsPlaceholder).toBe(false);
|
||||
expect(hasForumPlaceholder).toBe(false);
|
||||
expect(hasForumProxyPlaceholder).toBe(false);
|
||||
|
||||
const { api, home, forum } = testLocations;
|
||||
const { api, forum } = testLocations;
|
||||
expect(redirects.includes(`${api}/internal/:splat`)).toBe(true);
|
||||
expect(
|
||||
redirects.includes(
|
||||
`${home}/forum/t/free-code-camp-privacy-policy/19545 301`
|
||||
`${forum}/t/free-code-camp-privacy-policy/19545 301`
|
||||
)
|
||||
).toBe(true);
|
||||
expect(redirects.includes(`${forum}`)).toBe(true);
|
||||
|
@ -50,15 +47,15 @@ describe('createRedirects', () => {
|
|||
expect.assertions(3);
|
||||
|
||||
const api = 'api';
|
||||
const home = 'home';
|
||||
const news = 'news';
|
||||
const forum = 'forum';
|
||||
|
||||
const noApi = { forum, home };
|
||||
const noHome = { api, forum };
|
||||
const noForum = { api, home };
|
||||
const noApi = { forum, news };
|
||||
const noNews = { api, forum };
|
||||
const noForum = { api, news };
|
||||
|
||||
expect(() => createRedirects(noApi)).toThrow();
|
||||
expect(() => createRedirects(noHome)).toThrow();
|
||||
expect(() => createRedirects(noNews)).toThrow();
|
||||
expect(() => createRedirects(noForum)).toThrow();
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue