Skip to content

Commit

Permalink
Ensure that the generated package.json and tsconfig.json end with a n…
Browse files Browse the repository at this point in the history
…ewline. (#12186)
  • Loading branch information
Terfno authored Oct 11, 2024
1 parent 1f93fca commit 49c4f64
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
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

0 comments on commit 49c4f64

Please sign in to comment.