Completed
Push — master ( ad4939...82a867 )
by greg
01:49
created

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

Complexity

Total Complexity 13
Complexity/F 2.6

Size

Lines of Code 56
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 13
c 1
b 0
f 0
nc 1
mnd 3
bc 9
fnc 5
dl 0
loc 56
rs 10
bpm 1.8
cpm 2.6
noi 2

1 Function

Rating   Name   Duplication   Size   Complexity  
B duplicate.js ➔ duplicate 0 43 1
1
import path from 'path'
2
3
import {
4
  abeExtend,
5
  Manager,
6
  config,
7
  coreUtils,
8
  cmsData,
9
  cmsOperations
10
} from '../../'
11
12
const duplicate = function(oldPostUrl, template, newPath, name, req, isUpdate = false) {
13
  const p = new Promise((resolve, reject) => {
14
    abeExtend.hooks.instance.trigger('beforeDuplicate', oldPostUrl, template, newPath, name, req, isUpdate)
15
16
    let json = {}
17
    let revisions = []
18
    const newPostUrl = path.join(newPath, name) + '.' + config.files.templates.extension
19
    if(oldPostUrl != null) {
20
      const files = Manager.instance.getList()
21
      const oldPostDataPath = path.join(config.root, config.data.url, oldPostUrl.replace('.' + config.files.templates.extension, '.json'))
22
console.log(oldPostDataPath)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
23
      let posts = []
0 ignored issues
show
Unused Code introduced by
The assignment to variable posts seems to be never used. Consider removing it.
Loading history...
24
      posts = coreUtils.array.filter(files, 'path', oldPostDataPath)
25
console.log(posts)
26
      if(posts.length > 0 && posts[0].revisions != null) {
27
        revisions = posts[0].revisions
28
        if(revisions != null && revisions[0] != null) {
29
          json = cmsData.file.get(revisions[0].path)
30
          delete json.abe_meta
31
        }
32
      }
33
    }
34
35
    abeExtend.hooks.instance.trigger('afterDuplicate', json, oldPostUrl, template, newPath, name, req, isUpdate)
36
37
    var pCreate = cmsOperations.create(template, newPath, name, req, json, (isUpdate) ? false : true)
38
    pCreate.then((resSave) => {
39
      if (isUpdate && oldPostUrl !== newPostUrl) {
40
        abeExtend.hooks.instance.trigger('beforeUpdate', json, oldPostUrl, template, newPath, name, req, isUpdate)
41
        cmsOperations.remove.remove(oldPostUrl)
42
      }
43
      resolve(resSave)
44
    },
45
    () => {
46
      reject()
47
    }).catch(function(e) {
48
      console.error('[ERROR] abe-duplicate.js', e)
49
      reject()
50
    })
51
  })
52
53
  return p
54
}
55
56
export default duplicate