Total Complexity | 4 |
Complexity/F | 1.33 |
Lines of Code | 52 |
Function Count | 3 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import schema = require("./schema.json") |
||
2 | import { |
||
3 | readFileSync, |
||
4 | unlink, |
||
5 | exists, |
||
6 | open, |
||
7 | writeFile, |
||
8 | close, |
||
9 | mkdir, |
||
10 | rename |
||
11 | } from "fs" |
||
12 | import { tmpdir } from "os" |
||
13 | import { join } from "path" |
||
14 | import { promisify } from "util" |
||
15 | import { randomString } from "./utils" |
||
16 | |||
17 | const $exists = promisify(exists) |
||
18 | , _unlink = promisify(unlink) |
||
19 | , $open = promisify(open) |
||
20 | , $write = promisify(writeFile) |
||
21 | , $close = promisify(close) |
||
22 | , $mkdir = promisify(mkdir) |
||
23 | , $rename = promisify(rename) |
||
24 | , tempDir = join(tmpdir(), schema.title) |
||
25 | |||
26 | export { |
||
27 | readlineSync, |
||
28 | $exists, |
||
29 | $unlink, |
||
30 | $open, |
||
31 | $write, |
||
32 | $close, |
||
33 | $rename, |
||
34 | tempFileName, |
||
35 | tempDir |
||
36 | } |
||
37 | |||
38 | //TODO replace with common |
||
39 | function readlineSync(path: string, splitter: string) { |
||
40 | return readFileSync(path).toString().split(splitter) |
||
41 | } |
||
42 | |||
43 | function $unlink(source: Parameters<typeof _unlink>[0]) { |
||
44 | return $exists(source) |
||
45 | .then(ex => ex ? _unlink(source) : void 0) |
||
46 | } |
||
47 | |||
48 | async function tempFileName() { |
||
49 | await $exists(tempDir) || await $mkdir(tempDir) |
||
50 | return join(tempDir, randomString()) |
||
51 | } |
||
52 |