@cycoatrole/treex 0.0.0-stage → 1.0.0
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/LICENSE +17 -0
- package/README.md +86 -3
- package/bin/treex.js +8 -0
- package/package.json +4 -3
- package/src/exporter.js +18 -0
- package/src/formatter.js +63 -0
- package/src/index.js +53 -0
- package/src/parser.js +111 -0
- package/src/tree.js +90 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Cycoatrole
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
package/README.md
CHANGED
|
@@ -1,3 +1,86 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# TreeX
|
|
2
|
+
|
|
3
|
+
A powerful directory tree generator for Windows, built with Node.js.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```powershell
|
|
8
|
+
npm install -g treex
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or run without installing:
|
|
12
|
+
|
|
13
|
+
```powershell
|
|
14
|
+
npx treex
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```powershell
|
|
20
|
+
treex [directory] [options]
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Examples
|
|
24
|
+
|
|
25
|
+
Display the current directory:
|
|
26
|
+
|
|
27
|
+
```powershell
|
|
28
|
+
treex
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Display a specific directory:
|
|
32
|
+
|
|
33
|
+
```powershell
|
|
34
|
+
treex "D:\Users\Sahand\Projects"
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Include dot-prefixed entries:
|
|
38
|
+
|
|
39
|
+
```powershell
|
|
40
|
+
treex --all
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Display directories only:
|
|
44
|
+
|
|
45
|
+
```powershell
|
|
46
|
+
treex --dirs
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Limit directory depth:
|
|
50
|
+
|
|
51
|
+
```powershell
|
|
52
|
+
treex --depth 3
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Exclude directories:
|
|
56
|
+
|
|
57
|
+
```powershell
|
|
58
|
+
treex --exclude node_modules,.git
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Export as text:
|
|
62
|
+
|
|
63
|
+
```powershell
|
|
64
|
+
treex --output tree.txt
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Export as JSON:
|
|
68
|
+
|
|
69
|
+
```powershell
|
|
70
|
+
treex --json --output tree.json
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Print JSON directly:
|
|
74
|
+
|
|
75
|
+
```powershell
|
|
76
|
+
treex --json
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Requirements
|
|
80
|
+
|
|
81
|
+
- Node.js 18 or newer
|
|
82
|
+
- Windows
|
|
83
|
+
|
|
84
|
+
## License
|
|
85
|
+
|
|
86
|
+
MIT
|
package/bin/treex.js
ADDED
package/package.json
CHANGED
package/src/exporter.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
const fs = require("node:fs");
|
|
3
|
+
const path = require("node:path");
|
|
4
|
+
const { formatTree } = require("./formatter");
|
|
5
|
+
|
|
6
|
+
function exportTree(tree, filename, json) {
|
|
7
|
+
const outputPath = path.resolve(filename);
|
|
8
|
+
|
|
9
|
+
const content = json
|
|
10
|
+
? JSON.stringify(tree, null, 4)
|
|
11
|
+
: formatTree(tree) + "\n";
|
|
12
|
+
|
|
13
|
+
fs.writeFileSync(outputPath, content, "utf8");
|
|
14
|
+
|
|
15
|
+
return outputPath;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports = { exportTree };
|
package/src/formatter.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
|
|
2
|
+
function formatTree(tree) {
|
|
3
|
+
const lines = [tree.name];
|
|
4
|
+
|
|
5
|
+
function render(node, prefix) {
|
|
6
|
+
if (node.error) {
|
|
7
|
+
lines.push(`${prefix}└── [Access denied: ${node.error}]`);
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const children = node.children || [];
|
|
12
|
+
|
|
13
|
+
children.forEach((child, index) => {
|
|
14
|
+
const last = index === children.length - 1;
|
|
15
|
+
|
|
16
|
+
const branch = last ? "└── " : "├── ";
|
|
17
|
+
const nextPrefix = prefix + (last ? " " : "│ ");
|
|
18
|
+
|
|
19
|
+
let name = child.name;
|
|
20
|
+
|
|
21
|
+
if (child.type === "symlink") {
|
|
22
|
+
name += " [SYMLINK]";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
lines.push(prefix + branch + name);
|
|
26
|
+
|
|
27
|
+
if (child.type === "directory") {
|
|
28
|
+
render(child, nextPrefix);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
render(tree, "");
|
|
34
|
+
|
|
35
|
+
return lines.join("\n");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function countEntries(tree) {
|
|
39
|
+
let directories = 0;
|
|
40
|
+
let files = 0;
|
|
41
|
+
let symlinks = 0;
|
|
42
|
+
|
|
43
|
+
function count(node) {
|
|
44
|
+
for (const child of node.children || []) {
|
|
45
|
+
if (child.type === "directory") {
|
|
46
|
+
directories++;
|
|
47
|
+
count(child);
|
|
48
|
+
}
|
|
49
|
+
else if (child.type === "symlink") {
|
|
50
|
+
symlinks++;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
files++;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
count(tree);
|
|
59
|
+
|
|
60
|
+
return { directories, files, symlinks };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
module.exports = { formatTree, countEntries };
|
package/src/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
|
|
2
|
+
const path = require("node:path");
|
|
3
|
+
const { parseArgs, HELP } = require("./parser");
|
|
4
|
+
const { buildTree } = require("./tree");
|
|
5
|
+
const { formatTree, countEntries } = require("./formatter");
|
|
6
|
+
const { exportTree } = require("./exporter");
|
|
7
|
+
|
|
8
|
+
async function main(args) {
|
|
9
|
+
const options = parseArgs(args);
|
|
10
|
+
|
|
11
|
+
if (options.help) {
|
|
12
|
+
console.log(HELP);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (options.version) {
|
|
17
|
+
console.log("TreeX v1.0.0");
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const root = path.resolve(options.directory);
|
|
22
|
+
|
|
23
|
+
const tree = buildTree(root, options);
|
|
24
|
+
|
|
25
|
+
const counts = countEntries(tree);
|
|
26
|
+
|
|
27
|
+
if (options.output) {
|
|
28
|
+
const outputPath = exportTree(
|
|
29
|
+
tree,
|
|
30
|
+
options.output,
|
|
31
|
+
options.json
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
console.log(`TreeX: Exported successfully to ${outputPath}`);
|
|
35
|
+
}
|
|
36
|
+
else if (options.json) {
|
|
37
|
+
console.log(JSON.stringify(tree, null, 4));
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
console.log(formatTree(tree));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (!options.json && !options.output) {
|
|
44
|
+
console.log("");
|
|
45
|
+
console.log(
|
|
46
|
+
`${counts.directories} directories, ` +
|
|
47
|
+
`${counts.files} files, ` +
|
|
48
|
+
`${counts.symlinks} symbolic links`
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
module.exports = { main };
|
package/src/parser.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
|
|
2
|
+
const HELP = `
|
|
3
|
+
TreeX v1.0.0
|
|
4
|
+
Advanced directory tree generator
|
|
5
|
+
|
|
6
|
+
Usage:
|
|
7
|
+
treex [directory] [options]
|
|
8
|
+
|
|
9
|
+
Options:
|
|
10
|
+
-a, --all Include dot-prefixed entries
|
|
11
|
+
-d, --dirs Show directories only
|
|
12
|
+
--depth <number> Maximum directory depth
|
|
13
|
+
--exclude <names> Exclude comma-separated names
|
|
14
|
+
--output <file> Export tree to a file
|
|
15
|
+
--json Output JSON instead of text
|
|
16
|
+
-h, --help Show this help message
|
|
17
|
+
-v, --version Show version
|
|
18
|
+
|
|
19
|
+
Examples:
|
|
20
|
+
treex
|
|
21
|
+
treex "D:\\Projects"
|
|
22
|
+
treex -a
|
|
23
|
+
treex --depth 2
|
|
24
|
+
treex --exclude node_modules,.git
|
|
25
|
+
treex --output tree.txt
|
|
26
|
+
treex --json --output tree.json
|
|
27
|
+
`;
|
|
28
|
+
|
|
29
|
+
function parseArgs(args) {
|
|
30
|
+
const options = {
|
|
31
|
+
directory: process.cwd(),
|
|
32
|
+
all: false,
|
|
33
|
+
dirsOnly: false,
|
|
34
|
+
depth: Infinity,
|
|
35
|
+
exclude: [],
|
|
36
|
+
output: null,
|
|
37
|
+
json: false,
|
|
38
|
+
help: false,
|
|
39
|
+
version: false
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
let directoryProvided = false;
|
|
43
|
+
|
|
44
|
+
for (let i = 0; i < args.length; i++) {
|
|
45
|
+
const arg = args[i];
|
|
46
|
+
|
|
47
|
+
if (arg === "-h" || arg === "--help") {
|
|
48
|
+
options.help = true;
|
|
49
|
+
}
|
|
50
|
+
else if (arg === "-v" || arg === "--version") {
|
|
51
|
+
options.version = true;
|
|
52
|
+
}
|
|
53
|
+
else if (arg === "-a" || arg === "--all") {
|
|
54
|
+
options.all = true;
|
|
55
|
+
}
|
|
56
|
+
else if (arg === "-d" || arg === "--dirs") {
|
|
57
|
+
options.dirsOnly = true;
|
|
58
|
+
}
|
|
59
|
+
else if (arg === "--json") {
|
|
60
|
+
options.json = true;
|
|
61
|
+
}
|
|
62
|
+
else if (arg === "--depth") {
|
|
63
|
+
const value = args[++i];
|
|
64
|
+
|
|
65
|
+
if (value === undefined || !/^\d+$/.test(value)) {
|
|
66
|
+
throw new Error("--depth requires a non-negative integer.");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
options.depth = Number(value);
|
|
70
|
+
}
|
|
71
|
+
else if (arg === "--exclude") {
|
|
72
|
+
const value = args[++i];
|
|
73
|
+
|
|
74
|
+
if (!value) {
|
|
75
|
+
throw new Error("--exclude requires at least one name.");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
options.exclude.push(
|
|
79
|
+
...value.split(",").map(name => name.trim()).filter(Boolean)
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
else if (arg === "--output") {
|
|
83
|
+
const value = args[++i];
|
|
84
|
+
|
|
85
|
+
if (!value) {
|
|
86
|
+
throw new Error("--output requires a filename.");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
options.output = value;
|
|
90
|
+
}
|
|
91
|
+
else if (arg.startsWith("-")) {
|
|
92
|
+
throw new Error(`Unknown option: ${arg}`);
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
if (directoryProvided) {
|
|
96
|
+
throw new Error("Only one directory can be specified.");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
options.directory = arg;
|
|
100
|
+
directoryProvided = true;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (options.json && !options.output && !options.help && !options.version) {
|
|
105
|
+
// JSON is written to standard output when no output file is specified.
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return options;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
module.exports = { parseArgs, HELP };
|
package/src/tree.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
|
|
2
|
+
const fs = require("node:fs");
|
|
3
|
+
const path = require("node:path");
|
|
4
|
+
|
|
5
|
+
function buildTree(root, options) {
|
|
6
|
+
const stats = fs.statSync(root);
|
|
7
|
+
|
|
8
|
+
if (!stats.isDirectory()) {
|
|
9
|
+
throw new Error(`Not a directory: ${root}`);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const tree = {
|
|
13
|
+
name: path.basename(root) || root,
|
|
14
|
+
type: "directory",
|
|
15
|
+
path: root,
|
|
16
|
+
children: []
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function readDirectory(directory, node, depth) {
|
|
20
|
+
if (depth >= options.depth) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
let entries;
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
entries = fs.readdirSync(directory, {
|
|
28
|
+
withFileTypes: true
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
node.error = error.code || error.message;
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
entries = entries.filter(entry => {
|
|
37
|
+
if (!options.all && entry.name.startsWith(".")) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (options.exclude.includes(entry.name)) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (options.dirsOnly && !entry.isDirectory()) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return true;
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
entries.sort((a, b) => {
|
|
53
|
+
if (a.isDirectory() !== b.isDirectory()) {
|
|
54
|
+
return a.isDirectory() ? -1 : 1;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return a.name.localeCompare(b.name, undefined, {
|
|
58
|
+
numeric: true,
|
|
59
|
+
sensitivity: "base"
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
for (const entry of entries) {
|
|
64
|
+
const fullPath = path.join(directory, entry.name);
|
|
65
|
+
|
|
66
|
+
const child = {
|
|
67
|
+
name: entry.name,
|
|
68
|
+
path: fullPath,
|
|
69
|
+
type: entry.isDirectory()
|
|
70
|
+
? "directory"
|
|
71
|
+
: entry.isSymbolicLink()
|
|
72
|
+
? "symlink"
|
|
73
|
+
: "file"
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
if (entry.isDirectory()) {
|
|
77
|
+
child.children = [];
|
|
78
|
+
readDirectory(fullPath, child, depth + 1);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
node.children.push(child);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
readDirectory(root, tree, 0);
|
|
86
|
+
|
|
87
|
+
return tree;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
module.exports = { buildTree };
|