cartotree 0.1.0 → 0.2.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/.claude-plugin/marketplace.json +11 -0
- package/.claude-plugin/plugin.json +12 -0
- package/LICENSE +20 -20
- package/README.md +246 -200
- package/bin/cartotree.js +162 -55
- package/hooks/hooks.json +14 -0
- package/package.json +45 -15
- package/skills/cartotree/SKILL.md +32 -0
- package/src/ai.js +112 -112
- package/src/atlas/cache.js +46 -0
- package/src/atlas/files.js +56 -0
- package/src/atlas/git.js +65 -0
- package/src/atlas/init.js +114 -0
- package/src/atlas/links.js +87 -0
- package/src/atlas/locate.js +218 -0
- package/src/atlas/mcp.js +106 -0
- package/src/atlas/peek.js +24 -0
- package/src/atlas/symbols.js +41 -0
- package/src/clipboard.js +10 -10
- package/src/config.js +25 -25
- package/src/exports.js +74 -65
- package/src/ignore.js +30 -30
- package/src/sync.js +29 -29
- package/src/tree.js +96 -96
package/src/config.js
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
|
|
4
|
-
const DEFAULTS = {
|
|
5
|
-
ai: false,
|
|
6
|
-
json: false,
|
|
7
|
-
exports: true,
|
|
8
|
-
depth: 3,
|
|
9
|
-
copy: false,
|
|
10
|
-
sync: false,
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
function loadConfig(rootPath) {
|
|
14
|
-
const configPath = path.join(rootPath, '.cartotreerc');
|
|
15
|
-
if (!fs.existsSync(configPath)) return DEFAULTS;
|
|
16
|
-
|
|
17
|
-
try {
|
|
18
|
-
const parsed = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
|
19
|
-
return { ...DEFAULTS, ...parsed.default };
|
|
20
|
-
} catch (err) {
|
|
21
|
-
console.warn('⚠ .cartotreerc parse error, using defaults');
|
|
22
|
-
return DEFAULTS;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
const DEFAULTS = {
|
|
5
|
+
ai: false,
|
|
6
|
+
json: false,
|
|
7
|
+
exports: true,
|
|
8
|
+
depth: 3,
|
|
9
|
+
copy: false,
|
|
10
|
+
sync: false,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
function loadConfig(rootPath) {
|
|
14
|
+
const configPath = path.join(rootPath, '.cartotreerc');
|
|
15
|
+
if (!fs.existsSync(configPath)) return DEFAULTS;
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
const parsed = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
|
19
|
+
return { ...DEFAULTS, ...parsed.default };
|
|
20
|
+
} catch (err) {
|
|
21
|
+
console.warn('⚠ .cartotreerc parse error, using defaults');
|
|
22
|
+
return DEFAULTS;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
26
|
module.exports = { loadConfig };
|
package/src/exports.js
CHANGED
|
@@ -1,65 +1,74 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
|
|
3
|
-
const SUPPORTED_EXTENSIONS = ['.js', '.ts', '.jsx', '.tsx', '.mjs', '.cjs'];
|
|
4
|
-
|
|
5
|
-
function
|
|
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
|
-
// export
|
|
35
|
-
const
|
|
36
|
-
while ((match =
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// module.exports
|
|
50
|
-
const
|
|
51
|
-
while ((match =
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
|
|
3
|
+
const SUPPORTED_EXTENSIONS = ['.js', '.ts', '.jsx', '.tsx', '.mjs', '.cjs'];
|
|
4
|
+
|
|
5
|
+
function stripComments(src) {
|
|
6
|
+
return src
|
|
7
|
+
.replace(/\/\*[\s\S]*?\*\//g, '')
|
|
8
|
+
.replace(/(^|[^:'"`\\])\/\/.*$/gm, '$1');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function parseExports(filePath) {
|
|
12
|
+
const ext = SUPPORTED_EXTENSIONS.find(e => filePath.endsWith(e));
|
|
13
|
+
if (!ext) return null;
|
|
14
|
+
|
|
15
|
+
let content;
|
|
16
|
+
try {
|
|
17
|
+
content = fs.readFileSync(filePath, 'utf-8');
|
|
18
|
+
} catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// strip comments so examples like `// export { foo }` are not parsed
|
|
23
|
+
content = stripComments(content);
|
|
24
|
+
|
|
25
|
+
const names = new Set();
|
|
26
|
+
|
|
27
|
+
// export function foo / export async function foo / export class foo / export const foo
|
|
28
|
+
const namedPattern = /export\s+(?:async\s+)?(?:function\*?|const|let|var|class|abstract\s+class|type|interface|enum)\s+(\w+)/g;
|
|
29
|
+
let match;
|
|
30
|
+
while ((match = namedPattern.exec(content)) !== null) {
|
|
31
|
+
if (match[1] !== 'default') names.add(match[1]);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// export { foo, bar as baz }
|
|
35
|
+
const bracketPattern = /export\s*\{([^}]+)\}/g;
|
|
36
|
+
while ((match = bracketPattern.exec(content)) !== null) {
|
|
37
|
+
match[1].split(',')
|
|
38
|
+
.map(s => s.trim().split(/\s+as\s+/).pop().trim())
|
|
39
|
+
.filter(Boolean)
|
|
40
|
+
.forEach(n => names.add(n));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// export default function foo / export default class foo
|
|
44
|
+
const defaultNamedPattern = /export\s+default\s+(?:function|class)\s+(\w+)/g;
|
|
45
|
+
while ((match = defaultNamedPattern.exec(content)) !== null) {
|
|
46
|
+
names.add(match[1]);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// module.exports = { foo, bar }
|
|
50
|
+
const cjsPattern = /module\.exports\s*=\s*\{([^}]+)\}/g;
|
|
51
|
+
while ((match = cjsPattern.exec(content)) !== null) {
|
|
52
|
+
match[1].split(',')
|
|
53
|
+
.map(s => s.trim().split(':')[0].trim())
|
|
54
|
+
.filter(s => /^\w+$/.test(s))
|
|
55
|
+
.forEach(n => names.add(n));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// module.exports.foo = ...
|
|
59
|
+
const cjsDotPattern = /module\.exports\.(\w+)\s*=/g;
|
|
60
|
+
while ((match = cjsDotPattern.exec(content)) !== null) {
|
|
61
|
+
names.add(match[1]);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return names.size > 0 ? [...names] : null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function formatExports(names, max = 4) {
|
|
68
|
+
if (!names) return '';
|
|
69
|
+
const visible = names.slice(0, max);
|
|
70
|
+
const rest = names.length > max ? `, +${names.length - max}` : '';
|
|
71
|
+
return `<${visible.join(', ')}${rest}>`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
module.exports = { parseExports, formatExports, stripComments };
|
package/src/ignore.js
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
|
|
4
|
-
const DEFAULTS = [
|
|
5
|
-
'node_modules', '.git', 'dist', 'build', '.next',
|
|
6
|
-
'.nuxt', 'coverage', '.cache', '.DS_Store', 'Thumbs.db'
|
|
7
|
-
];
|
|
8
|
-
|
|
9
|
-
function loadIgnorePatterns(rootPath) {
|
|
10
|
-
const ignorePath = path.join(rootPath, '.cartotreeignore');
|
|
11
|
-
|
|
12
|
-
if (!fs.existsSync(ignorePath)) return DEFAULTS;
|
|
13
|
-
|
|
14
|
-
const lines = fs.readFileSync(ignorePath, 'utf-8')
|
|
15
|
-
.split('\n')
|
|
16
|
-
.map(l => l.trim())
|
|
17
|
-
.filter(l => l && !l.startsWith('#'));
|
|
18
|
-
|
|
19
|
-
return [...new Set([...DEFAULTS, ...lines])];
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function shouldIgnore(name, patterns) {
|
|
23
|
-
return patterns.some(pattern => {
|
|
24
|
-
if (pattern.startsWith('*.')) {
|
|
25
|
-
return name.endsWith(pattern.slice(1));
|
|
26
|
-
}
|
|
27
|
-
return name === pattern;
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
const DEFAULTS = [
|
|
5
|
+
'node_modules', '.git', 'dist', 'build', '.next',
|
|
6
|
+
'.nuxt', 'coverage', '.cache', '.DS_Store', 'Thumbs.db'
|
|
7
|
+
];
|
|
8
|
+
|
|
9
|
+
function loadIgnorePatterns(rootPath) {
|
|
10
|
+
const ignorePath = path.join(rootPath, '.cartotreeignore');
|
|
11
|
+
|
|
12
|
+
if (!fs.existsSync(ignorePath)) return DEFAULTS;
|
|
13
|
+
|
|
14
|
+
const lines = fs.readFileSync(ignorePath, 'utf-8')
|
|
15
|
+
.split('\n')
|
|
16
|
+
.map(l => l.trim())
|
|
17
|
+
.filter(l => l && !l.startsWith('#'));
|
|
18
|
+
|
|
19
|
+
return [...new Set([...DEFAULTS, ...lines])];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function shouldIgnore(name, patterns) {
|
|
23
|
+
return patterns.some(pattern => {
|
|
24
|
+
if (pattern.startsWith('*.')) {
|
|
25
|
+
return name.endsWith(pattern.slice(1));
|
|
26
|
+
}
|
|
27
|
+
return name === pattern;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
31
|
module.exports = { loadIgnorePatterns, shouldIgnore };
|
package/src/sync.js
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const chalk = require('chalk');
|
|
3
|
-
|
|
4
|
-
const START_MARKER = '<!-- CARTOTREE-START -->';
|
|
5
|
-
const END_MARKER = '<!-- CARTOTREE-END -->';
|
|
6
|
-
|
|
7
|
-
function syncToFile(tree, filePath) {
|
|
8
|
-
const plain = tree.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
|
|
9
|
-
const block = `${START_MARKER}\n\`\`\`\n${plain}\`\`\`\n${END_MARKER}`;
|
|
10
|
-
|
|
11
|
-
if (fs.existsSync(filePath)) {
|
|
12
|
-
let content = fs.readFileSync(filePath, 'utf-8');
|
|
13
|
-
const startIdx = content.indexOf(START_MARKER);
|
|
14
|
-
const endIdx = content.indexOf(END_MARKER);
|
|
15
|
-
|
|
16
|
-
if (startIdx !== -1 && endIdx !== -1) {
|
|
17
|
-
content = content.slice(0, startIdx) + block + content.slice(endIdx + END_MARKER.length);
|
|
18
|
-
} else {
|
|
19
|
-
content += `\n\n${block}\n`;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
fs.writeFileSync(filePath, content, 'utf-8');
|
|
23
|
-
console.log(chalk.green(`✔ Synced to ${filePath}`));
|
|
24
|
-
} else {
|
|
25
|
-
fs.writeFileSync(filePath, `# Project Structure\n\n${block}\n`, 'utf-8');
|
|
26
|
-
console.log(chalk.green(`✔ Created ${filePath}`));
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const chalk = require('chalk');
|
|
3
|
+
|
|
4
|
+
const START_MARKER = '<!-- CARTOTREE-START -->';
|
|
5
|
+
const END_MARKER = '<!-- CARTOTREE-END -->';
|
|
6
|
+
|
|
7
|
+
function syncToFile(tree, filePath) {
|
|
8
|
+
const plain = tree.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
|
|
9
|
+
const block = `${START_MARKER}\n\`\`\`\n${plain}\`\`\`\n${END_MARKER}`;
|
|
10
|
+
|
|
11
|
+
if (fs.existsSync(filePath)) {
|
|
12
|
+
let content = fs.readFileSync(filePath, 'utf-8');
|
|
13
|
+
const startIdx = content.indexOf(START_MARKER);
|
|
14
|
+
const endIdx = content.indexOf(END_MARKER);
|
|
15
|
+
|
|
16
|
+
if (startIdx !== -1 && endIdx !== -1) {
|
|
17
|
+
content = content.slice(0, startIdx) + block + content.slice(endIdx + END_MARKER.length);
|
|
18
|
+
} else {
|
|
19
|
+
content += `\n\n${block}\n`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
fs.writeFileSync(filePath, content, 'utf-8');
|
|
23
|
+
console.log(chalk.green(`✔ Synced to ${filePath}`));
|
|
24
|
+
} else {
|
|
25
|
+
fs.writeFileSync(filePath, `# Project Structure\n\n${block}\n`, 'utf-8');
|
|
26
|
+
console.log(chalk.green(`✔ Created ${filePath}`));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
30
|
module.exports = { syncToFile };
|
package/src/tree.js
CHANGED
|
@@ -1,97 +1,97 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
const chalk = require('chalk');
|
|
4
|
-
const { shouldIgnore } = require('./ignore');
|
|
5
|
-
const { parseExports, formatExports } = require('./exports');
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
function getFolderDescription(dirPath) {
|
|
9
|
-
const folderMd = path.join(dirPath, '.folder.md');
|
|
10
|
-
const readmeMd = path.join(dirPath, 'README.md');
|
|
11
|
-
|
|
12
|
-
if (fs.existsSync(folderMd)) {
|
|
13
|
-
const content = fs.readFileSync(folderMd, 'utf-8').trim();
|
|
14
|
-
return content.split('\n')[0].replace(/^#\s*/, '');
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
if (fs.existsSync(readmeMd)) {
|
|
18
|
-
const content = fs.readFileSync(readmeMd, 'utf-8').trim();
|
|
19
|
-
const firstLine = content.split('\n')[0].replace(/^#\s*/, '');
|
|
20
|
-
if (firstLine.length < 80) return firstLine;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function buildTree(dirPath, opts, prefix = '', currentDepth = 0) {
|
|
27
|
-
if (currentDepth > opts.depth) return '';
|
|
28
|
-
|
|
29
|
-
let entries;
|
|
30
|
-
try {
|
|
31
|
-
entries = fs.readdirSync(dirPath, { withFileTypes: true });
|
|
32
|
-
} catch (err) {
|
|
33
|
-
if (err.code === 'EACCES') {
|
|
34
|
-
return `${prefix}${chalk.red('[permission denied]')}\n`;
|
|
35
|
-
}
|
|
36
|
-
return `${prefix}${chalk.red(`[error: ${err.code}]`)}\n`;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const filtered = entries
|
|
40
|
-
.filter(e => !shouldIgnore(e.name, opts.exclude))
|
|
41
|
-
.sort((a, b) => {
|
|
42
|
-
if (a.isDirectory() && !b.isDirectory()) return -1;
|
|
43
|
-
if (!a.isDirectory() && b.isDirectory()) return 1;
|
|
44
|
-
return a.name.localeCompare(b.name);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
if (filtered.length === 0) {
|
|
48
|
-
return `${prefix}${chalk.gray('(empty)')}\n`;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
let output = '';
|
|
52
|
-
|
|
53
|
-
filtered.forEach((entry, index) => {
|
|
54
|
-
const isLast = index === filtered.length - 1;
|
|
55
|
-
const connector = isLast ? '└── ' : '├── ';
|
|
56
|
-
const childPrefix = isLast ? ' ' : '│ ';
|
|
57
|
-
const fullPath = path.join(dirPath, entry.name);
|
|
58
|
-
|
|
59
|
-
if (entry.isSymbolicLink()) {
|
|
60
|
-
try {
|
|
61
|
-
const target = fs.readlinkSync(fullPath);
|
|
62
|
-
output += `${prefix}${connector}${chalk.magenta(entry.name)}${chalk.gray(` -> ${target}`)}\n`;
|
|
63
|
-
} catch {
|
|
64
|
-
output += `${prefix}${connector}${chalk.magenta(entry.name)}${chalk.red(' -> [broken link]')}\n`;
|
|
65
|
-
}
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (entry.isDirectory()) {
|
|
70
|
-
const desc = getFolderDescription(fullPath);
|
|
71
|
-
const annotation = desc ? chalk.gray(` # ${desc}`) : '';
|
|
72
|
-
output += `${prefix}${connector}${chalk.cyan(entry.name)}/${annotation}\n`;
|
|
73
|
-
if (currentDepth < opts.depth) {
|
|
74
|
-
output += buildTree(fullPath, opts, prefix + childPrefix, currentDepth + 1);
|
|
75
|
-
}
|
|
76
|
-
} else {
|
|
77
|
-
const exports = opts.showExports ? parseExports(fullPath) : null;
|
|
78
|
-
const exportTag = exports ? chalk.yellow(` ${formatExports(exports)}`) : '';
|
|
79
|
-
output += `${prefix}${connector}${entry.name}${exportTag}\n`;
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
return output;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function generateTree(rootPath, opts) {
|
|
87
|
-
const rootName = path.basename(rootPath);
|
|
88
|
-
const desc = getFolderDescription(rootPath);
|
|
89
|
-
const annotation = desc ? chalk.gray(` # ${desc}`) : '';
|
|
90
|
-
|
|
91
|
-
let output = `${chalk.bold.cyan(rootName)}/${annotation}\n`;
|
|
92
|
-
output += buildTree(rootPath, opts);
|
|
93
|
-
|
|
94
|
-
return output;
|
|
95
|
-
}
|
|
96
|
-
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const chalk = require('chalk');
|
|
4
|
+
const { shouldIgnore } = require('./ignore');
|
|
5
|
+
const { parseExports, formatExports } = require('./exports');
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
function getFolderDescription(dirPath) {
|
|
9
|
+
const folderMd = path.join(dirPath, '.folder.md');
|
|
10
|
+
const readmeMd = path.join(dirPath, 'README.md');
|
|
11
|
+
|
|
12
|
+
if (fs.existsSync(folderMd)) {
|
|
13
|
+
const content = fs.readFileSync(folderMd, 'utf-8').trim();
|
|
14
|
+
return content.split('\n')[0].replace(/^#\s*/, '');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (fs.existsSync(readmeMd)) {
|
|
18
|
+
const content = fs.readFileSync(readmeMd, 'utf-8').trim();
|
|
19
|
+
const firstLine = content.split('\n')[0].replace(/^#\s*/, '');
|
|
20
|
+
if (firstLine.length < 80) return firstLine;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function buildTree(dirPath, opts, prefix = '', currentDepth = 0) {
|
|
27
|
+
if (currentDepth > opts.depth) return '';
|
|
28
|
+
|
|
29
|
+
let entries;
|
|
30
|
+
try {
|
|
31
|
+
entries = fs.readdirSync(dirPath, { withFileTypes: true });
|
|
32
|
+
} catch (err) {
|
|
33
|
+
if (err.code === 'EACCES') {
|
|
34
|
+
return `${prefix}${chalk.red('[permission denied]')}\n`;
|
|
35
|
+
}
|
|
36
|
+
return `${prefix}${chalk.red(`[error: ${err.code}]`)}\n`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const filtered = entries
|
|
40
|
+
.filter(e => !shouldIgnore(e.name, opts.exclude))
|
|
41
|
+
.sort((a, b) => {
|
|
42
|
+
if (a.isDirectory() && !b.isDirectory()) return -1;
|
|
43
|
+
if (!a.isDirectory() && b.isDirectory()) return 1;
|
|
44
|
+
return a.name.localeCompare(b.name);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
if (filtered.length === 0) {
|
|
48
|
+
return `${prefix}${chalk.gray('(empty)')}\n`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let output = '';
|
|
52
|
+
|
|
53
|
+
filtered.forEach((entry, index) => {
|
|
54
|
+
const isLast = index === filtered.length - 1;
|
|
55
|
+
const connector = isLast ? '└── ' : '├── ';
|
|
56
|
+
const childPrefix = isLast ? ' ' : '│ ';
|
|
57
|
+
const fullPath = path.join(dirPath, entry.name);
|
|
58
|
+
|
|
59
|
+
if (entry.isSymbolicLink()) {
|
|
60
|
+
try {
|
|
61
|
+
const target = fs.readlinkSync(fullPath);
|
|
62
|
+
output += `${prefix}${connector}${chalk.magenta(entry.name)}${chalk.gray(` -> ${target}`)}\n`;
|
|
63
|
+
} catch {
|
|
64
|
+
output += `${prefix}${connector}${chalk.magenta(entry.name)}${chalk.red(' -> [broken link]')}\n`;
|
|
65
|
+
}
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (entry.isDirectory()) {
|
|
70
|
+
const desc = getFolderDescription(fullPath);
|
|
71
|
+
const annotation = desc ? chalk.gray(` # ${desc}`) : '';
|
|
72
|
+
output += `${prefix}${connector}${chalk.cyan(entry.name)}/${annotation}\n`;
|
|
73
|
+
if (currentDepth < opts.depth) {
|
|
74
|
+
output += buildTree(fullPath, opts, prefix + childPrefix, currentDepth + 1);
|
|
75
|
+
}
|
|
76
|
+
} else {
|
|
77
|
+
const exports = opts.showExports ? parseExports(fullPath) : null;
|
|
78
|
+
const exportTag = exports ? chalk.yellow(` ${formatExports(exports)}`) : '';
|
|
79
|
+
output += `${prefix}${connector}${entry.name}${exportTag}\n`;
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
return output;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function generateTree(rootPath, opts) {
|
|
87
|
+
const rootName = path.basename(rootPath);
|
|
88
|
+
const desc = getFolderDescription(rootPath);
|
|
89
|
+
const annotation = desc ? chalk.gray(` # ${desc}`) : '';
|
|
90
|
+
|
|
91
|
+
let output = `${chalk.bold.cyan(rootName)}/${annotation}\n`;
|
|
92
|
+
output += buildTree(rootPath, opts);
|
|
93
|
+
|
|
94
|
+
return output;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
97
|
module.exports = { generateTree };
|