Respect original package.json indentation (#6375)

* fix(#6338): respect original indentation

* chore: add changeset
This commit is contained in:
Nate Moore 2023-02-27 10:09:54 -06:00 committed by GitHub
parent 71743aeca7
commit 754c5ca9aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'create-astro': patch
---
Respect original `package.json` indentation

View file

@ -50,17 +50,19 @@ const FILES_TO_UPDATE = {
'package.json': (file: string, overrides: { name: string }) => 'package.json': (file: string, overrides: { name: string }) =>
fs.promises fs.promises
.readFile(file, 'utf-8') .readFile(file, 'utf-8')
.then((value) => .then((value) => {
// Match first indent in the file or fallback to `\t`
const indent = /(^\s+)/m.exec(value)?.[1] ?? '\t';
fs.promises.writeFile( fs.promises.writeFile(
file, file,
JSON.stringify( JSON.stringify(
Object.assign(JSON.parse(value), Object.assign(overrides, { private: undefined })), Object.assign(JSON.parse(value), Object.assign(overrides, { private: undefined })),
null, null,
'\t' indent
), ),
'utf-8' 'utf-8'
) )
), }),
}; };
export default async function copyTemplate(tmpl: string, ctx: Context) { export default async function copyTemplate(tmpl: string, ctx: Context) {