Skip to content

Commit

Permalink
fix:update file + UT + other bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorybesson committed Nov 7, 2016
1 parent 5ac0c33 commit 6da4337
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/cli/cms/operations/duplicate.js
Expand Up @@ -15,7 +15,7 @@ const duplicate = function(oldPostUrl, template, newPath, name, req, isUpdate =

let json = {}
let revisions = []
const newPostUrl = path.join(newPath, name) + '.' + config.files.templates.extension
const newPostUrl = path.join(newPath, name)
if(oldPostUrl != null) {
const files = Manager.instance.getList()
const oldPostDataPath = path.join(config.root, config.data.url, oldPostUrl.replace('.' + config.files.templates.extension, '.json'))
Expand Down
6 changes: 3 additions & 3 deletions src/cli/cms/operations/post.js
Expand Up @@ -55,7 +55,7 @@ export function publish(filePath, tplPath, json) {
.then(() => {
resolve()
}).catch(function(e) {
console.error(e)
console.error(e.stack)
})
})

Expand Down Expand Up @@ -150,7 +150,7 @@ export function reject(filePath, tplPath, json) {
.then((resSave) => {
resolve()
}).catch(function(e) {
console.error(e)
console.error(e.stack)
})
})

Expand Down Expand Up @@ -184,7 +184,7 @@ export function reject(filePath, tplPath, json) {
resolve(result)
})
}).catch(function(e) {
console.error(e)
console.error(e.stack)
var result = {
success: 0,
error: 'reject error'
Expand Down
82 changes: 41 additions & 41 deletions src/cli/cms/operations/save.js
Expand Up @@ -107,56 +107,56 @@ export function save(url, tplPath = null, json = null, text = '', type = '', pre
}

cmsData.source.getDataList(path.dirname(tplUrl.publish.link), text, json)
.then(() => {
json = abeExtend.hooks.instance.trigger('afterGetDataListOnSave', json)
for(var prop in json){
if(typeof json[prop] === 'object' && Array.isArray(json[prop]) && json[prop].length === 1){
var valuesAreEmpty = true
json[prop].forEach(function (element) {
for(var p in element) {
if(element[p] !== ''){
valuesAreEmpty = false
}
}
})
if(valuesAreEmpty){
delete json[prop]
.then(() => {
json = abeExtend.hooks.instance.trigger('afterGetDataListOnSave', json)
for(var prop in json){
if(typeof json[prop] === 'object' && Array.isArray(json[prop]) && json[prop].length === 1){
var valuesAreEmpty = true
json[prop].forEach(function (element) {
for(var p in element) {
if(element[p] !== ''){
valuesAreEmpty = false
}
}
})
if(valuesAreEmpty){
delete json[prop]
}
}
}

var obj = {
publishAll:publishAll,
type:type,
template:{
path: fullTpl
},
html: {
path:pathIso.htmlPath
},
json: {
content: json,
path: pathIso.jsonPath
}
}
var obj = {
publishAll:publishAll,
type:type,
template:{
path: fullTpl
},
html: {
path:pathIso.htmlPath
},
json: {
content: json,
path: pathIso.jsonPath
}
}

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

obj.json.content[meta].complete = checkRequired(text, obj.json.content)
obj.json.content[meta].complete = checkRequired(text, obj.json.content)

var res = saveJsonAndHtml(tpl.replace(/^\/+/, ''), obj, text)
if (isRejectedDoc) {
res.reject = cmsData.fileAttr.delete(url).replace(path.join(config.root, config.draft.url), '')
}
var res = saveJsonAndHtml(tpl.replace(/^\/+/, ''), obj, text)
if (isRejectedDoc) {
res.reject = cmsData.fileAttr.delete(url).replace(path.join(config.root, config.draft.url), '')
}

abeExtend.hooks.instance.trigger('afterSave', obj)
cmsTemplates.assets.copy()
abeExtend.hooks.instance.trigger('afterSave', obj)

cmsTemplates.assets.copy()

resolve(res)
}).catch(function(e) {
console.error('Save.js', e)
})
resolve(res)
}).catch(function(e) {
console.error('save.js', e.stack)
})
})

return p
Expand Down
6 changes: 5 additions & 1 deletion src/cli/core/manager/Manager.js
Expand Up @@ -216,7 +216,11 @@ class Manager {
const index = found[0]
merged[parentRelativePath] = this._list[index]
merged[parentRelativePath].revisions.push(JSON.parse(JSON.stringify(revision)))
const sortedResult = cmsData.revision.sortRevisions(merged)
const sortedResult = cmsData.revision.sortRevisions(merged)
// Does the publish version has been removed (in the case of unpublish) ?
if(sortedResult[0]['publish'] && !coreUtils.file.exist(sortedResult[0]['publish']['path'])){
delete sortedResult[0]['publish']
}
this._list[index] = sortedResult[0]
} else {
let rev = []
Expand Down
2 changes: 1 addition & 1 deletion src/server/routes/get-main.js
Expand Up @@ -110,7 +110,7 @@ var route = function(req, res, next) {
}

var editorWidth = '33%'
req.headers && req.headers.cookie.split(';').forEach(function(cookie) {
req.headers && req.headers.cookie && req.headers.cookie.split(';').forEach(function(cookie) {
var parts = cookie.match(/(.*?)=(.*)$/)
if(parts[1] === 'editorWidth') editorWidth = parts[2]
})
Expand Down
20 changes: 12 additions & 8 deletions test/operations.js
Expand Up @@ -25,10 +25,6 @@ describe('Create', function() {
}.bind(this))
});

/**
* cmsOperations.create
*
*/
it('cmsOperations.create()', function(done) {
cmsOperations.create('article', '', 'article-2.html', {query: ''}, this.fixture.jsonArticle, false)
.then(function(resSave) {
Expand All @@ -43,10 +39,6 @@ describe('Create', function() {
}.bind(this));
});

/**
* cmsOperations.duplicate
*
*/
it('cmsOperations.duplicate()', function(done) {
cmsOperations.duplicate('article-1.html', 'article', '', 'article-2.html', {}, false)
.then(function(resSave) {
Expand All @@ -59,4 +51,16 @@ describe('Create', function() {
done()
}.bind(this))
});

it('cmsOperations.duplicate() update', function(done) {
cmsOperations.duplicate('article-1.html', 'article', '', 'article-1.html', {}, true)
.then(function(resSave) {
var json = path.join(config.root, config.data.url, resSave.abe_meta.link.replace('.html', '.json'))
var stat = fse.statSync(json)
if (stat) {
chai.expect(stat).to.not.be.undefined;
}
done()
}.bind(this))
});
});

0 comments on commit 6da4337

Please sign in to comment.