-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: cleanup blobs before build (#5413)
- Loading branch information
Showing
24 changed files
with
128 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { rm } from 'node:fs/promises' | ||
|
||
import { anyBlobsToUpload, getBlobsDir } from '../../utils/blobs.js' | ||
|
||
const coreStep = async function ({ buildDir, constants: { PUBLISH_DIR } }) { | ||
const blobsDir = getBlobsDir({ buildDir, publishDir: PUBLISH_DIR }) | ||
try { | ||
await rm(blobsDir, { recursive: true, force: true }) | ||
} catch { | ||
// Ignore errors if it fails, we can continue anyway. | ||
} | ||
|
||
return {} | ||
} | ||
|
||
const blobsPresent = async function ({ buildDir, constants: { PUBLISH_DIR } }): Promise<boolean> { | ||
return await anyBlobsToUpload({ buildDir, publishDir: PUBLISH_DIR }) | ||
} | ||
|
||
export const preCleanup = { | ||
event: 'onPreBuild', | ||
coreStep, | ||
coreStepId: 'pre_cleanup', | ||
coreStepName: 'Pre cleanup', | ||
coreStepDescription: () => 'Cleaning up leftover files from previous builds', | ||
condition: blobsPresent, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
import path from 'node:path' | ||
|
||
import { fdir } from 'fdir' | ||
|
||
const BLOBS_PATH = '.netlify/blobs/deploy' | ||
|
||
export const getBlobsDir = function ({ buildDir, publishDir }) { | ||
return path.resolve(buildDir, publishDir, BLOBS_PATH) | ||
} | ||
|
||
export const anyBlobsToUpload = async function ({ buildDir, publishDir }) { | ||
const blobsDir = getBlobsDir({ buildDir, publishDir }) | ||
const { files } = await new fdir().onlyCounts().crawl(blobsDir).withPromise() | ||
return files > 0 | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/build/tests/blobs_upload/fixtures/src_with_blobs/build.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { mkdir, writeFile } from 'node:fs/promises' | ||
|
||
await mkdir('dist/.netlify/blobs/deploy/nested', { recursive: true }) | ||
|
||
await Promise.all([ | ||
writeFile('dist/.netlify/blobs/deploy/something.txt', 'some value'), | ||
writeFile('dist/.netlify/blobs/deploy/with-metadata.txt', 'another value'), | ||
writeFile('dist/.netlify/blobs/deploy/$with-metadata.txt.json', JSON.stringify({ "meta": "data", "number": 1234 })), | ||
writeFile('dist/.netlify/blobs/deploy/nested/file.txt', 'file value'), | ||
writeFile('dist/.netlify/blobs/deploy/nested/$file.txt.json', JSON.stringify({ "some": "metadata" })), | ||
]) |
1 change: 0 additions & 1 deletion
1
...s/blobs_upload/fixtures/src_with_blobs/dist/.netlify/blobs/deploy/$with-metadata.txt.json
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
...sts/blobs_upload/fixtures/src_with_blobs/dist/.netlify/blobs/deploy/nested/$file.txt.json
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
...ild/tests/blobs_upload/fixtures/src_with_blobs/dist/.netlify/blobs/deploy/nested/file.txt
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
...build/tests/blobs_upload/fixtures/src_with_blobs/dist/.netlify/blobs/deploy/something.txt
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
...d/tests/blobs_upload/fixtures/src_with_blobs/dist/.netlify/blobs/deploy/with-metadata.txt
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
packages/build/tests/blobs_upload/fixtures/src_with_blobs/netlify.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
[build] | ||
command = "node build.mjs" | ||
base = "/" | ||
publish = "/dist" |
8 changes: 8 additions & 0 deletions
8
packages/build/tests/blobs_upload/fixtures/src_with_malformed_blobs_metadata/build.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { mkdir, writeFile } from 'node:fs/promises' | ||
|
||
await mkdir('dist/.netlify/blobs/deploy', { recursive: true }) | ||
|
||
await Promise.all([ | ||
writeFile('dist/.netlify/blobs/deploy/with-metadata.txt', 'another value'), | ||
writeFile('dist/.netlify/blobs/deploy/$with-metadata.txt.json', 'this is not json'), | ||
]) |
1 change: 0 additions & 1 deletion
1
...ures/src_with_malformed_blobs_metadata/dist/.netlify/blobs/deploy/$with-metadata.txt.json
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
...d/fixtures/src_with_malformed_blobs_metadata/dist/.netlify/blobs/deploy/with-metadata.txt
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
packages/build/tests/blobs_upload/fixtures/src_with_malformed_blobs_metadata/netlify.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
[build] | ||
command = "node build.mjs" | ||
base = "/" | ||
publish = "/dist" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/build/tests/pre_cleanup/fixtures/src_empty/netlify.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[build] | ||
base = "/" | ||
publish = "/" |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { access } from 'node:fs/promises' | ||
import { join } from 'path' | ||
|
||
import { Fixture } from '@netlify/testing' | ||
import test from 'ava' | ||
|
||
test('Build removes blobs directory before starting', async (t) => { | ||
const fixture = await new Fixture('./fixtures/with_preexisting_blobs').withCopyRoot({ git: false }) | ||
|
||
const blobsDir = join(fixture.repositoryRoot, 'out', '.netlify', 'blobs', 'deploy') | ||
|
||
await t.notThrowsAsync(access(blobsDir)) | ||
|
||
const { | ||
success, | ||
logs: { stdout }, | ||
} = await fixture | ||
.withFlags({ | ||
cwd: fixture.repositoryRoot, | ||
}) | ||
.runBuildProgrammatic() | ||
|
||
t.true(success) | ||
t.true(stdout.join('\n').includes('Cleaning up leftover files from previous builds')) | ||
|
||
await t.throwsAsync(access(blobsDir)) | ||
}) | ||
|
||
test('Build does not log if there is nothing to cleanup', async (t) => { | ||
const fixture = await new Fixture('./fixtures/src_empty').withCopyRoot({ git: false }) | ||
|
||
const blobsDir = join(fixture.repositoryRoot, 'out', '.netlify', 'blobs', 'deploy') | ||
|
||
await t.throwsAsync(access(blobsDir)) | ||
|
||
const { | ||
success, | ||
logs: { stdout }, | ||
} = await fixture | ||
.withFlags({ | ||
cwd: fixture.repositoryRoot, | ||
}) | ||
.runBuildProgrammatic() | ||
|
||
t.true(success) | ||
t.false(stdout.join('\n').includes('Cleaning up leftover files from previous builds')) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters