-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhelpers.ts
More file actions
118 lines (101 loc) · 3.26 KB
/
helpers.ts
File metadata and controls
118 lines (101 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import * as vscode from 'vscode';
import parse from 'json-to-ast';
import { exec, ExecOptions } from 'child_process';
export const getASTNode = (
children: parse.PropertyNode[],
type: string,
keyValue: string
) => {
return children.find(
child => child.key.type === type && child.key.value === keyValue
);
};
export const getRangeFromASTNode = (
node:
| parse.PropertyNode
| parse.LiteralNode
| parse.ObjectNode
| parse.ValueNode
) => {
const startLine = node?.loc?.start.line || 0;
const endLine = node?.loc?.end.line || 0;
const startColumn = node?.loc?.start.column || 0;
const endColumn = node?.loc?.end.column || 0;
// we remove 1 from the line numbers because vscode uses 0 based line numbers
return new vscode.Range(
new vscode.Position(startLine - 1, startColumn),
new vscode.Position(endLine - 1, endColumn)
);
};
export const getStartPositionFromASTNode = (
node:
| parse.PropertyNode
| parse.LiteralNode
| parse.ObjectNode
| parse.ValueNode
) => {
const startLine = node?.loc?.start.line || 0;
const startColumn = node?.loc?.start.column || 0;
return new vscode.Position(startLine - 1, startColumn);
};
export const isConfigFile = (document: vscode.TextDocument) => {
let isConfigFile = false;
const fileName = document.fileName.toLowerCase();
if (!(fileName.endsWith('.json') || fileName.endsWith('.jsonc'))) {
return false;
}
const documentNode = parse(document.getText()) as parse.ObjectNode;
// we know that its a config file if
// 1. the file name is devproxy.config.json
if (document.fileName.endsWith('devproxyrc.json')) {
isConfigFile = true;
}
// 2. the file contains a $schema property that contains dev-proxy and ends with rc.schema.json
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('rc.schema.json')) {
isConfigFile = true;
}
}
// 3. the file contains plugins array, as $schema is optional
const pluginsNode = getASTNode(
documentNode.children,
'Identifier',
'plugins'
);
if (pluginsNode && pluginsNode.value.type === 'Array') {
isConfigFile = true;
}
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);
});
};
export const executeCommand = async (cmd: string, options: ExecOptions = {}): Promise<string> => {
return new Promise((resolve, reject) => {
exec(cmd, options, (error, stdout, stderr) => {
if (error) {
reject(`exec error: ${error}`);
} else if (stderr) {
reject(`stderr: ${stderr}`);
} else {
resolve(stdout);
}
});
});
};