Passed
Push — master ( 5e5243...6385bd )
by Andrii
02:22
created

src/fs.ts   A

Complexity

Total Complexity 4
Complexity/F 1.33

Size

Lines of Code 52
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 46
mnd 1
bc 1
fnc 3
dl 0
loc 52
rs 10
bpm 0.3333
cpm 1.3333
noi 0
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A fs.ts ➔ tempFileName 0 4 1
A fs.ts ➔ readlineSync 0 4 1
A fs.ts ➔ $unlink 0 4 2
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