refactor(client): ts-migration of i18n/validate-keys.js (#44292)

* refactor - file extenstion change to .ts

* fix - imports syntax

* fix - migration complete

* fix - lint:prettier warn

* refactor - validate-keys in tools/scripts/lint/

* refactor - validate-keys.ts moved to tools/scripts/lint and added in tsconfig.json

* refactor - tsconfig.json file for tools

* fix - lint:ts script update and validate-keys.js added to .eslintignore

* fix - lint:prettier and ts compilation

* fix - ts compilation

* refactor - changes in .gitignore and validate-keys script
This commit is contained in:
Vinay
2021-11-30 22:07:00 +05:30
committed by GitHub
parent bcb0465a50
commit 5b61c8a56f
9 changed files with 59 additions and 42 deletions

View File

@ -5,6 +5,7 @@ api-server/src/public/**
api-server/lib/** api-server/lib/**
tools/contributor/** tools/contributor/**
tools/scripts/build/ensure-env.js tools/scripts/build/ensure-env.js
tools/scripts/lint/validate-keys.js
tools/scripts/build/tsconfig.tsbuildinfo tools/scripts/build/tsconfig.tsbuildinfo
config/i18n/all-langs.js config/i18n/all-langs.js
config/certification-settings.js config/certification-settings.js

View File

@ -47,7 +47,7 @@
"project": [ "project": [
"./tsconfig.json", "./tsconfig.json",
"./tools/ui-components/tsconfig.json", "./tools/ui-components/tsconfig.json",
"./tools/scripts/build/tsconfig.json" "./tools/tsconfig.json"
] ]
}, },
"extends": [ "extends": [

3
.gitignore vendored
View File

@ -167,7 +167,8 @@ config/i18n/all-langs.js
config/certification-settings.js config/certification-settings.js
tools/scripts/build/ensure-env.js tools/scripts/build/ensure-env.js
tools/scripts/build/tsconfig.tsbuildinfo tools/tsconfig.tsbuildinfo
tools/scripts/lint/validate-keys.js
### vim ### ### vim ###
# Swap # Swap

View File

@ -10,4 +10,5 @@ config/certification-settings.js
client/i18n/**/*.json client/i18n/**/*.json
docs/i18n docs/i18n
**/package-lock.json **/package-lock.json
tools/scripts/build/ensure-env.js tools/scripts/build/ensure-env.js
tools/scripts/lint/validate-keys.js

View File

@ -19,18 +19,18 @@
"author": "freeCodeCamp <team@freecodecamp.org>", "author": "freeCodeCamp <team@freecodecamp.org>",
"main": "none", "main": "none",
"scripts": { "scripts": {
"prebuild": "tsc -p ../tools/scripts/build/ && node ../tools/scripts/build/ensure-env.js && npm run build:workers -- --env production", "prebuild": "tsc -p ../tools/ && node ../tools/scripts/build/ensure-env.js && npm run build:workers -- --env production",
"build": "node --max_old_space_size=7168 node_modules/gatsby-cli build --prefix-paths", "build": "node --max_old_space_size=7168 node_modules/gatsby-cli build --prefix-paths",
"build:workers": "node --max_old_space_size=7168 node_modules/webpack-cli/bin/cli --config ./webpack-workers.js", "build:workers": "node --max_old_space_size=7168 node_modules/webpack-cli/bin/cli --config ./webpack-workers.js",
"clean": "gatsby clean", "clean": "gatsby clean",
"predevelop": "tsc -p ../tools/scripts/build/ && node ../tools/scripts/build/ensure-env.js && npm run build:workers -- --env development", "predevelop": "tsc -p ../tools/ && node ../tools/scripts/build/ensure-env.js && npm run build:workers -- --env development",
"develop": "node --max_old_space_size=4000 node_modules/gatsby-cli develop --inspect=9230", "develop": "node --max_old_space_size=4000 node_modules/gatsby-cli develop --inspect=9230",
"lint": "node ./i18n/schema-validation.js", "lint": "node ./i18n/schema-validation.js",
"serve": "gatsby serve -p 8000", "serve": "gatsby serve -p 8000",
"serve-ci": "serve -l 8000 -c ../serve.json public", "serve-ci": "serve -l 8000 -c ../serve.json public",
"prestand-alone": "npm run prebuild", "prestand-alone": "npm run prebuild",
"stand-alone": "gatsby develop", "stand-alone": "gatsby develop",
"validate-keys": "node ./i18n/validate-keys.js" "validate-keys": "tsc -p ../tools/ && node ../tools/scripts/lint/validate-keys.js"
}, },
"dependencies": { "dependencies": {
"@babel/plugin-proposal-export-default-from": "7.16.0", "@babel/plugin-proposal-export-default-from": "7.16.0",

View File

@ -31,7 +31,7 @@
"build:server": "cd ./api-server && npm run build", "build:server": "cd ./api-server && npm run build",
"clean": "npm-run-all clean:build-files clean:client clean:server clean:packages clean:root-deps", "clean": "npm-run-all clean:build-files clean:client clean:server clean:packages clean:root-deps",
"clean-and-develop": "npm run clean && npm ci && npm run develop", "clean-and-develop": "npm run clean && npm ci && npm run develop",
"clean:build-files": "shx rm ./tools/scripts/build/tsconfig.tsbuildinfo", "clean:build-files": "shx rm ./tools/tsconfig.tsbuildinfo",
"clean:client": "cd ./client && npm run clean", "clean:client": "cd ./client && npm run clean",
"clean:curriculum": "shx rm ./config/curriculum.json", "clean:curriculum": "shx rm ./config/curriculum.json",
"clean:gatsby-site": "npm run clean:client", "clean:gatsby-site": "npm run clean:client",
@ -56,7 +56,7 @@
"e2e:dev:watch": "start-test develop ':3000/status/ping|8000' cypress:dev:watch", "e2e:dev:watch": "start-test develop ':3000/status/ping|8000' cypress:dev:watch",
"e2e:prd:run": "npm run build && start-test ':3000/status/ping|8000' cypress:dev:run", "e2e:prd:run": "npm run build && start-test ':3000/status/ping|8000' cypress:dev:run",
"e2e:prd:watch": "npm run build && start-test ':3000/status/ping|8000' cypress:dev:watch", "e2e:prd:watch": "npm run build && start-test ':3000/status/ping|8000' cypress:dev:watch",
"ensure-env": "cd ./tools/scripts/build/ && tsc --incremental && cross-env DEBUG=fcc:* node ./ensure-env.js", "ensure-env": "cd ./tools/ && tsc --incremental && cross-env DEBUG=fcc:* node ./scripts/build/ensure-env.js",
"format": "run-s format:eslint format:prettier", "format": "run-s format:eslint format:prettier",
"format:eslint": "eslint . --fix", "format:eslint": "eslint . --fix",
"format:prettier": "prettier --write .", "format:prettier": "prettier --write .",
@ -65,7 +65,7 @@
"lint": "npm-run-all ensure-env -p lint:*", "lint": "npm-run-all ensure-env -p lint:*",
"lint:challenges": "cd ./curriculum && npm run lint", "lint:challenges": "cd ./curriculum && npm run lint",
"lint:js": "eslint --max-warnings 0 .", "lint:js": "eslint --max-warnings 0 .",
"lint:ts": "tsc && tsc -p tools/ui-components && tsc -p tools/scripts/build", "lint:ts": "tsc && tsc -p tools/ui-components && tsc -p tools",
"lint:prettier": "prettier --list-different .", "lint:prettier": "prettier --list-different .",
"postinstall": "npm run bootstrap", "postinstall": "npm run bootstrap",
"seed": "cross-env DEBUG=fcc:* node ./tools/scripts/seed/seedAuthUser", "seed": "cross-env DEBUG=fcc:* node ./tools/scripts/seed/seedAuthUser",

View File

@ -1,6 +0,0 @@
{
"compilerOptions": {
"typeRoots": ["../../node_modules/@types"],
"types": ["node"]
}
}

View File

@ -1,28 +1,32 @@
const fs = require('fs'); import fs from 'fs';
const path = require('path'); import path from 'path';
const introObject = require('./locales/english/intro.json'); import introObject from '../../../client/i18n/locales/english/intro.json';
const linksObject = require('./locales/english/links.json'); import linksObject from '../../../client/i18n/locales/english/links.json';
const metaObject = require('./locales/english/meta-tags.json'); import metaObject from '../../../client/i18n/locales/english/meta-tags.json';
const motivationObject = require('./locales/english/motivation.json'); import motivationObject from '../../../client/i18n/locales/english/motivation.json';
const translationsObject = require('./locales/english/translations.json'); import translationsObject from '../../../client/i18n/locales/english/translations.json';
const trendingObject = require('./locales/english/trending.json'); import trendingObject from '../../../client/i18n/locales/english/trending.json';
/** /**
* Function to flatten a nested object. Written specifically for * Function to flatten a nested object. Written specifically for
* our translation flow, the `namespace` value is used to create the * our translation flow, the `namespace` value is used to create the
* property chains that are used in the i18n replacement scripts. * property chains that are used in the i18n replacement scripts.
* @param {Object} obj
* @param {string} namespace
*/ */
const flattenAnObject = (obj, namespace = '') => { const flattenAnObject = (
const flattened = {}; obj: Record<string, unknown>,
Object.keys(obj).forEach(key => { namespace = ''
): Record<string, unknown> => {
const flattened: Record<string, unknown> = {};
Object.keys(obj).forEach((key: string) => {
if (Array.isArray(obj[key])) { if (Array.isArray(obj[key])) {
flattened[namespace ? `${namespace}.${key}` : key] = obj[key]; flattened[namespace ? `${namespace}.${key}` : key] = obj[key];
} else if (typeof obj[key] === 'object') { } else if (typeof obj[key] === 'object') {
Object.assign( Object.assign(
flattened, flattened,
flattenAnObject(obj[key], namespace ? `${namespace}.${key}` : key) flattenAnObject(
obj[key] as Record<string, unknown>,
namespace ? `${namespace}.${key}` : key
)
); );
} else { } else {
flattened[namespace ? `${namespace}.${key}` : key] = obj[key]; flattened[namespace ? `${namespace}.${key}` : key] = obj[key];
@ -31,25 +35,26 @@ const flattenAnObject = (obj, namespace = '') => {
return flattened; return flattened;
}; };
const translationKeys = Object.keys(flattenAnObject(translationsObject)); const translationKeys: string[] = Object.keys(
const metaKeys = Object.keys(flattenAnObject(metaObject)); flattenAnObject(translationsObject)
const motivationKeys = Object.keys(flattenAnObject(motivationObject)); );
const introKeys = Object.keys(flattenAnObject(introObject)); const metaKeys: string[] = Object.keys(flattenAnObject(metaObject));
const trendingKeys = Object.keys(flattenAnObject(trendingObject)); const motivationKeys: string[] = Object.keys(flattenAnObject(motivationObject));
const linksKeys = Object.keys(flattenAnObject(linksObject)); const introKeys: string[] = Object.keys(flattenAnObject(introObject));
const trendingKeys: string[] = Object.keys(flattenAnObject(trendingObject));
const linksKeys: string[] = Object.keys(flattenAnObject(linksObject));
/** /**
* Recursively read through the directory, grabbing .js files * Recursively read through the directory, grabbing .js files
* in each nested subdirectory and concatenating them all in * in each nested subdirectory and concatenating them all in
* to one string. * to one string.
* @param {String} filePath
*/ */
const readComponentCode = filePath => { const readComponentCode = (filePath: string): string => {
let code = ''; let code = '';
const isItFolder = fs.lstatSync(filePath).isDirectory(); const isItFolder: boolean = fs.lstatSync(filePath).isDirectory();
if (isItFolder) { if (isItFolder) {
const contents = fs.readdirSync(filePath); const contents: string[] = fs.readdirSync(filePath);
contents.forEach(file => { contents.forEach((file: string) => {
code += readComponentCode(path.join(filePath + '/' + file)); code += readComponentCode(path.join(filePath + '/' + file));
}); });
} else { } else {
@ -61,8 +66,10 @@ const readComponentCode = filePath => {
return code; return code;
}; };
const clientCodebase = readComponentCode(path.join(process.cwd() + '/src')); const clientCodebase: string = readComponentCode(
const serverCodebase = readComponentCode( path.join(process.cwd() + '/src')
);
const serverCodebase: string = readComponentCode(
path.join(process.cwd() + '/../api-server/src/server') path.join(process.cwd() + '/../api-server/src/server')
); );

13
tools/tsconfig.json Normal file
View File

@ -0,0 +1,13 @@
{
"include": [
"./scripts/lint/validate-keys.ts",
"./scripts/build/ensure-env.ts"
],
"compilerOptions": {
"esModuleInterop": true,
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"typeRoots": ["../node_modules/@types"],
"types": ["node"]
}
}