-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: STRF-10101 Check translation rows compilation on stencil bundle…
…/start (#1145)
- Loading branch information
Showing
10 changed files
with
211 additions
and
1 deletion.
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
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,113 @@ | ||
require('colors'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { recursiveReadDir } = require('../utils/fsUtils'); | ||
|
||
const LANG_HELPER_REGEXP = /{{\s*lang\s*(?:'|")((?:\w*(?:-\w*)*(\.\w*(?:-\w*)*)*)+)/gim; | ||
|
||
class LangpathsValidator { | ||
/** | ||
* | ||
* @param {String} themePath | ||
*/ | ||
constructor(themePath) { | ||
this.themePath = themePath; | ||
} | ||
|
||
async run(defaultLang = null) { | ||
const templatesPath = path.join(this.themePath, 'templates'); | ||
const paths = await this.getLangHelpersPaths(templatesPath); | ||
const dedupePaths = [...new Set(paths)]; | ||
const langFiles = await this.getLangFilesContent(defaultLang); | ||
const errors = this.validate(dedupePaths, langFiles); | ||
this.printErrors(errors); | ||
return errors; | ||
} | ||
|
||
printErrors(errors) { | ||
if (errors.length > 0) { | ||
console.log( | ||
'Warning: Your theme has some missing translations used in the theme:'.yellow, | ||
); | ||
console.log(errors.join('\n').yellow); | ||
} | ||
} | ||
|
||
searchLangPaths(fileContent, langPath) { | ||
const keys = langPath.split('.'); | ||
let value = fileContent; | ||
|
||
for (const key of keys) { | ||
// eslint-disable-next-line no-prototype-builtins | ||
if (value && value.hasOwnProperty(key)) { | ||
value = value[key]; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
return value; | ||
} | ||
|
||
validate(paths, langFiles) { | ||
const errors = [ | ||
...this.checkLangFiles(langFiles), | ||
...this.checkForMissingTranslations(paths, langFiles), | ||
]; | ||
return errors; | ||
} | ||
|
||
checkForMissingTranslations(paths, langFiles) { | ||
const errors = []; | ||
for (const langPath of paths) { | ||
// eslint-disable-next-line no-restricted-syntax,guard-for-in | ||
for (const langFile in langFiles) { | ||
const translation = this.searchLangPaths(langFiles[langFile], langPath); | ||
if (!translation) { | ||
errors.push(`Missing translation for ${langPath} in ${langFile}`); | ||
} | ||
} | ||
} | ||
return errors; | ||
} | ||
|
||
checkLangFiles(files) { | ||
if (files.length === 0) { | ||
return ['No lang files found in your theme']; | ||
} | ||
return []; | ||
} | ||
|
||
async getLangHelpersPaths(templatesPath) { | ||
const files = await recursiveReadDir(templatesPath); | ||
const paths = []; | ||
for await (const file of files) { | ||
const content = await fs.promises.readFile(file, { encoding: 'utf-8' }); | ||
const result = content.matchAll(LANG_HELPER_REGEXP); | ||
const arr = [...result]; | ||
if (arr.length > 0) { | ||
const langPath = arr[0][1]; | ||
paths.push(langPath); | ||
} | ||
} | ||
return paths; | ||
} | ||
|
||
async getLangFilesContent(defaultLang = null) { | ||
const filesContent = {}; | ||
const langPath = path.join(this.themePath, 'lang'); | ||
let files = await recursiveReadDir(langPath); | ||
|
||
if (defaultLang) { | ||
files = files.filter((file) => file.includes(defaultLang)); | ||
} | ||
|
||
for await (const file of files) { | ||
const content = await fs.promises.readFile(file, { encoding: 'utf-8' }); | ||
filesContent[file] = JSON.parse(content); | ||
} | ||
return filesContent; | ||
} | ||
} | ||
|
||
module.exports = LangpathsValidator; |
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,37 @@ | ||
const path = require('path'); | ||
|
||
const LangFilesValidator = require('./validator'); | ||
|
||
describe('lang/validator.js tests', () => { | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
describe('valid', () => { | ||
it('run with no errors', async () => { | ||
const themePath = path.join(process.cwd(), 'test/_mocks/themes/valid'); | ||
const validator = new LangFilesValidator(themePath); | ||
const errors = await validator.run(); | ||
|
||
expect(errors).toHaveLength(0); | ||
}); | ||
|
||
it('run with no errors providing default lang', async () => { | ||
const themePath = path.join(process.cwd(), 'test/_mocks/themes/valid'); | ||
const validator = new LangFilesValidator(themePath); | ||
const errors = await validator.run('en'); | ||
|
||
expect(errors).toHaveLength(0); | ||
}); | ||
}); | ||
|
||
describe('not valid', () => { | ||
it('run with lang helper that is not presented in lang file', async () => { | ||
const themePath = path.join(process.cwd(), 'test/_mocks/themes/invalid-translations'); | ||
const validator = new LangFilesValidator(themePath); | ||
const errors = await validator.run(); | ||
|
||
expect(errors).toHaveLength(1); | ||
}); | ||
}); | ||
}); |
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,20 @@ | ||
{ | ||
"header": { | ||
"welcome_back": "Welcome back, {name}" | ||
}, | ||
"footer": { | ||
"brands": "Popular Brands", | ||
"navigate": "Navigate", | ||
"info": "Info", | ||
"categories": "Categories", | ||
"call_us": "Call us at {phone_number}" | ||
}, | ||
"home": { | ||
"heading": "Home" | ||
}, | ||
"blog": { | ||
"recent_posts": "Recent Posts", | ||
"label": "Blog", | ||
"posted_by": "Posted by {name}" | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
test/_mocks/themes/invalid-translations/templates/components/a.html
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 @@ | ||
a |
7 changes: 7 additions & 0 deletions
7
test/_mocks/themes/invalid-translations/templates/components/b.html
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,7 @@ | ||
b | ||
|
||
|
||
<!-- test | ||
{{{stylesheet '/assets/custom/css/test.css'}}} --> | ||
|
||
more text here |
9 changes: 9 additions & 0 deletions
9
test/_mocks/themes/invalid-translations/templates/pages/page.html
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,9 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>page.html</title> | ||
</head> | ||
<body> | ||
{{ lang 'failed' }} | ||
</body> | ||
</html> |
12 changes: 12 additions & 0 deletions
12
test/_mocks/themes/invalid-translations/templates/pages/page2.html
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,12 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>page2.html</title> | ||
{{head.scripts}} | ||
</head> | ||
<body> | ||
<h1>{{theme_settings.customizable_title}}</h1> | ||
{{> components/b}} | ||
{{footer.scripts}} | ||
</body> | ||
</html> |