@lakindu_perera/toren 1.0.5 → 1.0.8

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.
@@ -0,0 +1,89 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+
4
+ /**
5
+ * Detect project info from the scanned repository.
6
+ */
7
+ export function detectProjectInfo({
8
+ rootPath,
9
+ projectType,
10
+ flatFiles,
11
+ entryPoints,
12
+ packageManager,
13
+ scripts
14
+ }) {
15
+ const fileSet = new Set(flatFiles);
16
+
17
+ let name = null;
18
+ const pkgPath = path.join(rootPath, 'package.json');
19
+ try {
20
+ if (fs.existsSync(pkgPath)) {
21
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
22
+ if (pkg.name) name = pkg.name;
23
+ }
24
+ } catch {}
25
+ if (!name) {
26
+ name = path.basename(rootPath) || null;
27
+ }
28
+
29
+ let runtime = null;
30
+ const pt = projectType.toLowerCase();
31
+ if (pt.includes('node') || fileSet.has('package.json')) runtime = 'Node.js';
32
+ else if (pt.includes('python')) runtime = 'Python';
33
+ else if (pt.includes('java') || pt.includes('spring')) runtime = 'JVM / Java';
34
+ else if (pt.includes('go')) runtime = 'Go';
35
+ else if (pt.includes('rust')) runtime = 'Rust';
36
+ else if (pt.includes('ruby')) runtime = 'Ruby';
37
+ else if (pt.includes('php') || pt.includes('composer')) runtime = 'PHP';
38
+ else if (pt.includes('elixir') || pt.includes('phoenix')) runtime = 'Erlang / BEAM';
39
+
40
+ let language = null;
41
+ if (fileSet.has('tsconfig.json') || pt.includes('angular')) {
42
+ language = 'TypeScript';
43
+ } else if (fileSet.has('package.json') && !fileSet.has('tsconfig.json')) {
44
+ language = 'JavaScript';
45
+ } else if (pt.includes('python')) language = 'Python';
46
+ else if (pt.includes('java') || pt.includes('spring')) language = 'Java';
47
+ else if (pt.includes('go')) language = 'Go';
48
+ else if (pt.includes('rust')) language = 'Rust';
49
+ else if (pt.includes('ruby')) language = 'Ruby';
50
+ else if (pt.includes('php')) language = 'PHP';
51
+ else if (pt.includes('elixir')) language = 'Elixir';
52
+
53
+ let framework = null;
54
+ if (projectType && projectType !== 'Unknown') {
55
+ framework = projectType;
56
+ }
57
+
58
+ let architecture = null;
59
+ if (pt.includes('next')) {
60
+ const hasApp = Array.from(fileSet).some(f => f.startsWith('app/'));
61
+ const hasPages = Array.from(fileSet).some(f => f.startsWith('pages/'));
62
+ if (hasApp && hasPages) architecture = 'Hybrid App/Pages Router';
63
+ else if (hasApp) architecture = 'App Router';
64
+ else if (hasPages) architecture = 'Pages Router';
65
+ }
66
+
67
+ const entryPoint = entryPoints.length > 0 ? entryPoints[0] : null;
68
+
69
+ let sourceDirectory = null;
70
+ if (entryPoint) {
71
+ if (entryPoint.startsWith('src/')) sourceDirectory = 'src';
72
+ else if (entryPoint.startsWith('app/')) sourceDirectory = 'app';
73
+ else if (entryPoint.startsWith('pages/')) sourceDirectory = 'pages';
74
+ }
75
+
76
+ return {
77
+ projectInfo: {
78
+ name,
79
+ projectType,
80
+ runtime,
81
+ language,
82
+ framework,
83
+ architecture,
84
+ packageManager,
85
+ entryPoint,
86
+ sourceDirectory
87
+ }
88
+ };
89
+ }
@@ -1,30 +1,246 @@
1
- import fs from 'node:fs';
1
+ /**
2
+ * @fileoverview Toren — Script Detector with Intelligence Layer
3
+ *
4
+ * Reads package.json scripts and enriches each entry with:
5
+ * - category — functional grouping (development, build, testing, etc.)
6
+ * - description — human-readable purpose string
7
+ * - usage — ready-to-run command string for the detected package manager
8
+ *
9
+ * Design contract:
10
+ * - Backward compatible: name and command are always present and unchanged.
11
+ * - category and description are null when no deterministic match exists.
12
+ * - usage is always a string — derived from packageManager or falls back to
13
+ * "npm run <name>" when packageManager is null.
14
+ * - Descriptions are never fabricated. Only canonical mappings and known
15
+ * command-keyword inferences are used.
16
+ * - Deterministic: same input always produces the same output.
17
+ * - Graceful: malformed or missing package.json returns an empty array.
18
+ *
19
+ * @module detectors/script-detector
20
+ */
21
+
22
+ import fs from 'node:fs';
2
23
  import path from 'node:path';
