0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-13 22:11:20 -05:00

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

This commit is contained in:
Takahito Sueda 2024-10-11 21:47:46 +09:00 committed by GitHub
parent 1f93fca1d3
commit 49c4f64673
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 11 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'create-astro': minor
---
Ensures new line at the end of the generated `package.json` and `tsconfig.json` files

View file

@ -108,7 +108,7 @@ const FILES_TO_UPDATE = {
parsedPackageJson.dependencies['@astrojs/check'] = `^${astroCheckVersion}`; parsedPackageJson.dependencies['@astrojs/check'] = `^${astroCheckVersion}`;
parsedPackageJson.dependencies.typescript = `^${typescriptVersion}`; 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) { } catch (err) {
// if there's no package.json (which is very unlikely), then do nothing // if there's no package.json (which is very unlikely), then do nothing
if (err && (err as any).code === 'ENOENT') return; if (err && (err as any).code === 'ENOENT') return;
@ -124,7 +124,7 @@ const FILES_TO_UPDATE = {
extends: `astro/tsconfigs/${options.value}`, extends: `astro/tsconfigs/${options.value}`,
}); });
await writeFile(file, JSON.stringify(result, null, 2)); await writeFile(file, JSON.stringify(result, null, 2) + '\n');
} else { } else {
throw new Error( throw new Error(
"There was an error applying the requested TypeScript settings. This could be because the template's tsconfig.json is malformed", "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 // If the template doesn't have a tsconfig.json, let's add one instead
await writeFile( await writeFile(
file, file,
JSON.stringify({ extends: `astro/tsconfigs/${options.value}` }, null, 2), JSON.stringify({ extends: `astro/tsconfigs/${options.value}` }, null, 2) + '\n',
); );
} }
} }

View file

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