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

src/cli/process/generate-posts.js   A

Complexity

Total Complexity 13
Complexity/F 1.44

Size

Lines of Code 93
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 0
wmc 13
c 3
b 0
f 1
nc 1
mnd 2
bc 12
fnc 9
dl 0
loc 93
rs 10
bpm 1.3333
cpm 1.4443
noi 3

3 Functions

Rating   Name   Duplication   Size   Complexity  
A generate-posts.js ➔ publishNext 0 51 3
A generate-posts.js ➔ ??? 0 4 1
A generate-posts.js ➔ startProcess 0 15 1
1
import path from 'path'
2
import fse from 'fs-extra'
3
import {init, log, trace, error, processConfig, getTime} from './initAbeForProcesses'
4
5
// IMPORT LIB
6
var Page = require('../../cli').Page
7
var cmsOperations = require('../../cli').cmsOperations
8
var abeExtend = require('../../cli').abeExtend
9
var Manager = require('../../cli').Manager
10
var cmsData = require('../../cli').cmsData
11
var cmsTemplates = require('../../cli').cmsTemplates
12
var config = require('../../cli').config
13
var templatesTexts = {}
14
15
function publishNext(files, tt, cb, i = 0) {
16
  var pub = files.shift()
17
  if(typeof pub !== 'undefined' && pub !== null) {
18
    
19
    var jsonObject = fse.readJsonSync(pub[processConfig.ABE_STATUS].path)
20
    i++
21
    var p = new Promise((resolve) => {
22
      if(typeof templatesTexts[jsonObject.abe_meta.template] === 'undefined' || templatesTexts[jsonObject.abe_meta.template] === null) {
23
        templatesTexts[jsonObject.abe_meta.template] = cmsTemplates.template.getTemplate(jsonObject.abe_meta.template)
24
      }
25
26
      cmsData.source.getDataList(path.dirname(jsonObject.abe_meta.link), templatesTexts[jsonObject.abe_meta.template], jsonObject, true)
27
        .then(() => {
28
          jsonObject = abeExtend.hooks.instance.trigger('afterGetDataListOnSave', jsonObject)
29
30
          var obj = {
31
            type: jsonObject.abe_meta.status,
32
            json: {
33
              content: jsonObject
34
            }
35
          }
36
37
          var page = new Page(obj.json.content.abe_meta.template, templatesTexts[jsonObject.abe_meta.template], obj.json.content, true)
38
39
          cmsOperations.save.saveHtml(
40
            path.join(config.root, processConfig.ABE_DESTINATION, jsonObject.abe_meta.link),
41
            page.html
42
          )
43
          
44
          obj = abeExtend.hooks.instance.trigger('afterSave', obj)
0 ignored issues
show
Unused Code introduced by
The assignment to variable obj seems to be never used. Consider removing it.
Loading history...
45
46
          trace('('+ getTime() + ') ' + i + ' - ' + pub[processConfig.ABE_STATUS].path.replace(config.root, '').replace(config.data.url, '') + ' (tpl: ' + jsonObject.abe_meta.template + ')')
47
          resolve()
48
        },
49
        () => {
50
          error('generate-posts ERROR on ' + pub[processConfig.ABE_STATUS].path.replace(config.root, '').replace(config.data.url, ''))
51
          resolve()
52
        })
53
    })
54
  
55
    p.then(function () {
56
      publishNext(files, tt, cb, i++)
57
    })
58
    .catch(function (e) {
59
      publishNext(files, tt, cb, i++)
60
      error('error', e)
61
    })
62
  }else {
63
    cb(i)
64
  }
65
}
66
67
function startProcess() {
68
  log('start publish all at path ' + processConfig.ABE_PATH)
69
  log('searching for file at ' + config.root)
70
  log('seach status: ' + processConfig.ABE_STATUS)
71
  log('save to: ' + path.join(config.root, processConfig.ABE_DESTINATION))
72
  var files = Manager.instance.getListWithStatusOnFolder(processConfig.ABE_STATUS, processConfig.ABE_PATH)
73
74
  log('Found ' + files.length + ' to republish')
75
76
  publishNext(files, files.length, function (i) {
77
    log('total ' + i + ' files')
78
    log('publish process finished ' + getTime())
79
    process.exit(0)
0 ignored issues
show
Compatibility Debugging Code Best Practice introduced by
Use of process.exit() is discouraged as it will potentially stop the complete node.js application. Consider quitting gracefully instead by throwing an Error.
Loading history...
80
  })
81
}
82
83
init('generate-posts',
84
  {
85
    ABE_STATUS: 'publish',
86
    ABE_PATH: '',
87
    ABE_DESTINATION: 'site'
88
  })
89
  .then(startProcess,
90
  (msg) => {
91
    error(msg)
92
    process.exit(0)
0 ignored issues
show
Compatibility Debugging Code Best Practice introduced by
Use of process.exit() is discouraged as it will potentially stop the complete node.js application. Consider quitting gracefully instead by throwing an Error.
Loading history...
93
  })