Completed
Push — master ( 7b2ec6...a10c33 )
by greg
02:03
created

abe-create.js ➔ ... ➔ ???   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
c 1
b 0
f 0
nc 4
dl 0
loc 43
rs 8.439
nop 2
1
import path from 'path'
2
import {
3
  fileUtils,
4
  FileParser,
5
  Util,
6
  cleanSlug,
7
  getTemplate,
8
  save,
9
  config,
10
  Hooks,
11
  removeDuplicateAttr,
12
  Manager
13
} from '../../'
14
15
var create = function(template, pathCreate, name, req, forceJson = {}, duplicate = false) {
16
  var p = new Promise((resolve, reject) => {
17
    Hooks.instance.trigger('beforeCreate', template, pathCreate, name, req, forceJson)
18
19
    var templatePath = fileUtils.getTemplatePath(template.replace(config.root, ''))
20
    var filePath = path.join(pathCreate, name)
21
    filePath = cleanSlug(filePath)
22
    filePath = fileUtils.getFilePath(filePath)
23
24
    if(templatePath !== null && filePath !== null) {
25
      var tplUrl = FileParser.getFileDataFromUrl(filePath)
26
        
27
      if(!fileUtils.isFile(tplUrl.json.path)) {
28
        var json = (forceJson) ? forceJson : {}
29
        var tpl = templatePath
30
        var text = getTemplate(tpl)
31
        if (duplicate) {
32
          json = removeDuplicateAttr(text, json)
33
        }
34
        text = Util.removeDataList(text)
35
        var resHook = Hooks.instance.trigger('beforeFirstSave', filePath, req.query, json, text)
36
        filePath = resHook.filePath
37
        json = resHook.json
38
        text = resHook.text
39
40
        Hooks.instance.trigger('afterCreate', json, text, pathCreate, name, req, forceJson)
41
        save(filePath, req.query.selectTemplate, json, text, 'draft', null, 'draft')
42
            .then((resSave) => {
43
              Manager.instance.updateList()
44
              filePath = resSave.htmlPath
45
              tplUrl = FileParser.getFileDataFromUrl(filePath)
46
              resolve(resSave.json)
47
            }).catch(function(e) {
48
              reject()
49
              console.error(e)
50
            })
51
      }else {
52
        var json = FileParser.getJson(tplUrl.json.path)
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable json already seems to be declared on line 28. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
53
        resolve(json, tplUrl.json.path)
54
      }
55
    }else {
56
      reject()
57
    }
58
  }).catch(function(e) {
59
    console.error(e)
60
    reject()
61
  })
62
63
  return p
64
}
65
66
export default create