1 | import * as fs from 'fs';
|
2 | import * as path from 'path';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | function deepReadDirSync(targetDir) {
|
12 | var targetDirAbsPath = path.resolve(targetDir);
|
13 |
|
14 | if (!fs.existsSync(targetDirAbsPath)) {
|
15 | throw new Error(`Cannot read contents of ${targetDirAbsPath}. Directory does not exist.`);
|
16 | }
|
17 |
|
18 | if (!fs.statSync(targetDirAbsPath).isDirectory()) {
|
19 | throw new Error(`Cannot read contents of ${targetDirAbsPath}, because it is not a directory.`);
|
20 | }
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | var deepReadCurrentDir = (currentDirAbsPath) => {
|
26 | return fs.readdirSync(currentDirAbsPath).reduce((absPaths, itemName) => {
|
27 | var itemAbsPath = path.join(currentDirAbsPath, itemName);
|
28 |
|
29 | if (fs.statSync(itemAbsPath).isDirectory()) {
|
30 | return [...absPaths, ...deepReadCurrentDir(itemAbsPath)];
|
31 | }
|
32 |
|
33 | return [...absPaths, itemAbsPath];
|
34 | }, []);
|
35 | };
|
36 |
|
37 | return deepReadCurrentDir(targetDirAbsPath).map(absPath => path.relative(targetDirAbsPath, absPath));
|
38 | }
|
39 |
|
40 | export { deepReadDirSync };
|
41 |
|