hudada-cli
Version:
专为程序员准备的本地文档搜索,快捷开发工具
3 lines (2 loc) • 3.75 kB
JavaScript
import{existsSync as e,mkdirSync as t,readdirSync as o,statSync as r,readFileSync as n,appendFileSync as s}from"fs";import i,{dirname as c,join as l,resolve as m,extname as a}from"path";import"os";import f from"chalk";import{marked as u}from"marked";import d from"open";import{fileURLToPath as p}from"url";const g=c(p(import.meta.url)),h=l(process.env.HOME||process.env.USERPROFILE||"",".my-cli"),y=l(h,"mydir.txt");function x(e,t){if(!t)return e;const o=new RegExp(t,"gi");return e.replace(o,(e=>f.bgYellow.white(e)))}e(h)||t(h,{recursive:!0});const w={text:[".md",".txt",".html",".js",".json",".css",".ts",".tsx",".conf"],image:[".png",".jpg",".jpeg",".gif",".bmp",".webp"]},C=l(g,"../data"),$=o(C).filter((e=>[...w.text,...w.image].some((t=>e.endsWith(t))))).map((e=>e.split(".")[0]));function v(e,t){const s=[];try{const i=o(e);for(const o of i){const i=l(e,o);if(r(i).isDirectory())s.push(...v(i,t));else if(w.text.some((e=>o.endsWith(e))))try{const e=n(i,"utf-8"),o=e.split("\n").filter((e=>e.toLowerCase().includes(t.toLowerCase())));if(o.length>0){const e=o.map((e=>x(e,t)));s.push({file:i.replace(C,"").slice(1),matches:e})}}catch(e){console.error(f.yellow(`警告: 无法读取文件 ${o}`))}}}catch(t){console.error(f.yellow(`警告: 无法读取目录 ${e}`))}return s}const j=e=>{try{return o(e).filter((t=>{const o=l(e,t),n=r(o).isDirectory(),s=[...w.text,...w.image].some((e=>t.endsWith(e)));return n||s}))}catch{return[]}};function L(t){try{if(!e(t))return[];return n(t,"utf-8").split("\n").filter((e=>e.trim()))}catch(e){return console.error(f.red(`读取 mydir.txt 时出错: ${e.message}`)),[]}}function b(){const t=[],o=l(g,"../data");e(o)&&t.push(o);try{if(e(y)){const o=n(y,"utf-8").split("\n").filter((e=>e.trim())).filter((t=>e(t)));t.push(...o)}}catch(e){console.error(f.yellow(`Warning: Could not read mydir.txt: ${e.message}`))}return t}function W(t){const o=b(),r=[...w.text,...w.image],n=t.toLowerCase();for(const s of o)if(r.some((e=>n.endsWith(e)))){const o=l(s,t);if(e(o))return o}else for(const o of r){const r=l(s,t+o);if(e(r))return r}return null}function E(t,o,r,c){const{mainCommand:l,subCommand:p}=o;let g="";if(t.append)try{const t=b();let r=process.argv.slice(2);const n=r.findIndex((e=>"-a"===e||"--append"===e));if(-1===n)return;const c=r.slice(0,n),[l,a]=c,u=r.slice(n+1).join(" ");if(!u)return void console.error(f.yellow("请提供要追加的文本"));a&&a.endsWith(".md");let d="";for(let r=t.length-1;r>-1;r--){let n=o.subCommand??"";n?n=i.extname(n)?n:n+".md":o.mainCommand=i.extname(o.mainCommand)?o.mainCommand:o.mainCommand+".md";let s=m(t[r],o.mainCommand,n);if(e(s)){d=s;break}}if(!d)return;return s(d,`\n${u}\n`),void console.log(f.green("文本已追加到文件"))}catch(e){return void console.error(f.red(`追加文本失败: ${e.message}`))}else g=`${l}${p?`/${p}`:""}`;const h=W(g);if(!h){const e=[...w.text,...w.image];return void console.error(f.red(`未找到文件: ${g} (支持的文件类型: ${e.join(", ")})`))}const y=a(h).toLowerCase();try{if(w.image.includes(y))return console.log(f.green(`正在打开图片文件: ${h}`)),void d(h);const e=n(h,"utf-8");if(t.search){const o=e.split("\n").filter((e=>e.toLowerCase().includes(t.search.toLowerCase())));if(o.length>0){console.log(f.green(`找到 ${o.length} 处匹配:`));const e=o.map((e=>x(e,t.search)));console.log(u(e.join("\n")))}else console.log(f.yellow("未找到匹配内容"))}else console.log(u(e))}catch(e){console.error(f.red(`处理文件时出错: ${e.message}`))}}export{w as SUPPORTED_EXTENSIONS,$ as availableFiles,h as configDir,C as dataPath,W as findFileInPaths,b as getAllSearchPaths,j as getDirectoryContent,L as getExistingPaths,E as getFileContent,y as mydirFile,v as searchInDirectory};