generate-codes-data.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import fs from 'fs'
  2. import path from 'path'
  3. import { fileURLToPath } from 'url'
  4. import { createHighlighter } from 'shiki'
  5. import { glob } from 'glob'
  6. const __dirname = path.dirname(fileURLToPath(import.meta.url))
  7. const codesSrcDir = path.resolve(__dirname, '../docs/codes')
  8. const outputDir = path.resolve(__dirname, '../docs/.vitepress/data')
  9. const outputFile = path.join(outputDir, 'codes-data.js')
  10. async function generate() {
  11. if (!fs.existsSync(codesSrcDir)) {
  12. console.log('Codes directory not found:', codesSrcDir)
  13. return
  14. }
  15. // Ensure output directory exists
  16. if (!fs.existsSync(outputDir)) {
  17. fs.mkdirSync(outputDir, { recursive: true })
  18. }
  19. console.log('Generating codes data...')
  20. const highlighter = await createHighlighter({
  21. themes: ['github-dark', 'github-light'],
  22. langs: ['cpp', 'lua', 'json', 'bash', 'xml', 'toml', 'yaml']
  23. })
  24. // Find all xmake.lua files to identify project roots
  25. const projectRoots = await glob('**/xmake.lua', {
  26. cwd: codesSrcDir,
  27. ignore: ['**/build/**', '**/.*/**']
  28. })
  29. const codes = {}
  30. for (const rootFile of projectRoots) {
  31. const projectDir = path.dirname(rootFile)
  32. // Use project directory relative path as key (e.g. "examples/cpp/basic_console")
  33. const key = projectDir
  34. const fullProjectDir = path.join(codesSrcDir, projectDir)
  35. // Get all files in this project
  36. const projectFiles = await glob('**/*.*', {
  37. cwd: fullProjectDir,
  38. ignore: [
  39. '**/build/**',
  40. '**/.*',
  41. '**/*.o',
  42. '**/*.obj',
  43. '**/*.exe',
  44. '**/*.bin',
  45. '**/test.lua',
  46. '**/*.cache/**',
  47. '**/*.gcm',
  48. '**/compile_commands.json',
  49. '**/compile_command.json'
  50. ],
  51. nodir: true
  52. })
  53. const files = []
  54. for (const file of projectFiles) {
  55. const fullPath = path.join(fullProjectDir, file)
  56. const content = fs.readFileSync(fullPath, 'utf-8')
  57. const ext = path.extname(file).toLowerCase().substring(1)
  58. // Map extension to language
  59. let language = ext
  60. if (file.endsWith('xmake.lua')) language = 'lua'
  61. else if (['h', 'hpp', 'c', 'cc', 'cxx'].includes(ext)) language = 'cpp'
  62. try {
  63. files.push({
  64. name: file,
  65. code: content,
  66. language: language,
  67. highlightedCode: highlighter.codeToHtml(content, {
  68. lang: language,
  69. themes: {
  70. light: 'github-light',
  71. dark: 'github-dark'
  72. }
  73. })
  74. })
  75. } catch (e) {
  76. // Fallback for unknown languages or binary files
  77. files.push({
  78. name: file,
  79. code: content,
  80. language: 'text'
  81. })
  82. }
  83. }
  84. // Sort files: xmake.lua first, then folders, then files
  85. files.sort((a, b) => {
  86. if (a.name === 'xmake.lua') return -1
  87. if (b.name === 'xmake.lua') return 1
  88. return a.name.localeCompare(b.name)
  89. })
  90. codes[key] = files
  91. }
  92. const content = `export const data = ${JSON.stringify(codes, null, 2)}`
  93. fs.writeFileSync(outputFile, content)
  94. console.log(`Generated ${outputFile}`)
  95. }
  96. generate().catch(console.error)