Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(create-astro): respect existing package.json#scripts #8911

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tame-wasps-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-astro': patch
---

Ensure an existing template's `package.json` `scripts` are respected when modifying `build`.
10 changes: 3 additions & 7 deletions packages/create-astro/src/actions/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion packages/create-astro/test/fixtures/not-empty/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
4 changes: 3 additions & 1 deletion packages/create-astro/test/fixtures/not-empty/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"extends": "astro/tsconfigs/strictest"
}
20 changes: 10 additions & 10 deletions packages/create-astro/test/typescript.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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();
});
});
10 changes: 7 additions & 3 deletions packages/create-astro/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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), {
Expand Down
Loading