const rootDir = path.resolve(__dirname); let dirsStr = ''; const createDirTree = (dir, skip) => { const recur = (curDir) => { fs.readdirSync(curDir, { withFileTypes: true }).forEach((value) => { if (skip.includes(value.name)) return; const childPath = path.join(curDir, value.name); const depths = childPath.replace(rootDir, '').split(path.sep).filter(Boolean).length; const isDirectory = value.isDirectory(); const prefix = `${'| '.repeat(depths - 1)}${isDirectory ? '+' : ''}-- `; console.log(`${prefix}${value.name}`); dirsStr += `${prefix}${value.name}\n` if (isDirectory) { recur(path.join(curDir, value.name)); } }); } recur(dir); const dirJson = { dirsStr: '`' + `${dirsStr}` + '`' } fs.writeFile('./src/Mock/dirs.json',JSON.stringify(dirJson) , function (err) { }) } createDirTree(rootDir, ["node_modules", ".vscode", ".git", ".next", "_next"])
在html展示时,替换换行符为br
dirStr.value = dirs.dirsStr.replace(/(\n|\r|\r\n|↵)/g, '<br/>')
将字符串中的空格符替换成HTML中正确显示连续空格的 转义字符
const text = 'Hello\s\s\sWorld\s2021' text.replace(/\s/g, ' ');