Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test-render-translation.js 3.2 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  1. #!/usr/bin/env node
  2. // [start-readme]
  3. //
  4. // Run this script to test-render all the translation files that have been changed (when compared to the `main` branch).
  5. //
  6. // [end-readme]
  7. const renderContent = require('../lib/render-content')
  8. const loadSiteData = require('../lib/site-data')
  9. const { loadPages } = require('../lib/pages')
  10. const languages = require('../lib/languages')
  11. const path = require('path')
  12. const { promisify } = require('util')
  13. const { execSync } = require('child_process')
  14. const exec = promisify(require('child_process').exec)
  15. const fs = require('fs')
  16. const frontmatter = require('../lib/frontmatter')
  17. const chalk = require('chalk')
  18. const { YAMLException } = require('js-yaml')
  19. main()
  20. async function main () {
  21. const siteData = await loadAndPatchSiteData()
  22. const pages = await loadPages()
  23. const contextByLanguage = {}
  24. for (const lang in languages) {
  25. const langObj = languages[lang]
  26. const [crowdinLangCode] = langObj.dir === '' ? 'en' : langObj.dir.split('/').slice(1)
  27. if (!crowdinLangCode) continue
  28. contextByLanguage[crowdinLangCode] = {
  29. site: siteData[langObj.code].site,
  30. currentLanguage: langObj.code,
  31. currentVersion: 'free-pro-team@latest'
  32. }
  33. }
  34. const rootDir = path.join(__dirname, '..')
  35. const changedFilesRelPaths = execSync('git -c diff.renameLimit=10000 diff --name-only origin/main | egrep "^translations/.*/.+.md$"', { maxBuffer: 1024 * 1024 * 100 })
  36. .toString()
  37. .split('\n')
  38. .filter(path => path !== '' && !path.endsWith('README.md'))
  39. .sort()
  40. console.log(`Found ${changedFilesRelPaths.length} translated files.`)
  41. for (const relPath of changedFilesRelPaths) {
  42. const fullPath = path.join(rootDir, relPath)
  43. const lang = relPath.split('/')[1]
  44. const context = {
  45. ...contextByLanguage[lang],
  46. pages,
  47. page: pages.find(page => page.fullPath === fullPath),
  48. redirects: {}
  49. }
  50. if (!context.page && !relPath.includes('data/reusables')) continue
  51. const fileContents = await fs.promises.readFile(fullPath, 'utf8')
  52. const { content } = frontmatter(fileContents)
  53. try {
  54. await renderContent.liquid.parseAndRender(content, context)
  55. } catch (err) {
  56. console.log(chalk.bold(relPath))
  57. console.log(chalk.red(` error message: ${err.message}`))
  58. }
  59. }
  60. }
  61. async function loadAndPatchSiteData (filesWithKnownIssues = {}) {
  62. try {
  63. const siteData = loadSiteData()
  64. return siteData
  65. } catch (error) {
  66. if (error instanceof YAMLException && error.mark) {
  67. const relPath = error.mark.name
  68. if (!filesWithKnownIssues[relPath]) {
  69. // Note the file as problematic
  70. filesWithKnownIssues[relPath] = true
  71. // This log is important as it will get ${relPath} written to a logfile
  72. console.log(chalk.bold(relPath))
  73. console.log(chalk.red(` error message: ${error.toString()}`))
  74. // Reset the file
  75. console.warn(`resetting file "${relPath}" due to loadSiteData error: ${error.toString()}`)
  76. await exec(`script/reset-translated-file.js --prefer-main ${relPath}`)
  77. // Try to load the site data again
  78. return loadAndPatchSiteData(filesWithKnownIssues)
  79. } else {
  80. console.error(`FATAL: Tried to reset file "${relPath}" but still had errors`)
  81. }
  82. }
  83. throw error
  84. }
  85. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...