Completed
Push — master ( 60f354...0d0dec )
by hung
11s
created

src/helper/input.js   A

Complexity

Total Complexity 6
Complexity/F 2

Size

Lines of Code 27
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 6
c 1
b 0
f 0
nc 1
mnd 2
bc 7
fnc 3
dl 0
loc 27
rs 10
bpm 2.3333
cpm 2
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B input.js ➔ ??? 0 23 4
1
const fs = require('fs')
2
const { join } = require('path')
3
const { Readable } = require('stream')
4
5
module.exports = (input, callback) => {
6
  if (typeof input === 'string') {
7
    // Input as file
8
    const path = join(__dirname, '../', input)
9
    if (!fs.existsSync(path)) {
10
      console.error(`Cannot read file ${path}`)
11
      return
12
    }
13
    const html = fs.readFileSync(path, 'utf8')
14
    callback(html)
15
  } else if (input instanceof Readable) {
16
    // Input as stream
17
    input.on('data', chunk => {
18
      callback(chunk.toString())
19
    })
20
    input.on('error', err => {
21
      console.error(err)
22
    })
23
  } else {
24
    // Unknown input
25
    console.error('Invalid Input')
26
  }
27
}
28