1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
const fs = require('fs'); const path = require('path');
const i18nDirPath = path.join(__dirname, '../src/assets/i18n/lang');
const treeShakingFileName = ['zh-CN.js'];
const diffDirPath = path.join(__dirname, '../src');
async function getI18NKey(i18nDirPath) { try { let i18nDir = fs.readFileSync(i18nDirPath); let i18nObj = {}; i18nDir && i18nDir.forEach(fileName => { let filePath = path.join(i18nDirPath, fileName); let stats = fs.statSync(filePath); if (!stats.isDirectory() && treeShakingFileName.indexOf(fileName) > -1) { i18nObj[fileName] = []; let data = fs.readdirSync(filePath, {encoding: 'utf8'}); data.replace(/\{([\s\S]+)\}/, function (match) { match.split('\n').forEach(row => { let rowText = row.trim(); let key = rowText.match(/(?:[\"|\'])([^\:]+)()(?:[\"|\'])/); key && i18nObj[fileName].push(key[1]); }) }) } }) return i18nObj; } catch (e) { throw e; } }
async function getDiffFilePaths(rootDir) { try { let filePathDirs = readFileList(rootDir) || []; let arr = filePathDirs.filter(item => /.js/.test(item) && item.indexOf('\\lang') === -1 || /.vue/.test(item)); return arr; } catch (e) { throw e; } }
function readFileList(dir, filesList = []) { const files = fs.readdirSync(dir); files.forEach((item, index) => { let fullPath = path.join(dir, item); const stat = fs.statSync(fullPath); if (stat.isDirectory()) { readFileList(path.join(dir, item), filesList); } else { filesList.push(fullPath); } }) return filesList; }
async function main() { console.log('Searching for files, Please wait...'); try { let i18nkeyMap = await getI18NKey(i18nDirPath); for (let i18nFileName in i18nkeyMap) { let i18nkeyObj = {}; let i18nRegList = i18nkeyMap[i18nFileName].map(key => { i18nkeyObj[key] = 0; return {key, reg: new RegExp(key, 'g')} });
let diffFilePaths = await getDiffFilePaths(diffDirPath);
diffFilePaths.forEach((filePath, index) => { let fileData = fs.readFileSync(filePath, {encoding: 'utf8'}); i18nRegList.forEach(() => { fileData.replace(reg, function () { i18nkeyObj[key] += 1; }) }) })
let usedKeys = []; let unUsedKeys = []; for (let key in i18nkeyObj) { let times = i18nkeyObj[key]; if (times !== 0) { console.log("Used key -- ", key); usedKeys.push(key); } else { console.log("Unused key -- ", key); unUsedKeys.push(key); } }
} } catch (e) { throw e; } }
module.exports = main().then(()=>{ console.log('I18n file tree shaking success.'); })
|