Passed
Push — master ( 61dd3f...482d1b )
by greg
01:51
created

abeImport.js ➔ abeImport   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
nc 4
dl 0
loc 34
rs 8.5806
nop 3

1 Function

Rating   Name   Duplication   Size   Complexity  
A abeImport.js ➔ ... ➔ ??? 0 6 2
1
import Handlebars from 'handlebars'
2
import fse from 'fs-extra'
3
import path from 'path'
4
5
import {
6
  Plugins
7
  ,fileUtils
8
  ,Hooks
9
} from '../../../'
10
11
export default function abeImport (file, config, ctx) {
12
  file = Hooks.instance.trigger('beforeImport', file, config, ctx)
13
14
  config = JSON.parse(config)
15
  let intlData = config.intlData
16
  var defaultPartials = `${__dirname.replace(/\/$/,'')}/${config.defaultPartials.replace(/\/$/,'')}`
17
  var custom = (config.custom !== '') ? `${config.root.replace(/\/$/,'')}/${config.custom.replace(/\/$/,'')}` : defaultPartials
18
  var pathToPartial = `${custom}/${file}.html`
19
  try{
20
    fse.statSync(pathToPartial)
21
  }
22
  catch(e){
23
    pathToPartial = `${defaultPartials}/${file}.html`
24
  }
25
  if (fileUtils.isFile(pathToPartial)) {
26
    var html = fse.readFileSync(pathToPartial, 'utf8')
27
  }else {
28
    html = ''
29
  }
30
31
  var pluginsPartials = Plugins.instance.getPartials()
32
  Array.prototype.forEach.call(pluginsPartials, (pluginPartials) => {
33
    var checkFile = path.join(pluginPartials, `${file}.html`)
34
    if (fileUtils.isFile(checkFile)) {
35
      html += fse.readFileSync(checkFile, 'utf8')
36
    }
37
  })
38
  html = Hooks.instance.trigger('afterImport', html, file, config, ctx)
39
40
  var template = Handlebars.compile(html)
41
  var res = new Handlebars.SafeString(template(ctx, {data: {intl: intlData}}))
42
43
  return res
44
}
45