Completed
Push — master ( 430c18...52d1c6 )
by
unknown
54s
created

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

Complexity

Total Complexity 20
Complexity/F 1.43

Size

Lines of Code 150
Function Count 14

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 0
wmc 20
c 1
b 1
f 0
nc 1
mnd 2
bc 23
fnc 14
dl 0
loc 150
rs 10
bpm 1.6428
cpm 1.4285
noi 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A post.js ➔ reject 0 22 1
B post.js ➔ unpublish 0 33 1
B post.js ➔ draft 0 36 1
B post.js ➔ publish 0 43 1
1
import path from 'path'
2
3
import {
4
  cmsData,
5
  Page,
6
  cmsOperations,
7
  cmsTemplates,
8
  coreUtils,
9
  config,
10
  abeExtend,
11
  Manager
12
} from '../../'
13
14
export function draft(filePath, json, workflow = 'draft') {
15
  var p = new Promise((resolve) => {
16
    abeExtend.hooks.instance.trigger('beforeDraft', json, filePath)
17
18
    var revisionPath = path.join(config.root, config.data.url, filePath.replace(`.${config.files.templates.extension}`, '.json'))
19
    revisionPath = coreUtils.file.addDateIsoToRevisionPath(revisionPath, workflow)
20
    var date = coreUtils.file.getDate(revisionPath)
21
    cmsData.metas.add(json, workflow, date)
22
23
    var template = cmsTemplates.template.getTemplate(json.abe_meta.template)
24
25
    cmsData.source.getDataList(path.dirname(json.abe_meta.link), template, json)
26
    .then(() => {
27
28
      json['abe_meta'].complete = cmsData.utils.getPercentOfRequiredTagsFilled(template, json)
29
30
      // var page = new Page(json.abe_meta.template, template, json, true)
31
      var result
32
      if (!cmsOperations.save.saveJson(revisionPath, json)) {
33
        result = {
34
          success: 0,
35
          error: 'cannot json save file'
36
        }
37
      }else {
38
        Manager.instance.updatePostInList(revisionPath)
39
        result = {
40
          success: 1,
41
          json: json
42
        }
43
      }
44
      resolve(result)
45
    })
46
  })
47
48
  return p
49
}
50
51
export function publish(filePath, json) {
52
  var p = new Promise((resolve) => {
53
    abeExtend.hooks.instance.trigger('beforePublish', json, filePath)
54
55
    var revisionPath = path.join(config.root, config.data.url, filePath.replace(`.${config.files.templates.extension}`, '.json'))
56
    var postPath = path.join(config.root, config.publish.url, filePath)
57
    // revisionPath = coreUtils.file.addDateIsoToRevisionPath(revisionPath, workflow)
58
    cmsData.metas.add(json, 'publish')
59
60
    var template = cmsTemplates.template.getTemplate(json.abe_meta.template)
61
62
    cmsData.source.getDataList(path.dirname(json.abe_meta.link), template, json)
63
    .then(() => {
64
      json['abe_meta'].complete = cmsData.utils.getPercentOfRequiredTagsFilled(template, json)
65
66
      var page = new Page(json.abe_meta.template, template, json, true)
67
      
68
      var result
69
      if (!cmsOperations.save.saveHtml(postPath, page.html)) {
70
        result = {
71
          success: 0,
72
          error: 'cannot html save file'
73
        }
74
      }else {
75
        if (!cmsOperations.save.saveJson(revisionPath, json)) {
76
          result = {
77
            success: 0,
78
            error: 'cannot json save file'
79
          }
80
        }else {
81
          Manager.instance.updatePostInList(revisionPath)
82
          result = {
83
            success: 1,
84
            json: json
85
          }
86
        }
87
      }
88
      resolve(result)
89
    })
90
  })
91
92
  return p
93
}
94
95
export function unpublish(filePath) {
96
  abeExtend.hooks.instance.trigger('beforeUnpublish', filePath)
97
98
  var p = new Promise((resolve, reject) => {
99
    var revisionPath = path.join(config.root, config.data.url, filePath.replace(`.${config.files.templates.extension}`, '.json'))
100
    var postPath = path.join(config.root, config.publish.url, filePath)
101
    if(coreUtils.file.exist(revisionPath)) {
102
      var json = JSON.parse(JSON.stringify(cmsData.file.get(revisionPath)))
103
      if(json.abe_meta.publish != null) {
104
        delete json.abe_meta.publish
105
      }
106
107
      var p = draft(
108
        filePath, 
109
        json,
110
        'draft'
111
      )
112
113
      p.then((result) => {
114
        cmsOperations.remove.removeFile(revisionPath, postPath)
115
        abeExtend.hooks.instance.trigger('afterUnpublish', revisionPath, postPath)
116
        var newRevisionPath = path.join(config.root, config.data.url, result.json.abe_meta.latest.abeUrl.replace(`.${config.files.templates.extension}`, '.json'))
117
        Manager.instance.updatePostInList(newRevisionPath)
118
        resolve(result)
119
      }).catch(function(e) {
120
        console.error('[ERROR] unpublish', e)
121
        reject()
122
      })
123
    }
124
  })
125
126
  return p
127
}
128
129
export function reject(filePath, json) {
130
  abeExtend.hooks.instance.trigger('beforeReject', filePath)
131
132
  var p = new Promise((resolve) => {
133
    if(json.abe_meta.publish != null) {
134
      delete json.abe_meta.publish
135
    }
136
    var p2 = draft(
137
        filePath, 
138
        json,
139
        'draft'
140
      )
141
    p2.then((result) => {
142
      abeExtend.hooks.instance.trigger('afterReject', result)
143
      resolve(result)
144
    }).catch(function(e) {
145
      console.error('[ERROR] reject.js', e)
146
    })
147
  })
148
149
  return p
150
}