@lakindu_perera/toren 1.0.5 → 1.0.7

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.
@@ -1,57 +1,153 @@
1
1
  /**
2
2
  * @fileoverview Toren — JSON Renderer
3
3
  *
4
- * Consumes a ScanResult and produces a clean JSON object for programmatic use.
4
+ * Consumes a ScanResult and produces a stable, consistently-ordered JSON
5
+ * object suitable for programmatic consumption.
6
+ *
7
+ * Design contract (mirrors all other renderers):
8
+ * - Accepts a ScanResult and an optional options object.
9
+ * - Never scans files or modifies the data it receives.
10
+ * - All arrays are always present — never undefined or missing.
11
+ * - Property order is fixed and documented below.
12
+ * - Backward compatible: no existing field is removed or renamed.
13
+ *
14
+ * Output property order:
15
+ * 1. meta — schema version + generator provenance
16
+ * 2. project — name (basename), path (relative), detected type
17
+ * 3. frameworks — derived array of detected frameworks ([] when none)
18
+ * 4. entryPoints — array of detected entry-point paths
19
+ * 5. configs — array of detected configuration file paths
20
+ * 6. scripts — array of { name, command } objects
21
+ * 7. statistics — file/folder counts and scan duration (structured)
22
+ * 8. structure — recursive file-tree array
23
+ * 9. summary — retained for backward compatibility (same data as statistics)
5
24
  */
6
25
 
7
26
  import path from 'node:path';
27
+ import { createRequire } from 'node:module';
28
+
29
+ const require = createRequire(import.meta.url);
30
+ const pkg = require('../../package.json');
31
+
32
+ // ---------------------------------------------------------------------------
33
+ // Tree mapper
34
+ // ---------------------------------------------------------------------------
8
35
 
36
+ /**
37
+ * Recursively map an internal DirNode / FileNode to a clean JSON shape.
38
+ * Directory nodes always include a `children` array (never undefined).
39
+ *
40
+ * @param {import('../scanner/scan.js').DirNode | import('../scanner/scan.js').FileNode} node
41
+ * @returns {{ type: 'folder'|'file', name: string, children?: object[] }}
42
+ */
9
43
  function mapTree(node) {
10
44
  if (node.type === 'directory') {
11
45
  return {
12
- type: 'folder',
13
- name: node.name,
14
- children: (node.children || []).map(mapTree)
15
- };
16
- } else {
17
- return {
18
- type: 'file',
19
- name: node.name
46
+ type: 'folder',
47
+ name: node.name,
48
+ children: (node.children || []).map(mapTree),
20
49
  };
21
50
  }
51
+ return {
52
+ type: 'file',
53
+ name: node.name,
54
+ };
22
55
  }
23
56
 
