Skip to content

Commit db1062b

Browse files
Copilotgarrytrinder
andcommitted
refactor: address code review feedback
- Use async vscode.workspace.fs.readFile instead of sync fs.readFileSync - Add type guard for parse result before casting to ObjectNode - Extract getNormalizedVersion() as shared utility to reduce duplication - Add regex pattern documentation and named constant - Check for workbench.action.chat.open command availability before invoking Co-authored-by: garrytrinder <11563347+garrytrinder@users.noreply.github.com>
1 parent 7aa3779 commit db1062b

5 files changed

Lines changed: 36 additions & 12 deletions

File tree

src/commands/upgrade-config.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from 'vscode';
22
import { Commands } from '../constants';
33
import { DevProxyInstall } from '../types';
4+
import { getNormalizedVersion } from '../utils';
45

56
/**
67
* Config upgrade commands.
@@ -25,9 +26,7 @@ async function upgradeConfigsWithCopilot(
2526
return;
2627
}
2728

28-
const devProxyVersion = devProxyInstall.isBeta
29-
? devProxyInstall.version.split('-')[0]
30-
: devProxyInstall.version;
29+
const devProxyVersion = getNormalizedVersion(devProxyInstall);
3130

3231
const fileList = fileUris?.length
3332
? fileUris.map(uri => `- ${vscode.workspace.asRelativePath(uri)}`).join('\n')
@@ -43,6 +42,15 @@ async function upgradeConfigsWithCopilot(
4342
].join('\n');
4443

4544
try {
45+
// workbench.action.chat.open requires GitHub Copilot Chat extension
46+
const allCommands = await vscode.commands.getCommands();
47+
if (!allCommands.includes('workbench.action.chat.open')) {
48+
vscode.window.showWarningMessage(
49+
'GitHub Copilot Chat is not available. Please install the GitHub Copilot extension to use this feature.'
50+
);
51+
return;
52+
}
53+
4654
await vscode.commands.executeCommand('workbench.action.chat.open', {
4755
query: prompt,
4856
isPartialQuery: false,

src/detect.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,13 @@ export const getDevProxyExe = (versionPreference: VersionPreference) => {
103103
? VersionExeName.Stable
104104
: VersionExeName.Beta;
105105
};
106+
107+
/**
108+
* Get the normalized Dev Proxy version from an install object.
109+
* Strips the beta pre-release suffix for version comparison.
110+
*/
111+
export const getNormalizedVersion = (devProxyInstall: DevProxyInstall): string => {
112+
return devProxyInstall.isBeta
113+
? devProxyInstall.version.split('-')[0]
114+
: devProxyInstall.version;
115+
};

src/notifications.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode';
22
import { DevProxyInstall } from './types';
33
import { Commands } from './constants';
4-
import { findOutdatedConfigFiles } from './utils';
4+
import { findOutdatedConfigFiles, getNormalizedVersion } from './utils';
55

66
export const handleStartNotification = (context: vscode.ExtensionContext) => {
77
const devProxyInstall = context.globalState.get<DevProxyInstall>('devProxyInstall');
@@ -68,9 +68,7 @@ export async function handleOutdatedConfigFilesNotification(
6868
return;
6969
}
7070

71-
const devProxyVersion = devProxyInstall.isBeta
72-
? devProxyInstall.version.split('-')[0]
73-
: devProxyInstall.version;
71+
const devProxyVersion = getNormalizedVersion(devProxyInstall);
7472

7573
const outdatedFiles = await findOutdatedConfigFiles(devProxyVersion);
7674

src/utils/config-detection.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as vscode from 'vscode';
2-
import * as fs from 'fs';
32
import parse from 'json-to-ast';
43

54
/**
@@ -98,7 +97,9 @@ export function isProxyFile(document: vscode.TextDocument): boolean {
9897
* @returns The version string (e.g., "0.24.0") or empty string if not found.
9998
*/
10099
export function extractVersionFromSchemaUrl(schemaUrl: string): string {
101-
const match = schemaUrl.match(/\/v(\d+\.\d+\.\d+(?:-[a-zA-Z0-9.-]+)?)\//);
100+
// Matches /vX.Y.Z/ or /vX.Y.Z-prerelease/ in schema URLs
101+
const versionPattern = /\/v(\d+\.\d+\.\d+(?:-[a-zA-Z0-9.-]+)?)\//;
102+
const match = schemaUrl.match(versionPattern);
102103
return match ? match[1] : '';
103104
}
104105

@@ -118,14 +119,21 @@ export async function findOutdatedConfigFiles(devProxyVersion: string): Promise<
118119

119120
for (const uri of jsonFiles) {
120121
try {
121-
const content = fs.readFileSync(uri.fsPath, 'utf-8');
122+
const contentBytes = await vscode.workspace.fs.readFile(uri);
123+
const content = Buffer.from(contentBytes).toString('utf-8');
122124

123125
// Quick check before parsing
124126
if (!content.includes('dev-proxy') || !content.includes('schema')) {
125127
continue;
126128
}
127129

128-
const documentNode = parse(content) as parse.ObjectNode;
130+
const rootNode = parse(content);
131+
132+
if (rootNode.type !== 'Object') {
133+
continue;
134+
}
135+
136+
const documentNode = rootNode as parse.ObjectNode;
129137
const schemaNode = getASTNode(documentNode.children, 'Identifier', '$schema');
130138

131139
if (!schemaNode) {

src/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ export {
2929
} from './shell';
3030

3131
// Re-export from detect for convenience
32-
export { getDevProxyExe } from '../detect';
32+
export { getDevProxyExe, getNormalizedVersion } from '../detect';

0 commit comments

Comments
 (0)