Skip to content

Commit

Permalink
feature: processes generate
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslabbe committed Oct 11, 2016
1 parent 48675a0 commit 08afa20
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 10 deletions.
22 changes: 13 additions & 9 deletions src/cli/cms/data/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export function fileList(obj, tplPath, match, jsonPage) {
return p
}

export function nextDataList(tplPath, jsonPage, match) {
export function nextDataList(tplPath, jsonPage, match, onlyDynamicSelect) {
var p = new Promise((resolve) => {
if(jsonPage['abe_source'] == null) {
jsonPage['abe_source'] = {}
Expand All @@ -160,12 +160,16 @@ export function nextDataList(tplPath, jsonPage, match) {

switch (type) {
case 'request':
requestList(obj, tplPath, match, jsonPage)
.then(() => {
resolve()
}).catch((e) => {
console.log('[ERROR] source.js requestList', e)
})
if (onlyDynamicSelect && obj.editable) {
resolve()
}else {
requestList(obj, tplPath, match, jsonPage)
.then(() => {
resolve()
}).catch((e) => {
console.log('[ERROR] source.js requestList', e)
})
}
break
case 'value':
valueList(obj, match, jsonPage)
Expand Down Expand Up @@ -200,13 +204,13 @@ export function nextDataList(tplPath, jsonPage, match) {
return p
}

export function getDataList(tplPath, text, jsonPage) {
export function getDataList(tplPath, text, jsonPage, onlyDynamicSelect = false) {
var p = new Promise((resolve) => {

var promises = []
var matches = cmsData.regex.getTagAbeTypeRequest(text)
Array.prototype.forEach.call(matches, (match) => {
promises.push(nextDataList(tplPath, jsonPage, match[0]))
promises.push(nextDataList(tplPath, jsonPage, match[0], onlyDynamicSelect))
})

Promise.all(promises)
Expand Down
1 change: 0 additions & 1 deletion src/cli/cms/data/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ export function execQuery(pathQuery, match, jsonPage) {
files = executeFromClause(request.from, pathQuery)
files = executeOrderByClause(files, request.orderby)
res = executeWhereClause(files, request.where, request.limit, request.columns, jsonPage)

return res
}

Expand Down
11 changes: 11 additions & 0 deletions src/cli/core/manager/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ class Manager {
return this._list
}

getListWithStatusOnFolder(status, folder = '') {
var list = []
folder = path.join(config.root, config.data.url, folder)
Array.prototype.forEach.call(this._list, (file) => {
if (typeof file[status] !== 'undefined' && file[status] !== null && file.path.indexOf(folder) > -1) {
list.push(file)
}
})
return list
}

setList(list) {
this._list = list

Expand Down
137 changes: 137 additions & 0 deletions src/cli/process/generate-posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import path from 'path'
import moment from 'moment'
import fse from 'fs-extra'
import debug from 'debug'
var log = debug('generate-posts:log')
log.color = 2
var traceLog = debug('generate-posts:trace')
var errorLog = debug('generate-posts:error')
errorLog.color = 1

var pConfig = {}
Array.prototype.forEach.call(process.argv, (item) => {
if (item.indexOf('=') > -1) {
var ar = item.split('=')
pConfig[ar[0]] = ar[1]
}
})

if(typeof pConfig.ABE_PATH === 'undefined' || pConfig.ABE_PATH === null) {
pConfig.ABE_PATH = ''
}

if(typeof pConfig.ABE_DESTINATION === 'undefined' || pConfig.ABE_DESTINATION === null) {
pConfig.ABE_DESTINATION = 'site'
}

log('loading dependencies...');
var dateStart
var templatesTexts = {}

function publishNext(published, tt, cb, i = 0) {
var pub = published.shift()
if(typeof pub !== 'undefined' && pub !== null) {

var jsonObject = fse.readJsonSync(pub.path)
if(typeof jsonObject.abe_meta !== 'undefined' && jsonObject.abe_meta !== null) {
i++
var p = new Promise((resolve) => {
if(typeof templatesTexts[jsonObject.abe_meta.template] === 'undefined' || templatesTexts[jsonObject.abe_meta.template] === null) {
templatesTexts[jsonObject.abe_meta.template] = cmsTemplates.template.getTemplate(jsonObject.abe_meta.template)
}

cmsData.source.getDataList(path.dirname(jsonObject.abe_meta.link), templatesTexts[jsonObject.abe_meta.template], jsonObject, true)
.then(() => {
jsonObject = abeExtend.hooks.instance.trigger('afterGetDataListOnSave', jsonObject)

var obj = {
publishAll:true,
type: jsonObject.abe_meta.status,
json: {
content: jsonObject
}
}
obj = abeExtend.hooks.instance.trigger('beforeSave', obj)

var page = new Page(obj.json.content.abe_meta.template, templatesTexts[jsonObject.abe_meta.template], obj.json.content, true)
cmsOperations.save.saveHtml(
path.join(config.root, pConfig.ABE_DESTINATION, jsonObject.abe_meta.link),
page.html
)

obj = abeExtend.hooks.instance.trigger('afterSave', obj)

traceLog('('+(moment(moment() - dateStart).format('mm:ss')) + 'sec) ' + i + ' - ' + pub.path.replace(config.root, '').replace(config.data.url, '') + ' (tpl: ' + jsonObject.abe_meta.template + ')')
resolve()
},
() => {
errorLog('publish-all ERROR on ' + pub.path.replace(config.root, '').replace(config.data.url, ''))
resolve()
})
})
}

p.then(function () {
publishNext(published, tt, cb, i++)
})
.catch(function (e) {
publishNext(published, tt, cb, i++)
errorLog('error', e)
})
}else {
cb(i)
}
}

function startProcess() {
dateStart = moment()
log('start publish all at path ' + pConfig.ABE_PATH);
log('searching for file at ' + config.root);
var files = Manager.instance.getListWithStatusOnFolder('publish', pConfig.ABE_PATH)

log('Found ' + files.length + ' to republish')

publishNext(files, files.length, function (i) {
log('total ' + i + ' files')
if (publishErrors.length > 0) {
errorLog('Errors ' + publishErrors.length + ' see ' + errorPath + ' for more info')
fse.writeJsonSync(errorPath, publishErrors, {
space: 2,
encoding: 'utf-8'
})
}
log('publish process finished ' + (moment(moment() - dateStart).format('mm:ss')) + 'sec')
process.exit(0)
})
}

var publishErrors = []
// var logsPub = ""
if(typeof pConfig.ABE_WEBSITE !== 'undefined' && pConfig.ABE_WEBSITE !== null) {
var config = require('../../cli').config
if(pConfig.ABE_WEBSITE) config.set({root: pConfig.ABE_WEBSITE.replace(/\/$/, '') + '/'})
try {
// IMPORT LIB
var Page = require('../../cli').Page
var cmsOperations = require('../../cli').cmsOperations
var abeExtend = require('../../cli').abeExtend
var Manager = require('../../cli').Manager
var cmsData = require('../../cli').cmsData
var cmsTemplates = require('../../cli').cmsTemplates
var Handlebars = require('../../cli').Handlebars

abeExtend.hooks.instance.trigger('afterHandlebarsHelpers', Handlebars)
Manager.instance.init()
.then(startProcess)
.catch((e) => {
errorLog('publish-all' + e)
})

} catch(e) {
errorLog('publish-all' + e)
}

}else {
errorLog('ABE_WEBSITE is not defined use node process.js ABE_WEBSITE=/pat/to/website')
process.exit(0)
}
15 changes: 15 additions & 0 deletions src/cli/process/initAbeForProcesses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function initAbeForProcess() {
var p = new Promise((resolve) => {

var pConfig = {}
Array.prototype.forEach.call(process.argv, (item) => {
if (item.indexOf('=') > -1) {
var ar = item.split('=')
pConfig[ar[0]] = ar[1]
}
})

})

return p
}

0 comments on commit 08afa20

Please sign in to comment.