Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand schema check to all files. Closes #80 #105

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Snippets: `devproxy-plugin-http-file-generator` - HttpFileGeneratorPlugin instance
- Snippets: `devproxy-plugin-http-file-generator-config` - HttpFileGeneratorPlugin config section
- Snippets: `devproxy-plugin-openai-mock-response` - OpenAIMockResponsePlugin instance
- File Diagnostic: Check that schema matches installed version of Dev Proxy expanded to all files

### Changed:

Expand Down
55 changes: 39 additions & 16 deletions src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { pluginSnippets } from "./constants";
import { getASTNode, getRangeFromASTNode } from "./helpers";
import { DevProxyInstall } from './types';

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

// check if schema version is compatible
const schemaNode = getASTNode(documentNode.children, 'Identifier', '$schema');
if (schemaNode) {
const schemaValue = (schemaNode.value as parse.LiteralNode).value as string;
const devProxyVersion = devProxyInstall.isBeta ? devProxyInstall.version.split('-')[0] : devProxyInstall.version;
if (!schemaValue.includes(`${devProxyVersion}`)) {
const diagnostic = new vscode.Diagnostic(
getRangeFromASTNode(schemaNode),
`Schema version is not compatible with the installed version of Dev Proxy. Expected v${devProxyVersion}`,
vscode.DiagnosticSeverity.Warning
);
diagnostic.code = 'invalidSchema';
diagnostics.push(diagnostic);
}
}
checkSchemaCompatibility(documentNode, devProxyInstall, diagnostics);

// check validity of plugins
const pluginsNode = getASTNode(
Expand Down Expand Up @@ -163,4 +150,40 @@ export const updateDiagnostics = (
}

collection.set(document.uri, diagnostics);
};
};

export const updateDiagnostics = (
context: vscode.ExtensionContext,
document: vscode.TextDocument,
collection: vscode.DiagnosticCollection,
): void => {
const devProxyInstall = context.globalState.get<DevProxyInstall>('devProxyInstall');
if (!devProxyInstall) {
return;
}

const diagnostics: vscode.Diagnostic[] = [];
const documentNode = parse(document.getText()) as parse.ObjectNode;

// check if schema version is compatible
checkSchemaCompatibility(documentNode, devProxyInstall, diagnostics);

collection.set(document.uri, diagnostics);
};

export const checkSchemaCompatibility = (documentNode: parse.ObjectNode, devProxyInstall: DevProxyInstall, diagnostics: vscode.Diagnostic[]) => {
const schemaNode = getASTNode(documentNode.children, 'Identifier', '$schema');
if (schemaNode) {
const schemaValue = (schemaNode.value as parse.LiteralNode).value as string;
const devProxyVersion = devProxyInstall.isBeta ? devProxyInstall.version.split('-')[0] : devProxyInstall.version;
if (!schemaValue.includes(`${devProxyVersion}`)) {
const diagnostic = new vscode.Diagnostic(
getRangeFromASTNode(schemaNode),
`Schema version is not compatible with the installed version of Dev Proxy. Expected v${devProxyVersion}`,
vscode.DiagnosticSeverity.Warning
);
diagnostic.code = 'invalidSchema';
diagnostics.push(diagnostic);
}
}
};
18 changes: 13 additions & 5 deletions src/documents.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
import * as vscode from 'vscode';
import { isConfigFile } from './helpers';
import { updateDiagnostics } from './diagnostics';
import { isConfigFile, isProxyFile } from './helpers';
import { updateConfigDiagnostics, updateDiagnostics } from './diagnostics';

export const registerDocumentListeners = (context: vscode.ExtensionContext, collection: vscode.DiagnosticCollection) => {
context.subscriptions.push(
vscode.workspace.onDidOpenTextDocument(document => {
if (isProxyFile(document)) {
updateDiagnostics(context, document, collection);
}
if (!isConfigFile(document)) {
return;
}
updateDiagnostics(context, document, collection);
updateConfigDiagnostics(context, document, collection);
})
);

context.subscriptions.push(
vscode.workspace.onDidChangeTextDocument(event => {
if (!isConfigFile(event.document)) {
if (!isConfigFile(event.document) || !isProxyFile(event.document)) {
collection.delete(event.document.uri);
return;
}
updateDiagnostics(context, event.document, collection);
if (isConfigFile(event.document)) {
updateConfigDiagnostics(context, event.document, collection);
}
if (isProxyFile(event.document)) {
updateDiagnostics(context, event.document, collection);
}
})
);
};
32 changes: 23 additions & 9 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ export const isConfigFile = (document: vscode.TextDocument) => {
return isConfigFile;
};

export const isProxyFile = (document: vscode.TextDocument) => {
let isProxyFile = false;
const documentNode = parse(document.getText()) as parse.ObjectNode;

const schemaNode = getASTNode(documentNode.children, 'Identifier', '$schema');
if (schemaNode) {
const schema = (schemaNode?.value as parse.LiteralNode).value as string;
if (schema.includes('dev-proxy') && schema.endsWith('.schema.json')) {
isProxyFile = true;
}
}
return isProxyFile;
};

export const sleep = (ms: number): Promise<void> => {
return new Promise(resolve => {
setTimeout(resolve, ms);
Expand All @@ -86,14 +100,14 @@ export const sleep = (ms: number): Promise<void> => {

export const executeCommand = async (cmd: string): Promise<string> => {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
reject(`exec error: ${error}`);
} else if (stderr) {
reject(`stderr: ${stderr}`);
} else {
resolve(stdout);
}
});
exec(cmd, (error, stdout, stderr) => {
if (error) {
reject(`exec error: ${error}`);
} else if (stderr) {
reject(`stderr: ${stderr}`);
} else {
resolve(stdout);
}
});
});
};