/
unlink.ts
46 lines (36 loc) · 1.28 KB
/
unlink.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
46
import ls from './ls.js';
import removeFile from '../helpers/removeFile.js';
import removeDir from '../helpers/removeDir.js';
import is from './is.js';
const unlink = (path: string): Promise<true> =>
new Promise(async (resolve, reject) => {
try {
const pathIs = await is(path);
if (pathIs === null) {
resolve(true);
return;
}
if (pathIs === 'File') {
resolve(await removeFile(path));
return;
}
if (pathIs === 'Directory') {
const pathContents = (await ls(path)).map((content) => content.filename);
if (pathContents.length === 0) {
resolve(await removeDir(path));
return;
}
for (const contentName of pathContents) {
const contentPath = `${path}/${contentName}`;
const contentIs = await is(contentPath);
if (contentIs === null) continue;
contentIs === 'File' ? await removeFile(contentPath) : await unlink(contentPath);
}
}
const confirmPathIs = await is(path);
resolve(confirmPathIs === null ? true : await removeDir(path));
} catch (error) {
reject(error);
}
});
export default unlink;