mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-01-07 09:03:27 -05:00
feat: get footer from cdn (#47174)
Co-authored-by: ahmad abdolsaheb <ahmad.abdolsaheb@gmail.com> Co-authored-by: scissorsneedfoodtoo <scissorsneedfoodtoo@gmail.com>
This commit is contained in:
47
tools/scripts/build/download-trending.ts
Normal file
47
tools/scripts/build/download-trending.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { writeFileSync } from 'fs';
|
||||
import fetch from 'node-fetch';
|
||||
import yaml from 'js-yaml';
|
||||
import { trendingSchemaValidator } from '../../../client/i18n/schema/trendingSchema';
|
||||
|
||||
import envData from '../../../config/env.json';
|
||||
const { clientLocale } = envData;
|
||||
|
||||
const createCdnUrl = (lang: string) =>
|
||||
`https://cdn.freecodecamp.org/universal/trending/${lang}.yaml`;
|
||||
|
||||
const download = async () => {
|
||||
const url = createCdnUrl(clientLocale);
|
||||
const res = await fetch(url);
|
||||
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));
|
||||
const trendingLocation = `./client/i18n/locales/${clientLocale}/trending.json`;
|
||||
writeFileSync(trendingLocation, trendingJSON);
|
||||
|
||||
const trendingObject = JSON.parse(trendingJSON) as Record<string, string>;
|
||||
const validationError =
|
||||
(trendingSchemaValidator(trendingObject).error as Error) || null;
|
||||
|
||||
if (validationError) {
|
||||
throw new Error(
|
||||
`
|
||||
----------------------------------------------------
|
||||
Error: The trending JSON is invalid.
|
||||
----------------------------------------------------
|
||||
Unable to validate the ${clientLocale} trending JSON schema: ${validationError.message}
|
||||
`
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
void download();
|
||||
@@ -19,8 +19,10 @@
|
||||
"author": "freeCodeCamp <team@freecodecamp.org>",
|
||||
"main": "none",
|
||||
"devDependencies": {
|
||||
"@types/js-yaml": "4.0.5",
|
||||
"debug": "4.3.2",
|
||||
"dotenv": "16.0.3",
|
||||
"js-yaml": "4.1.0",
|
||||
"readdirp": "3.6.0"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user