Commit 10066aac authored by Tim Neutkens's avatar Tim Neutkens Committed by JJ Kasper

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
parent 4c549391
...@@ -34,22 +34,20 @@ const unlinkFile = async (p: string, t = 1): Promise<void> => { ...@@ -34,22 +34,20 @@ const unlinkFile = async (p: string, t = 1): Promise<void> => {
/** /**
* Recursively delete directory contents * Recursively delete directory contents
* @param {string} dir Directory to delete the contents of * @param {string} dir Directory to delete the contents of
* @param {RegExp} [filter] Filter for the relative file path * @param {RegExp} [exclude] Exclude based on relative file path
* @param {boolean} [ensure] Ensures that parameter dir exists, this is not passed recursively
* @param {string} [previousPath] Ensures that parameter dir exists, this is not passed recursively * @param {string} [previousPath] Ensures that parameter dir exists, this is not passed recursively
* @returns Promise void * @returns Promise void
*/ */
export async function recursiveDelete( export async function recursiveDelete(
dir: string, dir: string,
filter?: RegExp, exclude?: RegExp,
previousPath: string = '', previousPath: string = ''
ensure?: boolean
): Promise<void> { ): Promise<void> {
let result let result
try { try {
result = await readdir(dir) result = await readdir(dir)
} catch (e) { } catch (e) {
if (e.code === 'ENOENT' && !ensure) { if (e.code === 'ENOENT') {
return return
} }
throw e throw e
...@@ -65,17 +63,13 @@ export async function recursiveDelete( ...@@ -65,17 +63,13 @@ export async function recursiveDelete(
return return
} }
if (pathStat.isDirectory()) { const pp = join(previousPath, part)
const pp = join(previousPath, part) if (pathStat.isDirectory() && (!exclude || !exclude.test(pp))) {
await recursiveDelete(absolutePath, filter, pp) await recursiveDelete(absolutePath, exclude, pp)
return rmdir(absolutePath)
if (!filter || filter.test(pp)) {
return rmdir(absolutePath)
}
return
} }
if (!filter || filter.test(join(previousPath, part))) { if (!exclude || !exclude.test(pp)) {
return unlinkFile(absolutePath) return unlinkFile(absolutePath)
} }
}) })
......
...@@ -236,7 +236,7 @@ export default class HotReloader { ...@@ -236,7 +236,7 @@ export default class HotReloader {
} }
async clean() { async clean() {
return recursiveDelete(join(this.dir, this.config.distDir)) return recursiveDelete(join(this.dir, this.config.distDir), /^cache/)
} }
async getWebpackConfig() { async getWebpackConfig() {
......
hello world
\ No newline at end of file
...@@ -6,13 +6,38 @@ import { join } from 'path' ...@@ -6,13 +6,38 @@ import { join } from 'path'
const resolveDataDir = join(__dirname, '..', 'isolated', '_resolvedata') const resolveDataDir = join(__dirname, '..', 'isolated', '_resolvedata')
const testResolveDataDir = join(__dirname, '..', 'isolated', 'test_resolvedata') const testResolveDataDir = join(__dirname, '..', 'isolated', 'test_resolvedata')
const testpreservefileDir = join(__dirname, '..', 'isolated', 'preservefiles')
describe('recursiveDelete', () => { describe('recursiveDelete', () => {
it('should work', async () => { 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)
}
})
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)
await recursiveDelete(testResolveDataDir) const cleanupResult = await recursiveReadDir(testpreservefileDir, /.*/)
const result = await recursiveReadDir(testResolveDataDir, /.*/) expect(cleanupResult.length).toBe(0)
expect(result.length).toBe(0) }
}) })
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment