cartotree 0.2.0 → 0.2.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/src/atlas/peek.js CHANGED
@@ -1,24 +1,24 @@
1
- /**
2
- * atlas peek — show only the lines of a file that matter for some words.
3
- * A compressed replacement for `grep -n` when an agent verifies a candidate file.
4
- */
5
- const { readSafe } = require('./files');
6
- const { splitWords } = require('./locate');
7
-
8
- function peek(root, file, words, { max = 10 } = {}) {
9
- const text = readSafe(root, file, 2_000_000);
10
- if (!text) return { file, error: 'cannot read file' };
11
- const q = [...new Set(splitWords(words.join(' ')))];
12
- const lines = text.split('\n');
13
- const hits = [];
14
- lines.forEach((ln, i) => {
15
- if (ln.length > 400) return;
16
- const ws = new Set(splitWords(ln));
17
- const m = q.filter(w => ws.has(w));
18
- if (m.length) hits.push({ line: i + 1, score: m.length, code: ln.trim().slice(0, 120) });
19
- });
20
- const top = hits.sort((a, b) => b.score - a.score || a.line - b.line).slice(0, max).sort((a, b) => a.line - b.line);
21
- return { file, totalLines: lines.length, matchedLines: hits.length, lines: top };
22
- }
23
-
24
- module.exports = { peek };
1
+ /**
2
+ * atlas peek — show only the lines of a file that matter for some words.
3
+ * A compressed replacement for `grep -n` when an agent verifies a candidate file.
4
+ */
5
+ const { readSafe } = require('./files');
6
+ const { splitWords } = require('./locate');
7
+
8
+ function peek(root, file, words, { max = 10 } = {}) {
9
+ const text = readSafe(root, file, 2_000_000);
10
+ if (!text) return { file, error: 'cannot read file' };
11
+ const q = [...new Set(splitWords(words.join(' ')))];
12
+ const lines = text.split('\n');
13
+ const hits = [];
14
+ lines.forEach((ln, i) => {
15
+ if (ln.length > 400) return;
16
+ const ws = new Set(splitWords(ln));
17
+ const m = q.filter(w => ws.has(w));
18
+ if (m.length) hits.push({ line: i + 1, score: m.length, code: ln.trim().slice(0, 120) });
19
+ });
20
+ const top = hits.sort((a, b) => b.score - a.score || a.line - b.line).slice(0, max).sort((a, b) => a.line - b.line);
21
+ return { file, totalLines: lines.length, matchedLines: hits.length, lines: top };
22
+ }
23
+
24
+ module.exports = { peek };
@@ -1,41 +1,41 @@
1
- /**
2
- * Lightweight, regex-based symbol facts (no TypeScript compiler):
3
- * exports, relative imports, and call sites.
4
- */
5
- const path = require('path');
6
- const { stripComments } = require('../exports');
7
-
8
- const EXPORT_RE = /export\s+(?:default\s+)?(?:declare\s+)?(?:async\s+)?(?:function\*?|const|let|var|class|abstract\s+class|type|interface|enum)\s+(\w+)/g;
9
- const EXPORT_LIST_RE = /export\s*\{([^}]+)\}/g;
10
- const IMPORT_RE = /(?:import\s[^'"]*?from\s*|import\s*\(\s*|require\s*\(\s*|export\s[^'"]*?from\s*)['"]([^'"]+)['"]/g;
11
-
12
- function parseExportsFromText(text) {
13
- const src = stripComments(text);
14
- const out = new Set();
15
- let m;
16
- while ((m = EXPORT_RE.exec(src))) out.add(m[1]);
17
- while ((m = EXPORT_LIST_RE.exec(src))) {
18
- m[1].split(',').map(s => s.trim().split(/\s+as\s+/).pop().trim()).filter(s => /^\w+$/.test(s)).forEach(n => out.add(n));
19
- }
20
- return [...out];
21
- }
22
-
23
- /** Relative imports resolved against the known file set. */
24
- function parseImports(text, fromFile, fileSet) {
25
- const out = new Set();
26
- let m;
27
- const src = stripComments(text);
28
- while ((m = IMPORT_RE.exec(src))) {
29
- const spec = m[1];
30
- if (!spec.startsWith('.')) continue;
31
- const base = path.posix.normalize(path.posix.join(path.posix.dirname(fromFile), spec));
32
- const stem = base.replace(/\.(m|c)?js$/, '');
33
- const cands = [base, ...['.ts', '.tsx', '.js', '.jsx', '.mjs'].map(e => stem + e),
34
- ...['index.ts', 'index.tsx', 'index.js'].map(i => `${base}/${i}`)];
35
- const hit = cands.find(c => fileSet.has(c));
36
- if (hit && hit !== fromFile) out.add(hit);
37
- }
38
- return [...out];
39
- }
40
-
41
- module.exports = { parseExportsFromText, parseImports };
1
+ /**
2
+ * Lightweight, regex-based symbol facts (no TypeScript compiler):
3
+ * exports, relative imports, and call sites.
4
+ */
5
+ const path = require('path');
6
+ const { stripComments } = require('../exports');
7
+
8
+ const EXPORT_RE = /export\s+(?:default\s+)?(?:declare\s+)?(?:async\s+)?(?:function\*?|const|let|var|class|abstract\s+class|type|interface|enum)\s+(\w+)/g;
9
+ const EXPORT_LIST_RE = /export\s*\{([^}]+)\}/g;
10
+ const IMPORT_RE = /(?:import\s[^'"]*?from\s*|import\s*\(\s*|require\s*\(\s*|export\s[^'"]*?from\s*)['"]([^'"]+)['"]/g;
11
+
12
+ function parseExportsFromText(text) {
13
+ const src = stripComments(text);
14
+ const out = new Set();
15
+ let m;
16
+ while ((m = EXPORT_RE.exec(src))) out.add(m[1]);
17
+ while ((m = EXPORT_LIST_RE.exec(src))) {
18
+ m[1].split(',').map(s => s.trim().split(/\s+as\s+/).pop().trim()).filter(s => /^\w+$/.test(s)).forEach(n => out.add(n));
19
+ }
20
+ return [...out];
21
+ }
22
+
23
+ /** Relative imports resolved against the known file set. */
24
+ function parseImports(text, fromFile, fileSet) {
25
+ const out = new Set();
26
+ let m;
27
+ const src = stripComments(text);
28
+ while ((m = IMPORT_RE.exec(src))) {
29
+ const spec = m[1];
30
+ if (!spec.startsWith('.')) continue;
31
+ const base = path.posix.normalize(path.posix.join(path.posix.dirname(fromFile), spec));
32
+ const stem = base.replace(/\.(m|c)?js$/, '');
33
+ const cands = [base, ...['.ts', '.tsx', '.js', '.jsx', '.mjs'].map(e => stem + e),
34
+ ...['index.ts', 'index.tsx', 'index.js'].map(i => `${base}/${i}`)];
35
+ const hit = cands.find(c => fileSet.has(c));
36
+ if (hit && hit !== fromFile) out.add(hit);
37
+ }
38
+ return [...out];
39
+ }
40
+
41
+ module.exports = { parseExportsFromText, parseImports };
package/src/clipboard.js CHANGED
@@ -1,11 +1,11 @@
1
- const clipboardy = require('clipboardy');
2
- const chalk = require('chalk');
3
-
4
- function copyToClipboard(text) {
5
- const plain = text.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
6
- clipboardy.writeSync(plain);
7
- console.log(chalk.green('✔ Copied to clipboard!'));
8
- console.log(chalk.gray('(Color codes stripped for clean paste)'));
9
- }
10
-
1
+ const clipboardy = require('clipboardy');
2
+ const chalk = require('chalk');
3
+
4
+ function copyToClipboard(text) {
5
+ const plain = text.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
6
+ clipboardy.writeSync(plain);
7
+ console.log(chalk.green('✔ Copied to clipboard!'));
8
+ console.log(chalk.gray('(Color codes stripped for clean paste)'));
9
+ }
10
+
11
11
  module.exports = { copyToClipboard };
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,74 +1,74 @@
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
-
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
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 };