|
| 1 | +import Path from 'node:path'; |
| 2 | + |
| 3 | +import {lstat as getStat, readFile, readdir, writeFile} from 'node:fs/promises'; |
| 4 | +import pMap from 'p-map'; |
| 5 | + |
| 6 | +import { fileURLToPath } from 'node:url'; |
| 7 | +const __filename = fileURLToPath(import.meta.url); |
| 8 | +export const projectDir = Path.resolve(__filename, '../../'); |
| 9 | + |
| 10 | +// import PKG from '../package.json' with { type: 'json' }; |
| 11 | +import {createRequire} from 'module'; |
| 12 | +const require = createRequire(import.meta.url); |
| 13 | +export const PKG = require('../package.json' ); |
| 14 | + |
| 15 | + |
| 16 | +import {ignoreDirs, categoryNameMap} from '../build.config.mjs'; |
| 17 | + |
| 18 | +export const absPath = (...paths) => Path.resolve(projectDir, ...paths); |
| 19 | +export const relativePath = (path) => Path.relative(projectDir, path); |
| 20 | +export const toCamelCase = (str) => str |
| 21 | + .replace(/\s(.)/g, ($1) => $1.toUpperCase()) |
| 22 | + .replace(/\s/g, '') |
| 23 | + .replace(/^(.)/, ($1) => $1.toUpperCase()); |
| 24 | + |
| 25 | +async function getTitle(path) { |
| 26 | + const content = await readFile(path, {encoding: 'utf8'}); |
| 27 | + const matched = content.match(/##? (.+)/); |
| 28 | + if (!matched) return ''; |
| 29 | + |
| 30 | + let title = matched[1]; |
| 31 | + title = title.replace(/\[(.*?)\]/g, (_m, p) => p); |
| 32 | + |
| 33 | + return title; |
| 34 | +} |
| 35 | + |
| 36 | +export async function scanDir(dirName, nodeMap, parentPath, _parent, level = 3) { |
| 37 | + const dirPath = absPath(parentPath, dirName); |
| 38 | + const filenames = await readdir(dirPath); |
| 39 | + const children = []; |
| 40 | + |
| 41 | + const curNode = nodeMap[dirPath] = nodeMap[dirPath] || { |
| 42 | + name: dirName, |
| 43 | + path: dirPath, |
| 44 | + title: categoryNameMap[dirName] || toCamelCase(dirName), |
| 45 | + isDir: true, |
| 46 | + intro: undefined, |
| 47 | + level, children, |
| 48 | + parent: undefined, |
| 49 | + }; |
| 50 | + |
| 51 | + await pMap(filenames, async (filename, index) => { |
| 52 | + const curPath = absPath(dirPath, filename); |
| 53 | + const stats = await getStat(curPath) |
| 54 | + |
| 55 | + if (stats.isSymbolicLink()) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (stats.isFile()) { |
| 60 | + if (filename.toLowerCase() === 'readme.md') { |
| 61 | + curNode.intro = await readFile(curPath, {encoding: 'utf8'}); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + return getTitle(curPath).then((title) => { |
| 66 | + children[index] = { |
| 67 | + name: dirName, |
| 68 | + path: curPath, |
| 69 | + title, |
| 70 | + isDir: false, |
| 71 | + intro: undefined, |
| 72 | + level, |
| 73 | + children: [], |
| 74 | + parent: curNode, |
| 75 | + }; |
| 76 | + }); |
| 77 | + } else if (stats.isDirectory()) { |
| 78 | + const subNode = await scanDir(filename, nodeMap, dirPath, curNode, level + 1); |
| 79 | + subNode.parent = curNode; |
| 80 | + children[index] = subNode; |
| 81 | + } else { |
| 82 | + // ignore |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + curNode.children = children.filter((n) => n) |
| 87 | + |
| 88 | + return curNode; |
| 89 | +} |
| 90 | + |
| 91 | +export async function getDirNames(dir) { |
| 92 | + const filenames = await readdir(dir); |
| 93 | + |
| 94 | + let dirNames = filenames.filter((name) => |
| 95 | + !name.startsWith('_') && !name.startsWith('.') |
| 96 | + && (!ignoreDirs.includes(name))); |
| 97 | + |
| 98 | + dirNames = await pMap(dirNames, async (dirName) => { |
| 99 | + const stats = await getStat(absPath(dirName)); |
| 100 | + return stats.isDirectory() ? dirName : null; |
| 101 | + }) |
| 102 | + |
| 103 | + return dirNames.filter((name) => name); |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | +export async function run(func) { |
| 108 | + try { |
| 109 | + await func(); |
| 110 | + } catch(err) { |
| 111 | + console.error('[failed] Error stack: %s', err.stack); |
| 112 | + process.exit(1); |
| 113 | + } |
| 114 | +} |
0 commit comments