diff --git a/bin/install.js b/bin/install.js index 5f5db615..e469e386 100755 --- a/bin/install.js +++ b/bin/install.js @@ -12,6 +12,7 @@ import { } from 'node:fs'; import { dirname, join, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; +import { parse, stringify } from 'comment-json'; import sortPackageJson from 'sort-package-json'; const projectPackageJsonPath = join(process.cwd(), 'package.json'); @@ -208,6 +209,42 @@ for (const { // Always overwrite tsconfig.json if (templateFileName === 'tsconfig.json') { overwriteExistingFile = true; + + const projectTsConfig = parse(readFileSync(filePathInProject, 'utf-8')); + const templateTsConfig = parse(readFileSync(templateFilePath, 'utf-8')); + + if ( + projectTsConfig && + templateTsConfig && + typeof projectTsConfig === 'object' && + !Array.isArray(projectTsConfig) && + projectTsConfig.compilerOptions && + typeof projectTsConfig.compilerOptions === 'object' && + !Array.isArray(projectTsConfig.compilerOptions) && + projectTsConfig.compilerOptions.paths && + typeof projectTsConfig.compilerOptions.paths === 'object' && + typeof templateTsConfig === 'object' && + !Array.isArray(templateTsConfig) && + templateTsConfig.compilerOptions && + typeof templateTsConfig.compilerOptions === 'object' && + !Array.isArray(templateTsConfig.compilerOptions) && + !templateTsConfig.compilerOptions.paths + ) { + templateTsConfig.compilerOptions.paths = + projectTsConfig.compilerOptions.paths; + + templateTsConfig.compilerOptions.paths = { + ...projectTsConfig.compilerOptions.paths, + ...templateTsConfig.compilerOptions.paths, + }; + } + + writeFileSync( + filePathInProject, + `${stringify(templateTsConfig, null, 2)}\n`, + ); + console.log('✅ Done merging default tsconfig.json'); + continue; } if (!overwriteExistingFile) { diff --git a/package.json b/package.json index 5cc45a00..379f8561 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "@next/eslint-plugin-next": "14.2.4", "@typescript-eslint/eslint-plugin": "8.0.0-alpha.30", "@typescript-eslint/parser": "8.0.0-alpha.30", + "comment-json": "^4.2.3", "eslint-config-flat-gitignore": "0.1.5", "eslint-import-resolver-typescript": "3.6.1", "eslint-plugin-import-x": "0.5.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index db5d995c..245b583a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -32,6 +32,9 @@ importers: '@typescript-eslint/parser': specifier: 8.0.0-alpha.30 version: 8.0.0-alpha.30(eslint@9.5.0)(typescript@5.5.2) + comment-json: + specifier: ^4.2.3 + version: 4.2.3 eslint: specifier: ^9.5.0 version: 9.5.0 @@ -510,6 +513,9 @@ packages: resolution: {integrity: sha512-itaWrbYbqpGXkGhZPGUulwnhVf5Hpy1xiCFsGqyIGglbBxmG5vSjxQen3/WGOjPpNEv1RtBLKxbmVXm8HpJStQ==} engines: {node: '>= 0.4'} + array-timsort@1.0.3: + resolution: {integrity: sha512-/+3GRL7dDAGEfM6TseQk/U+mi18TU2Ms9I3UlLdUMhz2hbvGNTKdj9xniwXfUqgYhHxRx0+8UnKkvlNwVU+cWQ==} + array-union@2.1.0: resolution: {integrity: sha512-HGyxoOTYUyCM6stUe6EJgnd4EoewAI7zMdfqO+kGjnlZmBDz/cR5pf8r/cR4Wq60sL/p0IkcjUEEPwS3GFrIyw==} engines: {node: '>=8'} @@ -634,6 +640,10 @@ packages: commander@2.20.3: resolution: {integrity: sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==} + comment-json@4.2.3: + resolution: {integrity: sha512-SsxdiOf064DWoZLH799Ata6u7iV658A11PlWtZATDlXPpKGJnbJZ5Z24ybixAi+LUUqJ/GKowAejtC5GFUG7Tw==} + engines: {node: '>= 6'} + concat-map@0.0.1: resolution: {integrity: sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==} @@ -643,6 +653,9 @@ packages: core-js-compat@3.37.1: resolution: {integrity: sha512-9TNiImhKvQqSUkOvk/mMRZzOANTiEVC7WaBNhHcKM7x+/5E1l5NvsysR19zuDQScE8k+kfQXWRN3AtS/eOSHpg==} + core-util-is@1.0.3: + resolution: {integrity: sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==} + cosmiconfig@9.0.0: resolution: {integrity: sha512-itvL5h8RETACmOTFc4UfIyB2RfEHi71Ax6E/PivVxq9NseKbOWpeyHEOIbmAw1rs8Ak0VursQNww7lf7YtUwzg==} engines: {node: '>=14'} @@ -963,6 +976,11 @@ packages: resolution: {integrity: sha512-MWkrWZbJsL2UwnjxTX3gG8FneachS/Mwg7tdGXce011sJd5b0JG54vat5KHnfSBODZ3Wvzd2WnjxyzsRoVv+ww==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} + esprima@4.0.1: + resolution: {integrity: sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==} + engines: {node: '>=4'} + hasBin: true + esquery@1.5.0: resolution: {integrity: sha512-YQLXUplAwJgCydQ78IMJywZCceoqk1oH01OERdSAJc/7U2AylwjhSCLDEtqwg811idIS/9fIU5GjG73IgjKMVg==} engines: {node: '>=0.10'} @@ -1153,6 +1171,10 @@ packages: resolution: {integrity: sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==} engines: {node: '>=8'} + has-own-prop@2.0.0: + resolution: {integrity: sha512-Pq0h+hvsVm6dDEa8x82GnLSYHOzNDt7f0ddFa3FqcQlgzEiptPqL+XrOJNavjOzSYiYWIrgeVYYgGlLmnxwilQ==} + engines: {node: '>=8'} + has-property-descriptors@1.0.2: resolution: {integrity: sha512-55JNKuIW+vq4Ke1BjOTjM2YctQIvCT7GFzHwmfZPGo5wnrgkid0YQtnAleFSqumZm4az3n2BS+erby5ipJdgrg==} @@ -1720,6 +1742,10 @@ packages: resolution: {integrity: sha512-qx+xQGZVsy55CH0a1hiVwHmqjLryfh7wQyF5HO07XJ9f7dQMY/gPQHhlyDkIzJKC+x2fUCpCcUODUUUFrm7SHA==} hasBin: true + repeat-string@1.6.1: + resolution: {integrity: sha512-PV0dzCYDNfRi1jCDbJzpW7jNNDRuCOG/jI5ctQcGKt/clZD+YcPS3yIlWuTJMmESC8aevCFmWJy5wjAFgNqN6w==} + engines: {node: '>=0.10'} + require-from-string@2.0.2: resolution: {integrity: sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==} engines: {node: '>=0.10.0'} @@ -2568,6 +2594,8 @@ snapshots: get-intrinsic: 1.2.4 is-string: 1.0.7 + array-timsort@1.0.3: {} + array-union@2.1.0: {} array.prototype.findlast@1.2.5: @@ -2714,6 +2742,14 @@ snapshots: commander@2.20.3: {} + comment-json@4.2.3: + dependencies: + array-timsort: 1.0.3 + core-util-is: 1.0.3 + esprima: 4.0.1 + has-own-prop: 2.0.0 + repeat-string: 1.6.1 + concat-map@0.0.1: {} convert-source-map@2.0.0: {} @@ -2722,6 +2758,8 @@ snapshots: dependencies: browserslist: 4.23.0 + core-util-is@1.0.3: {} + cosmiconfig@9.0.0(typescript@5.5.2): dependencies: env-paths: 2.2.1 @@ -3249,6 +3287,8 @@ snapshots: acorn-jsx: 5.3.2(acorn@8.11.3) eslint-visitor-keys: 4.0.0 + esprima@4.0.1: {} + esquery@1.5.0: dependencies: estraverse: 5.3.0 @@ -3441,6 +3481,8 @@ snapshots: has-flag@4.0.0: {} + has-own-prop@2.0.0: {} + has-property-descriptors@1.0.2: dependencies: es-define-property: 1.0.0 @@ -3973,6 +4015,8 @@ snapshots: dependencies: jsesc: 0.5.0 + repeat-string@1.6.1: {} + require-from-string@2.0.2: {} resolve-from@4.0.0: {}