Merge branch 'dev-v0.3.8' of github.com:fleetbase/fleetbase into dev-v0.3.8

This commit is contained in:
Munkh-erdene Damdinbazar
2024-01-24 14:46:46 +08:00
4 changed files with 2 additions and 205 deletions

View File

@@ -21,7 +21,7 @@
"lint:hbs:fix": "ember-template-lint . --fix",
"lint:js": "eslint . --cache",
"lint:js:fix": "eslint . --fix",
"lint:intl": "node scripts/intl-lint.js",
"lint:intl": "fleetbase-intl-lint",
"start": "pnpm run prebuild && ember serve",
"test": "concurrently \"npm:lint\" \"npm:test:*\" --names \"lint,test:\"",
"test:ember": "ember test"
@@ -54,6 +54,7 @@
"postcss-nth-list": "^1.0.2"
},
"devDependencies": {
"@fleetbase/intl-lint": "^0.0.1",
"@babel/core": "^7.23.2",
"@babel/eslint-parser": "^7.22.15",
"@babel/plugin-proposal-decorators": "^7.23.2",

View File

@@ -1,159 +0,0 @@
/*
===========================================================================
Internationalization Key Validation Script
===========================================================================
Script Purpose:
----------------
This Node.js script checks the usage of internationalization keys in your Ember project's JavaScript and Handlebars files. It ensures that all keys referenced in your project files are present in the specified YAML translation file. This helps prevent missing translations during runtime.
Usage:
------
To use this script, run it with Node.js from the command line, providing the path to your Ember project directory and the path to the YAML translation file. Optionally, you can include the '--silent' flag to suppress error throwing and allow the script to run to completion even if there are missing translations.
Example Command:
----------------
node intl-lint.js --silent
Script Behavior:
----------------
1. The script recursively processes all JavaScript (.js) and Handlebars (.hbs) files in the specified Ember project directory.
2. It extracts translation keys using regular expressions tailored for Handlebars and JavaScript files.
3. For each key found, it checks if the key exists in the specified YAML translation file.
4. If any missing keys are detected, the script logs them and optionally throws an error.
Script Options:
---------------
- Ember Project Path: The root directory of your Ember project. Modify the 'projectPath' variable to set the path.
- Translation File Path: The path to the YAML translation file. Modify the 'translationFilePath' variable to set the path.
- Silent Mode: Include the '--silent' flag to suppress error throwing and allow the script to run to completion even if there are missing translations.
Authors:
---------
- Fleetbase Pte Ltd <hello@fleetbase.io>
- Ronald A. Richardson <ron@fleetbase.io>
Contact:
---------
If you encounter issues or have questions, feel free to contact the authors or raise an issue on the project repository.
License:
--------
This script is open-source and distributed under the MIT license. Refer to the LICENSE file for details.
===========================================================================
*/
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const silentMode = process.argv.includes('--silent');
function findTranslationKeys(filePath) {
const content = fs.readFileSync(filePath, 'utf8');
// Regular expression for finding translation keys
let regex;
if (filePath.endsWith('.hbs')) {
// Regular expression for finding translation keys in Handlebars files
regex = /\{\{\s*t\s+["'`]([^"']+?)["'`]\s*}}|\(t\s+["'`]([^"']+?)["'`]\)/g;
} else if (filePath.endsWith('.js')) {
// Regular expression for finding translation keys in JavaScript files
regex = /this\.intl\.t\s*\(\s*["'`]([^"']+?)["'`]\s*(?:,\s*\{.*?\}\s*)?\)/g;
} else {
console.log(`Unsupported file type: ${filePath}`);
return [];
}
const keys = [];
let match;
while ((match = regex.exec(content)) !== null) {
// Matched key will be in one of the capturing groups 1 or 2
const key = match[1] || match[2];
if (key.trim() !== '') {
keys.push(key);
}
}
// Log the number of translation keys found in the file
console.log(`Found ${keys.length} translation key(s) in file: ${filePath}`);
return keys;
}
function checkKeysInTranslationFile(keys, translationFilePath) {
console.log(`Checking if translation keys exist in file: ${translationFilePath}`);
const translationContent = fs.readFileSync(translationFilePath, 'utf8');
const translationData = yaml.load(translationContent);
const missingKeys = keys.filter((key) => {
const nestedKeys = key.split('.');
let currentLevel = translationData;
for (const nestedKey of nestedKeys) {
if (currentLevel && currentLevel.hasOwnProperty(nestedKey)) {
currentLevel = currentLevel[nestedKey];
} else {
return true; // Missing key found
}
}
return false; // All nested keys found
});
return missingKeys;
}
function processDirectory(directoryPath, translationFilePath) {
const files = fs.readdirSync(directoryPath);
for (const file of files) {
const filePath = path.join(directoryPath, file);
if (fs.statSync(filePath).isDirectory()) {
// Recursively process subdirectories
processDirectory(filePath, translationFilePath);
} else if (file.endsWith('.js') || file.endsWith('.hbs')) {
console.log(`Checking file: ${filePath}`);
// Process JavaScript and Handlebars files
const keys = findTranslationKeys(filePath);
if (keys.length === 0) {
console.log('');
continue;
}
const missingKeys = checkKeysInTranslationFile(keys, translationFilePath);
if (missingKeys.length > 0) {
console.error(`File: ${filePath}`);
missingKeys.forEach((missingKey) => {
console.error(`🚫 Missing Translation: ${missingKey}`);
if (!silentMode) {
throw new Error(`🚫 Missing Translation: ${missingKey}`);
}
});
} else {
console.log(`All translation keys found in file: ${filePath}`);
}
console.log('');
}
}
}
function checkTranslationsInProject(projectPath, translationFilePath) {
console.log(`⏳ Starting translation key check in project: ${projectPath}`);
processDirectory(projectPath, translationFilePath);
console.log('✅ Translation key check completed.');
}
const projectPath = path.join(__dirname, '../app');
const translationFilePath = path.join(__dirname, '../translations/en-us.yaml');
try {
checkTranslationsInProject(projectPath, translationFilePath);
} catch (error) {
console.error('🚫 Translation key check failed:', error.message);
process.exit(1); // Exit with an error code
}

View File

@@ -1,32 +0,0 @@
{
"name": "fleetops-scripts",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "fleetops-scripts",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"js-yaml": "^4.1.0"
}
},
"node_modules/argparse": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz",
"integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q=="
},
"node_modules/js-yaml": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz",
"integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==",
"dependencies": {
"argparse": "^2.0.1"
},
"bin": {
"js-yaml": "bin/js-yaml.js"
}
}
}
}

View File

@@ -1,13 +0,0 @@
{
"name": "fleetops-scripts",
"version": "1.0.0",
"description": "Node scripts for maintaining and analyzing the fleetops extension",
"scripts": {
"intl-lint": "node ./intl-lint.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"js-yaml": "^4.1.0"
}
}