create-pdf-forge 1.0.0-canary.0 → 1.0.0-canary.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/package.json +1 -1
- package/src/tree.js +89 -0
- package/tsconfig.json +1 -1
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/src/tree.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/* biome-ignore lint: JavaScript file, TypeScript strict checks not needed */
|
|
2
|
+
import { promises as fs } from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
|
|
6
|
+
const SYMBOLS = {
|
|
7
|
+
BRANCH: '├── ',
|
|
8
|
+
EMPTY: '',
|
|
9
|
+
INDENT: ' ',
|
|
10
|
+
LAST_BRANCH: '└── ',
|
|
11
|
+
VERTICAL: '│ ',
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const getTreeLines = async (
|
|
15
|
+
dirPath,
|
|
16
|
+
depth,
|
|
17
|
+
currentDepth = 0,
|
|
18
|
+
filter = null,
|
|
19
|
+
) => {
|
|
20
|
+
const dirFullpath = path.resolve(dirPath);
|
|
21
|
+
const dirname = path.basename(dirFullpath);
|
|
22
|
+
let lines = [dirname];
|
|
23
|
+
|
|
24
|
+
const dirStat = await fs.stat(dirFullpath);
|
|
25
|
+
if (dirStat.isDirectory() && currentDepth < depth) {
|
|
26
|
+
const childDirents = await fs.readdir(dirFullpath, { withFileTypes: true });
|
|
27
|
+
|
|
28
|
+
// Apply filter if provided
|
|
29
|
+
const filteredDirents = filter
|
|
30
|
+
? childDirents.filter((dirent) => {
|
|
31
|
+
return filter({
|
|
32
|
+
...dirent,
|
|
33
|
+
parentPath: dirFullpath,
|
|
34
|
+
name: dirent.name,
|
|
35
|
+
});
|
|
36
|
+
})
|
|
37
|
+
: childDirents;
|
|
38
|
+
|
|
39
|
+
filteredDirents.sort((a, b) => {
|
|
40
|
+
// orders directories before files
|
|
41
|
+
if (a.isDirectory() && b.isFile()) {
|
|
42
|
+
return -1;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (a.isFile() && b.isDirectory()) {
|
|
46
|
+
return 1;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// orders by name because they are the same type
|
|
50
|
+
// either directory & directory
|
|
51
|
+
// or file & file
|
|
52
|
+
return b.name > a.name ? -1 : 1;
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
for (let i = 0; i < filteredDirents.length; i++) {
|
|
56
|
+
const dirent = filteredDirents[i];
|
|
57
|
+
const isLast = i === filteredDirents.length - 1;
|
|
58
|
+
|
|
59
|
+
const branchingSymbol = isLast ? SYMBOLS.LAST_BRANCH : SYMBOLS.BRANCH;
|
|
60
|
+
const verticalSymbol = isLast ? SYMBOLS.INDENT : SYMBOLS.VERTICAL;
|
|
61
|
+
|
|
62
|
+
if (dirent.isFile()) {
|
|
63
|
+
lines.push(`${branchingSymbol}${dirent.name}`);
|
|
64
|
+
} else {
|
|
65
|
+
const pathToDirectory = path.join(dirFullpath, dirent.name);
|
|
66
|
+
const treeLinesForSubDirectory = await getTreeLines(
|
|
67
|
+
pathToDirectory,
|
|
68
|
+
depth,
|
|
69
|
+
currentDepth + 1,
|
|
70
|
+
filter,
|
|
71
|
+
);
|
|
72
|
+
lines = lines.concat(
|
|
73
|
+
treeLinesForSubDirectory.map((line, index) =>
|
|
74
|
+
index === 0
|
|
75
|
+
? `${branchingSymbol}${line}`
|
|
76
|
+
: `${verticalSymbol}${line}`,
|
|
77
|
+
),
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return lines;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export const tree = async (dirPath, depth, filter = null) => {
|
|
87
|
+
const lines = await getTreeLines(dirPath, depth, 0, filter);
|
|
88
|
+
return lines.join(os.EOL);
|
|
89
|
+
};
|