Skip to content
This repository was archived by the owner on Jun 8, 2023. It is now read-only.

Commit 3baeccd

Browse files
authored
Merge pull request francescov1#86 from gadielkalleb/fix/json-parser-error
fix string to JSON conversion and update prettier version
2 parents 12bceb6 + 4e8c5c1 commit 3baeccd

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "mongoose-tsgen",
33
"description": "A Typescript interface generator for Mongoose that works out of the box.",
4-
"version": "9.0.0",
4+
"version": "9.0.1",
55
"author": "Francesco Virga @francescov1",
66
"bin": {
77
"mtgen": "./bin/run"
@@ -21,7 +21,7 @@
2121
"glob": "^7.1.6",
2222
"lodash": "^4.17.20",
2323
"mkdirp": "^1.0.4",
24-
"prettier": "^2.1.2",
24+
"prettier": "^2.5.1",
2525
"strip-json-comments": "^3.1.1",
2626
"ts-morph": "^9.1.0",
2727
"ts-node": "^9.1.1",

src/helpers/tsReader.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -372,15 +372,20 @@ export const registerUserTs = (basePath: string): (() => void) | null => {
372372

373373
// handle path aliases
374374
const tsConfigString = fs.readFileSync(foundPath, "utf8");
375-
const tsConfig = JSON.parse(stripJsonComments(tsConfigString));
376-
if (tsConfig?.compilerOptions?.paths) {
377-
const cleanup = require("tsconfig-paths").register({
378-
baseUrl: process.cwd(),
379-
paths: tsConfig.compilerOptions.paths
380-
});
381-
382-
return cleanup;
383-
}
384375

385-
return null;
376+
try {
377+
const tsConfig = JSON.parse(stripJsonComments(tsConfigString));
378+
if (tsConfig?.compilerOptions?.paths) {
379+
const cleanup = require("tsconfig-paths").register({
380+
baseUrl: process.cwd(),
381+
paths: tsConfig.compilerOptions.paths
382+
});
383+
384+
return cleanup;
385+
}
386+
387+
return null;
388+
} catch {
389+
throw new Error("Error parsing your tsconfig.json file, please ensure the format is valid");
390+
}
386391
};

src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ class MongooseTsgen extends Command {
7171

7272
// we cant set flags as `default` using the official oclif method since the defaults would overwrite flags provided in the config file.
7373
// instead, well just set "output" and "project" as default manually if theyre still missing after merge with configFile.
74-
configFileFlags.output = configFileFlags.output ?? "./src/interfaces";
75-
configFileFlags.project = configFileFlags.project ?? "./";
74+
configFileFlags.output = configFileFlags?.output ?? "./src/interfaces";
75+
configFileFlags.project = configFileFlags?.project ?? "./";
7676

7777
return {
7878
flags: {
@@ -136,12 +136,13 @@ class MongooseTsgen extends Command {
136136
this.log(`Writing interfaces to ${genFilePath}`);
137137

138138
generator.saveFile({ genFilePath, sourceFile });
139+
139140
if (!flags["no-format"]) await formatter.format([genFilePath]);
140141
this.log("Writing complete 🐒");
141142
process.exit();
142143
}
143144
} catch (error) {
144-
this.error(error as Error);
145+
this.error(error as Error, { exit: 1 });
145146
}
146147
}
147148
}

0 commit comments

Comments
 (0)