All files index.ts

100% Statements 41/41
87.09% Branches 27/31
100% Functions 9/9
100% Lines 40/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126                                                                                          7x                     17x 17x 16x 86x         86x 2x   84x 6x   78x 72x   6x   16x 4x 14x 14x 2x   14x     12x   46x 46x 8x 8x 38x 38x         16x 48x 14x   34x       1x 61x 61x 61x 51x 51x 51x   61x             51x     1x 1x    
import fs from 'node:fs';
import path from 'node:path';
 
export interface RecursiveReaddirFilesOptions {
  /**
   * Ignore files
   * @example `/\/(node_modules|\.git)/`
   */
  ignored?: RegExp;
  /**
   * Specifies a list of `glob` patterns that match files to be included in compilation.
   * @example `/(\.json)$/`
   */
  include?: RegExp;
  /**
   * Specifies a list of files to be excluded from compilation.
   * @example `/(package\.json)$/`
   */
  exclude?: RegExp;
  /** Provide filtering methods to filter data. */
  filter?: (item: IFileDirStat) => boolean;
}
 
export interface IFileDirStat extends Partial<fs.Stats> {
  /**
   * @example `/a/sum.jpg` => `sum.jpg`
   */
  name: string;
  /**
   * @example `/basic/src/utils/sum.ts`
   */
  path: string;
  /**
   * @example `/a/b.jpg` => `jpg`
   */
  ext?: string;
}
 
type Callback = (filepath: string, stat: IFileDirStat) => void;
 
export default function recursiveReaddirFiles(
  rootPath: string,
  options: RecursiveReaddirFilesOptions = {},
  callback?: Callback,
): Promise<IFileDirStat[]> {
  return getFiles(rootPath, options, [], callback);
}
 
export { recursiveReaddirFiles };
 
async function getFiles(
  rootPath: string,
  options: RecursiveReaddirFilesOptions = {},
  files: IFileDirStat[] = [],
  callback?: Callback,
): Promise<IFileDirStat[]> {
  const { ignored, include, exclude, filter } = options;
  const filesData = await fs.promises.readdir(rootPath);
  const fileDir: IFileDirStat[] = filesData
    .map((file) => ({
      name: file,
      path: path.join(rootPath, file),
    }))
    .filter((item) => {
      if (include && include.test(item.path)) {
        return true;
      }
      if (exclude && exclude.test(item.path)) {
        return false;
      }
      if (ignored) {
        return !ignored.test(item.path);
      }
      return true;
    });
  if (callback) {
    fileDir.map(async (item: IFileDirStat) => {
      const stat = await getStat(item.path);
      if (stat.isDirectory()) {
        getFiles(item.path, options, [], callback);
      }
      callback(item.path, stat);
    });
  } else {
    await Promise.all(
      fileDir.map(async (item: IFileDirStat) => {
        const stat = await getStat(item.path);
        if (stat.isDirectory()) {
          const arr = await getFiles(item.path, options, []);
          files = files.concat(arr);
        } else Eif (stat.isFile()) {
          files.push(stat);
        }
      }),
    );
  }
  return files.filter((item) => {
    if (filter && typeof filter === 'function') {
      return filter(item);
    }
    return true;
  });
}
 
export const getStat = async (filepath: string): Promise<IFileDirStat> => {
  const stat = (await fs.promises.stat(filepath)) as IFileDirStat;
  stat.ext = '';
  if (stat.isFile()) {
    stat.ext = getExt(filepath);
    stat.name = path.basename(filepath);
    stat.path = path.resolve(filepath);
  }
  return stat;
};
 
/**
 * Get ext
 * @param {String} filePath `/a/b.jpg` => `jpg`
 */
export const getExt = (filePath: string) => path.extname(filePath).replace(/^\./, '').toLowerCase();
 
/** CommonJS default export hack */
Eif (typeof module === 'object' && typeof module.exports === 'object') {
  module.exports = Object.assign(module.exports.default, module.exports);
}