Issues (791)

src/cli/process/generate-posts.js (3 issues)

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
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
  })