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');
function getValidDirectory(prompt) { return new Promise((resolve) => { const readline = require('readline').createInterface({ input: process.stdin, output: process.stdout });
readline.question(prompt, (inputPath) => { const cleanedPath = inputPath.trim().replace(/ /g, ''); if (!fs.existsSync(cleanedPath)) { console.log(`路径不存在: ${cleanedPath}`); readline.close(); resolve(getValidDirectory(prompt)); } else if (!fs.lstatSync(cleanedPath).isDirectory()) { console.log(`这不是一个有效的目录: ${cleanedPath}`); readline.close(); resolve(getValidDirectory(prompt)); } else { readline.close(); resolve(cleanedPath); } }); }); }
function compareDirectories(prevDir, currDir) { let newFiles = []; let existingFiles = [];
function walkDir(dir, callback) { fs.readdirSync(dir).forEach(file => { const fullPath = path.join(dir, file); if (fs.lstatSync(fullPath).isDirectory()) { walkDir(fullPath, callback); } else { callback(fullPath); } }); }
walkDir(currDir, (currFilePath) => { const relPath = path.relative(currDir, currFilePath); const prevFilePath = path.join(prevDir, relPath); if (!fs.existsSync(prevFilePath)) { newFiles.push(currFilePath); } else { existingFiles.push(currFilePath); } });
return { newFiles, existingFiles }; }
function classifyFilesByExtension(files, extensions) { let classifiedFiles = {}; extensions.forEach(ext => classifiedFiles[ext] = []); let otherFiles = [];
files.forEach(file => { const ext = path.extname(file).toLowerCase(); if (extensions.includes(ext)) { classifiedFiles[ext].push(file); } else { otherFiles.push(file); } });
return { classifiedFiles, otherFiles }; }
function sortFilesBySize(files, descending = true) { return files.sort((a, b) => { const sizeA = fs.statSync(a).size; const sizeB = fs.statSync(b).size; return descending ? sizeB - sizeA : sizeA - sizeB; }); }
function addUrlPrefix(files, prefix, basePath) { return files.map(file => { const relPath = path.relative(basePath, file).replace(/\\/g, '/'); return `${prefix}/${relPath}`; }); }
function saveClassifiedFiles(classifiedFiles) { const outputDir = 'output'; if (fs.existsSync(outputDir)) { fs.rmdirSync(outputDir, { recursive: true }); console.log(`已清空目录: ${outputDir}`); } fs.mkdirSync(outputDir);
Object.keys(classifiedFiles).forEach(ext => { const filePath = path.join(outputDir, `${ext.substring(1)}.txt`); fs.writeFileSync(filePath, classifiedFiles[ext].join('\n')); }); }
async function main() { const previousVersionPath = await getValidDirectory("请输入上一个版本的remote目录路径: "); const currentVersionPath = await getValidDirectory("请输入当前要热更版本的remote目录路径: ");
const { newFiles, existingFiles } = compareDirectories(previousVersionPath, currentVersionPath);
const readline = require('readline').createInterface({ input: process.stdin, output: process.stdout });
readline.question("请输入要分类的后缀名(多个后缀用逗号分隔,直接回车统计所有后缀): ", (extensionsInput) => { let extensions; if (extensionsInput.trim()) { extensions = extensionsInput.split(',').map(ext => ext.trim().toLowerCase()); extensions = extensions.map(ext => ext.startsWith('.') ? ext : `.${ext}`); } else { extensions = Array.from(new Set(newFiles.map(file => path.extname(file).toLowerCase()))); }
let { classifiedFiles, otherFiles } = classifyFilesByExtension(newFiles, extensions);
readline.question("请输入排序规则 (默认从大到小排序,输入N从小到大排序): ", (sortOrder) => { const descending = sortOrder.trim().toUpperCase() !== 'N';
for (let ext in classifiedFiles) { classifiedFiles[ext] = sortFilesBySize(classifiedFiles[ext], descending); }
readline.question("请输入要添加的URL前缀: ", (urlPrefix) => { for (let ext in classifiedFiles) { classifiedFiles[ext] = addUrlPrefix(classifiedFiles[ext], urlPrefix, currentVersionPath); }
saveClassifiedFiles(classifiedFiles); readline.close(); }); }); }); }
main();
|