Prevent the root from being deleted during the build (#4845)

* Prevent the root from being deleted during the build

* Adding a changeset
This commit is contained in:
Matthew Phillips 2022-09-22 16:37:16 -04:00 committed by GitHub
parent 426f3bb40f
commit 3389f0ce91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 80 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Prevent the root folder from being deleted during the build

View file

@ -91,6 +91,7 @@ class AstroBuilder {
serverEntry: 'entry.mjs', serverEntry: 'entry.mjs',
}; };
await runHookBuildStart({ config: this.settings.config, buildConfig, logging: this.logging }); await runHookBuildStart({ config: this.settings.config, buildConfig, logging: this.logging });
this.validateConfig();
info(this.logging, 'build', `output target: ${colors.green(this.settings.config.output)}`); info(this.logging, 'build', `output target: ${colors.green(this.settings.config.output)}`);
if (this.settings.adapter) { if (this.settings.adapter) {
@ -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 */ /** Stats */
private async printStats({ private async printStats({
logging, logging,

View file

@ -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;
}
});
});

View file

@ -0,0 +1,5 @@
import { defineConfig } from 'astro/config';
export default defineConfig({
outDir: './'
});

View file

@ -0,0 +1,6 @@
{
"name": "@test/dont-delete-me",
"dependencies": {
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,8 @@
<html>
<head>
<title>Testing</title>
</head>
<body>
</body>
</html>

View file

@ -1511,6 +1511,12 @@ importers:
dependencies: dependencies:
astro: link:../../.. astro: link:../../..
packages/astro/test/fixtures/dont-delete-me:
specifiers:
astro: workspace:*
dependencies:
astro: link:../../..
packages/astro/test/fixtures/entry-file-names: packages/astro/test/fixtures/entry-file-names:
specifiers: specifiers:
'@astrojs/preact': 'workspace:' '@astrojs/preact': 'workspace:'