Completed
Push — master ( 6b23d7...ea705b )
by
unknown
02:05
created

generate-posts.js ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 1
c 3
b 0
f 1
nc 1
dl 0
loc 4
rs 10
nop 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
13
var config = require('../../cli').config
14
15
var dateStart
0 ignored issues
show
Unused Code introduced by
The variable dateStart seems to be never used. Consider removing it.
Loading history...
16
var templatesTexts = {}
17
18
function publishNext(published, tt, cb, i = 0) {
19
  var pub = published.shift()
20
  if(typeof pub !== 'undefined' && pub !== null) {
21
    
22
    var jsonObject = fse.readJsonSync(pub.path)
23
    i++
24
    var p = new Promise((resolve) => {
25
      if(typeof templatesTexts[jsonObject.abe_meta.template] === 'undefined' || templatesTexts[jsonObject.abe_meta.template] === null) {
26
        templatesTexts[jsonObject.abe_meta.template] = cmsTemplates.template.getTemplate(jsonObject.abe_meta.template)
27
      }
28
29
      cmsData.source.getDataList(path.dirname(jsonObject.abe_meta.link), templatesTexts[jsonObject.abe_meta.template], jsonObject, true)
30
        .then(() => {
31
          jsonObject = abeExtend.hooks.instance.trigger('afterGetDataListOnSave', jsonObject)
32
33
          var obj = {
34
            publishAll:true,
35
            type: jsonObject.abe_meta.status,
36
            json: {
37
              content: jsonObject
38
            }
39
          }
40
          obj = abeExtend.hooks.instance.trigger('beforeSave', obj)
41
42
          var page = new Page(obj.json.content.abe_meta.template, templatesTexts[jsonObject.abe_meta.template], obj.json.content, true)
43
44
          cmsOperations.save.saveHtml(
45
            path.join(config.root, processConfig.ABE_DESTINATION, jsonObject.abe_meta.link),
46
            page.html
47
          )
48
          
49
          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...
50
51
          trace('('+ getTime() + ') ' + i + ' - ' + pub.path.replace(config.root, '').replace(config.data.url, '') + ' (tpl: ' + jsonObject.abe_meta.template + ')')
52
          resolve()
53
        },
54
        () => {
55
          error('publish-all ERROR on ' + pub.path.replace(config.root, '').replace(config.data.url, ''))
56
          resolve()
57
        })
58
    })
59
  
60
    p.then(function () {
61
      publishNext(published, tt, cb, i++)
62
    })
63
    .catch(function (e) {
64
      publishNext(published, tt, cb, i++)
65
      error('error', e)
66
    })
67
  }else {
68
    cb(i)
69
  }
70
}
71
72
function startProcess() {
73
  log('start publish all at path ' + processConfig.ABE_PATH);
74
  log('searching for file at ' + config.root);
75
  var files = Manager.instance.getListWithStatusOnFolder('publish', processConfig.ABE_PATH)
76
77
  log('Found ' + files.length + ' to republish')
78
79
  publishNext(files, files.length, function (i) {
80
    log('total ' + i + ' files')
81
    log('publish process finished ' + getTime())
82
    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...
83
  })
84
}
85
86
init('generate-posts',
87
  {
88
    ABE_PATH: '',
89
    ABE_DESTINATION: 'site'
90
  })
91
  .then(startProcess,
92
  (msg) => {
93
    error(msg)
94
    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...
95
  })