Skip to content

Commit

Permalink
fix(update-files): tsconfig needed to be deep merged
Browse files Browse the repository at this point in the history
I tried the existing utility (@typescript-eslint/utils) for merging, but that balloned the cli.js to
110k lines, so I chose to use lodash for the merge, as it was already a peer dependency. I am sorry
if there was another merging utility already in place, I didn't find it :(

fix #6058
  • Loading branch information
Twiggeh committed Mar 27, 2024
1 parent 921aceb commit 18200bb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"@types/cross-spawn": "^6.0.6",
"@types/eslint": "^8.56.6",
"@types/express": "^4.17.21",
"@types/lodash": "^4.17.0",
"@types/node": "^20.11.30",
"@types/path-browserify": "^1.0.2",
"@types/prompts": "^2.4.9",
Expand All @@ -97,6 +98,7 @@
"execa": "8.0.1",
"express": "4.18.3",
"install": "^0.13.0",
"lodash": "^4.17.21",
"monaco-editor": "^0.45.0",
"mri": "1.2.0",
"path-browserify": "1.0.1",
Expand Down
11 changes: 8 additions & 3 deletions packages/qwik/src/cli/add/update-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'node:fs';
import type { FsUpdates, UpdateAppOptions } from '../types';
import { extname, join } from 'node:path';
import { getPackageManager } from '../utils/utils';
import mergeWith from 'lodash/mergeWith';

export async function mergeIntegrationDir(
fileUpdates: FsUpdates,
Expand All @@ -23,7 +24,7 @@ export async function mergeIntegrationDir(
} else if (s.isFile()) {
if (destName === 'package.json') {
await mergePackageJsons(fileUpdates, srcChildPath, destChildPath);
} else if (destName === 'settings.json') {
} else if (destName === 'settings.json' || destName === 'tsconfig.json') {
await mergeJsons(fileUpdates, srcChildPath, destChildPath);
} else if (destName === 'README.md') {
await mergeReadmes(fileUpdates, srcChildPath, destChildPath);
Expand Down Expand Up @@ -86,11 +87,15 @@ async function mergeJsons(fileUpdates: FsUpdates, srcPath: string, destPath: str
try {
const srcPkgJson = JSON.parse(srcContent);
const destPkgJson = JSON.parse(await fs.promises.readFile(destPath, 'utf-8'));
Object.assign(srcPkgJson, destPkgJson);
const mergedJson = mergeWith(srcPkgJson, destPkgJson, (value, srcValue) => {
if (Array.isArray(value)) {
return value.concat(srcValue);
}
});

fileUpdates.files.push({
path: destPath,
content: JSON.stringify(srcPkgJson, null, 2) + '\n',
content: JSON.stringify(mergedJson, null, 2) + '\n',
type: 'modify',
});
} catch (e) {
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 18200bb

Please sign in to comment.