extract-chinese.js 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. const fs = require("fs");
  2. const path = require("path");
  3. const SRC = path.join(__dirname, "../src");
  4. const SKIP = /copy\.vue$|weatherInfo copy/i;
  5. const CHINESE_RE = /[\u4e00-\u9fff][\u4e00-\u9fff\d,。、?!:;""''()【】\-\s%%·]*/g;
  6. function walk(dir, files = []) {
  7. for (const name of fs.readdirSync(dir)) {
  8. const p = path.join(dir, name);
  9. const stat = fs.statSync(p);
  10. if (stat.isDirectory()) {
  11. if (name !== "node_modules") walk(p, files);
  12. } else if (/\.(vue|js)$/.test(name) && !SKIP.test(p)) {
  13. files.push(p);
  14. }
  15. }
  16. return files;
  17. }
  18. const set = new Set();
  19. for (const file of walk(SRC)) {
  20. const content = fs.readFileSync(file, "utf8");
  21. const matches = content.match(CHINESE_RE) || [];
  22. matches.forEach((m) => {
  23. const s = m.trim();
  24. if (s.length >= 2 && /[\u4e00-\u9fff]/.test(s)) set.add(s);
  25. });
  26. }
  27. const list = [...set].sort((a, b) => b.length - a.length);
  28. console.log(JSON.stringify(list, null, 2));