3
24
 
25
+ // ---------------------------------------------------------------------------
26
+ // Description and category tables — keyed by exact script name (lowercase)
27
+ // ---------------------------------------------------------------------------
28
+
29
+ /**
30
+ * Maps a known script name to its canonical human-readable description.
31
+ * @type {Map<string, string>}
32
+ */
33
+ const NAME_DESCRIPTIONS = new Map([
34
+ ['dev', 'Start the development server'],
35
+ ['develop', 'Start the development server'],
36
+ ['start', 'Start the application'],
37
+ ['serve', 'Serve the application'],
38
+ ['preview', 'Preview the production build'],
39
+ ['build', 'Create a production build'],
40
+ ['test', 'Run the test suite'],
41
+ ['test:watch', 'Run tests in watch mode'],
42
+ ['test:e2e', 'Run end-to-end tests'],
43
+ ['lint', 'Run code-quality checks'],
44
+ ['lint:fix', 'Run code-quality checks and fix supported issues'],
45
+ ['format', 'Format the codebase'],
46
+ ['typecheck', 'Run static type checks'],
47
+ ['check', 'Run project checks'],
48
+ ['clean', 'Remove generated build artifacts'],
49
+ ['generate', 'Generate project files or code'],
50
+ ['migrate', 'Run database migrations'],
51
+ ['seed', 'Seed the database'],
52
+ ['deploy', 'Deploy the application'],
53
+ ]);
54
+
55
+ /**
56
+ * Maps a known script name to its functional category.
57
+ * @type {Map<string, string>}
58
+ */
59
+ const NAME_CATEGORIES = new Map([
60
+ ['dev', 'development'],
61
+ ['develop', 'development'],
62
+ ['start', 'development'],
63
+ ['serve', 'development'],
64
+ ['preview', 'development'],
65
+ ['build', 'build'],
66
+ ['test', 'testing'],
67
+ ['test:watch', 'testing'],
68
+ ['test:e2e', 'testing'],
69
+ ['lint', 'quality'],
70
+ ['lint:fix', 'quality'],
71
+ ['format', 'quality'],
72
+ ['typecheck', 'quality'],
73
+ ['check', 'quality'],
74
+ ['clean', 'utility'],
75
+ ['generate', 'utility'],
76
+ ['migrate', 'database'],
77
+ ['seed', 'database'],
78
+ ['deploy', 'deployment'],
79
+ ]);
80
+
81
+ // ---------------------------------------------------------------------------
82
+ // Command-keyword inference — ordered most-specific first
83
+ // ---------------------------------------------------------------------------
84
+
85
+ /**
86
+ * Ordered list of command-keyword → { description, category } inferences.
87
+ * Evaluated only when a script name has no entry in NAME_DESCRIPTIONS.
88
+ * More specific patterns (e.g. "playwright test") must appear before broader
89
+ * ones (e.g. "jest") to prevent false matches.
90
+ *
91
+ * @type {Array<{ keyword: string, description: string, category: string }>}
92
+ */
93
+ const COMMAND_INFERENCE = [
94
+ { keyword: 'playwright test', description: 'Run end-to-end tests', category: 'testing' },
95
+ { keyword: 'cypress run', description: 'Run end-to-end tests', category: 'testing' },
96
+ { keyword: 'vitest', description: 'Run the test suite', category: 'testing' },
97
+ { keyword: 'jest', description: 'Run the test suite', category: 'testing' },
98
+ { keyword: 'tsc --noEmit', description: 'Run static type checks', category: 'quality' },
99
+ { keyword: 'eslint', description: 'Run code-quality checks', category: 'quality' },
100
+ { keyword: 'prettier', description: 'Format the codebase', category: 'quality' },
101
+ ];
102
+
103
+ // ---------------------------------------------------------------------------
104
+ // Usage builder
105
+ // ---------------------------------------------------------------------------
106
+
107
+ /**
108
+ * Script names that npm treats as built-in lifecycle commands.
109
+ * These do NOT need the "run" subcommand: `npm test`, `npm start`, etc.
110
+ * @type {Set<string>}
111
+ */
112
+ const NPM_LIFECYCLE = new Set(['test', 'start', 'stop', 'restart']);
113
+
114
+ /**
115
+ * Script names that bun exposes as direct subcommands (no "run" needed).
116
+ * @type {Set<string>}
117
+ */
118
+ const BUN_LIFECYCLE = new Set(['test', 'start']);
119
+
120
+ /**
121
+ * Build the ready-to-run usage string for a script.
122
+ *
123
+ * Rules:
124
+ * npm: lifecycle commands (test, start…) → `npm <name>`
125
+ * all others → `npm run <name>`
126
+ * pnpm: all scripts → `pnpm <name>` (pnpm forwards directly)
127
+ * yarn: all scripts → `yarn <name>`
128
+ * bun: lifecycle commands (test, start) → `bun <name>`
129
+ * all others → `bun run <name>`
130
+ * null: conservative fallback → `npm run <name>` (always valid)
131
+ *
132
+ * @param {string} name - Script name
133
+ * @param {string|null} packageManager - Detected package manager or null
134
+ * @returns {string}
135
+ */
136
+ function buildUsage(name, packageManager) {
137
+ switch (packageManager) {
138
+ case 'npm':
139
+ return NPM_LIFECYCLE.has(name) ? `npm ${name}` : `npm run ${name}`;
140
+
141
+ case 'pnpm':
142
+ // pnpm forwards all script names without requiring the 'run' sub-command.
143
+ return `pnpm ${name}`;
144
+
145
+ case 'yarn':
146
+ // yarn similarly runs scripts directly without 'run'.
147
+ return `yarn ${name}`;
148
+
149
+ case 'bun':
150
+ return BUN_LIFECYCLE.has(name) ? `bun ${name}` : `bun run ${name}`;
151
+
152
+ default:
153
+ // null or unknown — conservative: `npm run <name>` is always valid even
154
+ // for lifecycle names, so we keep it unconditionally safe.
155
+ return `npm run ${name}`;
156
+ }
157
+ }
158
+
159
+ // ---------------------------------------------------------------------------
160
+ // Description + category resolver
161
+ // ---------------------------------------------------------------------------
162
+
163
+ /**
164
+ * Resolve description and category for a single script entry.
165
+ * Priority: exact name match → command-keyword inference → null.
166
+ *
167
+ * @param {string} name
168
+ * @param {string} command
169
+ * @returns {{ description: string|null, category: string|null }}
170
+ */
171
+ function resolveIntelligence(name, command) {
172
+ // 1. Exact name match (canonical mapping — highest confidence).
173
+ const nameDescription = NAME_DESCRIPTIONS.get(name) ?? null;
174
+ const nameCategory = NAME_CATEGORIES.get(name) ?? null;
175
+
176
+ if (nameDescription !== null || nameCategory !== null) {
177
+ return { description: nameDescription, category: nameCategory };
178
+ }
179
+
180
+ // 2. Command-keyword inference (fallback — only for unknown names).
181
+ for (const { keyword, description, category } of COMMAND_INFERENCE) {
182
+ if (command.includes(keyword)) {
183
+ return { description, category };
184
+ }
185
+ }
186
+
187
+ // 3. Unknown — do not fabricate.
188
+ return { description: null, category: null };
189
+ }
190
+
191
+ // ---------------------------------------------------------------------------
192
+ // Types (JSDoc — no TypeScript dependency required)
193
+ // ---------------------------------------------------------------------------
194
+
4
195
  /**
5
- * Detects npm/package scripts from package.json in the project root.
196
+ * @typedef {Object} ScriptItem
197
+ * @property {string} name - Script name as declared in package.json
198
+ * @property {string} command - Raw script command string
199
+ * @property {string|null} category - Functional category, or null when unknown
200
+ * @property {string|null} description - Human-readable purpose, or null when unknown
201
+ * @property {string} usage - Ready-to-run invocation string
202
+ */
203
+
204
+ // ---------------------------------------------------------------------------
205
+ // Public API
206
+ // ---------------------------------------------------------------------------
207
+
208
+ /**
209
+ * Detect and enrich npm/package scripts from package.json.
6
210
  *
7
- * @param {string} rootPath - The absolute path of the scanned root
8
- * @returns {{ scripts: Array<{name: string, command: string}> }}
211
+ * The `packageManager` parameter is optional for backward compatibility.
212
+ * When omitted (or null), usage strings fall back to `npm run <name>`.
213
+ *
214
+ * @param {string} rootPath - Absolute path of the scanned root
215
+ * @param {string|null} [packageManager=null] - Detected package manager name
216
+ * @returns {{ scripts: ScriptItem[] }}
9
217
  */
