diff --git a/package.json b/package.json index 7bce6a353..065035e62 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "glob": "^7.1.6", "inquirer": "^7.0.1", "is-git-dirty": "^1.0.0", + "json5": "^2.1.1", "metro-react-native-babel-preset": "^0.57.0", "yargs": "^15.0.2" }, @@ -53,6 +54,7 @@ "@types/fs-extra": "^8.0.1", "@types/glob": "^7.1.1", "@types/inquirer": "^6.5.0", + "@types/json5": "^0.0.30", "@types/yargs": "^13.0.3", "commitlint": "^8.2.0", "eslint": "^6.7.2", diff --git a/src/targets/typescript.ts b/src/targets/typescript.ts index d5b8522f8..13e277708 100644 --- a/src/targets/typescript.ts +++ b/src/targets/typescript.ts @@ -3,6 +3,7 @@ import path from 'path'; import child_process from 'child_process'; import fs from 'fs-extra'; import del from 'del'; +import JSON5 from 'json5'; import { platform } from 'os'; import { Input } from '../types'; @@ -19,37 +20,47 @@ export default async function build({ root, output, report }: Input) { try { if (await fs.pathExists(tsconfig)) { - const config = JSON.parse(await fs.readFile(tsconfig, 'utf-8')); - - if (config.compilerOptions) { - const conflicts: string[] = []; - - if (config.compilerOptions.noEmit !== undefined) { - conflicts.push('compilerOptions.noEmit'); - } - - if (config.compilerOptions.emitDeclarationOnly !== undefined) { - conflicts.push('compilerOptions.emitDeclarationOnly'); - } - - if (config.compilerOptions.outDir) { - conflicts.push('compilerOptions.outDir'); - } - - if (config.compilerOptions.declarationDir) { - conflicts.push('compilerOptions.declarationDir'); - } - - if (conflicts.length) { - report.warn( - `Found following options in the config file which can conflict with the CLI options. Please remove them from ${chalk.blue( - 'tsconfig.json' - )}:${conflicts.reduce( - (acc, curr) => acc + `\n${chalk.gray('-')} ${chalk.yellow(curr)}`, - '' - )}` - ); + try { + const config = JSON5.parse(await fs.readFile(tsconfig, 'utf-8')); + + if (config.compilerOptions) { + const conflicts: string[] = []; + + if (config.compilerOptions.noEmit !== undefined) { + conflicts.push('compilerOptions.noEmit'); + } + + if (config.compilerOptions.emitDeclarationOnly !== undefined) { + conflicts.push('compilerOptions.emitDeclarationOnly'); + } + + if (config.compilerOptions.declarationDir) { + conflicts.push('compilerOptions.declarationDir'); + } + + if ( + config.compilerOptions.outDir && + path.join(root, config.compilerOptions.outDir) !== output + ) { + conflicts.push('compilerOptions.outDir'); + } + + if (conflicts.length) { + report.warn( + `Found following options in the config file which can conflict with the CLI options. Please remove them from ${chalk.blue( + 'tsconfig.json' + )}:${conflicts.reduce( + (acc, curr) => + acc + `\n${chalk.gray('-')} ${chalk.yellow(curr)}`, + '' + )}` + ); + } } + } catch (e) { + report.warn( + `Couldn't parse 'tsconfig.json'. There might be validation errors.` + ); } } @@ -73,6 +84,8 @@ export default async function build({ root, output, report }: Input) { output, ]); + await del([path.join(output, 'tsconfig.tsbuildinfo')]); + report.success( `Wrote definition files to ${chalk.blue(path.relative(root, output))}` ); diff --git a/yarn.lock b/yarn.lock index 0c21bea47..d8ebb149a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1570,6 +1570,11 @@ resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.3.tgz#bdfd69d61e464dcc81b25159c270d75a73c1a636" integrity sha512-Il2DtDVRGDcqjDtE+rF8iqg1CArehSK84HZJCT7AMITlyXRBpuPhqGLDQMowraqqu1coEaimg4ZOqggt6L6L+A== +"@types/json5@^0.0.30": + version "0.0.30" + resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.30.tgz#44cb52f32a809734ca562e685c6473b5754a7818" + integrity sha512-sqm9g7mHlPY/43fcSNrCYfOeX9zkTTK+euO5E6+CVijSMm5tTjkVdwdqRkY3ljjIAf8679vps5jKUoJBCLsMDA== + "@types/minimatch@*": version "3.0.3" resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d" @@ -4322,6 +4327,13 @@ json5@^2.1.0: dependencies: minimist "^1.2.0" +json5@^2.1.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/json5/-/json5-2.1.1.tgz#81b6cb04e9ba496f1c7005d07b4368a2638f90b6" + integrity sha512-l+3HXD0GEI3huGq1njuqtzYK8OYJyXMkOLtQ53pjWh89tvWS2h6l+1zMkYWqlb57+SiQodKZyvMEFb2X+KrFhQ== + dependencies: + minimist "^1.2.0" + jsonfile@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"