Skip to content
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

Ensure that the generated package.json and tsconfig.json end with a newline. #12186

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tidy-bugs-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-astro': minor
---

Ensures new line at the end of the generated `package.json` and `tsconfig.json` files
6 changes: 3 additions & 3 deletions packages/create-astro/src/actions/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const FILES_TO_UPDATE = {
parsedPackageJson.dependencies['@astrojs/check'] = `^${astroCheckVersion}`;
parsedPackageJson.dependencies.typescript = `^${typescriptVersion}`;

await writeFile(file, JSON.stringify(parsedPackageJson, null, indent), 'utf-8');
await writeFile(file, JSON.stringify(parsedPackageJson, null, indent) + '\n', 'utf-8');
} catch (err) {
// if there's no package.json (which is very unlikely), then do nothing
if (err && (err as any).code === 'ENOENT') return;
Expand All @@ -124,7 +124,7 @@ const FILES_TO_UPDATE = {
extends: `astro/tsconfigs/${options.value}`,
});

await writeFile(file, JSON.stringify(result, null, 2));
await writeFile(file, JSON.stringify(result, null, 2) + '\n');
} else {
throw new Error(
"There was an error applying the requested TypeScript settings. This could be because the template's tsconfig.json is malformed",
Expand All @@ -135,7 +135,7 @@ const FILES_TO_UPDATE = {
// If the template doesn't have a tsconfig.json, let's add one instead
await writeFile(
file,
JSON.stringify({ extends: `astro/tsconfigs/${options.value}` }, null, 2),
JSON.stringify({ extends: `astro/tsconfigs/${options.value}` }, null, 2) + '\n',
);
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/create-astro/test/typescript.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ describe('typescript: setup tsconfig', async () => {
assert.deepEqual(JSON.parse(fs.readFileSync(tsconfig, { encoding: 'utf-8' })), {
extends: 'astro/tsconfigs/strict',
});
assert(fs.readFileSync(tsconfig, { encoding: 'utf-8' }).endsWith('\n'), 'The file does not end with a newline');
});

it('exists', async () => {
Expand All @@ -100,6 +101,7 @@ describe('typescript: setup tsconfig', async () => {
assert.deepEqual(JSON.parse(fs.readFileSync(tsconfig, { encoding: 'utf-8' })), {
extends: 'astro/tsconfigs/strict',
});
assert(fs.readFileSync(tsconfig, { encoding: 'utf-8' }).endsWith('\n'), 'The file does not end with a newline');
});
});

Expand All @@ -124,6 +126,7 @@ describe('typescript: setup package', async () => {
);

await setupTypeScript('strictest', { cwd: fileURLToPath(root), install: false });
assert(fs.readFileSync(packageJson, { encoding: 'utf-8' }).endsWith('\n'), 'The file does not end with a newline');
const { scripts, dependencies } = JSON.parse(
fs.readFileSync(packageJson, { encoding: 'utf-8' }),
);
Expand Down
Loading