Skip to content

Commit 05436eb

Browse files
committed
Allow comments in tsconfig.json files
Previously, if your tsconfig.json file contained a comment (allowed by the TS compiler) the ts-import-types-cli would throw the following error: `ts-import-types-cli --project /home/joshuacc/projects/azure-iots-saas/ux-renderer/tsconfig.json is not a tsconfig.json file` This is because the node `require` function can only parse valid (commentless) JSON. This PR swaps out the use of `require` in favor of directly reading the file contents, stripping all comments, and only then parsing it to confirm that it is valid JSON. Hopefully this will save other users some confusion!
1 parent 8b1e038 commit 05436eb

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"dependencies": {
1111
"chalk": "4.1.0",
1212
"commander": "6.2.0",
13+
"strip-json-comments": "^3.1.1",
1314
"ts-morph": "9.1.0"
1415
},
1516
"devDependencies": {

src/bin.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import chalk = require('chalk');
44
import program = require('commander');
55
import { resolve } from 'path';
66
import { tsImportTypes } from '.';
7+
import stripJsonComments from 'strip-json-comments';
8+
import fs from 'fs';
79

810
const sourcePatterns: string[] = [];
911

@@ -24,7 +26,7 @@ const project = program.project || './tsconfig.json';
2426
const tsConfigFilePath = resolve(process.cwd(), project);
2527

2628
try {
27-
require(tsConfigFilePath);
29+
JSON.parse(stripJsonComments(fs.readFileSync(tsConfigFilePath, 'utf8')));
2830
} catch (err) {
2931
const message = `ts-import-types-cli --project ${tsConfigFilePath} is not a tsconfig.json file`;
3032
console.error(chalk.red(message));

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,11 @@ source-map@^0.6.0:
306306
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
307307
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
308308

309+
strip-json-comments@^3.1.1:
310+
version "3.1.1"
311+
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-3.1.1.tgz#31f1281b3832630434831c310c01cccda8cbe006"
312+
integrity sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==
313+
309314
supports-color@^7.1.0:
310315
version "7.2.0"
311316
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-7.2.0.tgz#1b7dcdcb32b8138801b3e478ba6a51caa89648da"

0 commit comments

Comments
 (0)