Skip to content

Commit c07ed92

Browse files
committed
Update config file check
Closes #30
1 parent 6e05d08 commit c07ed92

14 files changed

+174
-42
lines changed

.vscode-test.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import { defineConfig } from '@vscode/test-cli';
1+
import {defineConfig} from '@vscode/test-cli';
22

33
export default defineConfig({
4-
files: 'out/test/**/*.test.js',
4+
files: 'out/test/**/*.test.js',
5+
mocha: {
6+
ui: 'tdd',
7+
timeout: 20000,
8+
},
59
});

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@
3434
"scripts": {
3535
"vscode:prepublish": "npm run package",
3636
"compile": "webpack",
37+
"copy-examples": "node scripts/copy-examples.js",
3738
"watch": "webpack --watch",
3839
"package": "webpack --mode production --devtool hidden-source-map",
3940
"compile-tests": "tsc -p . --outDir out",
4041
"watch-tests": "tsc -p . -w --outDir out",
41-
"pretest": "npm run compile-tests && npm run compile && npm run lint",
42+
"pretest": "npm run compile-tests && npm run compile && npm run copy-examples && npm run lint",
4243
"lint": "gts lint",
4344
"test": "vscode-test",
4445
"clean": "gts clean",

scripts/copy-examples.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
function copyFolderSync(from, to) {
5+
fs.mkdirSync(to, {recursive: true});
6+
fs.readdirSync(from).forEach(element => {
7+
if (fs.lstatSync(path.join(from, element)).isFile()) {
8+
fs.copyFileSync(path.join(from, element), path.join(to, element));
9+
} else {
10+
copyFolderSync(path.join(from, element), path.join(to, element));
11+
}
12+
});
13+
}
14+
15+
const srcDir = 'src/test/examples';
16+
const destDir = 'out/test/examples';
17+
18+
copyFolderSync(srcDir, destDir);
19+
console.log('Directory and contents have been copied successfully.');

src/extension.ts

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode';
22
import parse from 'json-to-ast';
3-
import {pluginSnippets} from './constants';
4-
import {getASTNode, getRangeFromASTNode} from './helpers';
3+
import { pluginSnippets } from './constants';
4+
import { getASTNode, getRangeFromASTNode, isConfigFile } from './helpers';
55

66
export const activate = (context: vscode.ExtensionContext) => {
77
const collection = vscode.languages.createDiagnosticCollection('Dev Proxy');
@@ -29,46 +29,18 @@ const updateDiagnostics = (
2929
): void => {
3030
let diagnostics: vscode.Diagnostic[] = [];
3131

32-
// we need to ensure that we are looking at a config file before we start
33-
const fileContents = parse(document.getText()) as parse.ObjectNode;
34-
let isConfigFile = false;
32+
const documentNode = parse(document.getText()) as parse.ObjectNode;
3533

36-
// we know that its a config file if
37-
// 1. the file name is devproxy.config.json
38-
if (document.fileName.endsWith('devproxyrc.json')) {
39-
isConfigFile = true;
40-
}
41-
42-
// 2. the file contains a $schema property with the value of https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v1.0/rc.schema.json
43-
const schemaNode = getASTNode(fileContents.children, 'Identifier', '$schema');
44-
if (schemaNode) {
45-
const schema = (schemaNode?.value as parse.LiteralNode).value as string;
46-
if (schema.includes('dev-proxy') && schema.endsWith('rc.schema.json')) {
47-
isConfigFile = true;
48-
}
34+
if (!isConfigFile(document)) {
35+
return;
4936
}
5037

51-
// 3. the file contains the following properties: urlsToWatch and plugins, as $schema is optional
52-
const pluginsNode = getASTNode(
53-
fileContents.children,
54-
'Identifier',
55-
'plugins'
56-
);
38+
// check if urlsToWatch is empty
5739
const urlsToWatchNode = getASTNode(
58-
fileContents.children,
40+
documentNode.children,
5941
'Identifier',
6042
'urlsToWatch'
61-
);
62-
if (pluginsNode && urlsToWatchNode) {
63-
isConfigFile = true;
64-
}
65-
66-
// if its not a config file, we don't need to do anything
67-
if (!isConfigFile) {
68-
return;
69-
}
70-
71-
// check if urlsToWatch is empty
43+
);
7244
if (
7345
urlsToWatchNode &&
7446
(urlsToWatchNode.value as parse.ArrayNode).children.length === 0
@@ -83,6 +55,11 @@ const updateDiagnostics = (
8355
}
8456

8557
// check validity of plugins
58+
const pluginsNode = getASTNode(
59+
documentNode.children,
60+
'Identifier',
61+
'plugins'
62+
);
8663
if (
8764
pluginsNode &&
8865
(pluginsNode.value as parse.ArrayNode).children.length !== 0
@@ -146,7 +123,7 @@ const updateDiagnostics = (
146123
configSectionNode.value as parse.LiteralNode
147124
).value as string;
148125
const configSection = getASTNode(
149-
fileContents.children,
126+
documentNode.children,
150127
'Identifier',
151128
configSectionName
152129
);
@@ -170,4 +147,4 @@ const updateDiagnostics = (
170147
collection.set(document.uri, diagnostics);
171148
};
172149

173-
export const deactivate = () => {};
150+
export const deactivate = () => { };

src/helpers.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,37 @@ export const getRangeFromASTNode = (
2525
new vscode.Position(endLine - 1, endColumn)
2626
);
2727
};
28+
29+
export const isConfigFile = (document: vscode.TextDocument) => {
30+
let isConfigFile = false;
31+
32+
const documentNode = parse(document.getText()) as parse.ObjectNode;
33+
34+
// we know that its a config file if
35+
// 1. the file name is devproxy.config.json
36+
if (document.fileName.endsWith('devproxyrc.json')) {
37+
isConfigFile = true;
38+
}
39+
40+
// 2. the file contains a $schema property that contains dev-proxy and ends with rc.schema.json
41+
const schemaNode = getASTNode(documentNode.children, 'Identifier', '$schema');
42+
if (schemaNode) {
43+
const schema = (schemaNode?.value as parse.LiteralNode).value as string;
44+
if (schema.includes('dev-proxy') && schema.endsWith('rc.schema.json')) {
45+
isConfigFile = true;
46+
}
47+
}
48+
49+
// 3. the file contains plugins array, as $schema is optional
50+
const pluginsNode = getASTNode(
51+
documentNode.children,
52+
'Identifier',
53+
'plugins'
54+
);
55+
56+
if (pluginsNode && pluginsNode.value.type === 'Array') {
57+
isConfigFile = true;
58+
}
59+
60+
return isConfigFile;
61+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v1.0/mockresponseplugin.schema.json",
3+
"plugins": {}
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v1.0/mockresponseplugin.schema.json"
3+
}

src/test/examples/config-plugins.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": []
3+
}

src/test/examples/config-schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v1.0/rc.schema.json",
3+
"plugins": []
4+
}

src/test/examples/devproxyrc.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v1.0/rc.schema.json",
3+
"plugins": []
4+
}

0 commit comments

Comments
 (0)