Skip to content

Commit a2d7e24

Browse files
authored
Expand schema check to all files. Closes #80 (#105)
1 parent 41f1dd4 commit a2d7e24

File tree

4 files changed

+76
-30
lines changed

4 files changed

+76
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Snippets: `devproxy-plugin-http-file-generator` - HttpFileGeneratorPlugin instance
1515
- Snippets: `devproxy-plugin-http-file-generator-config` - HttpFileGeneratorPlugin config section
1616
- Snippets: `devproxy-plugin-openai-mock-response` - OpenAIMockResponsePlugin instance
17+
- File Diagnostic: Check that schema matches installed version of Dev Proxy expanded to all files
1718

1819
### Changed:
1920

src/diagnostics.ts

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { pluginSnippets } from "./constants";
44
import { getASTNode, getRangeFromASTNode } from "./helpers";
55
import { DevProxyInstall } from './types';
66

7-
export const updateDiagnostics = (
7+
export const updateConfigDiagnostics = (
88
context: vscode.ExtensionContext,
99
document: vscode.TextDocument,
1010
collection: vscode.DiagnosticCollection,
@@ -17,20 +17,7 @@ export const updateDiagnostics = (
1717
const documentNode = parse(document.getText()) as parse.ObjectNode;
1818

1919
// check if schema version is compatible
20-
const schemaNode = getASTNode(documentNode.children, 'Identifier', '$schema');
21-
if (schemaNode) {
22-
const schemaValue = (schemaNode.value as parse.LiteralNode).value as string;
23-
const devProxyVersion = devProxyInstall.isBeta ? devProxyInstall.version.split('-')[0] : devProxyInstall.version;
24-
if (!schemaValue.includes(`${devProxyVersion}`)) {
25-
const diagnostic = new vscode.Diagnostic(
26-
getRangeFromASTNode(schemaNode),
27-
`Schema version is not compatible with the installed version of Dev Proxy. Expected v${devProxyVersion}`,
28-
vscode.DiagnosticSeverity.Warning
29-
);
30-
diagnostic.code = 'invalidSchema';
31-
diagnostics.push(diagnostic);
32-
}
33-
}
20+
checkSchemaCompatibility(documentNode, devProxyInstall, diagnostics);
3421

3522
// check validity of plugins
3623
const pluginsNode = getASTNode(
@@ -163,4 +150,40 @@ export const updateDiagnostics = (
163150
}
164151

165152
collection.set(document.uri, diagnostics);
166-
};
153+
};
154+
155+
export const updateDiagnostics = (
156+
context: vscode.ExtensionContext,
157+
document: vscode.TextDocument,
158+
collection: vscode.DiagnosticCollection,
159+
): void => {
160+
const devProxyInstall = context.globalState.get<DevProxyInstall>('devProxyInstall');
161+
if (!devProxyInstall) {
162+
return;
163+
}
164+
165+
const diagnostics: vscode.Diagnostic[] = [];
166+
const documentNode = parse(document.getText()) as parse.ObjectNode;
167+
168+
// check if schema version is compatible
169+
checkSchemaCompatibility(documentNode, devProxyInstall, diagnostics);
170+
171+
collection.set(document.uri, diagnostics);
172+
};
173+
174+
export const checkSchemaCompatibility = (documentNode: parse.ObjectNode, devProxyInstall: DevProxyInstall, diagnostics: vscode.Diagnostic[]) => {
175+
const schemaNode = getASTNode(documentNode.children, 'Identifier', '$schema');
176+
if (schemaNode) {
177+
const schemaValue = (schemaNode.value as parse.LiteralNode).value as string;
178+
const devProxyVersion = devProxyInstall.isBeta ? devProxyInstall.version.split('-')[0] : devProxyInstall.version;
179+
if (!schemaValue.includes(`${devProxyVersion}`)) {
180+
const diagnostic = new vscode.Diagnostic(
181+
getRangeFromASTNode(schemaNode),
182+
`Schema version is not compatible with the installed version of Dev Proxy. Expected v${devProxyVersion}`,
183+
vscode.DiagnosticSeverity.Warning
184+
);
185+
diagnostic.code = 'invalidSchema';
186+
diagnostics.push(diagnostic);
187+
}
188+
}
189+
};

src/documents.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
import * as vscode from 'vscode';
2-
import { isConfigFile } from './helpers';
3-
import { updateDiagnostics } from './diagnostics';
2+
import { isConfigFile, isProxyFile } from './helpers';
3+
import { updateConfigDiagnostics, updateDiagnostics } from './diagnostics';
44

55
export const registerDocumentListeners = (context: vscode.ExtensionContext, collection: vscode.DiagnosticCollection) => {
66
context.subscriptions.push(
77
vscode.workspace.onDidOpenTextDocument(document => {
8+
if (isProxyFile(document)) {
9+
updateDiagnostics(context, document, collection);
10+
}
811
if (!isConfigFile(document)) {
912
return;
1013
}
11-
updateDiagnostics(context, document, collection);
14+
updateConfigDiagnostics(context, document, collection);
1215
})
1316
);
1417

1518
context.subscriptions.push(
1619
vscode.workspace.onDidChangeTextDocument(event => {
17-
if (!isConfigFile(event.document)) {
20+
if (!isConfigFile(event.document) || !isProxyFile(event.document)) {
1821
collection.delete(event.document.uri);
1922
return;
2023
}
21-
updateDiagnostics(context, event.document, collection);
24+
if (isConfigFile(event.document)) {
25+
updateConfigDiagnostics(context, event.document, collection);
26+
}
27+
if (isProxyFile(event.document)) {
28+
updateDiagnostics(context, event.document, collection);
29+
}
2230
})
2331
);
2432
};

src/helpers.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ export const isConfigFile = (document: vscode.TextDocument) => {
7878
return isConfigFile;
7979
};
8080

81+
export const isProxyFile = (document: vscode.TextDocument) => {
82+
let isProxyFile = false;
83+
const documentNode = parse(document.getText()) as parse.ObjectNode;
84+
85+
const schemaNode = getASTNode(documentNode.children, 'Identifier', '$schema');
86+
if (schemaNode) {
87+
const schema = (schemaNode?.value as parse.LiteralNode).value as string;
88+
if (schema.includes('dev-proxy') && schema.endsWith('.schema.json')) {
89+
isProxyFile = true;
90+
}
91+
}
92+
return isProxyFile;
93+
};
94+
8195
export const sleep = (ms: number): Promise<void> => {
8296
return new Promise(resolve => {
8397
setTimeout(resolve, ms);
@@ -86,14 +100,14 @@ export const sleep = (ms: number): Promise<void> => {
86100

87101
export const executeCommand = async (cmd: string): Promise<string> => {
88102
return new Promise((resolve, reject) => {
89-
exec(cmd, (error, stdout, stderr) => {
90-
if (error) {
91-
reject(`exec error: ${error}`);
92-
} else if (stderr) {
93-
reject(`stderr: ${stderr}`);
94-
} else {
95-
resolve(stdout);
96-
}
97-
});
103+
exec(cmd, (error, stdout, stderr) => {
104+
if (error) {
105+
reject(`exec error: ${error}`);
106+
} else if (stderr) {
107+
reject(`stderr: ${stderr}`);
108+
} else {
109+
resolve(stdout);
110+
}
111+
});
98112
});
99113
};

0 commit comments

Comments
 (0)