diff --git a/.changeset/tidy-bugs-yawn.md b/.changeset/tidy-bugs-yawn.md new file mode 100644 index 000000000000..e395435dcac3 --- /dev/null +++ b/.changeset/tidy-bugs-yawn.md @@ -0,0 +1,5 @@ +--- +'create-astro': minor +--- + +Ensures new line at the end of the generated `package.json` and `tsconfig.json` files diff --git a/packages/create-astro/src/actions/typescript.ts b/packages/create-astro/src/actions/typescript.ts index 169aef7089fe..6fb6c7104af5 100644 --- a/packages/create-astro/src/actions/typescript.ts +++ b/packages/create-astro/src/actions/typescript.ts @@ -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; @@ -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", @@ -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', ); } } diff --git a/packages/create-astro/test/typescript.test.js b/packages/create-astro/test/typescript.test.js index 8787bc509656..b5e09c2c027b 100644 --- a/packages/create-astro/test/typescript.test.js +++ b/packages/create-astro/test/typescript.test.js @@ -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 () => { @@ -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'); }); }); @@ -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' }), );