const { resolve } = require('path');
const { readdirSync, statSync } = require('fs');

/* 收集markdown文档 */
const collection = (dir, deep) => {
  const files = readdirSync(dir);
  const fileMap = files.reduce((acc, item) => {
    const filePath = resolve(dir, item);
    const state = statSync(filePath);
    if (deep && state.isDirectory()) {
      acc.push(...collection(filePath, deep));
    } else {
      if (filePath.endsWith('.md')) {
        acc.push(filePath);
      }
    }
    return acc;
  }, []);

  return fileMap;
};

module.exports = {
  collection,
};
