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:
parent
426f3bb40f
commit
3389f0ce91
7 changed files with 80 additions and 0 deletions
5
.changeset/hot-lamps-search.md
Normal file
5
.changeset/hot-lamps-search.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Prevent the root folder from being deleted during the build
|
|
@ -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) {
|
||||
|
@ -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,
|
||||
|
|
40
packages/astro/test/dont-delete-root.test.js
Normal file
40
packages/astro/test/dont-delete-root.test.js
Normal 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;
|
||||
}
|
||||
});
|
||||
});
|
5
packages/astro/test/fixtures/dont-delete-me/astro.config.mjs
vendored
Normal file
5
packages/astro/test/fixtures/dont-delete-me/astro.config.mjs
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
import { defineConfig } from 'astro/config';
|
||||
|
||||
export default defineConfig({
|
||||
outDir: './'
|
||||
});
|
6
packages/astro/test/fixtures/dont-delete-me/package.json
vendored
Normal file
6
packages/astro/test/fixtures/dont-delete-me/package.json
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"name": "@test/dont-delete-me",
|
||||
"dependencies": {
|
||||
"astro": "workspace:*"
|
||||
}
|
||||
}
|
8
packages/astro/test/fixtures/dont-delete-me/src/pages/index.astro
vendored
Normal file
8
packages/astro/test/fixtures/dont-delete-me/src/pages/index.astro
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>Testing</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -1511,6 +1511,12 @@ importers:
|
|||
dependencies:
|
||||
astro: link:../../..
|
||||
|
||||
packages/astro/test/fixtures/dont-delete-me:
|
||||
specifiers:
|
||||
astro: workspace:*
|
||||
dependencies:
|
||||
astro: link:../../..
|
||||
|
||||
packages/astro/test/fixtures/entry-file-names:
|
||||
specifiers:
|
||||
'@astrojs/preact': 'workspace:'
|
||||
|
|
Loading…
Reference in a new issue