Skip to content

Commit 8229a89

Browse files
authored
fix(starter): fix up vscode settings merge 🐼 (#7858)
* fix(starter): fix up vscode settings merge πŸš€ * chore: change changeset 🍟 * chore: add json5.parse * chore: use @croct/json5-parser * chore: fix up linter * chore: fix up linter * test: improve test to cover the new implementation
1 parent 89b5f3a commit 8229a89

File tree

5 files changed

+57
-7
lines changed

5 files changed

+57
-7
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'create-qwik': patch
3+
---
4+
5+
FIX: fix up vscode settings merge. Use JSON5 to parse settings.json to prevent parsing errors.

β€Žpackages/qwik/package.jsonβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
},
3131
"devDependencies": {
3232
"@builder.io/qwik": "workspace:^",
33+
"@croct/json5-parser": "0.1.1",
3334
"domino": "2.1.6",
3435
"ignore": "5.3.1",
3536
"image-size": "1.1.1",

β€Žpackages/qwik/src/cli/add/update-files.tsβ€Ž

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { JsonParser, JsonObjectNode } from '@croct/json5-parser';
12
import fs from 'node:fs';
23
import { extname, join } from 'node:path';
34
import type { FsUpdates, UpdateAppOptions } from '../types';
@@ -28,8 +29,8 @@ export async function mergeIntegrationDir(
2829

2930
if (destName === 'package.json') {
3031
await mergePackageJsons(fileUpdates, srcChildPath, destRootPath);
31-
} else if (destName === 'settings.json') {
32-
await mergeJsons(fileUpdates, srcChildPath, finalDestPath);
32+
} else if (destDir.endsWith('.vscode') && destName === 'settings.json') {
33+
await mergeVSCodeSettings(fileUpdates, srcChildPath, finalDestPath);
3334
} else if (destName === 'README.md') {
3435
await mergeReadmes(fileUpdates, srcChildPath, finalDestPath);
3536
} else if (
@@ -110,16 +111,19 @@ async function mergePackageJsons(fileUpdates: FsUpdates, srcPath: string, destPa
110111
}
111112
}
112113

113-
async function mergeJsons(fileUpdates: FsUpdates, srcPath: string, destPath: string) {
114+
async function mergeVSCodeSettings(fileUpdates: FsUpdates, srcPath: string, destPath: string) {
114115
const srcContent = await fs.promises.readFile(srcPath, 'utf-8');
115116
try {
116-
const srcPkgJson = JSON.parse(srcContent);
117-
const destPkgJson = JSON.parse(await fs.promises.readFile(destPath, 'utf-8'));
118-
Object.assign(srcPkgJson, destPkgJson);
117+
const srcPkgJson = JsonParser.parse(srcContent, JsonObjectNode);
118+
const destPkgJson = JsonParser.parse(
119+
await fs.promises.readFile(destPath, 'utf-8'),
120+
JsonObjectNode
121+
);
122+
destPkgJson.update({ ...destPkgJson.toJSON(), ...srcPkgJson.toJSON() });
119123

120124
fileUpdates.files.push({
121125
path: destPath,
122-
content: JSON.stringify(srcPkgJson, null, 2) + '\n',
126+
content: destPkgJson.toString() + '\n',
123127
type: 'modify',
124128
});
125129
} catch (e) {

β€Žpackages/qwik/src/cli/add/update-files.unit.tsβ€Ž

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { JsonObjectNode, JsonParser } from '@croct/json5-parser';
12
import { fs } from 'memfs';
23
import { join } from 'path';
34
import { describe, expect, test, vi } from 'vitest';
@@ -39,6 +40,16 @@ function createFakeFiles(dir: string) {
3940
fs.writeFileSync(join(dir, 'fake.ts'), 'fake file');
4041
fs.writeFileSync(join(dir, 'package.json'), '{"name": "fake"}');
4142
fs.writeFileSync(join(dir, 'src', 'global.css'), 'p{color: red}');
43+
fs.mkdirSync(join(dir, '.vscode'), { recursive: true });
44+
const settings = JsonParser.parse<JsonObjectNode>(
45+
`{
46+
// Comment
47+
"name": "John Doe",
48+
"age": 42,
49+
}`,
50+
JsonObjectNode
51+
);
52+
fs.writeFileSync(join(dir, '.vscode', 'settings.json'), settings.toString());
4253
}
4354

4455
describe('mergeIntegrationDir', () => {
@@ -51,6 +62,7 @@ describe('mergeIntegrationDir', () => {
5162
const expectedResults = [
5263
'destDir/subDestDir/fake.ts',
5364
'destDir/subDestDir/package.json',
65+
'destDir/subDestDir/.vscode/settings.json',
5466
'destDir/subDestDir/src/global.css',
5567
];
5668

@@ -64,6 +76,18 @@ describe('mergeIntegrationDir', () => {
6476
const monorepoSubDir = join(fakeDestDir, 'apps', 'subpackage', 'src');
6577
fs.mkdirSync(monorepoSubDir, { recursive: true });
6678
fs.writeFileSync(join(monorepoSubDir, 'global.css'), '/* CSS */');
79+
const settings = JsonParser.parse<JsonObjectNode>(
80+
`{
81+
// Comment Foo
82+
"css.lint.unknownAtRules": "ignore"
83+
}`,
84+
JsonObjectNode
85+
);
86+
fs.mkdirSync(join(fakeDestDir, 'apps', 'subpackage', '.vscode'));
87+
fs.writeFileSync(
88+
join(fakeDestDir, 'apps', 'subpackage', '.vscode', 'settings.json'),
89+
settings.toString()
90+
);
6791

6892
// Add a file that should stay in the root
6993
fs.writeFileSync(join(fakeSrcDir, 'should-stay-in-root.ts'), 'fake file');
@@ -84,6 +108,7 @@ describe('mergeIntegrationDir', () => {
84108
`destDir/subDestDir/should-stay-in-root.ts`,
85109
`destDir/subDestDir/package.json`,
86110
`destDir/subDestDir/should-stay/should-also-stay.ts`,
111+
'destDir/subDestDir/apps/subpackage/.vscode/settings.json',
87112
`destDir/subDestDir/apps/subpackage/src/global.css`,
88113
];
89114

β€Žpnpm-lock.yamlβ€Ž

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
Β (0)