Skip to content

Commit d85ba39

Browse files
Merge pull request #148 from codacy/add-language
feat: run cli discover on file creation CF-1720
2 parents 536660a + 51b85af commit d85ba39

5 files changed

Lines changed: 83 additions & 2 deletions

File tree

src/cli/CodacyCli.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export abstract class CodacyCli {
3636

3737
public abstract analyze(options: { file?: string; tool?: string }): Promise<ProcessedSarifResult[] | null>
3838

39+
public abstract configDiscover(filePath: string): Promise<void>
40+
3941
public getCliCommand(): string {
4042
return this._cliCommand
4143
}

src/cli/MacCodacyCli.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,36 @@ export class MacCodacyCli extends CodacyCli {
220220
}
221221
}
222222
}
223+
224+
public async configDiscover(filePath: string): Promise<void> {
225+
await this.preflightCodacyCli(true)
226+
227+
if (!this.getCliCommand()) {
228+
throw new Error('CLI command not found. Please install the CLI first.')
229+
}
230+
231+
Logger.debug(`Running Codacy CLI config discover for ${filePath}`)
232+
233+
try {
234+
const { stdout, stderr } = await this.execAsync(
235+
`${this.getCliCommand()} config discover ${this.preparePathForExec(filePath)}`
236+
)
237+
238+
if (stderr) {
239+
Logger.warn(`Codacy CLI config discover warnings: ${stderr}`)
240+
}
241+
242+
if (stdout) {
243+
Logger.debug(`Codacy CLI config discover output: ${stdout}`)
244+
}
245+
246+
Logger.debug(`Codacy CLI config discover completed for ${filePath}`)
247+
} catch (error: unknown) {
248+
if (error instanceof CodacyError) {
249+
throw error
250+
} else {
251+
throw new CodacyError('Failed to run Codacy config discover', error as Error, 'CLI')
252+
}
253+
}
254+
}
223255
}

src/cli/WinCodacyCli.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ export class WinCodacyCli extends CodacyCli {
3131
public analyze(_options: { file?: string; tool?: string }): Promise<ProcessedSarifResult[] | null> {
3232
throw new Error(NOT_SUPPORTED)
3333
}
34+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
35+
public configDiscover(_filePath: string): Promise<void> {
36+
throw new Error(NOT_SUPPORTED)
37+
}
3438
}

src/extension.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,40 @@ import { APIState, Repository as GitRepository } from './git/git'
2222
import { configureGuardrails, configureMCP, updateMCPConfig, updateMCPState } from './commands/configureMCP'
2323
import { addRepository, joinOrganization } from './onboarding'
2424

25+
/**
26+
* Handle file creation events by running Codacy CLI config discover
27+
*/
28+
const handleFileCreation = async (filePath: string, codacyCloud: CodacyCloud) => {
29+
const ignorePaths = [
30+
'.git/',
31+
'node_modules/',
32+
'.codacy/',
33+
'.vscode/',
34+
'.windsurf/',
35+
'.cursor/',
36+
'.github/',
37+
'readme/',
38+
]
39+
try {
40+
if (ignorePaths.some((path) => filePath.includes(path))) {
41+
return
42+
}
43+
44+
// Skip if it's not a regular file
45+
const stat = await vscode.workspace.fs.stat(vscode.Uri.file(filePath))
46+
if (stat.type !== vscode.FileType.File) {
47+
return
48+
}
49+
50+
Logger.debug(`File created: ${filePath}, running config discover...`)
51+
52+
// Run config discover for the new file
53+
await codacyCloud.cli?.configDiscover(filePath)
54+
} catch (error) {
55+
Logger.warn(`Failed to run config discover for ${filePath}: ${error}`)
56+
}
57+
}
58+
2559
/**
2660
* Helper function to register all extension commands
2761
* @param context
@@ -268,6 +302,15 @@ export async function activate(context: vscode.ExtensionContext) {
268302
item.onClick()
269303
})
270304

305+
// Listen for file creation and run config discover
306+
context.subscriptions.push(
307+
vscode.workspace.onDidCreateFiles(async (event) => {
308+
for (const file of event.files) {
309+
await handleFileCreation(file.fsPath, codacyCloud)
310+
}
311+
})
312+
)
313+
271314
const analysisMode = vscode.workspace.getConfiguration().get('codacy.cli.analysisMode')
272315
const cliVersion = vscode.workspace.getConfiguration().get('codacy.cli.cliVersion')
273316
// When the user doesn't have a specific version, update the CLI to the latest version

src/views/ProblemsDiagnosticCollection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ export class CliIssueDiagnostic extends vscode.Diagnostic {
6363
result.level === 'error'
6464
? vscode.DiagnosticSeverity.Error
6565
: result.level === 'warning'
66-
? vscode.DiagnosticSeverity.Warning
67-
: vscode.DiagnosticSeverity.Information
66+
? vscode.DiagnosticSeverity.Warning
67+
: vscode.DiagnosticSeverity.Information
6868

6969
const range = new vscode.Range(
7070
(result.region?.startLine || 1) - 1,

0 commit comments

Comments
 (0)