Skip to content

Commit ad5f0e9

Browse files
committed
fix(gatsby): fix eperm when cache getting cleared
1 parent ae999cf commit ad5f0e9

File tree

4 files changed

+244
-219
lines changed

4 files changed

+244
-219
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"use strict";
2+
3+
// Prefixes should be globs (i.e. of the form "/*" or "/foo/*")
4+
const validatePrefixEntry = prefix => {
5+
if (!prefix.match(/^\//) || !prefix.match(/\/\*$/)) {
6+
throw Error(`Plugin "gatsby-plugin-client-only-paths" found invalid prefix pattern: ${prefix}`);
7+
}
8+
};
9+
10+
exports.onCreatePage = ({
11+
page,
12+
actions
13+
}, {
14+
prefixes
15+
}) => {
16+
const {
17+
createPage
18+
} = actions;
19+
const re = {};
20+
prefixes.forEach(validatePrefixEntry); // Don't set matchPath again if it's already been set.
21+
22+
if (page.matchPath || page.path.match(/dev-404-page/)) {
23+
return;
24+
}
25+
26+
prefixes.forEach(prefix => {
27+
if (!re[prefix]) {
28+
// Remove the * from the prefix and memoize
29+
const trimmedPrefix = prefix.replace(/\*$/, ``);
30+
re[prefix] = new RegExp(`^${trimmedPrefix}`);
31+
} // Ensure that the path ends in a trailing slash, since it can be removed.
32+
33+
34+
const path = page.path.match(/\/$/) ? page.path : `${page.path}/`;
35+
36+
if (path.match(re[prefix])) {
37+
page.matchPath = prefix.replace(/\*$/, `*`);
38+
createPage(page);
39+
}
40+
});
41+
};

packages/gatsby/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
"date-fns": "^2.25.0",
5656
"debug": "^3.2.7",
5757
"deepmerge": "^4.2.2",
58-
"del": "^5.1.0",
5958
"detect-port": "^1.3.0",
6059
"devcert": "^1.2.0",
6160
"dotenv": "^8.6.0",
@@ -93,6 +92,7 @@
9392
"gatsby-telemetry": "^3.11.0-next.1",
9493
"gatsby-worker": "^1.11.0-next.0",
9594
"glob": "^7.2.0",
95+
"globby": "^13.1.1",
9696
"got": "^11.8.2",
9797
"graphql": "^15.7.2",
9898
"graphql-compose": "^9.0.7",

packages/gatsby/src/services/initialize.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as fs from "fs-extra"
44
import { releaseAllMutexes } from "gatsby-core-utils/mutex"
55
import md5File from "md5-file"
66
import crypto from "crypto"
7-
import del from "del"
7+
import { globby as glob } from "globby"
88
import path from "path"
99
import telemetry from "gatsby-telemetry"
1010

@@ -284,12 +284,18 @@ export async function initialize({
284284
}
285285
)
286286
activity.start()
287-
await del([
288-
`public/**/*.{html,css}`,
289-
`!public/page-data/**/*`,
290-
`!public/static`,
291-
`!public/static/**/*.{html,css}`,
292-
])
287+
const files = await glob(
288+
[
289+
`public/**/*.{html,css}`,
290+
`!public/page-data/**/*`,
291+
`!public/static`,
292+
`!public/static/**/*.{html,css}`,
293+
],
294+
{
295+
cwd: program.directory,
296+
}
297+
)
298+
await Promise.all(files.map(file => fs.remove(file)))
293299
activity.end()
294300
}
295301

@@ -429,28 +435,30 @@ export async function initialize({
429435

430436
const deleteGlobs = [
431437
// By default delete all files & subdirectories
432-
`${cacheDirectory}/**`,
433-
`!${cacheDirectory}/data`,
434-
`${cacheDirectory}/data/**`,
435-
`!${cacheDirectory}/data/gatsby-core-utils/`,
436-
`!${cacheDirectory}/data/gatsby-core-utils/**`,
437-
`!${cacheDirectory}/compiled`,
438+
`.cache/**`,
439+
`.cache/data/**`,
440+
`!.cache/data/gatsby-core-utils/**`,
441+
`!.cache/compiled`,
438442
]
439443

440444
if (process.env.GATSBY_EXPERIMENTAL_PRESERVE_FILE_DOWNLOAD_CACHE) {
441445
// Stop the caches directory from being deleted, add all sub directories,
442446
// but remove gatsby-source-filesystem
443-
deleteGlobs.push(`!${cacheDirectory}/caches`)
444-
deleteGlobs.push(`${cacheDirectory}/caches/*`)
445-
deleteGlobs.push(`!${cacheDirectory}/caches/gatsby-source-filesystem`)
447+
deleteGlobs.push(`!.cache/caches`)
448+
deleteGlobs.push(`.cache/caches/*`)
449+
deleteGlobs.push(`!.cache/caches/gatsby-source-filesystem`)
446450
}
447451

448452
if (process.env.GATSBY_EXPERIMENTAL_PRESERVE_WEBPACK_CACHE) {
449453
// Add webpack
450-
deleteGlobs.push(`!${cacheDirectory}/webpack`)
454+
deleteGlobs.push(`!.cache/webpack`)
451455
}
452456

453-
await del(deleteGlobs)
457+
const files = await glob(deleteGlobs, {
458+
cwd: program.directory,
459+
})
460+
461+
await Promise.all(files.map(file => fs.remove(file)))
454462
} catch (e) {
455463
reporter.error(`Failed to remove .cache files.`, e)
456464
}

0 commit comments

Comments
 (0)