From 8aca5641673f0468bafde2065acdfadd2787086f Mon Sep 17 00:00:00 2001 From: nicolaslabbe Date: Mon, 7 Nov 2016 12:07:10 +0100 Subject: [PATCH] refactor unpublis --- src/cli/cms/data/metas.js | 13 +++++++ src/cli/cms/operations/create.js | 62 ++++++++++++++------------------ src/cli/cms/operations/post.js | 39 +++++--------------- src/cli/core/manager/Manager.js | 2 ++ 4 files changed, 50 insertions(+), 66 deletions(-) diff --git a/src/cli/cms/data/metas.js b/src/cli/cms/data/metas.js index 3545577..4ebf5e4 100644 --- a/src/cli/cms/data/metas.js +++ b/src/cli/cms/data/metas.js @@ -25,3 +25,16 @@ export function add(json, type, date = null) { json[meta][type].latest.date = currentDate json[meta][type].latest.abeUrl = abeUrl } + + +export function create(json, template, url) { + let meta = config.meta.name + if (json[meta] == null) { + json[meta] = {} + } + + json[meta].template = template + json[meta].link = url + + return json +} diff --git a/src/cli/cms/operations/create.js b/src/cli/cms/operations/create.js index 94fb5e3..ebca05c 100644 --- a/src/cli/cms/operations/create.js +++ b/src/cli/cms/operations/create.js @@ -13,44 +13,34 @@ var create = function(template, pathCreate, name, req, forceJson = {}, duplicate var p = new Promise((resolve, reject) => { abeExtend.hooks.instance.trigger('beforeCreate', template, pathCreate, name, req, forceJson) - var templatePath = path.join(config.root, config.templates.url, `${template}.${config.files.templates.extension}`) - var filePath = path.join(pathCreate, name) - filePath = coreUtils.slug.clean(filePath) - filePath = path.join(config.root, config.draft.url, filePath.replace(config.root,'')) - if(templatePath !== null && filePath !== null) { - var tplUrl = cmsData.file.fromUrl(filePath) - - if(!coreUtils.file.exist(tplUrl.json.path)) { - var json = (forceJson) ? forceJson : {} - var tpl = templatePath - var text = cmsTemplates.template.getTemplate(tpl) - if (duplicate) { - json = cmsData.values.removeDuplicate(text, json) - } - text = cmsData.source.removeDataList(text) - var resHook = abeExtend.hooks.instance.trigger('beforeFirstSave', filePath, req.query, json, text) - filePath = resHook.filePath - json = resHook.json - text = resHook.text - - abeExtend.hooks.instance.trigger('afterCreate', json, text, pathCreate, name, req, forceJson) - cmsOperations.save.save(filePath, template, json, text, 'draft') - .then((resSave) => { - Manager.instance.updatePostInList(resSave.jsonPath) - filePath = resSave.htmlPath - tplUrl = cmsData.file.fromUrl(filePath) - resolve(resSave.json) - }).catch(function(e) { - reject() - console.error(e) - }) - }else { - json = cmsData.file.get(tplUrl.json.path) - resolve(json, tplUrl.json.path) + var postUrl = path.join(pathCreate, name) + postUrl = coreUtils.slug.clean(postUrl) + + var filePath = path.join(config.root, config.draft.url, postUrl) + + var json = (forceJson) ? forceJson : {} + json = cmsData.metas.create(json, template, postUrl) + + if (duplicate) { + if(json.abe_meta.publish != null) { + delete json.abe_meta.publish } - }else { - reject() + json = cmsData.values.removeDuplicate(text, json) } + var resHook = abeExtend.hooks.instance.trigger('beforeFirstSave', filePath, req.query, json) + + var p2 = cmsOperations.post.draft( + postUrl, + json.abe_meta.template, + json, + "draft" + ) + p2.then((result) => { + abeExtend.hooks.instance.trigger('afterCreate', result) + resolve(result.json) + }).catch(function(e) { + console.error('[ERROR] create.js', e) + }) }).catch(function(e) { console.error(e) }) diff --git a/src/cli/cms/operations/post.js b/src/cli/cms/operations/post.js index dff19ea..ded1470 100644 --- a/src/cli/cms/operations/post.js +++ b/src/cli/cms/operations/post.js @@ -95,9 +95,11 @@ export function publish(filePath, tplPath, json) { export function unpublish(filePath) { abeExtend.hooks.instance.trigger('beforeUnpublish', 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))) + + 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) + if(coreUtils.file.exist(revisionPath)) { + var json = JSON.parse(JSON.stringify(cmsData.file.get(revisionPath))) if(json.abe_meta.publish != null) { delete json.abe_meta.publish } @@ -110,11 +112,11 @@ export function unpublish(filePath) { ) 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) + cmsOperations.remove.removeFile(revisionPath, postPath) + abeExtend.hooks.instance.trigger('afterUnpublish', revisionPath, postPath) + Manager.instance.updatePostInList(revisionPath.replace(new RegExp('\\/', 'g'), path.sep)) }).catch(function(e) { - console.error('[ERROR] post-publish.js', e) + console.error('[ERROR] unpublish', e) }) } } @@ -123,45 +125,22 @@ export function reject(filePath, tplPath, json) { abeExtend.hooks.instance.trigger('beforeReject', filePath) var p = new Promise((resolve, reject) => { - 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 } - var p2 = draft( filePath, json.abe_meta.template, json, "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 } \ No newline at end of file diff --git a/src/cli/core/manager/Manager.js b/src/cli/core/manager/Manager.js index 4decc64..70ec9d6 100644 --- a/src/cli/core/manager/Manager.js +++ b/src/cli/core/manager/Manager.js @@ -202,6 +202,8 @@ class Manager { */ updatePostInList(pathFile){ const parentRelativePath = cmsData.fileAttr.delete(pathFile).replace(this._pathData + path.sep, '') + console.log('* * * * * * * * * * * * * * * * * * * * * * * * * * * * *') + console.log('parentRelativePath', parentRelativePath) const found = coreUtils.array.find(this._list, 'parentRelativePath', parentRelativePath) const json = cmsData.file.get(pathFile) let merged = {}