10
- export function detectScripts(rootPath) {
218
+ export function detectScripts(rootPath, packageManager = null) {
11
219
  const scripts = [];
12
220
  const pkgPath = path.join(rootPath, 'package.json');
13
221
 
14
222
  try {
15
223
  if (fs.existsSync(pkgPath)) {
16
224
  const content = fs.readFileSync(pkgPath, 'utf8');
17
- const pkg = JSON.parse(content);
225
+ const pkg = JSON.parse(content);
18
226
 
19
227
  if (pkg.scripts && typeof pkg.scripts === 'object') {
20
228
  for (const [name, command] of Object.entries(pkg.scripts)) {
21
- if (typeof command === 'string') {
22
- scripts.push({ name, command });
23
- }
229
+ if (typeof command !== 'string') continue;
230
+
231
+ const { description, category } = resolveIntelligence(name, command);
232
+
233
+ scripts.push({
234
+ name,
235
+ command,
236
+ category,
237
+ description,
238
+ usage: buildUsage(name, packageManager),
239
+ });
24
240
  }
25
241
  }
26
242
  }
27
- } catch (error) {
243
+ } catch {
28
244
  // Return empty scripts on malformed or unreadable package.json
29
245
  }
30
246
 
@@ -1,16 +1,45 @@
1
1
  /**
2
- * @fileoverview Focused output modes handling for Toren CLI.
2
+ * @fileoverview Toren — Focused Output Mode Handler
3
+ *
4
+ * Handles the six focused output flags:
5
+ * --project-type --frameworks --entry-points
6
+ * --structure --configs --scripts
7
+ *
8
+ * Design contract:
9
+ * - Each flag renders exactly one section to stdout using the console visual
10
+ * style (ANSI colours, section header + divider).
11
+ * - Empty-state messages are canonical strings declared in EMPTY below.
12
+ * They must end with a period and stay in sync with console-renderer.js.
13
+ * - No business logic. All data comes from the ScanResult passed in.
14
+ * - Stateless: renderFocusedMode() may be called multiple times safely.
3
15
  */
4
16
 
5
17
  import { renderStructure } from './renderers/console-renderer.js';
6
18
 
19
+ // ---------------------------------------------------------------------------
20
+ // Canonical empty-state messages
21
+ // Keep these in sync with the equivalent strings in console-renderer.js.
22
+ // ---------------------------------------------------------------------------
23
+
24
+ const EMPTY = {
25
+ frameworks: 'No frameworks detected.',
26
+ entryPoints: 'No entry points detected.',
27
+ configs: 'No configuration files detected.',
28
+ scripts: 'No package scripts detected.',
29
+ importantFiles: 'No important files detected.',
30
+ health: 'No project health observations available.',
31
+ };
32
+
7
33
  export const FOCUSED_FLAGS = [
8
34
  '--project-type',
9
35
  '--frameworks',
10
36
  '--entry-points',
11
37
  '--structure',
12
38
  '--configs',
13
- '--scripts'
39
+ '--scripts',
40
+ '--summary',
41
+ '--important-files',
42
+ '--health'
14
43
  ];
15
44
 
16
45
  /**
@@ -25,8 +54,10 @@ export function getFocusedModeInfo(args) {
25
54
  if (activeFlags.length > 1) {
26
55
  return {
27
56
  error: true,
28
- message: '\x1b[31mError: focused output flags are mutually exclusive. Please use only one of:\x1b[0m\n' +
29
- FOCUSED_FLAGS.map(f => ` ${f}`).join('\n') + '\n'
57
+ title: 'Conflicting options',
58
+ message: 'Focused output flags are mutually exclusive.',
59
+ detailLabel: 'Provided flags',
60
+ detailValue: activeFlags.join(', ')
30
61
  };
31
62
  }
32
63
 
@@ -36,6 +67,37 @@ export function getFocusedModeInfo(args) {
36
67
  };
37
68
  }
38
69
 
70
+ /**
71
+ * Print a bold-white section title followed by a dim matched-length divider
72
+ * and a blank line — same visual pattern as console-renderer.js's section().
73
+ *
74
+ * @param {string} title
75
+ */
76
+ function printSectionHeader(title) {
77
+ console.log(`\x1b[1m\x1b[97m${title}\x1b[0m`);
78
+ console.log(`\x1b[2m${'─'.repeat(title.length)}\x1b[0m`);
79
+ console.log('');
80
+ }
81
+
82
+ /**
83
+ * Print a focused section: header, then items or an empty-state message.
84
+ *
85
+ * @param {string} title - Section heading
86
+ * @param {string[]} items - Pre-formatted lines to print
87
+ * @param {string} emptyMsg - Canonical empty-state message (ends with '.')
88
+ */
89
+ function section(title, items, emptyMsg) {
90
+ printSectionHeader(title);
91
+
92
+ if (!items || items.length === 0) {
93
+ console.log(`\x1b[2m${emptyMsg}\x1b[0m`);
94
+ } else {
95
+ for (const item of items) {
96
+ console.log(item);
97
+ }
98
+ }
99
+ }
100
+
39
101
  /**
40
102
  * Renders the scan result based on the active focused mode.
41
103
  *
@@ -44,53 +106,111 @@ export function getFocusedModeInfo(args) {
44
106
  */
45
107
  export function renderFocusedMode(mode, result) {
46
108
  switch (mode) {
47
- case '--project-type':
48
- console.log(`Project Type: ${result.projectType}`);
109
+ case '--project-type': {
110
+ // projectType is always a string; 'Unknown' when undetected.
111
+ section('Project Type', [result.projectType], 'Unknown');
49
112
  break;
50
-
51
- case '--frameworks':
52
- if (!result.projectType || result.projectType === 'Unknown') {
53
- console.log('Frameworks: None detected');
54
- } else {
55
- console.log('Frameworks:');
56
- console.log(`- ${result.projectType}`);
57
- }
113
+ }
114
+
115
+ case '--frameworks': {
116
+ const frameworks = (!result.projectType || result.projectType === 'Unknown')
117
+ ? []
118
+ : [result.projectType];
119
+ section('Frameworks', frameworks, EMPTY.frameworks);
58
120
  break;
59
-
60
- case '--entry-points':
61
- if (!result.entryPoints || result.entryPoints.length === 0) {
62
- console.log('Entry Points: None detected');
63
- } else {
64
- console.log('Entry Points:');
65
- result.entryPoints.forEach(ep => console.log(`- ${ep}`));
66
- }
121
+ }
122
+
123
+ case '--entry-points': {
124
+ section('Entry Points', result.entryPoints || [], EMPTY.entryPoints);
67
125
  break;
68
-
69
- case '--structure':
126
+ }
127
+
128
+ case '--structure': {
70
129
  renderStructure(result);
71
130
  break;
72
-
73
- case '--configs':
74
- console.log('Configuration Files');
75
- console.log('───────────────────\n');
76
- if (!result.configs || result.configs.length === 0) {
77
- console.log('No configuration files detected.');
78
- } else {
79
- result.configs.forEach(c => console.log(`✓ ${c}`));
131
+ }
132
+
133
+ case '--configs': {
134
+ section('Configuration Files', result.configs || [], EMPTY.configs);
135
+ break;
136
+ }
137
+
138
+ case '--scripts': {
139
+ const scriptsList = [];
140
+ if (result.scripts && result.scripts.length > 0) {
141
+ for (const s of result.scripts) {
142
+ const usage = s.usage || `npm run ${s.name}`;
143
+ scriptsList.push(`\x1b[97m${usage}\x1b[0m`);
144
+ if (s.description) {
145
+ scriptsList.push(` \x1b[2m${s.description}\x1b[0m\n`);
146
+ } else {
147
+ scriptsList.push(` \x1b[2m${s.command}\x1b[0m\n`);
148
+ }
149
+ }
80
150
  }
151
+ // Remove trailing newline from last element if it exists
152
+ if (scriptsList.length > 0) {
153
+ scriptsList[scriptsList.length - 1] = scriptsList[scriptsList.length - 1].replace(/\n$/, '');
154
+ }
155
+ section('Package Scripts', scriptsList, EMPTY.scripts);
156
+ break;
157
+ }
158
+
159
+ case '--summary': {
160
+ const p = result.projectInfo || {};
161
+ const lines = [
162
+ `Name ${p.name || 'Unknown'}`,
163
+ `Type ${result.projectType || 'Unknown'}`,
164
+ ];
165
+ if (p.runtime) lines.push(`Runtime ${p.runtime}`);
166
+ if (p.language) lines.push(`Language ${p.language}`);
167
+ if (p.framework) lines.push(`Framework ${p.framework}`);
168
+ if (p.architecture) lines.push(`Architecture ${p.architecture}`);
169
+ if (result.packageManager) lines.push(`Package Manager ${result.packageManager}`);
170
+ if (p.entryPoint) lines.push(`Entry Point ${p.entryPoint}`);
171
+ if (p.sourceDirectory) lines.push(`Source Directory ${p.sourceDirectory}`);
172
+
173
+ lines.push(`Files ${result.flatFiles ? result.flatFiles.length : 0}`);
174
+ lines.push(`Folders ${result.totalFolders || 0}`);
175
+
176
+ section('Project Summary', lines, 'No summary available.');
81
177
  break;
178
+ }
82
179
 
83
- case '--scripts':
84
- console.log('Available Scripts\n');
85
- if (!result.scripts || result.scripts.length === 0) {
86
- console.log('No scripts found');
87
- } else {
88
- const maxNameLen = Math.max(...result.scripts.map(s => s.name.length));
89
- result.scripts.forEach(s => {
90
- const paddedName = s.name.padEnd(maxNameLen + 6, ' ');
91
- console.log(`${paddedName}${s.command}`);
180
+ case '--important-files': {
181
+ const items = [];
182
+ if (result.importantFiles && result.importantFiles.length > 0) {
183
+ const top10 = result.importantFiles.slice(0, 10);
184
+ top10.forEach((f, idx) => {
185
+ items.push(`${idx + 1}. \x1b[97m${f.path}\x1b[0m`);
186
+ items.push(` \x1b[2m${f.reason}\x1b[0m\n`);
92
187
  });
188
+ // Remove trailing newline
189
+ if (items.length > 0) {
190
+ items[items.length - 1] = items[items.length - 1].replace(/\n$/, '');
191
+ }
192
+ }
193
+ section('Important Files', items, EMPTY.importantFiles);
194
+ break;
195
+ }
196
+
197
+ case '--health': {
198
+ const items = [];
199
+ if (result.health && result.health.length > 0) {
200
+ for (const h of result.health) {
201
+ let icon = 'ℹ';
202
+ if (h.status === 'pass') icon = '✓';
203
+ else if (h.status === 'warning') icon = '⚠';
204
+
205
+ let color = '\x1b[97m';
206
+ if (h.status === 'pass') color = '\x1b[32m';
207
+ else if (h.status === 'warning') color = '\x1b[33m';
208
+
209
+ items.push(`${color}${icon}\x1b[0m ${h.message}`);
210
+ }
93
211
  }
212
+ section('Project Health', items, EMPTY.health);
94
213
  break;
214
+ }
95
215
  }
96
216
  }