build-locales.js 814 B

123456789101112131415161718192021222324252627
  1. const fs = require('fs');
  2. const path = require('path');
  3. const rootDir = path.dirname(__dirname);
  4. const srcDir = `${rootDir}/js/i18n/locales`;
  5. const distDir = `${rootDir}/dist/js/locales`;
  6. const reConvert = /export default (\{\s+)([\w'-]+):([\s\S]+\})\n\};/m;
  7. const rePropNameFix = /\.('\w+-\w+')/;
  8. if (fs.existsSync(distDir)) {
  9. // empty dist dir
  10. fs.readdirSync(distDir).forEach((file) => {
  11. fs.unlinkSync(`${distDir}/${file}`);
  12. });
  13. } else {
  14. fs.mkdirSync(distDir, {recursive: true});
  15. }
  16. // copy locales to dist
  17. fs.readdirSync(srcDir).forEach((file) => {
  18. const src = fs.readFileSync(`${srcDir}/${file}`, 'utf8');
  19. const output = src
  20. .replace(reConvert, '(function () $1Datepicker.locales.$2 =$3;\n}());')
  21. .replace(rePropNameFix, '[$1]');
  22. fs.writeFileSync(`${distDir}/${file}`, output);
  23. });