12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- import * as fs from 'fs';
- import * as path from 'path';
- function deepReadDirSync(targetDir) {
- const targetDirAbsPath = path.resolve(targetDir);
- if (!fs.existsSync(targetDirAbsPath)) {
- throw new Error(`Cannot read contents of ${targetDirAbsPath}. Directory does not exist.`);
- }
- if (!fs.statSync(targetDirAbsPath).isDirectory()) {
- throw new Error(`Cannot read contents of ${targetDirAbsPath}, because it is not a directory.`);
- }
-
-
-
- const deepReadCurrentDir = (currentDirAbsPath) => {
- return fs.readdirSync(currentDirAbsPath).reduce((absPaths, itemName) => {
- const itemAbsPath = path.join(currentDirAbsPath, itemName);
- if (fs.statSync(itemAbsPath).isDirectory()) {
- return absPaths.concat(deepReadCurrentDir(itemAbsPath));
- }
- absPaths.push(itemAbsPath);
- return absPaths;
- }, []);
- };
- return deepReadCurrentDir(targetDirAbsPath).map(absPath => path.relative(targetDirAbsPath, absPath));
- }
- export { deepReadDirSync };
|