Skip to content

Commit 16d203c

Browse files
committed
refactor: update utility modules
- Update interactive-expand and command-builder - Clean up imports and remove unused code - Update error filtering and offline cache
1 parent 3a02261 commit 16d203c

File tree

6 files changed

+69
-51
lines changed

6 files changed

+69
-51
lines changed

src/utils/coana-spawn.mts

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,41 +58,42 @@ export async function spawnCoana(
5858
mixinsEnv['SOCKET_CLI_API_PROXY'] = proxyUrl
5959
}
6060

61+
// Execute Coana
6162
try {
6263
const localCoanaPath = constants.ENV['SOCKET_CLI_COANA_LOCAL_PATH']
63-
// Use local Coana CLI if path is provided.
64-
if (localCoanaPath) {
64+
// Use local Coana CLI if path is provided.
65+
if (localCoanaPath) {
66+
const finalEnv = {
67+
...process.env,
68+
...constants.processEnv,
69+
...mixinsEnv,
70+
...spawnEnv,
71+
}
72+
const spawnResult = await spawn('node', [localCoanaPath, ...args], {
73+
cwd: shadowOptions.cwd,
74+
env: finalEnv,
75+
stdio: spawnExtra?.['stdio'] || 'inherit',
76+
})
77+
78+
return {
79+
ok: true,
80+
data: spawnResult.stdout ? spawnResult.stdout.toString() : '',
81+
}
82+
}
83+
84+
// Use npx to run coana.
85+
const coanaVersion =
86+
constants.ENV['INLINED_SOCKET_CLI_COANA_TECH_CLI_VERSION']
87+
const packageSpec = `@coana-tech/cli@~${coanaVersion}`
88+
6589
const finalEnv = {
6690
...process.env,
6791
...constants.processEnv,
6892
...mixinsEnv,
6993
...spawnEnv,
7094
}
71-
const spawnResult = await spawn('node', [localCoanaPath, ...args], {
72-
cwd: shadowOptions.cwd,
73-
env: finalEnv,
74-
stdio: spawnExtra?.['stdio'] || 'inherit',
75-
})
76-
77-
return {
78-
ok: true,
79-
data: spawnResult.stdout ? spawnResult.stdout.toString() : '',
80-
}
81-
}
82-
83-
// Use npx to run coana.
84-
const coanaVersion =
85-
constants.ENV['INLINED_SOCKET_CLI_COANA_TECH_CLI_VERSION']
86-
const packageSpec = `@coana-tech/cli@~${coanaVersion}`
87-
88-
const finalEnv = {
89-
...process.env,
90-
...constants.processEnv,
91-
...mixinsEnv,
92-
...spawnEnv,
93-
}
9495

95-
const result = await runShadowCommand(packageSpec, args, {
96+
const result = await runShadowCommand(packageSpec, args, {
9697
cwd:
9798
typeof shadowOptions.cwd === 'string'
9899
? shadowOptions.cwd

src/utils/command-builder.mts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,13 @@ export interface ParentCommandOptions {
124124
}
125125

126126
export function buildParentCommand(options: ParentCommandOptions): CliSubcommand {
127-
const { defaultSubcommand, description, hidden = false, name, subcommands } = options
127+
const { description, hidden = false, name, subcommands } = options
128+
// defaultSubcommand is intentionally unused - kept for API compatibility
128129

129130
return {
130131
description,
131132
hidden,
132-
async run(argv: readonly string[], importMeta: ImportMeta, { parentName }: { parentName: string }) {
133+
async run(_argv: readonly string[], _importMeta: ImportMeta, _options: { parentName: string }) {
133134
// This is typically handled by meowWithSubcommands
134135
// but we can provide a fallback
135136
logger.log(`Available subcommands for ${name}:`)

src/utils/error-filter.mts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,28 @@ const NODE_ERROR_PATTERNS = [
2828
/^TypeError: .* is not a function$/,
2929

3030
// Stack trace lines
31-
/^\s*at\s+.*\(/, // " at functionName (/path/to/file.js:123:45)"
32-
/^\s*at\s+\/.*:\d+:\d+$/, // " at /path/to/file.js:123:45"
33-
/^\s*at\s+node:/, // Node internal modules
34-
/^\s*at\s+async\s+/, // Async stack traces
31+
// " at functionName (/path/to/file.js:123:45)"
32+
/^\s*at\s+.*\(/,
33+
// " at /path/to/file.js:123:45"
34+
/^\s*at\s+\/.*:\d+:\d+$/,
35+
// Node internal modules
36+
/^\s*at\s+node:/,
37+
// Async stack traces
38+
/^\s*at\s+async\s+/,
3539

3640
// Node.js internal paths
3741
/node_modules\//,
3842
/internal\/modules\//,
3943
/internal\/process\//,
4044

4145
// V8 error decoration
42-
/^\s*\^+$/, // Error position indicators like "^^^^^"
46+
// Error position indicators like "^^^^^"
47+
/^\s*\^+$/,
4348
/^SyntaxError:/,
4449

4550
// Node warnings
46-
/^\(node:\d+\)/, // "(node:12345) Warning: ..."
51+
// "(node:12345) Warning: ..."
52+
/^\(node:\d+\)/,
4753
/^DeprecationWarning:/,
4854
/^ExperimentalWarning:/,
4955

@@ -72,8 +78,9 @@ function cleanErrorMessage(line: string): string | null {
7278
}
7379

7480
// Clean up common Node.js error prefixes
81+
// Remove "Error: " prefix
7582
line = line
76-
.replace(/^Error:\s+/, '') // Remove "Error: " prefix
83+
.replace(/^Error:\s+/, '')
7784
.replace(/^TypeError:\s+/, '')
7885
.replace(/^ReferenceError:\s+/, '')
7986
.replace(/^RangeError:\s+/, '')

src/utils/interactive-expand.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ export function formatExpandableList(
265265
expanded?: boolean
266266
} = {}
267267
): string {
268-
const { expanded = false, maxItemsCollapsed = 3 } = options
269268
const theme = getTheme()
269+
const { expanded = false, maxItemsCollapsed = 3 } = options
270270

271271
if (items.length <= maxItemsCollapsed || expanded) {
272272
const list = items.map(item => ` • ${item}`).join('\n')

src/utils/offline-cache.mts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { mkdir, readFile, writeFile } from 'node:fs/promises'
66
import { homedir } from 'node:os'
77
import { join } from 'node:path'
88

9-
import colors from 'yoctocolors-cjs'
10-
119
import { logger } from '@socketsecurity/registry/lib/logger'
1210

1311
interface CacheEntry<T = any> {
@@ -19,14 +17,17 @@ interface CacheEntry<T = any> {
1917
}
2018

2119
interface CacheOptions {
22-
ttl?: number // Time to live in milliseconds
23-
offline?: boolean // Force offline mode
24-
refresh?: boolean // Force refresh even if cached
25-
namespace?: string // Cache namespace for organization
20+
// Time to live in milliseconds
21+
ttl?: number
22+
// Force offline mode
23+
offline?: boolean
24+
// Force refresh even if cached
25+
refresh?: boolean
26+
// Cache namespace for organization
27+
namespace?: string
2628
}
2729

2830
const CACHE_DIR = join(homedir(), '.socket', '_cacache')
29-
const CACHE_METADATA_FILE = 'cache-metadata.json'
3031

3132
/**
3233
* Get cache key for a given operation
@@ -65,7 +66,8 @@ async function readFromCache<T>(
6566
// Check if expired
6667
const now = Date.now()
6768
if (entry.ttl > 0 && now - entry.timestamp > entry.ttl) {
68-
return null // Expired
69+
// Expired
70+
return null
6971
}
7072

7173
return entry
@@ -88,8 +90,9 @@ async function writeToCache<T>(
8890
const entry: CacheEntry<T> = {
8991
data,
9092
timestamp: Date.now(),
91-
ttl: options.ttl || 3600000, // Default 1 hour
92-
metadata: options.namespace ? { namespace: options.namespace } : undefined,
93+
// Default 1 hour
94+
ttl: options.ttl || 3600000,
95+
...(options.namespace ? { metadata: { namespace: options.namespace } } : {}),
9396
}
9497

9598
await writeFile(path, JSON.stringify(entry, null, 2))
@@ -209,12 +212,16 @@ export async function getCacheStats(): Promise<{
209212
oldest?: Date
210213
newest?: Date
211214
}> {
212-
const stats = {
215+
const stats: {
216+
size: number
217+
entries: number
218+
namespaces: string[]
219+
oldest?: Date
220+
newest?: Date
221+
} = {
213222
size: 0,
214223
entries: 0,
215-
namespaces: [] as string[],
216-
oldest: undefined as Date | undefined,
217-
newest: undefined as Date | undefined,
224+
namespaces: [],
218225
}
219226

220227
if (!existsSync(CACHE_DIR)) {
@@ -232,12 +239,14 @@ export async function getCacheStats(): Promise<{
232239

233240
for (const ns of namespaces) {
234241
const nsPath = join(CACHE_DIR, ns)
242+
// eslint-disable-next-line no-await-in-loop
235243
const files = await readdir(nsPath)
236244

237245
for (const file of files) {
238246
if (file.endsWith('.json')) {
239247
stats.entries++
240248
const filePath = join(nsPath, file)
249+
// eslint-disable-next-line no-await-in-loop
241250
const fileStat = await stat(filePath)
242251
stats.size += fileStat.size
243252

src/utils/project-context.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export async function getProjectContext(cwd: string = process.cwd()): Promise<Pr
149149
type: packageManager,
150150
root,
151151
hasLockFile,
152-
framework,
152+
...(framework ? { framework } : {}),
153153
isMonorepo: monorepo,
154154
}
155155
}

0 commit comments

Comments
 (0)