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

fs.ts ➔ readlineSync   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
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