Completed
Push — master ( b1fdb8...513637 )
by
unknown
03:08
created

post.js ➔ ... ➔ p1.catch   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 9
loc 9
rs 9.6666
nop 1
1
import path from 'path'
2
3
import {
4
  cmsData,
5
  cmsOperations,
6
  coreUtils,
7
  config,
8
  abeExtend,
9
  Manager
10
} from '../../'
11
12 View Code Duplication
export function publish(filePath, tplPath, json) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
13
  var p = new Promise((resolve, reject) => {
14
    abeExtend.hooks.instance.trigger('beforePublish', json, filePath, tplPath)
15
    var p1 = new Promise((resolve) => {
16
      cmsOperations.save.save(
17
        path.join(config.root, config.draft.url, filePath.replace(config.root)),
18
        tplPath,
19
        json,
20
        '',
21
        'draft',
22
        null,
23
        'publish')
24
        .then(() => {
25
          resolve()
26
        }).catch(function(e) {
27
          console.error(e)
28
        })
29
    })
30
31
    p1.then((resSave) => {
32
      cmsOperations.save.save(
33
        path.join(config.root, config.draft.url, filePath.replace(config.root)),
34
        tplPath,
35
        json,
36
        '',
37
        'publish',
38
        resSave,
39
        'publish')
40
        .then((resSave) => {
41
          var result
42
          if(typeof resSave.error !== 'undefined' && resSave.error !== null  ){
43
            result = {
44
              success: 0,
45
              error: resSave.error
46
            }
47
          } else if(typeof resSave.reject !== 'undefined' && resSave.reject !== null){
48
            result = resSave
49
          } else if(typeof resSave.json !== 'undefined' && resSave.json !== null){
50
            result = {
51
              success: 1,
52
              json: resSave.json
53
            }
54
          }
55
          abeExtend.hooks.instance.trigger('afterPublish', result)
0 ignored issues
show
Bug introduced by
The variable result does not seem to be initialized in case typeof resSave.json !==...& resSave.json !== null on line 49 is false. Are you sure the function trigger handles undefined variables?
Loading history...
56
          Manager.instance.updateList()
57
          resolve(result)
58
        }).catch(function(e) {
59
          console.error('post.js', e)
60
          var result = {
61
            success: 0,
62
            error: 'publish error'
63
          }
64
          abeExtend.hooks.instance.trigger('afterPublish', result)
65
          resolve(result)
66
        })
67
    }).catch(function(e) {
68
      console.error('post.js', e)
69
      var result = {
70
        success: 0,
71
        error: 'publish error'
72
      }
73
      abeExtend.hooks.instance.trigger('afterPublish', result)
74
      resolve(result)
75
    })
76
  })
77
78
  return p
79
}
80
81
export function unpublish(filePath) {
82
  abeExtend.hooks.instance.trigger('beforeUnpublish', filePath)
83
  filePath = coreUtils.slug.clean(filePath)
84
  var tplUrl = cmsData.file.fromUrl(path.join(config.publish.url, filePath))
85
  if(coreUtils.file.exist(tplUrl.json.path)) {
86
    var json = JSON.parse(JSON.stringify(cmsData.file.get(tplUrl.json.path)))
87
    if(json.abe_meta.publish != null) {
88
      delete json.abe_meta.publish
89
    }
90
91
    cmsOperations.save.save(
92
      path.join(config.root, config.draft.url, json.abe_meta.link.replace(config.root)),
93
      json.abe_meta.template,
94
      json,
95
      '',
96
      'reject',
97
      null,
98
      'reject'
99
    )
100
    .then(() => {
101
      cmsOperations.remove.removeFile(tplUrl.publish.path, tplUrl.publish.json)
102
      abeExtend.hooks.instance.trigger('afterUnpublish', tplUrl.publish.path, tplUrl.publish.json)
103
      Manager.instance.updateList()
104
    })
105
  }
106
}
107
108 View Code Duplication
export function reject(filePath, tplPath, json) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
109
  var p = new Promise((resolve, reject) => {
110
    var p1 = new Promise((resolve) => {
111
      cmsOperations.save.save(
112
        path.join(config.root, config.draft.url, filePath.replace(config.root)),
113
        tplPath,
114
        json,
115
        '',
116
        'draft',
117
        null,
118
        'reject')
119
        .then((resSave) => {
0 ignored issues
show
Unused Code introduced by
The parameter resSave is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
120
          resolve()
121
        }).catch(function(e) {
122
          console.error(e)
123
        })
124
    })
125
126
    p1.then((resSave) => {
127
      cmsOperations.save.save(
128
        path.join(config.root, config.draft.url, filePath.replace(config.root)),
129
        tplPath,
130
        json,
131
        '',
132
        'reject',
133
        resSave,
134
        'reject')
135
        .then((resSave) => {
136
          var result
137
          if(typeof resSave.error !== 'undefined' && resSave.error !== null  ){
138
            result = {
139
              success: 0,
140
              error: resSave.error
141
            }
142
          } else if(typeof resSave.reject !== 'undefined' && resSave.reject !== null){
143
            result = resSave
144
          } else if(typeof resSave.json !== 'undefined' && resSave.json !== null){
145
            result = {
146
              success: 1,
147
              json: resSave.json
148
            }
149
          }
150
          abeExtend.hooks.instance.trigger('afterReject', result)
0 ignored issues
show
Bug introduced by
The variable result does not seem to be initialized in case typeof resSave.json !==...& resSave.json !== null on line 144 is false. Are you sure the function trigger handles undefined variables?
Loading history...
151
          Manager.instance.updateList()
152
          resolve(result)
153
        })
154
    }).catch(function(e) {
155
      console.error(e)
156
      var result = {
157
        success: 0,
158
        error: 'reject error'
159
      }
160
      abeExtend.hooks.instance.trigger('afterReject', result)
161
      resolve(result)
162
    })
163
  })
164
165
  return p
166
}