-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtests.utils.ts
45 lines (35 loc) · 1.15 KB
/
tests.utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import * as fs from 'fs';
import * as path from 'path';
import chalk from 'chalk';
const { red, bold, bgBlue } = chalk;
const isDirectory = (...target: string[]) => {
return fs.lstatSync(path.resolve('', ...target)).isDirectory();
};
const readDirectory = (...target: string[]) => {
return fs.readdirSync(path.resolve('', ...target));
};
const removeRecursively = (...target: string[]) => {
const contents = readDirectory(...target);
contents.forEach(content => {
if (isDirectory(...target, content)) {
removeRecursively(...target, content);
} else {
console.log(
bgBlue(' TEST CLEANUP '),
red(`Removing ${bold(path.join('', ...target, content))}`)
);
fs.unlinkSync(path.resolve(...target, content));
}
});
console.log(
bgBlue(' TEST CLEANUP '),
red(`Removing ${bold(path.join('', ...target))}`)
);
fs.rmdirSync(path.resolve(...target));
};
export const cleanUp = () => {
const testRootDir = './__temp__';
if (fs.existsSync(path.resolve(testRootDir))) {
removeRecursively(testRootDir);
}
};