@zentryc5t/ts-plugin-for-bruno 1.0.4 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/out/index.js +99 -32
- package/out/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +156 -43
package/out/index.js
CHANGED
|
@@ -20,22 +20,38 @@ function init(_modules) {
|
|
|
20
20
|
};
|
|
21
21
|
proxy.getSuggestionDiagnostics = (fileName) => {
|
|
22
22
|
const defaultDiagnostics = info.languageService.getSuggestionDiagnostics(fileName);
|
|
23
|
-
|
|
23
|
+
const isBruFile = isBrunoFile(fileName);
|
|
24
|
+
if (!isBruFile && !isJsFileFromBrunoCollection(info, fileName)) {
|
|
24
25
|
return defaultDiagnostics;
|
|
25
26
|
}
|
|
26
|
-
const
|
|
27
|
+
const allDiagnostics = filterDefaultDiagnostics(info, fileName, defaultDiagnostics);
|
|
27
28
|
// The ts server always reports errors when importing a Javascript function in a .bru file.
|
|
28
|
-
return
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
return isBruFile
|
|
30
|
+
? allDiagnostics.filter(
|
|
31
|
+
// Do not show diagnostics that only make sense for Typescript files.
|
|
32
|
+
// Bru file code blocks should be treated like Javascript functions instead.
|
|
33
|
+
// A list of diagnostics can be found here: https://github.com/microsoft/TypeScript/blob/main/src/compiler/diagnosticMessages.json
|
|
34
|
+
({ code }) => (code < 7043 || code > 7050) &&
|
|
35
|
+
code != 80001 &&
|
|
36
|
+
code != 80004)
|
|
37
|
+
: allDiagnostics.filter(({ code }) => code != 80001); // Bruno currently only supports CommonJS modules.
|
|
33
38
|
};
|
|
34
|
-
// All hovers are provided by the extension implementation
|
|
39
|
+
// All hovers are provided by the extension implementation for '.bru' and '.js' files within collections.
|
|
40
|
+
// Only for the temp js file, the ts language service has to be used.
|
|
35
41
|
proxy.getQuickInfoAtPosition = (fileName, position) => {
|
|
36
|
-
|
|
42
|
+
if (isBrunoFile(fileName) ||
|
|
43
|
+
(isJsFileFromBrunoCollection(info, fileName) &&
|
|
44
|
+
!isTempJsFile(fileName))) {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
return info.languageService.getQuickInfoAtPosition(fileName, position);
|
|
48
|
+
};
|
|
49
|
+
// All completions for '.js' files within collections are provided by the extension already.
|
|
50
|
+
proxy.getCompletionsAtPosition = (fileName, position, options) => {
|
|
51
|
+
return isJsFileFromBrunoCollection(info, fileName) &&
|
|
52
|
+
!isTempJsFile(fileName)
|
|
37
53
|
? undefined
|
|
38
|
-
: info.languageService.
|
|
54
|
+
: info.languageService.getCompletionsAtPosition(fileName, position, options);
|
|
39
55
|
};
|
|
40
56
|
return proxy;
|
|
41
57
|
}
|
|
@@ -44,18 +60,19 @@ function init(_modules) {
|
|
|
44
60
|
};
|
|
45
61
|
}
|
|
46
62
|
function filterDefaultDiagnostics(info, fileName, defaultDiagnostics) {
|
|
47
|
-
if (
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
let fileContent = undefined;
|
|
52
|
-
const scriptSnapshot = info.languageServiceHost.getScriptSnapshot(fileName);
|
|
53
|
-
if (scriptSnapshot) {
|
|
54
|
-
fileContent = scriptSnapshot.getText(0, scriptSnapshot.getLength());
|
|
63
|
+
if (isJsFileFromBrunoCollection(info, fileName)) {
|
|
64
|
+
// Avoid showing errors for temp js files.
|
|
65
|
+
if (isTempJsFile(fileName)) {
|
|
66
|
+
return [];
|
|
55
67
|
}
|
|
56
|
-
|
|
57
|
-
|
|
68
|
+
const fileContent = getFileContent(info, fileName);
|
|
69
|
+
if (!fileContent) {
|
|
70
|
+
return [];
|
|
58
71
|
}
|
|
72
|
+
return filterOutDiagnosticsForInbuiltRuntimeFunctions(defaultDiagnostics, fileContent);
|
|
73
|
+
}
|
|
74
|
+
else if (isBrunoFile(fileName)) {
|
|
75
|
+
const fileContent = getFileContent(info, fileName);
|
|
59
76
|
if (!fileContent) {
|
|
60
77
|
return [];
|
|
61
78
|
}
|
|
@@ -66,20 +83,14 @@ function filterDefaultDiagnostics(info, fileName, defaultDiagnostics) {
|
|
|
66
83
|
indizes.push({ blockName, ...indizesForBlock });
|
|
67
84
|
}
|
|
68
85
|
}
|
|
69
|
-
if (indizes.length
|
|
70
|
-
return defaultDiagnostics.filter(({ start, length }) => start != undefined &&
|
|
71
|
-
length != undefined &&
|
|
72
|
-
indizes.some(({ startIndex: blockStart, endIndex: blockEnd }) => start >= blockStart && start <= blockEnd) &&
|
|
73
|
-
// Do not show diagnostics for functions that are provided by Bruno at runtime
|
|
74
|
-
!["bru", "req", "res", "test", "expect"].includes(fileContent.substring(start, start + length)) &&
|
|
75
|
-
// Avoid showing incorrect error when using `require`
|
|
76
|
-
// (the error seems to only occur for short periods of time when typescript type definitions have not been reloaded for a while)
|
|
77
|
-
fileContent.substring(start, start + length) !=
|
|
78
|
-
"require");
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
86
|
+
if (indizes.length == 0) {
|
|
81
87
|
return [];
|
|
82
88
|
}
|
|
89
|
+
return filterOutDiagnosticsForInbuiltRuntimeFunctions(defaultDiagnostics.filter(({ start }) => start != undefined &&
|
|
90
|
+
indizes.some(({ startIndex: blockStart, endIndex: blockEnd }) => start >= blockStart && start <= blockEnd)), fileContent);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
return defaultDiagnostics;
|
|
83
94
|
}
|
|
84
95
|
}
|
|
85
96
|
function getCodeBlockStartAndEndIndex(fullTextContent, blockName) {
|
|
@@ -104,9 +115,65 @@ function getCodeBlockStartAndEndIndex(fullTextContent, blockName) {
|
|
|
104
115
|
endIndex: blockStartIndex + blockNodeInSubDocument.end,
|
|
105
116
|
};
|
|
106
117
|
}
|
|
118
|
+
function isJsFileFromBrunoCollection(info, fileName) {
|
|
119
|
+
return (0, path_1.extname)(fileName) == ".js" && isInABrunoCollection(info, fileName);
|
|
120
|
+
}
|
|
121
|
+
function filterOutDiagnosticsForInbuiltRuntimeFunctions(diagnosticsToFilter, fileContent) {
|
|
122
|
+
return diagnosticsToFilter.filter(({ start, length }) => start != undefined &&
|
|
123
|
+
length != undefined &&
|
|
124
|
+
// Do not show diagnostics for functions that are provided by Bruno at runtime
|
|
125
|
+
!["bru", "req", "res", "test", "expect"].includes(fileContent.substring(start, start + length)) &&
|
|
126
|
+
// Avoid showing incorrect error when using `require`
|
|
127
|
+
// (the error seems to only occur for short periods of time when typescript type definitions have not been reloaded for a while)
|
|
128
|
+
fileContent.substring(start, start + length) != "require");
|
|
129
|
+
}
|
|
130
|
+
function getFileContent(info, fileName) {
|
|
131
|
+
const scriptSnapshot = info.languageServiceHost.getScriptSnapshot(fileName);
|
|
132
|
+
if (scriptSnapshot) {
|
|
133
|
+
return scriptSnapshot.getText(0, scriptSnapshot.getLength());
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
return info.languageServiceHost.readFile(fileName);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
function isInABrunoCollection(info, fileName) {
|
|
140
|
+
let currentDirectory = (0, path_1.dirname)(fileName);
|
|
141
|
+
let isCollectionRoot = isACollectionRootFolder(info, currentDirectory);
|
|
142
|
+
while (info.serverHost.directoryExists(currentDirectory) &&
|
|
143
|
+
!isCollectionRoot &&
|
|
144
|
+
(0, path_1.dirname)(currentDirectory) != currentDirectory // dirname of a base folder in the file system seems to return the input folder again
|
|
145
|
+
) {
|
|
146
|
+
currentDirectory = (0, path_1.dirname)(currentDirectory);
|
|
147
|
+
isCollectionRoot = isACollectionRootFolder(info, currentDirectory);
|
|
148
|
+
}
|
|
149
|
+
return isCollectionRoot;
|
|
150
|
+
}
|
|
151
|
+
function isACollectionRootFolder(info, directoryPath) {
|
|
152
|
+
return info.serverHost
|
|
153
|
+
.readDirectory(directoryPath, undefined, undefined, undefined, 1)
|
|
154
|
+
.some((fileName) => {
|
|
155
|
+
if (!fileName.endsWith("bruno.json")) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
const content = info.languageServiceHost.readFile(fileName);
|
|
159
|
+
if (!content) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
try {
|
|
163
|
+
const parsedContent = JSON.parse(content);
|
|
164
|
+
return parsedContent && parsedContent.type == "collection";
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}
|
|
107
171
|
function isBrunoFile(fileName) {
|
|
108
172
|
return (0, path_1.extname)(fileName) == ".bru";
|
|
109
173
|
}
|
|
174
|
+
function isTempJsFile(fileName) {
|
|
175
|
+
return (0, path_1.basename)(fileName) == "__temp_bru_reference.js";
|
|
176
|
+
}
|
|
110
177
|
var TextBlockName;
|
|
111
178
|
(function (TextBlockName) {
|
|
112
179
|
TextBlockName["Tests"] = "tests";
|
package/out/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,+
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,+BAAkD;AAClD,qDAAqD;AAErD,SAAS,IAAI,CAAC,QAEb;IACG,SAAS,MAAM,CAAC,IAAgC;QAC5C,0BAA0B;QAC1B,MAAM,KAAK,GAAuB,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAEtD,KAAK,MAAM,CAAC,IAAI,MAAM,CAAC,IAAI,CACvB,IAAI,CAAC,eAAe,CACS,EAAE;YAC/B,MAAM,CAAC,GAAG,IAAI,CAAC,eAAe,CAAC,CAAC,CAAE,CAAC;YACnC,yEAAyE;YACzE,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,IAAe,EAAE,EAAE;YAC9B,yEAAyE;YACzE,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,eAAe,EAAE,IAAI,CAAC,CAAC;SAC3C;QAED,KAAK,CAAC,u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CAAC,mBAAmB,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QAE5D,IAAI,CAAC,OAAO,EAAE;YACV,OAAO,KAAK,CAAC;SAChB;QAED,IAAI;YACA,MAAM,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAEzB,CAAC;YAEhB,OAAO,aAAa,IAAI,aAAa,CAAC,IAAI,IAAI,YAAY,CAAC;SAC9D;QAAC,MAAM;YACJ,OAAO,KAAK,CAAC;SAChB;IACL,CAAC,CAAC,CAAC;AACX,CAAC;AAED,SAAS,WAAW,CAAC,QAAgB;IACjC,OAAO,IAAA,cAAO,EAAC,QAAQ,CAAC,IAAI,MAAM,CAAC;AACvC,CAAC;AAED,SAAS,YAAY,CAAC,QAAgB;IAClC,OAAO,IAAA,eAAQ,EAAC,QAAQ,CAAC,IAAI,yBAAyB,CAAC;AAC3D,CAAC;AAED,IAAK,aAIJ;AAJD,WAAK,aAAa;IACd,gCAAe,CAAA;IACf,wDAAuC,CAAA;IACvC,4DAA2C,CAAA;AAC/C,CAAC,EAJI,aAAa,KAAb,aAAa,QAIjB;AAED,iBAAS,IAAI,CAAC"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { extname } from "path";
|
|
1
|
+
import { basename, dirname, extname } from "path";
|
|
2
2
|
import * as ts from "typescript/lib/tsserverlibrary";
|
|
3
3
|
|
|
4
4
|
function init(_modules: {
|
|
@@ -38,31 +38,58 @@ function init(_modules: {
|
|
|
38
38
|
const defaultDiagnostics =
|
|
39
39
|
info.languageService.getSuggestionDiagnostics(fileName);
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
const isBruFile = isBrunoFile(fileName);
|
|
42
|
+
|
|
43
|
+
if (!isBruFile && !isJsFileFromBrunoCollection(info, fileName)) {
|
|
42
44
|
return defaultDiagnostics;
|
|
43
45
|
}
|
|
44
|
-
|
|
46
|
+
|
|
47
|
+
const allDiagnostics = filterDefaultDiagnostics(
|
|
45
48
|
info,
|
|
46
49
|
fileName,
|
|
47
50
|
defaultDiagnostics,
|
|
48
51
|
) as ts.DiagnosticWithLocation[];
|
|
49
52
|
|
|
50
53
|
// The ts server always reports errors when importing a Javascript function in a .bru file.
|
|
51
|
-
return
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
return isBruFile
|
|
55
|
+
? allDiagnostics.filter(
|
|
56
|
+
// Do not show diagnostics that only make sense for Typescript files.
|
|
57
|
+
// Bru file code blocks should be treated like Javascript functions instead.
|
|
58
|
+
// A list of diagnostics can be found here: https://github.com/microsoft/TypeScript/blob/main/src/compiler/diagnosticMessages.json
|
|
59
|
+
({ code }) =>
|
|
60
|
+
(code < 7_043 || code > 7_050) &&
|
|
61
|
+
code != 80_001 &&
|
|
62
|
+
code != 80_004,
|
|
63
|
+
)
|
|
64
|
+
: allDiagnostics.filter(({ code }) => code != 80_001); // Bruno currently only supports CommonJS modules.
|
|
57
65
|
};
|
|
58
66
|
|
|
59
|
-
// All hovers are provided by the extension implementation
|
|
67
|
+
// All hovers are provided by the extension implementation for '.bru' and '.js' files within collections.
|
|
68
|
+
// Only for the temp js file, the ts language service has to be used.
|
|
60
69
|
proxy.getQuickInfoAtPosition = (fileName, position) => {
|
|
61
|
-
|
|
70
|
+
if (
|
|
71
|
+
isBrunoFile(fileName) ||
|
|
72
|
+
(isJsFileFromBrunoCollection(info, fileName) &&
|
|
73
|
+
!isTempJsFile(fileName))
|
|
74
|
+
) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return info.languageService.getQuickInfoAtPosition(
|
|
79
|
+
fileName,
|
|
80
|
+
position,
|
|
81
|
+
);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// All completions for '.js' files within collections are provided by the extension already.
|
|
85
|
+
proxy.getCompletionsAtPosition = (fileName, position, options) => {
|
|
86
|
+
return isJsFileFromBrunoCollection(info, fileName) &&
|
|
87
|
+
!isTempJsFile(fileName)
|
|
62
88
|
? undefined
|
|
63
|
-
: info.languageService.
|
|
89
|
+
: info.languageService.getCompletionsAtPosition(
|
|
64
90
|
fileName,
|
|
65
91
|
position,
|
|
92
|
+
options,
|
|
66
93
|
);
|
|
67
94
|
};
|
|
68
95
|
|
|
@@ -79,19 +106,25 @@ function filterDefaultDiagnostics(
|
|
|
79
106
|
fileName: string,
|
|
80
107
|
defaultDiagnostics: (ts.Diagnostic | ts.DiagnosticWithLocation)[],
|
|
81
108
|
) {
|
|
82
|
-
if (
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
fileContent = info.languageServiceHost.readFile(fileName);
|
|
109
|
+
if (isJsFileFromBrunoCollection(info, fileName)) {
|
|
110
|
+
// Avoid showing errors for temp js files.
|
|
111
|
+
if (isTempJsFile(fileName)) {
|
|
112
|
+
return [];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const fileContent = getFileContent(info, fileName);
|
|
116
|
+
|
|
117
|
+
if (!fileContent) {
|
|
118
|
+
return [];
|
|
93
119
|
}
|
|
94
120
|
|
|
121
|
+
return filterOutDiagnosticsForInbuiltRuntimeFunctions(
|
|
122
|
+
defaultDiagnostics,
|
|
123
|
+
fileContent,
|
|
124
|
+
);
|
|
125
|
+
} else if (isBrunoFile(fileName)) {
|
|
126
|
+
const fileContent = getFileContent(info, fileName);
|
|
127
|
+
|
|
95
128
|
if (!fileContent) {
|
|
96
129
|
return [];
|
|
97
130
|
}
|
|
@@ -113,30 +146,23 @@ function filterDefaultDiagnostics(
|
|
|
113
146
|
}
|
|
114
147
|
}
|
|
115
148
|
|
|
116
|
-
if (indizes.length
|
|
117
|
-
return
|
|
118
|
-
|
|
149
|
+
if (indizes.length == 0) {
|
|
150
|
+
return [];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return filterOutDiagnosticsForInbuiltRuntimeFunctions(
|
|
154
|
+
defaultDiagnostics.filter(
|
|
155
|
+
({ start }) =>
|
|
119
156
|
start != undefined &&
|
|
120
|
-
length != undefined &&
|
|
121
157
|
indizes.some(
|
|
122
158
|
({ startIndex: blockStart, endIndex: blockEnd }) =>
|
|
123
159
|
start >= blockStart && start <= blockEnd,
|
|
124
|
-
)
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
),
|
|
131
|
-
) &&
|
|
132
|
-
// Avoid showing incorrect error when using `require`
|
|
133
|
-
// (the error seems to only occur for short periods of time when typescript type definitions have not been reloaded for a while)
|
|
134
|
-
(fileContent as string).substring(start, start + length) !=
|
|
135
|
-
"require",
|
|
136
|
-
);
|
|
137
|
-
} else {
|
|
138
|
-
return [];
|
|
139
|
-
}
|
|
160
|
+
),
|
|
161
|
+
),
|
|
162
|
+
fileContent,
|
|
163
|
+
);
|
|
164
|
+
} else {
|
|
165
|
+
return defaultDiagnostics;
|
|
140
166
|
}
|
|
141
167
|
}
|
|
142
168
|
|
|
@@ -183,10 +209,97 @@ function getCodeBlockStartAndEndIndex(
|
|
|
183
209
|
};
|
|
184
210
|
}
|
|
185
211
|
|
|
212
|
+
function isJsFileFromBrunoCollection(
|
|
213
|
+
info: ts.server.PluginCreateInfo,
|
|
214
|
+
fileName: string,
|
|
215
|
+
) {
|
|
216
|
+
return extname(fileName) == ".js" && isInABrunoCollection(info, fileName);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function filterOutDiagnosticsForInbuiltRuntimeFunctions(
|
|
220
|
+
diagnosticsToFilter: (ts.Diagnostic | ts.DiagnosticWithLocation)[],
|
|
221
|
+
fileContent: string,
|
|
222
|
+
) {
|
|
223
|
+
return diagnosticsToFilter.filter(
|
|
224
|
+
({ start, length }) =>
|
|
225
|
+
start != undefined &&
|
|
226
|
+
length != undefined &&
|
|
227
|
+
// Do not show diagnostics for functions that are provided by Bruno at runtime
|
|
228
|
+
!["bru", "req", "res", "test", "expect"].includes(
|
|
229
|
+
fileContent.substring(start, start + length),
|
|
230
|
+
) &&
|
|
231
|
+
// Avoid showing incorrect error when using `require`
|
|
232
|
+
// (the error seems to only occur for short periods of time when typescript type definitions have not been reloaded for a while)
|
|
233
|
+
fileContent.substring(start, start + length) != "require",
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function getFileContent(info: ts.server.PluginCreateInfo, fileName: string) {
|
|
238
|
+
const scriptSnapshot = info.languageServiceHost.getScriptSnapshot(fileName);
|
|
239
|
+
|
|
240
|
+
if (scriptSnapshot) {
|
|
241
|
+
return scriptSnapshot.getText(0, scriptSnapshot.getLength());
|
|
242
|
+
} else {
|
|
243
|
+
return info.languageServiceHost.readFile(fileName);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function isInABrunoCollection(
|
|
248
|
+
info: ts.server.PluginCreateInfo,
|
|
249
|
+
fileName: string,
|
|
250
|
+
) {
|
|
251
|
+
let currentDirectory = dirname(fileName);
|
|
252
|
+
let isCollectionRoot = isACollectionRootFolder(info, currentDirectory);
|
|
253
|
+
|
|
254
|
+
while (
|
|
255
|
+
info.serverHost.directoryExists(currentDirectory) &&
|
|
256
|
+
!isCollectionRoot &&
|
|
257
|
+
dirname(currentDirectory) != currentDirectory // dirname of a base folder in the file system seems to return the input folder again
|
|
258
|
+
) {
|
|
259
|
+
currentDirectory = dirname(currentDirectory);
|
|
260
|
+
isCollectionRoot = isACollectionRootFolder(info, currentDirectory);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return isCollectionRoot;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function isACollectionRootFolder(
|
|
267
|
+
info: ts.server.PluginCreateInfo,
|
|
268
|
+
directoryPath: string,
|
|
269
|
+
) {
|
|
270
|
+
return info.serverHost
|
|
271
|
+
.readDirectory(directoryPath, undefined, undefined, undefined, 1)
|
|
272
|
+
.some((fileName) => {
|
|
273
|
+
if (!fileName.endsWith("bruno.json")) {
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const content = info.languageServiceHost.readFile(fileName);
|
|
278
|
+
|
|
279
|
+
if (!content) {
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
try {
|
|
284
|
+
const parsedContent = JSON.parse(content) as
|
|
285
|
+
| { type: string }
|
|
286
|
+
| undefined;
|
|
287
|
+
|
|
288
|
+
return parsedContent && parsedContent.type == "collection";
|
|
289
|
+
} catch {
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
|
|
186
295
|
function isBrunoFile(fileName: string) {
|
|
187
296
|
return extname(fileName) == ".bru";
|
|
188
297
|
}
|
|
189
298
|
|
|
299
|
+
function isTempJsFile(fileName: string) {
|
|
300
|
+
return basename(fileName) == "__temp_bru_reference.js";
|
|
301
|
+
}
|
|
302
|
+
|
|
190
303
|
enum TextBlockName {
|
|
191
304
|
Tests = "tests",
|
|
192
305
|
PreRequestScript = "script:pre-request",
|