Completed
Push — master ( 9899df...273a12 )
by
unknown
02:23
created

src/server/routes/post-publish.js   A

Complexity

Total Complexity 16
Complexity/F 2

Size

Lines of Code 65
Function Count 8

Duplication

Duplicated Lines 65
Ratio 100 %

Importance

Changes 0
Metric Value
cc 0
wmc 16
c 0
b 0
f 0
nc 1
mnd 1
bc 11
fnc 8
dl 65
loc 65
rs 10
bpm 1.375
cpm 2
noi 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A post-publish.js ➔ route 55 55 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1 View Code Duplication
import {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
  fileUtils,
3
  cmsOperations,
4
  cleanSlug,
5
  Hooks,
6
  Manager
7
} from '../../cli'
8
9
var route = function(req, res, next){
10
  Hooks.instance.trigger('beforeRoute', req, res, next)
11
  if(typeof res._header !== 'undefined' && res._header !== null) return
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
12
13
  var filePath = cleanSlug(req.body.filePath)
14
  var p = new Promise((resolve) => {
15
    cmsOperations.save.save(
16
      fileUtils.getFilePath(filePath),
17
      req.body.tplPath,
18
      req.body.json,
19
      '',
20
      'draft',
21
      null,
22
      'publish')
23
      .then(() => {
24
        resolve()
25
      }).catch(function(e) {
26
        console.error(e)
27
      })
28
  })
29
30
  p.then((resSave) => {
31
    cmsOperations.save.save(
32
      fileUtils.getFilePath(req.body.filePath),
33
      req.body.tplPath,
34
      req.body.json,
35
      '',
36
      'publish',
37
      resSave,
38
      'publish')
39
      .then((resSave) => {
40
        if(typeof resSave.error !== 'undefined' && resSave.error !== null  ){
41
          res.set('Content-Type', 'application/json')
42
          res.send(JSON.stringify({error: resSave.error}))
43
        }
44
        var result
45
        if(typeof resSave.reject !== 'undefined' && resSave.reject !== null){
46
          result = resSave
47
        }
48
        if(typeof resSave.json !== 'undefined' && resSave.json !== null){
49
          result = {
50
            success: 1,
51
            json: resSave.json
52
          }
53
        }
54
        Manager.instance.updateList()
55
        res.set('Content-Type', 'application/json')
56
        res.send(JSON.stringify(result))
0 ignored issues
show
Bug introduced by
The variable result does not seem to be initialized in case typeof resSave.reject !...resSave.reject !== null on line 45 is false. Are you sure the function stringify handles undefined variables?
Loading history...
57
      }).catch(function(e) {
58
        console.error('post-publish.js', e)
59
      })
60
  }).catch(function(e) {
61
    console.error('post-publish.js', e)
62
  })
63
}
64
65
export default route