UNPKG

eformat

Version:

支持在多个目录下快速批量编辑和格式化 HTML 文件.

188 lines (175 loc) 6.69 kB
#!/usr/bin/env node /* * @Author: 马文龙 * @Date: 2025-05-23 15:40:52 * @LastEditors: 马文龙 * @LastEditTime: 2025-06-17 14:25:45 * @FilePath: \eformat\bin\cli.js * @Description: */ const { formatHtml, selectDirectory } = require("../src/format-html"); const path = require("path"); const packageJson = require("../package.json"); const fs = require("fs"); const inquirer = require("inquirer"); const args = process.argv.slice(2); if (args.includes("-v") || args.includes("--version")) { console.log(`eformat v${packageJson.version}`); process.exit(0); } if (args.includes("--select")) { (async () => { while (true) { const file = await selectDirectory(process.cwd()); if (!file) break; try { formatHtml(file); console.log(`√ 成功格式化 ${path.basename(file)}`); } catch (error) { console.error(`× 格式化失败: ${error.message}`); } const { continueSelect } = await inquirer.prompt([ { type: "confirm", name: "continueSelect", message: "是否继续选择其他文件?", default: true, }, ]); if (!continueSelect) break; } })(); } else if (args.includes("--file")) { const fileIndex = args.indexOf("--file") + 1; const filePath = args[fileIndex]; if (!filePath) { console.error( "× 请提供文件路径,例如:format-html-tool --file path/file.html" ); process.exit(1); } try { const absolutePath = path.resolve(filePath); if (!fs.existsSync(absolutePath)) { console.error(`× 文件不存在: ${absolutePath}`); process.exit(1); } formatHtml(absolutePath); console.log(`√ 成功格式化 ${path.basename(absolutePath)}`); } catch (error) { console.error(`× 格式化失败: ${error.message}`); process.exit(1); } } else { // 默认行为:查找并格式化最近修改的HTML文件 console.log("eformat: 正在查找最近编辑的HTML文件..."); const currentDir = process.cwd(); // 使用子进程执行PowerShell命令,查找最近修改的HTML文件 const { execSync } = require("child_process"); try { // 查找当前目录及其子目录中最近修改的5个HTML文件,排除node_modules目录 const command = `Get-ChildItem -Path "${currentDir}" -Recurse -Include *.html,*.htm | Where-Object { $_.FullName -notlike '*\\node_modules\\*' } | Sort-Object LastWriteTime -Descending | Select-Object -First 5 | ForEach-Object { $_.FullName }`; const result = execSync(`powershell -Command "${command}"`, { encoding: "utf8", }); // 解析结果,获取文件路径列表 // 使用正则表达式分割路径,以处理Windows和Unix路径 const recentFiles = result .trim() .split(/[\r\n]+/) .filter(Boolean); if (recentFiles.length === 0) { console.log("没有找到HTML文件。"); console.log("用法: eformat --select 或 eformat --file path/file.html"); process.exit(0); } else if (recentFiles.length === 1) { // 如果只有一个HTML文件,直接格式化 const filePath = recentFiles[0]; console.log( `eformat: 找到最近编辑的HTML文件: ${path.basename(filePath)}` ); console.log(`eformat: 正在格式化文件 ${path.basename(filePath)}...`); try { formatHtml(filePath); console.log(`√ 成功格式化 ${path.basename(filePath)}`); } catch (error) { console.error(`× 格式化失败: ${error.message}`); process.exit(1); } } else { // 如果有多个HTML文件,让用户选择 console.log("找到多个最近编辑的HTML文件,请选择要格式化的文件:"); (async () => { const choices = recentFiles.map((file) => ({ name: `${path.basename(file)} (${file})`, value: file, })); const { selectedFile } = await inquirer.prompt([ { type: "list", name: "selectedFile", message: "选择要格式化的HTML文件:", choices: choices, }, ]); console.log( `eformat: 正在格式化文件 ${path.basename(selectedFile)}...` ); try { formatHtml(selectedFile); console.log(`√ 成功格式化 ${path.basename(selectedFile)}`); } catch (error) { console.error(`× 格式化失败: ${error.message}`); process.exit(1); } })(); } } catch (error) { // 如果PowerShell命令执行失败,回退到原来的方法 console.log("无法查找最近编辑的文件,回退到检查当前目录..."); console.log("eformat: 正在检查当前目录下的HTML文件..."); const files = fs.readdirSync(currentDir); const htmlFiles = files.filter((file) => [".html", ".htm"].includes(path.extname(file).toLowerCase()) ); console.log(`eformat: 找到 ${htmlFiles.length} 个HTML文件`); if (htmlFiles.length === 0) { console.log("当前目录下没有找到HTML文件。"); console.log("用法: eformat --select 或 eformat --file path/file.html"); process.exit(0); } else if (htmlFiles.length === 1) { // 如果只有一个HTML文件,直接格式化 const filePath = path.join(currentDir, htmlFiles[0]); console.log(`eformat: 正在格式化文件 ${htmlFiles[0]}...`); try { formatHtml(filePath); console.log(`√ 成功格式化 ${htmlFiles[0]}`); } catch (error) { console.error(`× 格式化失败: ${error.message}`); process.exit(1); } } else { // 如果有多个HTML文件,让用户选择 console.log("当前目录下有多个HTML文件,请选择要格式化的文件:"); (async () => { const { selectedFile } = await inquirer.prompt([ { type: "list", name: "selectedFile", message: "选择要格式化的HTML文件:", choices: htmlFiles, }, ]); const filePath = path.join(currentDir, selectedFile); console.log(`eformat: 正在格式化文件 ${selectedFile}...`); try { formatHtml(filePath); console.log(`√ 成功格式化 ${selectedFile}`); } catch (error) { console.error(`× 格式化失败: ${error.message}`); process.exit(1); } })(); } } }