generate-locales-all.js 827 B

123456789101112131415161718192021222324252627
  1. import { join as joinPaths } from 'path'
  2. import { fileURLToPath } from 'url'
  3. import { readFile } from 'fs/promises'
  4. import { globby } from 'globby'
  5. import handlebars from 'handlebars'
  6. const thisPkgDir = joinPaths(fileURLToPath(import.meta.url), '../..')
  7. const templatePath = joinPaths(thisPkgDir, 'src/locales-all.js.tpl')
  8. const localesDir = joinPaths(thisPkgDir, 'src/locales')
  9. export function getWatchPaths() {
  10. return [
  11. templatePath,
  12. localesDir,
  13. ]
  14. }
  15. export default async function() {
  16. const localeFilenames = await globby('*.ts', { cwd: localesDir })
  17. const localeCodes = localeFilenames.map((filename) => filename.replace(/\.ts$/, ''))
  18. const templateText = await readFile(templatePath, 'utf8')
  19. const template = handlebars.compile(templateText)
  20. const code = template({ localeCodes })
  21. return code
  22. }