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

src/cli/cms/operations/abe-duplicate.js   A

Complexity

Total Complexity 17
Complexity/F 2.83

Size

Lines of Code 66
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 17
c 1
b 0
f 0
nc 1
mnd 3
bc 11
fnc 6
dl 0
loc 66
rs 10
bpm 1.8333
cpm 2.8333
noi 6

1 Function

Rating   Name   Duplication   Size   Complexity  
A abe-duplicate.js ➔ duplicate 0 49 1
1
import path from 'path'
2
import {
3
  Hooks,
4
  fileUtils,
0 ignored issues
show
Unused Code introduced by
The variable fileUtils seems to be never used. Consider removing it.
Loading history...
5
  FileParser,
6
  fileAttr,
0 ignored issues
show
Unused Code introduced by
The variable fileAttr seems to be never used. Consider removing it.
Loading history...
7
  Util,
0 ignored issues
show
Unused Code introduced by
The variable Util seems to be never used. Consider removing it.
Loading history...
8
  Manager,
9
  cleanSlug,
0 ignored issues
show
Unused Code introduced by
The variable cleanSlug seems to be never used. Consider removing it.
Loading history...
10
  getTemplate,
0 ignored issues
show
Unused Code introduced by
The variable getTemplate seems to be never used. Consider removing it.
Loading history...
11
  config,
12
  save,
0 ignored issues
show
Unused Code introduced by
The variable save seems to be never used. Consider removing it.
Loading history...
13
  abeCreate
14
} from '../../'
15
16
var duplicate = function(oldFilePath, template, newPath, name, req, isUpdate = false) {
17
  var p = new Promise((resolve, reject) => {
18
    Hooks.instance.trigger('beforeDuplicate', oldFilePath, template, newPath, name, req, isUpdate)
19
20
    var json = {}
21
    var revisions = []
22
    if(typeof oldFilePath !== 'undefined' && oldFilePath !== null) {
23
      var files = Manager.instance.getList()
24
      var fileWithoutExtension = oldFilePath.replace('.' + config.files.templates.extension, '')
25
26
      var doc = null
27
      Array.prototype.forEach.call(files, (file) => {
28
        if (file.path.indexOf(fileWithoutExtension) > -1) {
29
          doc = file
30
        }
31
      })
32
33
      if(typeof doc.revisions !== 'undefined' && doc.revisions !== null) {
34
        revisions = doc.revisions
35
36
        if(typeof revisions !== 'undefined' && revisions !== null
37
          && typeof revisions[0] !== 'undefined' && revisions[0] !== null) {
38
          json = FileParser.getJson(revisions[0].path)
39
        }
40
      }
41
      
42
      delete json.abe_meta
43
    }
44
45
    if (isUpdate) {
46
      Hooks.instance.trigger('beforeUpdate', json, oldFilePath, template, newPath, name, req, isUpdate)
47
      FileParser.deleteFile(oldFilePath)
48
    }
49
    Hooks.instance.trigger('afterDuplicate', json, oldFilePath, template, newPath, name, req, isUpdate)
50
51
    var pCreate = abeCreate(template, newPath, name, req, json, (isUpdate) ? false : true)
52
    pCreate.then((resSave) => {
53
      resolve(resSave)
54
    },
55
    () => {
56
      reject()
57
    }).catch(function(e) {
58
      console.error('[ERROR] abe-duplicate.js', e)
59
      reject()
60
    })
61
  })
62
63
  return p
64
}
65
66
export default duplicate