generated from freeCodeCamp/template
-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: download trending footer links json before builds (#1083)
- Loading branch information
1 parent
90b134c
commit 420d5cd
Showing
6 changed files
with
263 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
const { readFileSync, writeFileSync } = require('fs'); | ||
const path = require('path'); | ||
const fetch = require('node-fetch'); | ||
const yaml = require('js-yaml'); | ||
|
||
const { currentLocale_i18n } = require('../config'); | ||
const trendingSchemaValidator = require('./schemas/trending-schema'); | ||
|
||
const download = async clientLocale => { | ||
const trendingURL = `https://cdn.freecodecamp.org/universal/trending/${clientLocale}.yaml`; | ||
const trendingLocation = path.resolve( | ||
__dirname, | ||
`../config/i18n/locales/${clientLocale}/trending.json` | ||
); | ||
|
||
const loadLocalTrendingJSON = () => { | ||
const localTrendingJSON = readFileSync(trendingLocation, 'utf8'); | ||
|
||
if (!localTrendingJSON) { | ||
throw new Error( | ||
` | ||
---------------------------------------------------- | ||
Error: ${trendingLocation} is missing. | ||
---------------------------------------------------- | ||
` | ||
); | ||
} | ||
|
||
return localTrendingJSON; | ||
}; | ||
|
||
const loadTrendingJSON = async () => { | ||
try { | ||
const res = await fetch(trendingURL); | ||
|
||
if (!res.ok) { | ||
throw new Error( | ||
` | ||
---------------------------------------------------- | ||
Error: The CDN is missing the trending YAML file. | ||
---------------------------------------------------- | ||
Unable to fetch the ${clientLocale} footer: ${res.statusText} | ||
` | ||
); | ||
} | ||
|
||
const data = await res.text(); | ||
const trendingJSON = JSON.stringify(yaml.load(data)); | ||
|
||
return trendingJSON; | ||
} catch (err) { | ||
if (process.env.FREECODECAMP_NODE_ENV === 'production') { | ||
throw new Error(err.message); | ||
} | ||
|
||
return loadLocalTrendingJSON(); | ||
} | ||
}; | ||
|
||
const trendingJSON = await loadTrendingJSON(); | ||
|
||
writeFileSync(trendingLocation, trendingJSON); | ||
|
||
const trendingObject = JSON.parse(trendingJSON); | ||
const validationError = trendingSchemaValidator(trendingObject).error || null; | ||
|
||
if (validationError) { | ||
throw new Error( | ||
` | ||
---------------------------------------------------- | ||
Error: The trending JSON is invalid. | ||
---------------------------------------------------- | ||
Unable to validate the ${clientLocale} trending JSON schema: ${validationError.message} | ||
` | ||
); | ||
} | ||
}; | ||
|
||
if (!currentLocale_i18n) | ||
throw Error('currentLocale_i18n must be set to a valid locale'); | ||
|
||
download(currentLocale_i18n); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const Joi = require('joi'); | ||
|
||
const schema = Joi.object().keys({ | ||
article0title: Joi.string().required(), | ||
article0link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article1title: Joi.string().required(), | ||
article1link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article2title: Joi.string().required(), | ||
article2link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article3title: Joi.string().required(), | ||
article3link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article4title: Joi.string().required(), | ||
article4link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article5title: Joi.string().required(), | ||
article5link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article6title: Joi.string().required(), | ||
article6link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article7title: Joi.string().required(), | ||
article7link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article8title: Joi.string().required(), | ||
article8link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article9title: Joi.string().required(), | ||
article9link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article10title: Joi.string().required(), | ||
article10link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article11title: Joi.string().required(), | ||
article11link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article12title: Joi.string().required(), | ||
article12link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article13title: Joi.string().required(), | ||
article13link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article14title: Joi.string().required(), | ||
article14link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article15title: Joi.string().required(), | ||
article15link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article16title: Joi.string().required(), | ||
article16link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article17title: Joi.string().required(), | ||
article17link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article18title: Joi.string().required(), | ||
article18link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article19title: Joi.string().required(), | ||
article19link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article20title: Joi.string().required(), | ||
article20link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article21title: Joi.string().required(), | ||
article21link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article22title: Joi.string().required(), | ||
article22link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article23title: Joi.string().required(), | ||
article23link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article24title: Joi.string().required(), | ||
article24link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article25title: Joi.string().required(), | ||
article25link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article26title: Joi.string().required(), | ||
article26link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article27title: Joi.string().required(), | ||
article27link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article28title: Joi.string().required(), | ||
article28link: Joi.string().uri({ scheme: 'https' }).required(), | ||
article29title: Joi.string().required(), | ||
article29link: Joi.string().uri({ scheme: 'https' }).required() | ||
}); | ||
|
||
const trendingSchemaValidator = trendingObj => schema.validate(trendingObj); | ||
|
||
module.exports = trendingSchemaValidator; |