-
Notifications
You must be signed in to change notification settings - Fork 11
Prevent Complete tsconfig file Overwriting #388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7d7e4a0
5ab451c
c76c612
fec4e5e
eccf7cb
dc6ae3c
d16c3d5
3f6af5a
8f434da
3e3ecd5
6e76a68
df161e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+216
to
+231
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplify validation logic and fix incomplete merge condition. The validation has two issues:
Fix the merge logic to handle all cases: +import isPlainObj from 'is-plain-obj';
+
// ... in the tsconfig.json handling block ...
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
+ isPlainObj(projectTsConfig) &&
+ isPlainObj(projectTsConfig.compilerOptions) &&
+ isPlainObj(projectTsConfig.compilerOptions.paths) &&
+ isPlainObj(templateTsConfig) &&
+ isPlainObj(templateTsConfig.compilerOptions)
) {
- templateTsConfig.compilerOptions.paths =
- projectTsConfig.compilerOptions.paths;
-
- templateTsConfig.compilerOptions.paths = {
- ...projectTsConfig.compilerOptions.paths,
- ...templateTsConfig.compilerOptions.paths,
- };
+ const { assign } = await import('comment-json');
+
+ if (isPlainObj(templateTsConfig.compilerOptions.paths)) {
+ // Template has paths - merge with project paths (template takes precedence)
+ templateTsConfig.compilerOptions.paths = assign(
+ {},
+ projectTsConfig.compilerOptions.paths,
+ templateTsConfig.compilerOptions.paths
+ );
+ } else {
+ // Template has no paths - preserve project paths
+ templateTsConfig.compilerOptions.paths = assign(
+ {},
+ projectTsConfig.compilerOptions.paths
+ );
+ }
}Note: You'll also need to add As per coding guidelines (referenced by karlhorky). 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| templateTsConfig.compilerOptions.paths = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| projectTsConfig.compilerOptions.paths; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+233
to
+234
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove redundant assignment. Lines 233-234 assign the project paths to template paths, but this assignment is immediately overwritten by lines 236-239. Since the condition on line 231 ensures the template has no paths, this assignment serves no purpose. Apply this diff to remove the redundant code: - templateTsConfig.compilerOptions.paths =
- projectTsConfig.compilerOptions.paths;
-
templateTsConfig.compilerOptions.paths = {
...projectTsConfig.compilerOptions.paths,
...templateTsConfig.compilerOptions.paths,
};Note: This comment applies to the current implementation. If you apply the refactor suggested for lines 216-231, this entire section will be replaced with the corrected merge logic. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| templateTsConfig.compilerOptions.paths = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...projectTsConfig.compilerOptions.paths, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...templateTsConfig.compilerOptions.paths, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+236
to
+239
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Object spread loses comment metadata. Using the object spread operator ( Per the comment-json documentation, you must use + const { assign } = await import('comment-json');
+
templateTsConfig.compilerOptions.paths =
- {
- ...projectTsConfig.compilerOptions.paths,
- ...templateTsConfig.compilerOptions.paths,
- };
+ assign(
+ {},
+ projectTsConfig.compilerOptions.paths,
+ templateTsConfig.compilerOptions.paths
+ );Note: This comment applies to the current implementation. If you apply the refactor suggested for lines 216-231, this section will be replaced with the corrected merge logic that already uses Based on learnings. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writeFileSync( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| filePathInProject, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| `${stringify(templateTsConfig, null, 2)}\n`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log('✅ Done merging default tsconfig.json'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!overwriteExistingFile) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add error handling for JSON parsing.
The
parse()calls can throw exceptions if the JSON files are malformed or contain syntax errors. This could crash the installation process without helpful error messages.Apply this diff to add error handling:
📝 Committable suggestion
🤖 Prompt for AI Agents