57
+ // ---------------------------------------------------------------------------
58
+ // Helpers
59
+ // ---------------------------------------------------------------------------
60
+
61
+ /**
62
+ * Derive a `frameworks` array from the scanner's `projectType` string.
63
+ * Returns a single-element array when a framework is detected, empty otherwise.
64
+ * This is a pure presentation decision — no business logic is added.
65
+ *
66
+ * @param {string} projectType
67
+ * @returns {string[]}
68
+ */
69
+ function deriveFrameworks(projectType) {
70
+ if (!projectType || projectType === 'Unknown') return [];
71
+ return [projectType];
72
+ }
73
+
74
+ // ---------------------------------------------------------------------------
75
+ // Public API
76
+ // ---------------------------------------------------------------------------
77
+
78
+ /**
79
+ * Render a ScanResult as a stable JSON document to stdout.
80
+ *
81
+ * @param {import('../scanner/scan.js').ScanResult} result
82
+ * @param {{ cwd?: string }} [options]
83
+ */
24
84
  export function render(result, options = {}) {
25
85
  const {
26
86
  rootPath,
27
87
  projectType,
28
- entryPoints,
29
- configs = [],
30
- scripts = [],
88
+ entryPoints = [],
89
+ configs = [],
90
+ scripts = [],
31
91
  tree,
32
- flatFiles,
33
- totalFolders,
92
+ flatFiles = [],
93
+ totalFolders = 0,
34
94
  scanDurationMs,
35
95
  } = result;
36
96
 
37
- const cwd = options.cwd ?? process.cwd();
38
- const relRoot = path.relative(cwd, rootPath) || '.';
97
+ const cwd = options.cwd ?? process.cwd();
98
+ const relRoot = path.relative(cwd, rootPath) || '.';
99
+ const rootName = path.basename(rootPath) || relRoot;
100
+
101
+ // Shared statistics values — computed once, used in both `statistics` and
102
+ // the backward-compatible `summary` block.
103
+ // Guard against NaN/undefined: JSON.stringify(NaN) produces null, breaking
104
+ // the schema guarantee that durationMs is always a number.
105
+ const totalFiles = flatFiles.length;
106
+ const durationMs = Number.isFinite(scanDurationMs) ? Math.round(scanDurationMs) : 0;
39
107
 
40
108
  const output = {
109
+ // 1. Provenance — lets consumers detect schema changes.
110
+ meta: {
111
+ generatedBy: 'Toren',
112
+ version: pkg.version,
113
+ schema: 1,
114
+ },
115
+
116
+ // 2. Project identity — name is the human-readable basename; path is the
117
+ // relative path used for filesystem resolution.
41
118
  project: {
119
+ name: rootName,
42
120
  path: relRoot,
43
121
  type: projectType,
44
- framework: projectType,
45
122
  },
123
+
124
+ // 3. Detected frameworks — always an array.
125
+ frameworks: deriveFrameworks(projectType),
126
+
127
+ // 4–6. Discovery results — all arrays, always present, never null.
128
+ entryPoints: Array.isArray(entryPoints) ? entryPoints : [],
129
+ configs: Array.isArray(configs) ? configs : [],
130
+ scripts: Array.isArray(scripts) ? scripts : [],
131
+
132
+ // 7. Structured statistics — always a complete object with numeric values.
133
+ // durationMs is rounded to the nearest millisecond (integer).
134
+ statistics: {
135
+ files: totalFiles,
136
+ folders: totalFolders,
137
+ durationMs,
138
+ },
139
+
140
+ // 8. File-tree — array of root-level nodes; always present.
141
+ // Empty array when the scanned directory is empty.
142
+ structure: (tree?.children || []).map(mapTree),
143
+
144
+ // 9. Backward-compatible summary block — preserved for existing consumers.
145
+ // Contains the same data under the original field names.
46
146
  summary: {
47
- totalFiles: flatFiles.length,
147
+ totalFiles,
48
148
  totalFolders,
49
- scanDurationMs: Math.round(scanDurationMs),
149
+ scanDurationMs: durationMs,
50
150
  },
51
- entryPoints,
52
- configs,
53
- scripts,
54
- structure: (tree.children || []).map(mapTree)
55
151
  };
56
152
 
57
153
  console.log(JSON.stringify(output, null, 2));
@@ -28,37 +28,7 @@ import path from 'node:path';
28
28
  // Plain-text tree builder (Markdown-safe, no ANSI, no emoji)
29
29
  // ---------------------------------------------------------------------------
30
30
 
31
- /**
32
- * Build an in-memory nested tree from a flat list of relative file paths.
33
- * This avoids repeating the walk already done by the scanner while keeping
34
- * the markdown renderer completely self-contained.
35
- *
36
- * @param {string[]} flatFiles - Relative file paths produced by scan()
37
- * @returns {{ name: string, type: 'directory'|'file', children: object }[]}
38
- */
39
- function buildTree(flatFiles) {
40
- /** @type {{ type: string, children: Record<string, object> }} */
41
- const root = { type: 'directory', children: {} };
42
-
43
- for (const filePath of flatFiles) {
44
- const parts = filePath.split(/[/\\]/).filter(Boolean);
45
- let node = root;
46
-
47
- for (let i = 0; i < parts.length; i++) {
48
- const part = parts[i];
49
- const isLeaf = i === parts.length - 1;
50
-
51
- if (!node.children[part]) {
52
- node.children[part] = isLeaf
53
- ? { type: 'file', name: part }
54
- : { type: 'directory', name: part, children: {} };
55
- }
56
- node = node.children[part];
57
- }
58
- }
59
31
 
60
- return root;
61
- }
62
32
 
63
33
  /**
64
34
  * Recursively serialise a tree node into classic tree-connector lines.
@@ -81,11 +51,7 @@ function serializeNode(node, prefix, isLast, lines, depth = 0, maxDepth = 5) {
81
51
  lines.push(`${prefix}${connector}${label}`);
82
52
 
83
53
  if (node.type === 'directory') {
84
- const children = Object.values(node.children || {}).sort((a, b) => {
85
- // Directories before files, then alphabetically.
86
- if (a.type !== b.type) return a.type === 'directory' ? -1 : 1;
87
- return a.name.localeCompare(b.name);
88
- });
54
+ const children = node.children || [];
89
55
 
90
56
  // Truncate deep directories with an ellipsis rather than cutting silently.
91
57
  if (depth === maxDepth - 1 && children.length > 0) {
@@ -107,21 +73,18 @@ function serializeNode(node, prefix, isLast, lines, depth = 0, maxDepth = 5) {
107
73
  }
108
74
 
109
75
  /**
110
- * Convert a flat file list into a Markdown-safe tree string.
76
+ * Convert a ScanResult tree into a Markdown-safe tree string.
111
77
  *
112
- * @param {string[]} flatFiles
78
+ * @param {import('../scanner/scan.js').DirNode} tree
113
79
  * @param {string} rootName - Display name for the root node (e.g. "my-app/")
80
+ * @param {number} totalFiles
114
81
  * @returns {string}
115
82
  */
116
- function buildTreeString(flatFiles, rootName) {
117
- if (flatFiles.length === 0) return '';
83
+ function buildTreeString(tree, rootName, totalFiles) {
84
+ if (totalFiles === 0) return '';
118
85
 
119
- const root = buildTree(flatFiles);
120
86
  const lines = [`${rootName}/`];
121
- const children = Object.values(root.children).sort((a, b) => {
122
- if (a.type !== b.type) return a.type === 'directory' ? -1 : 1;
123
- return a.name.localeCompare(b.name);
124
- });
87
+ const children = tree.children || [];
125
88
 
126
89
  for (let i = 0; i < children.length; i++) {
127
90
  serializeNode(children[i], '', i === children.length - 1, lines);
@@ -165,6 +128,19 @@ function markdownTable(rows) {
165
128
  return [head, hr, ...body].join('\n');
166
129
  }
167
130
 
131
+ /**
132
+ * Derive a frameworks array from the projectType string.
133
+ * Returns [] when no specific framework is detected.
134
+ * Mirrors the identical derivation in console-renderer.js and json-renderer.js.
135
+ *
136
+ * @param {string} projectType
137
+ * @returns {string[]}
138
+ */
139
+ function deriveFrameworks(projectType) {
140
+ if (!projectType || projectType === 'Unknown') return [];
141
+ return [projectType];
142
+ }
143
+
168
144
  /**
169
145
  * Build a right-aligned Markdown table (used for statistics).
170
146
  *
@@ -190,11 +166,9 @@ function statsTable(rows) {
190
166
 
191
167
  /** @param {string[]} out */
192
168
  function sectionTitle(out) {
193
- out.push('# Project Analysis Report');
169
+ out.push('# Toren Report');
194
170
  out.push('');
195
171
  out.push('Generated by **Toren** — Codebase Onboarding Intelligence');
196
- out.push('');
197
- out.push('---');
198
172
  }
199
173
 
200
174
  /**
@@ -206,7 +180,7 @@ function sectionSummary(out, result, relRoot) {
206
180
  const { projectType, flatFiles, totalFolders } = result;
207
181
 
208
182
  out.push('');
209
- out.push('## Project Summary');
183
+ out.push('## Project');
210
184
  out.push('');
211
185
  out.push(markdownTable([
212
186
  ['Project Type', projectType],
@@ -214,8 +188,26 @@ function sectionSummary(out, result, relRoot) {
214
188
  ['Total Files', String(flatFiles.length)],
215
189
  ['Total Folders', String(totalFolders)],
216
190
  ]));
191
+ }
192
+
193
+ /**
194
+ * @param {string[]} out
195
+ * @param {string} projectType
196
+ */
197
+ function sectionFrameworks(out, projectType) {
198
+ const frameworks = deriveFrameworks(projectType);
199
+
200
+ out.push('');
201
+ out.push('## Frameworks');
217
202
  out.push('');
218
- out.push('---');
203
+
204
+ if (frameworks.length === 0) {
205
+ out.push('No frameworks detected.');
206
+ } else {
207
+ for (const fw of frameworks) {
208
+ out.push(`- ${fw}`);
209
+ }
210
+ }
219
211
  }
220
212
 
221
213
  /**
@@ -224,7 +216,7 @@ function sectionSummary(out, result, relRoot) {
224
216
  */
225
217
  function sectionConfigurationFiles(out, configs) {
226
218
  out.push('');
227
- out.push('## Configuration Files');
219
+ out.push('## Configurations');
228
220
  out.push('');
229
221
 
230
222
  if (configs.length === 0) {
@@ -234,9 +226,6 @@ function sectionConfigurationFiles(out, configs) {
234
226
  out.push(`- ${c}`);
235
227
  }
236
228
  }
237
-
238
- out.push('');
239
- out.push('---');
240
229
  }
241
230
 
242
231
  /**
@@ -245,21 +234,16 @@ function sectionConfigurationFiles(out, configs) {
245
234
  */
246
235
  function sectionPackageScripts(out, scripts) {
247
236
  out.push('');
248
- out.push('## Package Scripts');
237
+ out.push('## Scripts');
249
238
  out.push('');
250
239
 
251
240
  if (scripts.length === 0) {
252
241
  out.push('No package scripts detected.');
253
242
  } else {
254
- out.push('| Script | Command |');
255
- out.push('|--------|---------|');
256
243
  for (const s of scripts) {
257
- out.push(`| \`${s.name}\` | \`${s.command}\` |`);
244
+ out.push(`- ${s.name}: ${s.command}`);
258
245
  }
259
246
  }
260
-
261
- out.push('');
262
- out.push('---');
263
247
  }
264
248
 
265
249
  /**
@@ -275,35 +259,29 @@ function sectionEntryPoints(out, entryPoints) {
275
259
  out.push('No entry points detected.');
276
260
  } else {
277
261
  for (const ep of entryPoints) {
278
- out.push(`- \`${ep}\``);
262
+ out.push(`- ${ep}`);
279
263
  }
280
264
  }
281
-
282
- out.push('');
283
- out.push('---');
284
265
  }
285
266
 
286
267
  /**
287
268
  * @param {string[]} out
288
- * @param {string[]} flatFiles
289
- * @param {string} rootName
269
+ * @param {import('../scanner/scan.js').DirNode} tree
270
+ * @param {number} totalFiles
271
+ * @param {string} rootName
290
272
  */
291
- function sectionFolderStructure(out, flatFiles, rootName) {
273
+ function sectionFolderStructure(out, tree, totalFiles, rootName) {
274
+ // Contract: omit section entirely when no files were scanned.
275
+ if (totalFiles === 0) return;
276
+
292
277
  out.push('');
293
- out.push('## Folder Structure');
278
+ out.push('## Structure');
294
279
  out.push('');
295
280
 
296
- if (flatFiles.length === 0) {
297
- out.push('No files scanned.');
298
- } else {
299
- const tree = buildTreeString(flatFiles, rootName);
300
- out.push('```text');
301
- out.push(tree);
302
- out.push('```');
303
- }
304
-
305
- out.push('');
306
- out.push('---');
281
+ const treeStr = buildTreeString(tree, rootName, totalFiles);
282
+ out.push('```text');
283
+ out.push(treeStr);
284
+ out.push('```');
307
285
  }
308
286
 
309
287
  /**
@@ -321,21 +299,6 @@ function sectionStatistics(out, result) {
321
299
  ['Folders', totalFolders],
322
300
  ['Scan Duration', formatDuration(scanDurationMs)],
323
301
  ]));
324
- out.push('');
325
- out.push('---');
326
- }
327
-
328
- /**
329
- * @param {string[]} out
330
- */
331
- function sectionScanInfo(out) {
332
- out.push('');
333
- out.push('## Scan Information');
334
- out.push('');
335
- out.push('Generated by **Toren**');
336
- out.push('');
337
- out.push('Output Format: Markdown');
338
- out.push('');
339
302
  }
340
303
 
341
304
  // ---------------------------------------------------------------------------
@@ -352,23 +315,23 @@ function sectionScanInfo(out) {
352
315
  * @param {{ cwd?: string }} [options]
353
316
  */
354
317
  export function render(result, options = {}) {
355
- const { rootPath, entryPoints, configs = [], scripts = [], flatFiles } = result;
318
+ const { rootPath, projectType, entryPoints, configs = [], scripts = [], flatFiles, tree } = result;
356
319
 
357
320
  const cwd = options.cwd ?? process.cwd();
358
321
  const relRoot = path.relative(cwd, rootPath) || '.';
359
- const rootName = path.basename(rootPath) || relRoot;
322
+ const rootName = relRoot === '.' ? path.basename(rootPath) : relRoot;
360
323
 
361
324
  /** @type {string[]} */
362
325
  const out = [];
363
326
 
364
327
  sectionTitle(out);
365
328
  sectionSummary(out, result, relRoot);
329
+ sectionFrameworks(out, projectType);
366
330
  sectionEntryPoints(out, entryPoints);
367
331
  sectionConfigurationFiles(out, configs);
368
332
  sectionPackageScripts(out, scripts);
369
- sectionFolderStructure(out, flatFiles, rootName);
370
333
  sectionStatistics(out, result);
371
- sectionScanInfo(out);
334
+ sectionFolderStructure(out, tree, flatFiles.length, rootName);
372
335
 
373
336
  console.log(out.join('\n'));
374
337
  }
@@ -186,7 +186,11 @@ function walkDirectory(dirPath, rootPath, flatFiles, includeHidden, maxFiles) {
186
186
  node.children.push(childNode);
187
187
  } else if (dirent.isFile()) {
188
188
  if (flatFiles.length >= maxFiles) {
189
- throw new Error(`Max file scan limit exceeded (${maxFiles} files). Use --max-files <number> to increase the limit.`);
189
+ const err = new Error(`Max file scan limit exceeded (${maxFiles} files).`);
190
+ err.title = 'Scan limit exceeded';
191
+ err.detailLabel = 'Hint';
192
+ err.detailValue = 'Use --max-files <number> to increase the limit.';
193
+ throw err;
190
194
  }
191
195
  const relFilePath = toPosix(path.relative(rootPath, childPath));
192
196
 
@@ -449,8 +453,18 @@ export function scan(targetPath, options = {}) {
449
453
  let stat;
450
454
  try {
451
455
  stat = fs.statSync(rootPath);
452
- } catch {
453
- throw new Error(`Path does not exist: ${rootPath}`);
456
+ } catch (err) {
457
+ const error = new Error(`Path does not exist: ${rootPath}`);
458
+ if (err.code === 'EACCES' || err.code === 'EPERM') {
459
+ error.title = 'Permission denied';
460
+ error.message = 'You do not have permission to access the specified path.';
461
+ } else {
462
+ error.title = 'Invalid directory';
463
+ error.message = 'The specified path does not exist.';
464
+ }
465
+ error.detailLabel = 'Path';
466
+ error.detailValue = rootPath;
467
+ throw error;
454
468
  }
455
469
 
456
470
  /** @type {Array<string>} */
@@ -494,7 +508,12 @@ export function scan(targetPath, options = {}) {
494
508
  configs = detectConfigs(flatFiles).configs;
495
509
  scripts = detectScripts(path.dirname(rootPath)).scripts;
496
510
  } else {
497
- throw new Error(`Path is neither a file nor a directory: ${rootPath}`);
511
+ const err = new Error(`Path is neither a file nor a directory: ${rootPath}`);
512
+ err.title = 'Unsupported path type';
513
+ err.message = 'The specified path is neither a file nor a directory.';
514
+ err.detailLabel = 'Path';
515
+ err.detailValue = rootPath;
516
+ throw err;
498
517
  }
499
518
 
500
519
  const scanDurationMs = performance.now() - startTime;