Passed
Pull Request — develop (#35)
by Bjarn
01:24
created

filesystem.ts ➔ ensureDirectoryExists   A

Complexity

Conditions 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 17
c 0
b 0
f 0
rs 9.85
cc 3
1
import {existsSync, mkdirSync} from 'fs'
2
import {error} from './console'
3
4
/**
5
 * Ensure the given path exists, then return a string or false when failed.
6
 * @param path
7
 */
8
async function ensureDirectoryExists(path: string): Promise<false | string> {
9
    if (!existsSync(path)) {
10
        try {
11
            await mkdirSync(path, {mode: 0o755})
12
            return path
13
        } catch (e) {
14
            error(e.message)
15
            return false
16
        }
17
    }
18
19
    return path
20
}
21
22
export {
23
    ensureDirectoryExists
24
}