Skip to content

Commit cf32e59

Browse files
fhammerschmidtcristianoc
authored andcommitted
Wrap bsconfig read function in try/catch
1 parent 185826e commit cf32e59

File tree

1 file changed

+66
-61
lines changed

1 file changed

+66
-61
lines changed

server/src/utils.ts

Lines changed: 66 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -187,37 +187,42 @@ export const toCamelCase = (text: string): string => {
187187
};
188188

189189
const readBsConfig = (projDir: p.DocumentUri) => {
190-
let bsconfigFile = fs.readFileSync(
191-
path.join(projDir, c.bsconfigPartialPath),
192-
{ encoding: "utf-8" }
193-
);
190+
try {
191+
let bsconfigFile = fs.readFileSync(
192+
path.join(projDir, c.bsconfigPartialPath),
193+
{ encoding: "utf-8" }
194+
);
194195

195-
return JSON.parse(bsconfigFile);
196+
let result = JSON.parse(bsconfigFile);
197+
return result;
198+
} catch (e) {
199+
return null;
200+
}
196201
};
197202

198203
export const getNamespaceNameFromBsConfig = (
199204
projDir: p.DocumentUri
200205
): execResult => {
201-
try {
202-
let bsconfig = readBsConfig(projDir);
203-
let result = "";
204-
205-
if (bsconfig.namespace === true) {
206-
result = toCamelCase(bsconfig.name);
207-
} else if (typeof bsconfig.namespace === "string") {
208-
result = toCamelCase(bsconfig.namespace);
209-
}
206+
let bsconfig = readBsConfig(projDir);
207+
let result = "";
210208

211-
return {
212-
kind: "success",
213-
result,
214-
};
215-
} catch (e) {
209+
if (!bsconfig) {
216210
return {
217211
kind: "error",
218-
error: e instanceof Error ? e.message : String(e),
212+
error: "Could not read bsconfig",
219213
};
220214
}
215+
216+
if (bsconfig.namespace === true) {
217+
result = toCamelCase(bsconfig.name);
218+
} else if (typeof bsconfig.namespace === "string") {
219+
result = toCamelCase(bsconfig.namespace);
220+
}
221+
222+
return {
223+
kind: "success",
224+
result,
225+
};
221226
};
222227

223228
export let createInterfaceFileUsingValidBscExePath = (
@@ -251,54 +256,54 @@ export let getCompiledFilePath = (
251256
filePath: string,
252257
projDir: string
253258
): execResult => {
254-
try {
255-
let bsConfig = readBsConfig(projDir);
256-
257-
let pkgSpecs = bsConfig["package-specs"];
258-
let pathFragment = "";
259-
let moduleFormatObj: any = {};
260-
261-
let module = c.bsconfigModuleDefault;
262-
let suffix = c.bsconfigSuffixDefault;
263-
264-
if (pkgSpecs && pkgSpecs.module) {
265-
moduleFormatObj = pkgSpecs;
266-
} else if (pkgSpecs && pkgSpecs[0]) {
267-
if (typeof pkgSpecs[0] === "string") {
268-
module = pkgSpecs[0];
269-
} else {
270-
moduleFormatObj = pkgSpecs[0];
271-
}
272-
}
259+
let bsconfig = readBsConfig(projDir);
273260

274-
if (moduleFormatObj["module"]) {
275-
module = moduleFormatObj["module"];
276-
}
261+
if (!bsconfig) {
262+
return {
263+
kind: "error",
264+
error: "Could not read bsconfig",
265+
};
266+
}
277267

278-
if (!moduleFormatObj["in-source"]) {
279-
pathFragment = "lib/" + module.replace("-", "_");
280-
}
268+
let pkgSpecs = bsconfig["package-specs"];
269+
let pathFragment = "";
270+
let moduleFormatObj: any = {};
271+
272+
let module = c.bsconfigModuleDefault;
273+
let suffix = c.bsconfigSuffixDefault;
281274

282-
if (moduleFormatObj.suffix) {
283-
suffix = moduleFormatObj.suffix;
284-
} else if (bsConfig.suffix) {
285-
suffix = bsConfig.suffix;
275+
if (pkgSpecs && pkgSpecs.module) {
276+
moduleFormatObj = pkgSpecs;
277+
} else if (pkgSpecs && pkgSpecs[0]) {
278+
if (typeof pkgSpecs[0] === "string") {
279+
module = pkgSpecs[0];
280+
} else {
281+
moduleFormatObj = pkgSpecs[0];
286282
}
283+
}
287284

288-
let partialFilePath = filePath.split(projDir)[1];
289-
let compiledPartialPath = replaceFileExtension(partialFilePath, suffix);
290-
let result = path.join(projDir, pathFragment, compiledPartialPath);
285+
if (moduleFormatObj["module"]) {
286+
module = moduleFormatObj["module"];
287+
}
291288

292-
return {
293-
kind: "success",
294-
result,
295-
};
296-
} catch (e) {
297-
return {
298-
kind: "error",
299-
error: e instanceof Error ? e.message : String(e),
300-
};
289+
if (!moduleFormatObj["in-source"]) {
290+
pathFragment = "lib/" + module.replace("-", "_");
291+
}
292+
293+
if (moduleFormatObj.suffix) {
294+
suffix = moduleFormatObj.suffix;
295+
} else if (bsconfig.suffix) {
296+
suffix = bsconfig.suffix;
301297
}
298+
299+
let partialFilePath = filePath.split(projDir)[1];
300+
let compiledPartialPath = replaceFileExtension(partialFilePath, suffix);
301+
let result = path.join(projDir, pathFragment, compiledPartialPath);
302+
303+
return {
304+
kind: "success",
305+
result,
306+
};
302307
};
303308

304309
export let runBuildWatcherUsingValidBuildPath = (

0 commit comments

Comments
 (0)