diff --git a/.changeset/tame-wasps-exercise.md b/.changeset/tame-wasps-exercise.md new file mode 100644 index 000000000000..660a782d480a --- /dev/null +++ b/.changeset/tame-wasps-exercise.md @@ -0,0 +1,5 @@ +--- +'create-astro': patch +--- + +Ensure an existing template's `package.json` `scripts` are respected when modifying `build`. diff --git a/packages/create-astro/src/actions/typescript.ts b/packages/create-astro/src/actions/typescript.ts index cc00f20e5807..3be99e179cad 100644 --- a/packages/create-astro/src/actions/typescript.ts +++ b/packages/create-astro/src/actions/typescript.ts @@ -96,13 +96,9 @@ const FILES_TO_UPDATE = { // in case of any other template already have astro checks defined, we don't want to override it if (typeof buildScript === 'string' && !buildScript.includes('astro check')) { - const newPackageJson = Object.assign(parsedPackageJson, { - scripts: { - build: 'astro check && ' + buildScript, - }, - }); - - await writeFile(file, JSON.stringify(newPackageJson, null, indent), 'utf-8'); + // Mutate the existing object to avoid changing user-defined script order + parsedPackageJson.scripts.build = `astro check && ${buildScript}`; + await writeFile(file, JSON.stringify(parsedPackageJson, null, indent), 'utf-8'); } } catch (err) { // if there's no package.json (which is very unlikely), then do nothing diff --git a/packages/create-astro/test/fixtures/not-empty/package.json b/packages/create-astro/test/fixtures/not-empty/package.json index 4c5b89162027..dad4a5e5c0f8 100644 --- a/packages/create-astro/test/fixtures/not-empty/package.json +++ b/packages/create-astro/test/fixtures/not-empty/package.json @@ -2,6 +2,8 @@ "name": "@test/create-astro-not-empty", "private": true, "scripts": { - "build": "astro build" + "dev": "astro dev", + "build": "astro check && astro build", + "preview": "astro preview" } } \ No newline at end of file diff --git a/packages/create-astro/test/fixtures/not-empty/tsconfig.json b/packages/create-astro/test/fixtures/not-empty/tsconfig.json index 9e26dfeeb6e6..3fd7ae6e839e 100644 --- a/packages/create-astro/test/fixtures/not-empty/tsconfig.json +++ b/packages/create-astro/test/fixtures/not-empty/tsconfig.json @@ -1 +1,3 @@ -{} \ No newline at end of file +{ + "extends": "astro/tsconfigs/strictest" +} \ No newline at end of file diff --git a/packages/create-astro/test/typescript.test.js b/packages/create-astro/test/typescript.test.js index a6c09af99d2a..30697bbe44d8 100644 --- a/packages/create-astro/test/typescript.test.js +++ b/packages/create-astro/test/typescript.test.js @@ -84,6 +84,8 @@ describe('typescript', () => { }); describe('typescript: setup tsconfig', () => { + beforeEach(() => resetFixtures()); + it('none', async () => { const root = new URL('./fixtures/empty/', import.meta.url); const tsconfig = new URL('./tsconfig.json', root); @@ -92,8 +94,6 @@ describe('typescript: setup tsconfig', () => { expect(JSON.parse(fs.readFileSync(tsconfig, { encoding: 'utf-8' }))).to.deep.eq({ extends: 'astro/tsconfigs/strict', }); - - await resetFixtures(); }); it('exists', async () => { @@ -103,20 +103,18 @@ describe('typescript: setup tsconfig', () => { expect(JSON.parse(fs.readFileSync(tsconfig, { encoding: 'utf-8' }))).to.deep.eq({ extends: 'astro/tsconfigs/strict', }); - - await resetFixtures(); }); }); describe('typescript: setup package', () => { + beforeEach(() => resetFixtures()); + it('none', async () => { const root = new URL('./fixtures/empty/', import.meta.url); const packageJson = new URL('./package.json', root); await setupTypeScript('strictest', { cwd: fileURLToPath(root), install: false }); expect(fs.existsSync(packageJson)).to.be.false; - - await resetFixtures(); }); it('none', async () => { @@ -127,10 +125,12 @@ describe('typescript: setup package', () => { 'astro build' ); await setupTypeScript('strictest', { cwd: fileURLToPath(root), install: false }); - expect(JSON.parse(fs.readFileSync(packageJson, { encoding: 'utf-8' })).scripts.build).to.be.eq( - 'astro check && astro build' + const { scripts } = JSON.parse(fs.readFileSync(packageJson, { encoding: 'utf-8' })); + + expect(Object.keys(scripts)).to.deep.eq(['dev', 'build', 'preview'], 'does not override existing scripts'); + expect(scripts.build).to.eq( + 'astro check && astro build', + 'prepends astro check command' ); - - await resetFixtures(); }); }); diff --git a/packages/create-astro/test/utils.js b/packages/create-astro/test/utils.js index 56ef556050f6..f83769e0f8d9 100644 --- a/packages/create-astro/test/utils.js +++ b/packages/create-astro/test/utils.js @@ -33,18 +33,22 @@ export function setup() { const resetEmptyFixture = () => fs.promises.rm(new URL('./fixtures/empty/tsconfig.json', import.meta.url)); + const resetNotEmptyFixture = async () => { const packagePath = new URL('./fixtures/not-empty/package.json', import.meta.url); const tsconfigPath = new URL('./fixtures/not-empty/tsconfig.json', import.meta.url); + const packageJsonData = JSON.parse(await fs.promises.readFile(packagePath, { encoding: 'utf-8' })); const overriddenPackageJson = Object.assign( - JSON.parse(await fs.promises.readFile(packagePath, { encoding: 'utf-8' })), + packageJsonData, { scripts: { + dev: 'astro dev', build: 'astro build', - }, + preview: 'astro preview' + } } - ); + ) return Promise.all([ fs.promises.writeFile(packagePath, JSON.stringify(overriddenPackageJson, null, 2), {