Skip to content

Commit

Permalink
refactor: save
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslabbe committed Nov 7, 2016
1 parent a775e9e commit 5a74115
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 273 deletions.
9 changes: 4 additions & 5 deletions src/cli/cms/data/metas.js
Expand Up @@ -3,9 +3,8 @@ import {
,config
} from '../../'

export function add(tpl, json, type, obj = {}, date = null, realType = 'draft') {
export function add(json, type, date = null) {
let meta = config.meta.name

var currentDate = (date != null && date !== '') ? date : new Date()
var abeUrl = (type === 'publish') ? json[meta].link : cmsData.fileAttr.add(json[meta].link, 'd' + cmsData.revision.removeStatusAndDateFromFileName(currentDate.toISOString())) + ''

Expand All @@ -16,13 +15,13 @@ export function add(tpl, json, type, obj = {}, date = null, realType = 'draft')
date: currentDate,
abeUrl: abeUrl
}
json[meta].status = realType === 'reject' ? 'draft' : realType
json[meta].status = type
if(json[meta][type] == null) {
json[meta][type] = JSON.parse(JSON.stringify(obj))
json[meta][type] = {latest: {}}
json[meta][type].date = currentDate
json[meta][type].abeUrl = abeUrl
}
json[meta][type].latest = JSON.parse(JSON.stringify(obj))
// json[meta][type].latest = JSON.parse(JSON.stringify(obj))
json[meta][type].latest.date = currentDate
json[meta][type].latest.abeUrl = abeUrl
}
2 changes: 1 addition & 1 deletion src/cli/cms/operations/create.js
Expand Up @@ -34,7 +34,7 @@ var create = function(template, pathCreate, name, req, forceJson = {}, duplicate
text = resHook.text

abeExtend.hooks.instance.trigger('afterCreate', json, text, pathCreate, name, req, forceJson)
cmsOperations.save.save(filePath, template, json, text, 'draft', null, 'draft')
cmsOperations.save.save(filePath, template, json, text, 'draft')
.then((resSave) => {
Manager.instance.updatePostInList(resSave.jsonPath)
filePath = resSave.htmlPath
Expand Down
245 changes: 107 additions & 138 deletions src/cli/cms/operations/post.js
Expand Up @@ -2,39 +2,47 @@ import path from 'path'

import {
cmsData,
Page,
cmsOperations,
cmsTemplates,
coreUtils,
config,
abeExtend,
Manager
} from '../../'

export function draft(filePath, tplPath, json, workflow = 'draft', type = 'draft') {
export function draft(filePath, tplPath, json, workflow = 'draft') {
var p = new Promise((resolve, reject) => {
abeExtend.hooks.instance.trigger('beforeDraft', json, filePath, tplPath)
cmsOperations.save.save(
path.join(config.root, config.draft.url, filePath.replace(config.root)),
tplPath,
json,
'',
workflow,
null,
type)
.then((resSave) => {
var result
if(typeof resSave.error !== 'undefined' && resSave.error !== null ){
result = {error: resSave.error}
}else if(typeof resSave.reject !== 'undefined' && resSave.reject !== null){
result = resSave
}else if(typeof resSave.json !== 'undefined' && resSave.json !== null){
Manager.instance.updatePostInList(resSave.jsonPath)
result = {
success: 1,
json: resSave.json
}

var revisionPath = path.join(config.root, config.data.url, filePath.replace(`.${config.files.templates.extension}`, '.json'))
revisionPath = coreUtils.file.addDateIsoToRevisionPath(revisionPath, workflow)
var date = coreUtils.file.getDate(revisionPath)
cmsData.metas.add(json, workflow, date)

var template = cmsTemplates.template.getTemplate(json.abe_meta.template)

cmsData.source.getDataList(path.dirname(json.abe_meta.link), template, json)
.then(() => {

json['abe_meta'].complete = cmsOperations.save.checkRequired(template, json)

// var page = new Page(json.abe_meta.template, template, json, true)
var result
if (!cmsOperations.save.saveJson(revisionPath, json)) {
result = {
success: 0,
error: "cannot json save file"
}
resolve(result)
})
}else {
Manager.instance.updatePostInList(revisionPath)
result = {
success: 1,
json: json
}
}
resolve(result)
})
})

return p
Expand All @@ -43,65 +51,41 @@ export function draft(filePath, tplPath, json, workflow = 'draft', type = 'draft
export function publish(filePath, tplPath, json) {
var p = new Promise((resolve, reject) => {
abeExtend.hooks.instance.trigger('beforePublish', json, filePath, tplPath)
var p1 = new Promise((resolve) => {
cmsOperations.save.save(
path.join(config.root, config.draft.url, filePath.replace(config.root)),
tplPath,
json,
'',
'draft',
null,
'publish')
.then(() => {
resolve()
}).catch(function(e) {
console.error(e)
})
})

p1.then((resSave) => {
cmsOperations.save.save(
path.join(config.root, config.draft.url, filePath.replace(config.root)),
tplPath,
json,
'',
'publish',
resSave,
'publish')
.then((resSave) => {
var result
if(typeof resSave.error !== 'undefined' && resSave.error !== null ){
result = {
success: 0,
error: resSave.error
}
} else if(typeof resSave.reject !== 'undefined' && resSave.reject !== null){
result = resSave
} else if(typeof resSave.json !== 'undefined' && resSave.json !== null){
result = {
success: 1,
json: resSave.json
}
}
abeExtend.hooks.instance.trigger('afterPublish', result)
Manager.instance.updatePostInList(resSave.jsonPath)
resolve(result)
}).catch(function(e) {
console.error('post.js', e)
var result = {
var revisionPath = path.join(config.root, config.data.url, filePath.replace(`.${config.files.templates.extension}`, '.json'))
var postPath = path.join(config.root, config.publish.url, filePath)
// revisionPath = coreUtils.file.addDateIsoToRevisionPath(revisionPath, workflow)
cmsData.metas.add(json, "publish")

var template = cmsTemplates.template.getTemplate(json.abe_meta.template)

cmsData.source.getDataList(path.dirname(json.abe_meta.link), template, json)
.then(() => {

json['abe_meta'].complete = cmsOperations.save.checkRequired(template, json)

var page = new Page(json.abe_meta.template, template, json, true)

var result
if (!cmsOperations.save.saveHtml(postPath, page.html)) {
result = {
success: 0,
error: "cannot html save file"
}
}else {
if (!cmsOperations.save.saveJson(revisionPath, json)) {
result = {
success: 0,
error: 'publish error'
error: "cannot json save file"
}
abeExtend.hooks.instance.trigger('afterPublish', result)
resolve(result)
})
}).catch(function(e) {
console.error('post.js', e)
var result = {
success: 0,
error: 'publish error'
}else {
Manager.instance.updatePostInList(revisionPath)
result = {
success: 1,
json: json
}
}
}
abeExtend.hooks.instance.trigger('afterPublish', result)
resolve(result)
})
})
Expand All @@ -111,89 +95,74 @@ export function publish(filePath, tplPath, json) {

export function unpublish(filePath) {
abeExtend.hooks.instance.trigger('beforeUnpublish', filePath)
filePath = coreUtils.slug.clean(filePath)

var tplUrl = cmsData.file.fromUrl(path.join(config.publish.url, filePath))
if(coreUtils.file.exist(tplUrl.json.path)) {
var json = JSON.parse(JSON.stringify(cmsData.file.get(tplUrl.json.path)))
if(json.abe_meta.publish != null) {
delete json.abe_meta.publish
}

cmsOperations.save.save(
path.join(config.root, config.draft.url, json.abe_meta.link.replace(config.root)),
var p = draft(
filePath,
json.abe_meta.template,
json,
'',
'reject',
null,
'reject'
"draft"
)
.then((resSave) => {

p.then((result) => {
cmsOperations.remove.removeFile(tplUrl.publish.path, tplUrl.publish.json)
abeExtend.hooks.instance.trigger('afterUnpublish', tplUrl.publish.path, tplUrl.publish.json)
Manager.instance.updatePostInList(resSave.jsonPath)
}).catch(function(e) {
console.error('[ERROR] post-publish.js', e)
})
}
}

export function reject(filePath, tplPath, json) {
abeExtend.hooks.instance.trigger('beforeReject', filePath)

var p = new Promise((resolve, reject) => {
var p1 = new Promise((resolve) => {
cmsOperations.save.save(
path.join(config.root, config.draft.url, filePath.replace(config.root)),
tplPath,
json,
'',
'draft',
null,
'reject')
.then((resSave) => {
resolve()
}).catch(function(e) {
console.error(e)
})
})
var tplUrl = cmsData.file.fromUrl(path.join(config.publish.url, filePath))
// if(coreUtils.file.exist(tplUrl.json.path)) {
var json = JSON.parse(JSON.stringify(cmsData.file.get(tplUrl.json.path)))
if(json.abe_meta.publish != null) {
delete json.abe_meta.publish
}

p1.then((resSave) => {
cmsOperations.save.save(
path.join(config.root, config.draft.url, filePath.replace(config.root)),
tplPath,
var p2 = draft(
filePath,
json.abe_meta.template,
json,
'',
'reject',
resSave,
'reject')
.then((resSave) => {
var result
if(typeof resSave.error !== 'undefined' && resSave.error !== null ){
result = {
success: 0,
error: resSave.error
}
} else if(typeof resSave.reject !== 'undefined' && resSave.reject !== null){
resSave.success = 1
result = resSave
} else if(typeof resSave.json !== 'undefined' && resSave.json !== null){
result = {
success: 1,
json: resSave.json
}
}
abeExtend.hooks.instance.trigger('afterReject', result)
Manager.instance.updatePostInList(resSave.jsonPath)
resolve(result)
})
}).catch(function(e) {
console.error(e)
var result = {
success: 0,
error: 'reject error'
}
abeExtend.hooks.instance.trigger('afterReject', result)
resolve(result)
})
"draft"
)

p2.then((result) => {
abeExtend.hooks.instance.trigger('afterReject', result)
Manager.instance.updatePostInList(resSave.jsonPath)
resolve(result)
}).catch(function(e) {
console.error('[ERROR] post-publish.js', e)
})
// }
})

// var p = new Promise((resolve, reject) => {
// cmsOperations.save.save(
// path.join(config.root, config.draft.url, filePath.replace(config.root)),
// tplPath,
// json,
// '',
// 'draft')
// .then((resSave) => {
// abeExtend.hooks.instance.trigger('afterReject', result)
// Manager.instance.updatePostInList(resSave.jsonPath)
// resolve(result)
// }).catch(function(e) {
// console.error(e)
// })
// })

return p
}

0 comments on commit 5a74115

Please sign in to comment.