hudada-cli
Version:
专为程序员准备的本地文档搜索,快捷开发工具
3 lines (2 loc) • 685 B
JavaScript
import*as o from"fs";import*as t from"path";import{fileURLToPath as r}from"url";const i=r(import.meta.url),n=t.dirname(i);async function e(r){try{console.log(r);const i=t.join(n,"comment"),e=(await o.promises.readdir(i)).filter((r=>r.endsWith(".txt")&&o.statSync(t.join(i,r)).isFile()));if(0===e.length)throw new Error("没有找到任何 txt 文件");if(r?.all){for(const r of e){const n=await o.promises.readFile(t.join(i,r),"utf-8");console.log(n)}return}const s=e[Math.floor(Math.random()*e.length)],a=await o.promises.readFile(t.join(i,s),"utf-8");console.log(a)}catch(o){throw console.error("读取文件时发生错误:",o),o}}export{e as getRandomComment};