Skip to content

Commit

Permalink
feat: support comments in JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Sep 28, 2020
1 parent f0eb0c2 commit 33a2896
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -52,6 +52,7 @@
"eslint-plugin-prettier": "^3.1.4",
"execa": "^4.0.3",
"inquirer": "^7.3.3",
"json5": "^2.1.3",
"meow": "^7.1.1",
"ncp": "^2.0.0",
"prettier": "^2.1.2",
Expand All @@ -65,6 +66,7 @@
"@types/eslint": "^7.2.3",
"@types/fs-extra": "^9.0.1",
"@types/inquirer": "^7.3.1",
"@types/json5": "0.0.30",
"@types/mocha": "^8.0.3",
"@types/ncp": "^2.0.4",
"@types/node": "^14.11.2",
Expand Down
13 changes: 10 additions & 3 deletions src/util.ts
Expand Up @@ -20,6 +20,7 @@ import * as rimraf from 'rimraf';
import {promisify} from 'util';
import * as ncp from 'ncp';
import * as writeFileAtomic from 'write-file-atomic';
import * as JSON5 from 'json5';

export const readFilep = promisify(fs.readFile);
export const rimrafp = promisify(rimraf);
Expand All @@ -38,7 +39,7 @@ export interface DefaultPackage extends Bag<string> {

export async function readJsonp(jsonPath: string) {
const contents = await readFilep(jsonPath, {encoding: 'utf8'});
return JSON.parse(contents);
return JSON5.parse(contents);
}

export interface ReadFileP {
Expand Down Expand Up @@ -76,7 +77,14 @@ async function getBase(
readFiles.add(filePath);
try {
const json = await customReadFilep(filePath, 'utf8');
let contents = JSON.parse(json);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let contents: any;
try {
contents = JSON5.parse(json);
} catch (e) {
e.message = `Unable to parse ${filePath}!\n${e.message}`;
throw e;
}

if (contents.extends) {
const nextFile = await getBase(
Expand All @@ -85,7 +93,6 @@ async function getBase(
readFiles,
path.dirname(filePath)
);
// eslint-disable-next-line @typescript-eslint/no-use-before-define
contents = combineTSConfig(nextFile, contents);
}

Expand Down
18 changes: 18 additions & 0 deletions test/test-clean.ts
Expand Up @@ -49,6 +49,24 @@ describe('clean', () => {
});
});

it('should gracefully handle JSON with comments', () => {
const invalidJson = `
{
// hah, comments in JSON, what a world
compilerOptions: {outDir: '.'}
}`;
return withFixtures({'tsconfig.json': invalidJson}, async () => {
await clean(OPTIONS);
});
});

it('should gracefully error if tsconfig has invalid JSON', () => {
const invalidJson = "silly bear, this isn't JSON!";
return withFixtures({'tsconfig.json': invalidJson}, async () => {
assert.rejects(clean(OPTIONS), /Unable to parse/);
});
});

it('should avoid deleting .', () => {
return withFixtures(
{'tsconfig.json': JSON.stringify({compilerOptions: {outDir: '.'}})},
Expand Down

0 comments on commit 33a2896

Please sign in to comment.