convert-bsdp-locales.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. const fs = require('fs');
  2. const path = require('path');
  3. const usage = `USAGE:
  4. node ${path.basename(process.argv[1])} [options] source_dir
  5. source_dir: path to the locale directory of bootstrap-datepicker's source
  6. Options:
  7. -h|--help print this help
  8. `;
  9. const src = process.argv[2];
  10. if (!src || src === '-h' || src === '--help') {
  11. console.log(usage);
  12. process.exit();
  13. }
  14. if (!fs.existsSync(src) || !fs.statSync(src).isDirectory()) {
  15. console.error(`${src} is not a directory.`);
  16. process.exit(1);
  17. }
  18. const reFilename = /^bootstrap-datepicker\.([a-z]+(-[A-Za-z]+)?)\.js$/;
  19. const files = fs.readdirSync(src);
  20. if (files.length < 2 || !reFilename.test(files[1])) {
  21. console.error(`${src} is not a bootstrap-datepicker's locale directory.`);
  22. process.exit(1);
  23. }
  24. const destDir = path.resolve(__dirname, '../js/i18n/locales');
  25. if (!fs.existsSync(destDir)) {
  26. fs.mkdirSync(destDir, {recursive: true});
  27. }
  28. files.forEach(file => {
  29. const matched = file.match(reFilename);
  30. if (!matched) {
  31. return;
  32. }
  33. const lang = matched[1].replace('latin', 'latn');
  34. if (lang === 'en-US') {
  35. return;
  36. }
  37. const langKey = lang.indexOf('-') > -1 ? `'${lang}'` : lang;
  38. let contents = fs.readFileSync(path.resolve(src, file), 'utf8');
  39. if (contents.indexOf('DEPRECATED') > -1) {
  40. return;
  41. }
  42. contents = contents
  43. .replace(/\t/g, ' ')
  44. .replace(/; *\( *function *\( *\$ *\) *\{/, 'export default {')
  45. .replace(/ *\$\.fn\.datepicker\.dates\[.+?\] =/, ` ${langKey}:`)
  46. .replace(/ *};/, ' }')
  47. .replace(/ *\} *\( *jQuery *\) *\);\s*/, '};\n')
  48. .replace(/ {5,}/g, ' ')
  49. .replace(/: *(\S)/g, ': $1')
  50. .replace(/ +\n/g, '\n')
  51. .replace(/(titleFormat:.+)yyyy/, '$1y');
  52. fs.writeFileSync(path.resolve(destDir, `${lang}.js`), contents);
  53. });