Skip to content

Commit

Permalink
Prevent the root from being deleted during the build
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp committed Sep 22, 2022
1 parent e9eb4d1 commit 18b81b4
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class AstroBuilder {
serverEntry: 'entry.mjs',
};
await runHookBuildStart({ config: this.settings.config, buildConfig, logging: this.logging });
this.validateConfig();

info(this.logging, 'build', `output target: ${colors.green(this.settings.config.output)}`);
if (this.settings.adapter) {
Expand Down Expand Up @@ -171,6 +172,15 @@ class AstroBuilder {
}
}

private validateConfig() {
const { config } = this.settings;

// outDir gets blown away so it can't be the root.
if(config.outDir.toString() === config.root.toString()) {
throw new Error(`the outDir cannot be the root folder. Please build to a folder such as dist.`);
}
}

/** Stats */
private async printStats({
logging,
Expand Down
40 changes: 40 additions & 0 deletions packages/astro/test/dont-delete-root.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { expect } from 'chai';
import { loadFixture } from './test-utils.js';
import * as fs from 'fs';

describe('outDir set to project root', async () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

/** @type {Error | undefined} */
let error;

before(async () => {
fixture = await loadFixture({ root: './fixtures/dont-delete-me/' });
try {
await fixture.build();
} catch(err) {
error = err;
}
});

it('Throws an error when you attempt to build', async () => {
expect(error).to.be.an.instanceOf(Error);
expect(error.message).to.match(/outDir cannot be the root folder/);
});

it('Files have not been deleted', async () => {
const expectedFiles = [
'package.json',
'astro.config.mjs',
'src/pages/index.astro'
];

for(const rel of expectedFiles) {
const root = new URL('./fixtures/dont-delete-me/', import.meta.url);
const url = new URL('./' + rel, root);
const stats = await fs.promises.stat(url);
expect(stats).to.not.be.undefined;
}
});
});
5 changes: 5 additions & 0 deletions packages/astro/test/fixtures/dont-delete-me/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from 'astro/config';

export default defineConfig({
outDir: './'
});
6 changes: 6 additions & 0 deletions packages/astro/test/fixtures/dont-delete-me/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@test/dont-delete-me",
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Testing</title>
</head>
<body>

</body>
</html>
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 18b81b4

Please sign in to comment.