Skip to content

Commit

Permalink
refactoring: cmsData
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorybesson committed Oct 7, 2016
1 parent 2f2c412 commit 675e71b
Show file tree
Hide file tree
Showing 25 changed files with 344 additions and 342 deletions.
6 changes: 3 additions & 3 deletions src/cli/Builder.js
Expand Up @@ -7,7 +7,7 @@ import {
FileParser,
config,
fileAttr,
cmsTemplate,
cmsTemplates,
Page
} from './'

Expand All @@ -28,7 +28,7 @@ class Builder {
.replace('.' + config.files.templates.extension, '.json')

var json = fse.readJsonSync(file.path)
var text = cmsTemplate.template.getTemplate(json.abe_meta.template)
var text = cmsTemplates.template.getTemplate(json.abe_meta.template)

cmsData.source.getDataList(path.dirname(json.abe_meta.link), text, json)
.then(() => {
Expand All @@ -42,7 +42,7 @@ class Builder {
}
else if(file.path.indexOf('.json') > -1){
var json = fse.readJsonSync(file.path)
var text = cmsTemplate.template.getTemplate(json.abe_meta.template)
var text = cmsTemplates.template.getTemplate(json.abe_meta.template)

cmsData.source.getDataList(path.dirname(json.abe_meta.link), text, json)
.then(() => {
Expand Down
8 changes: 4 additions & 4 deletions src/cli/cms/Page.js
Expand Up @@ -6,7 +6,7 @@ import {
cmsEditor,
abeEngine,
cmsData,
cmsTemplate,
cmsTemplates,
config,
Hooks,
Manager
Expand Down Expand Up @@ -117,7 +117,7 @@ export default class Page {
var matches = this.template.match(eachSource)
if(typeof matches !== 'undefined' && matches !== null) {
Array.prototype.forEach.call(matches, (match) => {
this.template = this.template.replace(match, `${match}<!-- [[${keys[i]}]] ${cmsTemplate.encodeAbeTagAsComment(match)} -->`)
this.template = this.template.replace(match, `${match}<!-- [[${keys[i]}]] ${cmsTemplates.encodeAbeTagAsComment(match)} -->`)
})
}
}
Expand All @@ -139,7 +139,7 @@ export default class Page {
}

// I compile the text
var compiledTemplate = Handlebars.compile((!this._onlyHTML) ? cmsTemplate.insertDebugtoolUtilities(this.template) : this.template)
var compiledTemplate = Handlebars.compile((!this._onlyHTML) ? cmsTemplates.insertDebugtoolUtilities(this.template) : this.template)

// I create the html page ! yeah !!!
this.html = compiledTemplate(json, {data: {intl: config.intlData}})
Expand Down Expand Up @@ -210,7 +210,7 @@ export default class Page {
var textEachWithIndex = block.replace(/(<(?![\/])[A-Za-z0-9!-]*)/g, '$1 data-abe-block="' + key + '{{@index}}"')

// je remplace le block dans le texte par ça
this.template = this.template.replace(block, textEachWithIndex + `<!-- [[${key}]] ${cmsTemplate.encodeAbeTagAsComment(blockHtml)} -->`)
this.template = this.template.replace(block, textEachWithIndex + `<!-- [[${key}]] ${cmsTemplates.encodeAbeTagAsComment(blockHtml)} -->`)
}

// Pour chaque tag Abe, je mets en forme ce tag avec des data- supplémentaires
Expand Down
129 changes: 129 additions & 0 deletions src/cli/cms/data/file.js
@@ -0,0 +1,129 @@
import path from 'path'
import fse from 'fs-extra'

import {
Hooks,
FileParser,
cmsData,
config
} from '../../'

export function getAllWithKeys(withKeys) {
var files = FileParser.getFiles(path.join(config.root, config.data.url), true, 99, /\.json/)
var filesArr = []

var i = 0

files.forEach(function (file) {
var cleanFile = file
var json = cmsData.file.get(file.path)

if(typeof json.abe_meta.latest !== 'undefined' && json.abe_meta.latest !== null
&& typeof json.abe_meta.latest !== 'undefined' && json.abe_meta.latest !== null
&& typeof json.abe_meta.latest.date !== 'undefined' && json.abe_meta.latest.date !== null) {
file.date = json.abe_meta.latest.date
}

if(typeof json.abe_meta !== 'undefined' && json.abe_meta !== null) {
var date = null
if (typeof json.abe_meta.latest !== 'undefined' && json.abe_meta.latest !== null
&& typeof json.abe_meta.latest.date !== 'undefined' && json.abe_meta.latest.date !== null) {
date = json.abe_meta.latest.date
}else if (typeof json.abe_meta.date !== 'undefined' && json.abe_meta.date !== null) {
date = json.abe_meta.date
}
cleanFile.abe_meta = {
date: date
, type: (typeof json.abe_meta.type !== 'undefined' && json.abe_meta.type !== null) ? json.abe_meta.type : null
, link: (typeof json.abe_meta.link !== 'undefined' && json.abe_meta.link !== null) ? json.abe_meta.link : null
, template: (typeof json.abe_meta.template !== 'undefined' && json.abe_meta.template !== null) ? json.abe_meta.template : null
, status: (typeof json.abe_meta.status !== 'undefined' && json.abe_meta.status !== null) ? json.abe_meta.status : null
, cleanName: (typeof json.abe_meta.cleanName !== 'undefined' && json.abe_meta.cleanName !== null) ? json.abe_meta.cleanName : null
, cleanFilename: (typeof json.abe_meta.cleanFilename !== 'undefined' && json.abe_meta.cleanFilename !== null) ? json.abe_meta.cleanFilename : null
}
}
Array.prototype.forEach.call(withKeys, (key) => {
var keyFirst = key.split('.')[0]
cleanFile[keyFirst] = json[keyFirst]
})
filesArr.push(cleanFile)
})

var merged = cmsData.revision.getFilesMerged(filesArr)

Hooks.instance.trigger('afterGetAllFiles', merged)
return merged
}

export function get(pathJson) {
var json = {}
pathJson = Hooks.instance.trigger('beforeGetJson', pathJson)

try {
var stat = fse.statSync(pathJson)
if (stat) {
json = fse.readJsonSync(pathJson)
}
}catch(e) {
}

json = Hooks.instance.trigger('afterGetJson', json)
return json
}

export function fromUrl(url) {
var res = {
root: '',
draft: {
dir: '',
file: '',
path: ''
},
publish: {
dir: '',
file: '',
link: '',
path: '',
json: ''
},
json: {
path: '',
file: ''
}
}

if(typeof url !== 'undefined' && url !== null) {

var dir = path.dirname(url).replace(config.root, '')
var filename = path.basename(url)
var link = url.replace(config.root, '')
link = link.replace(/^\//, '').split('/')
link.shift()
link = cmsData.fileAttr.delete('/' + link.join('/').replace(/\/$/, ''))

let draft = config.draft.url
let publish = config.publish.url
let data = config.data.url

res.root = config.root

// set dir path draft/json
res.draft.dir = FileParser.changePathEnv(path.join(config.root, dir), draft)
res.json.dir = FileParser.changePathEnv(path.join(config.root, dir), data)
res.publish.dir = FileParser.changePathEnv(path.join(config.root, dir), publish)
res.publish.json = res.json.dir

// set filename draft/json
res.draft.file = filename
res.publish.file = cmsData.fileAttr.delete(filename)
res.publish.link = link
res.json.file = filename.replace(`.${config.files.templates.extension}`, '.json')
res.publish.json = path.join(res.json.dir, cmsData.fileAttr.delete(res.json.file))

// set filename draft/json
res.draft.path = path.join(res.draft.dir, res.draft.file)
res.publish.path = path.join(res.publish.dir, res.publish.file)
res.json.path = path.join(res.json.dir, res.json.file)
}
return res
}
22 changes: 12 additions & 10 deletions src/cli/cms/data/index.js
Expand Up @@ -6,16 +6,18 @@ import * as regex from './regex'
import * as sql from './sql'
import * as source from './source'
import * as revision from './revision'
import * as meta from './meta'
import * as metas from './metas'
import * as file from './file'

export {
values
,attributes
,attr
,fileAttr
,sql
,source
,revision
,regex
,meta
values,
attributes,
attr,
fileAttr,
sql,
source,
revision,
regex,
metas,
file
}
24 changes: 24 additions & 0 deletions src/cli/cms/data/meta.js → src/cli/cms/data/metas.js
Expand Up @@ -25,4 +25,28 @@ export function add(tpl, json, type, obj = {}, date = null, realType = 'draft')
json[meta][type].latest = JSON.parse(JSON.stringify(obj))
json[meta][type].latest.date = currentDate
json[meta][type].latest.abeUrl = abeUrl
}

export function get(arr) {
var res = []
Array.prototype.forEach.call(arr, (file) => {
let meta = config.meta.name

var jsonPath = cmsData.file.fromUrl(file.path).json.path
var json = cmsData.file.get(jsonPath)
if(typeof json[meta] === 'undefined' || json[meta] === null) json[meta] = {}
file['template'] = json[meta].template
if(typeof json[meta].latest !== 'undefined' && json[meta].latest !== null) {
file['date'] = json[meta].latest.date
}
if(typeof json[meta].complete === 'undefined' || json[meta].complete === null) {
json[meta].complete = 0
}
if(typeof json[meta] !== 'undefined' && json[meta] !== null) {
file[config.meta.name] = json[meta]
}
res.push(file)
})

return res
}
28 changes: 4 additions & 24 deletions src/cli/cms/data/revision.js
Expand Up @@ -12,14 +12,14 @@ import {
export function getFilesRevision(urls, fileName) {
var res = []
var number = 1
var tplUrl = FileParser.getFileDataFromUrl(fileName)
var tplUrl = cmsData.file.fromUrl(fileName)
fileName = fileName.split('/')
fileName = fileName[fileName.length - 1]
var publishDate = new Date()
var json = null

if(coreUtils.file.exist(tplUrl.publish.json)) {
json = FileParser.getJson(tplUrl.publish.json)
json = cmsData.file.get(tplUrl.publish.json)
if(typeof json !== 'undefined' && json !== null
&& typeof json[config.meta.name] !== 'undefined' && json[config.meta.name] !== null) {
publishDate = new Date(json[config.meta.name].latest.date)
Expand Down Expand Up @@ -47,9 +47,9 @@ export function getFilesRevision(urls, fileName) {
urlObj.version = number
number = number + 1

var tplUrlObj = FileParser.getFileDataFromUrl(urlObj.path)
var tplUrlObj = cmsData.file.fromUrl(urlObj.path)
if(coreUtils.file.exist(tplUrlObj.publish.json)) {
var jsonObj = FileParser.getJson(tplUrlObj.publish.json)
var jsonObj = cmsData.file.get(tplUrlObj.publish.json)
urlObj[config.meta.name] = jsonObj[config.meta.name]
}
res.push(urlObj)
Expand Down Expand Up @@ -212,24 +212,4 @@ export function getFilesMerged(files) {
})

return arMerged
}

export function deleteOlderRevisionByType(fileName, type) {
var folder = fileName.split('/')
var file = folder.pop()
var extension = file.replace(/.*?\./, '')
folder = folder.join('/')
var stat = fse.statSync(folder)
if(stat){
var files = FileParser.getFiles(folder, true, 1, new RegExp('\\.' + extension))
files.forEach(function (fileItem) {
var fname = cmsData.fileAttr.delete(fileItem.cleanPath)
var ftype = cmsData.fileAttr.get(fileItem.cleanPath).s
if(fname === file && ftype === type){
var fileDraft = fileItem.path.replace(/-abe-./, '-abe-d')
FileParser.removeFile(fileItem.path, FileParser.changePathEnv(fileItem.path, config.data.url).replace(new RegExp('\\.' + extension), '.json'))
FileParser.removeFile(fileDraft, FileParser.changePathEnv(fileDraft, config.data.url).replace(new RegExp('\\.' + extension), '.json'))
}
})
}
}
2 changes: 1 addition & 1 deletion src/cli/cms/data/source.js
Expand Up @@ -141,7 +141,7 @@ export function urlList(obj, tplPath, match, jsonPage) {

export function fileList(obj, tplPath, match, jsonPage) {
var p = new Promise((resolve) => {
jsonPage['abe_source'][obj.key] = FileParser.getJson(path.join(config.root, obj.sourceString))
jsonPage['abe_source'][obj.key] = cmsData.file.get(path.join(config.root, obj.sourceString))
resolve()
})

Expand Down
10 changes: 5 additions & 5 deletions src/cli/cms/operations/create.js
Expand Up @@ -2,7 +2,7 @@ import path from 'path'
import {
FileParser,
coreUtils,
cmsTemplate,
cmsTemplates,
cmsOperations,
config,
Hooks,
Expand All @@ -20,12 +20,12 @@ var create = function(template, pathCreate, name, req, forceJson = {}, duplicate
filePath = path.join(config.root, config.draft.url, filePath.replace(config.root))

if(templatePath !== null && filePath !== null) {
var tplUrl = FileParser.getFileDataFromUrl(filePath)
var tplUrl = cmsData.file.fromUrl(filePath)

if(!coreUtils.file.exist(tplUrl.json.path)) {
var json = (forceJson) ? forceJson : {}
var tpl = templatePath
var text = cmsTemplate.template.getTemplate(tpl)
var text = cmsTemplates.template.getTemplate(tpl)
if (duplicate) {
json = cmsData.values.removeDuplicate(text, json)
}
Expand All @@ -40,14 +40,14 @@ var create = function(template, pathCreate, name, req, forceJson = {}, duplicate
.then((resSave) => {
Manager.instance.updateList()
filePath = resSave.htmlPath
tplUrl = FileParser.getFileDataFromUrl(filePath)
tplUrl = cmsData.file.fromUrl(filePath)
resolve(resSave.json)
}).catch(function(e) {
reject()
console.error(e)
})
}else {
json = FileParser.getJson(tplUrl.json.path)
json = cmsData.file.get(tplUrl.json.path)
resolve(json, tplUrl.json.path)
}
}else {
Expand Down
5 changes: 3 additions & 2 deletions src/cli/cms/operations/duplicate.js
Expand Up @@ -3,6 +3,7 @@ import {
FileParser,
Manager,
config,
cmsData,
cmsOperations
} from '../../'

Expand All @@ -28,7 +29,7 @@ var duplicate = function(oldFilePath, template, newPath, name, req, isUpdate = f

if(typeof revisions !== 'undefined' && revisions !== null
&& typeof revisions[0] !== 'undefined' && revisions[0] !== null) {
json = FileParser.getJson(revisions[0].path)
json = cmsData.file.get(revisions[0].path)
}
}

Expand All @@ -37,7 +38,7 @@ var duplicate = function(oldFilePath, template, newPath, name, req, isUpdate = f

if (isUpdate) {
Hooks.instance.trigger('beforeUpdate', json, oldFilePath, template, newPath, name, req, isUpdate)
FileParser.deleteFile(oldFilePath)
cmsOperations.remove.remove(oldFilePath)
}
Hooks.instance.trigger('afterDuplicate', json, oldFilePath, template, newPath, name, req, isUpdate)

Expand Down

0 comments on commit 675e71b

Please sign in to comment.