Exclude cache from being deleted in dev (#9448)
* Exclude cache from being deleted in dev * Exclude cache directory from delete * Fix clean regex and update tests * Remove extra param comment
This commit is contained in:
parent
4c549391de
commit
10066aac9a
5 changed files with 40 additions and 20 deletions
|
@ -34,22 +34,20 @@ const unlinkFile = async (p: string, t = 1): Promise<void> => {
|
|||
/**
|
||||
* Recursively delete directory contents
|
||||
* @param {string} dir Directory to delete the contents of
|
||||
* @param {RegExp} [filter] Filter for the relative file path
|
||||
* @param {boolean} [ensure] Ensures that parameter dir exists, this is not passed recursively
|
||||
* @param {RegExp} [exclude] Exclude based on relative file path
|
||||
* @param {string} [previousPath] Ensures that parameter dir exists, this is not passed recursively
|
||||
* @returns Promise void
|
||||
*/
|
||||
export async function recursiveDelete(
|
||||
dir: string,
|
||||
filter?: RegExp,
|
||||
previousPath: string = '',
|
||||
ensure?: boolean
|
||||
exclude?: RegExp,
|
||||
previousPath: string = ''
|
||||
): Promise<void> {
|
||||
let result
|
||||
try {
|
||||
result = await readdir(dir)
|
||||
} catch (e) {
|
||||
if (e.code === 'ENOENT' && !ensure) {
|
||||
if (e.code === 'ENOENT') {
|
||||
return
|
||||
}
|
||||
throw e
|
||||
|
@ -65,17 +63,13 @@ export async function recursiveDelete(
|
|||
return
|
||||
}
|
||||
|
||||
if (pathStat.isDirectory()) {
|
||||
const pp = join(previousPath, part)
|
||||
await recursiveDelete(absolutePath, filter, pp)
|
||||
|
||||
if (!filter || filter.test(pp)) {
|
||||
return rmdir(absolutePath)
|
||||
}
|
||||
return
|
||||
const pp = join(previousPath, part)
|
||||
if (pathStat.isDirectory() && (!exclude || !exclude.test(pp))) {
|
||||
await recursiveDelete(absolutePath, exclude, pp)
|
||||
return rmdir(absolutePath)
|
||||
}
|
||||
|
||||
if (!filter || filter.test(join(previousPath, part))) {
|
||||
if (!exclude || !exclude.test(pp)) {
|
||||
return unlinkFile(absolutePath)
|
||||
}
|
||||
})
|
||||
|
|
|
@ -236,7 +236,7 @@ export default class HotReloader {
|
|||
}
|
||||
|
||||
async clean() {
|
||||
return recursiveDelete(join(this.dir, this.config.distDir))
|
||||
return recursiveDelete(join(this.dir, this.config.distDir), /^cache/)
|
||||
}
|
||||
|
||||
async getWebpackConfig() {
|
||||
|
|
0
test/isolated/_resolvedata/aa/cache.js
Normal file
0
test/isolated/_resolvedata/aa/cache.js
Normal file
1
test/isolated/_resolvedata/cache/test.txt
vendored
Normal file
1
test/isolated/_resolvedata/cache/test.txt
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
hello world
|
|
@ -6,13 +6,38 @@ import { join } from 'path'
|
|||
|
||||
const resolveDataDir = join(__dirname, '..', 'isolated', '_resolvedata')
|
||||
const testResolveDataDir = join(__dirname, '..', 'isolated', 'test_resolvedata')
|
||||
const testpreservefileDir = join(__dirname, '..', 'isolated', 'preservefiles')
|
||||
|
||||
describe('recursiveDelete', () => {
|
||||
it('should work', async () => {
|
||||
await recursiveCopy(resolveDataDir, testResolveDataDir)
|
||||
expect.assertions(1)
|
||||
try {
|
||||
await recursiveCopy(resolveDataDir, testResolveDataDir)
|
||||
await recursiveDelete(testResolveDataDir)
|
||||
const result = await recursiveReadDir(testResolveDataDir, /.*/)
|
||||
expect(result.length).toBe(0)
|
||||
} finally {
|
||||
await recursiveDelete(testResolveDataDir)
|
||||
}
|
||||
})
|
||||
|
||||
await recursiveDelete(testResolveDataDir)
|
||||
const result = await recursiveReadDir(testResolveDataDir, /.*/)
|
||||
expect(result.length).toBe(0)
|
||||
it('should exclude', async () => {
|
||||
expect.assertions(2)
|
||||
try {
|
||||
await recursiveCopy(resolveDataDir, testpreservefileDir, {
|
||||
overwrite: true,
|
||||
})
|
||||
// preserve cache dir
|
||||
await recursiveDelete(testpreservefileDir, /^cache/)
|
||||
|
||||
const result = await recursiveReadDir(testpreservefileDir, /.*/)
|
||||
expect(result.length).toBe(1)
|
||||
} finally {
|
||||
// Ensure test cleanup
|
||||
await recursiveDelete(testpreservefileDir)
|
||||
|
||||
const cleanupResult = await recursiveReadDir(testpreservefileDir, /.*/)
|
||||
expect(cleanupResult.length).toBe(0)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue