/
ensureDir.ts
40 lines (30 loc) · 1.08 KB
/
ensureDir.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
import { SFTP as sftp } from '../functions/connect';
import is from './is.js';
const ensureDir = (path: string): Promise<true> =>
new Promise(async (resolve, reject) => {
try {
const dirs = [];
const paths = path.split('/');
for (const dir of paths) {
dirs.push(dir);
const currentDir = dirs.join('/');
if (currentDir.trim().length === 0 || currentDir === '/') continue;
const currentPathStat = await is(currentDir);
if (currentPathStat === 'Directory') continue;
if (currentPathStat === null)
await new Promise(() => {
sftp.mkdir(currentDir, (err) => {
err ? reject(err) : resolve(true);
});
});
if (currentPathStat === 'File') {
reject(new Error(`The path "${currentDir}" exists, but is not a directory`));
return;
}
}
resolve(true);
} catch (error) {
reject(error);
}
});
export default ensureDir;