Skip to content

Commit

Permalink
refactor request okey
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslabbe committed Sep 22, 2016
1 parent df84324 commit dc5e0c6
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 154 deletions.
53 changes: 35 additions & 18 deletions src/cli/helpers/abe-get-select-template-keys.js
Expand Up @@ -16,32 +16,49 @@ import {
Manager
} from '../../cli'

var traverseFileSystem = function (currentPath, arr) {
var res = []
var files = fse.readdirSync(currentPath);
for (var i in files) {
var currentFile = currentPath + '/' + files[i]
var stats = fse.statSync(currentFile)
if (stats.isFile()) {
if (currentFile.indexOf(config.files.templates.extension) > -1) {
res.push(currentFile)
}
}
else if (stats.isDirectory()) {
res = res.concat(traverseFileSystem(currentFile))
}
}
return res
};

var findTemplates = function(templatesPath) {
var p = new Promise((resolve, reject) => {
execFile('find', [ templatesPath ], (err, stdout, stderr) => {
if (err) reject(err)

var file_list = stdout.split('\n')
var file_list_with_extention = []
Array.prototype.forEach.call(file_list, (file) => {
if (file.indexOf(config.files.templates.extension) > -1) {
file_list_with_extention.push(file)
}
})

resolve(file_list_with_extention)
});
let templatesList = traverseFileSystem(templatesPath)
resolve(templatesList)
})

return p
}

var findRequestKeys = function(file_list_with_extention) {
/**
* Get columns and where.left ids of a select statement
*
* select title, image from ../ where template=""
*
* return [title, image, template]
*
* @param {Array} templatesList ["article.html", "other.html"]
* @return {Promise}
*/
var findRequestColumns = function(templatesList) {
var whereKeysCheck = {}
var whereKeys = []
var p = new Promise((resolve, reject) => {
let util = new Util()
Array.prototype.forEach.call(file_list_with_extention, (file) => {
Array.prototype.forEach.call(templatesList, (file) => {
var template = fse.readFileSync(file, 'utf8')
var matches = util.dataRequest(template)

Expand Down Expand Up @@ -78,15 +95,15 @@ var findRequestKeys = function(file_list_with_extention) {
var getSelectTemplateKeys = function(templatesPath) {
var p = new Promise((resolve, reject) => {
findTemplates(templatesPath)
.then((file_list_with_extention) => {
.then((templatesList) => {

findRequestKeys(file_list_with_extention)
findRequestColumns(templatesList)
.then((whereKeys) => {

resolve(whereKeys)
},
() => {
console.log('findRequestKeys reject')
console.log('findRequestColumns reject')
reject()
})
.catch((e) => {
Expand Down
19 changes: 7 additions & 12 deletions src/cli/helpers/abe-sql.js
Expand Up @@ -403,7 +403,7 @@ export default class Sql {
for(let file of files) {
if(limit < maxLimit || maxLimit === -1) {
if (file.published === true) {
var doc = Sql.executeWhereClauseToFile(file, wheres, jsonPage)
var doc = Sql.executeWhereClauseOnDocument(file, wheres, jsonPage)

if(doc) {
var json = JSON.parse(JSON.stringify(doc))
Expand Down Expand Up @@ -570,26 +570,21 @@ export default class Sql {
return shouldAdd
}

static executeWhereClauseToFile(file, wheres, jsonPage) {
var json = file
// if (fileUtils.isFile(file.path)) {
// json = fse.readJsonSync(file.path)
// }
//
var shouldAdd = json
static executeWhereClauseOnDocument(jsonDoc, wheres, jsonOriginalDoc) {
var shouldAdd = jsonDoc

if(typeof wheres !== 'undefined' && wheres !== null) {
let meta = config.meta.name
if(typeof json[meta] !== 'undefined' && json[meta] !== null) {
if(typeof jsonDoc[meta] !== 'undefined' && jsonDoc[meta] !== null) {
Array.prototype.forEach.call(wheres, (where) => {
var value
var compare

if(where.left === 'template' || where.left === 'abe_meta.template') {
value = FileParser.getTemplate(json[meta].template)
value = FileParser.getTemplate(jsonDoc[meta].template)
}else {
try {
value = eval('json.' + where.left)
value = eval('jsonDoc.' + where.left)
}catch(e) {
// console.log('e', e)
}
Expand All @@ -599,7 +594,7 @@ export default class Sql {
var matchVariable = /^{{(.*)}}$/.exec(compare)
if(typeof matchVariable !== 'undefined' && matchVariable !== null && matchVariable.length > 0) {
try {
var shouldCompare = eval('jsonPage.' + matchVariable[1])
var shouldCompare = eval('jsonOriginalDoc.' + matchVariable[1])
if(typeof shouldCompare !== 'undefined' && shouldCompare !== null) {
compare = shouldCompare
}else {
Expand Down
4 changes: 1 addition & 3 deletions src/cli/helpers/file-parser.js
Expand Up @@ -391,15 +391,14 @@ export default class FileParser {
}
}

static getAllFilesWithMeta(withKeys) {
static getAllFilesWithKeys(withKeys) {
var site = folderUtils.folderInfos(config.root)

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

var i = 0

var t2 = new TimeMesure('files.forEach')
files.forEach(function (file) {
// var t = new TimeMesure('add files')
var cleanFile = file
Expand All @@ -416,7 +415,6 @@ export default class FileParser {
filesArr.push(cleanFile)
// t.duration()
})
t2.duration()

var merged = fileUtils.getFilesMerged(filesArr)

Expand Down
134 changes: 14 additions & 120 deletions src/cli/helpers/file-utils.js
Expand Up @@ -217,79 +217,6 @@ export default class FileUtils {
})
}
}

/* TODO: put this method in its right helper */
// static checkMergedFile(file, merged) {

// var cleanFilePath = file.cleanFilePath
// var revision = file
// revision.status = file.status
// revision.filePath = file.filePath
// revision.date = file.abe_meta && file.abe_meta.latest ? file.abe_meta.latest.date : ''
// revision.template = file[config.meta.name].template ? file[config.meta.name].template.replace(/^\/+/, '') : ''
// revision.cleanFilePath = cleanFilePath

// revision[config.meta.name] = file[config.meta.name]

// if(typeof merged[cleanFilePath] === 'undefined' || merged[cleanFilePath] === null) {
// merged[cleanFilePath] = {}

// merged[cleanFilePath].cleanFilePath = revision.cleanFilePath
// merged[cleanFilePath].date = revision.date
// merged[cleanFilePath].template = revision.template
// merged[cleanFilePath][config.meta.name] = revision[config.meta.name]
// merged[cleanFilePath][file.status] = revision
// }else {
// var oldDate = new Date(merged[cleanFilePath].date)
// var newDate = new Date(revision.date)
// var oldStatus = ''
// if(typeof merged[cleanFilePath][revision.status] !== 'undefined' && merged[cleanFilePath][revision.status] !== null) {
// oldStatus = merged[cleanFilePath][revision.status].status
// }
// var newStatus = revision.status

// // if draft > publish
// if(typeof merged[cleanFilePath][newStatus] === 'undefined' || merged[cleanFilePath][newStatus] === null) {
// merged[cleanFilePath][newStatus] = revision
// }else if(newDate > oldDate && oldStatus === newStatus) {
// merged[cleanFilePath][file.status] = revision
// }
// }
// }

/* TODO: put this method in its right helper */
// static mergeFiles(files1, files2) {
// var merged = {}
// var arMerged = []

// Array.prototype.forEach.call(files1, (file) => {
// FileUtils.checkMergedFile(file, merged)
// })

// Array.prototype.forEach.call(files2, (file) => {
// FileUtils.checkMergedFile(file, merged)
// })

// Array.prototype.forEach.call(Object.keys(merged), (key) => {
// var merge = merged[key]
// var publishedDate = (typeof merge.published !== 'undefined' && merge.published !== null) ? new Date(merge.published.date) : null
// var draftDate = (typeof merge.draft !== 'undefined' && merge.draft !== null) ? new Date(merge.draft.date) : null

// if(publishedDate !== null && draftDate !== null && publishedDate >= draftDate) {
// merge.draft = null
// }
// var revision = {
// path: merge.cleanFilePath,
// template: merge.template,
// published: merge.published,
// date: merge.date,
// draft: merge.draft
// }
// arMerged.push(revision)
// })

// return arMerged
// }

/* TODO: put this method in its right helper */
static getFilesMerged(files) {
Expand All @@ -298,62 +225,29 @@ export default class FileUtils {

Array.prototype.forEach.call(files, (file) => {
var cleanFilePath = file.cleanFilePath
var revision = file
revision.status = file.status
revision.filePath = file.filePath
revision.date = file.abe_meta && file.abe_meta.latest ? file.abe_meta.latest.date : ''
revision.template = file[config.meta.name].template ? file[config.meta.name].template.replace(/^\/+/, '') : ''
revision.cleanFilePath = cleanFilePath

revision[config.meta.name] = file[config.meta.name]

if(typeof merged[cleanFilePath] === 'undefined' || merged[cleanFilePath] === null) {
merged[cleanFilePath] = file
// merged[cleanFilePath] = {}

// merged[cleanFilePath].cleanFilePath = revision.cleanFilePath
// merged[cleanFilePath].date = revision.date
// merged[cleanFilePath].template = revision.template
// merged[cleanFilePath][config.meta.name] = revision[config.meta.name]
merged[cleanFilePath][file.status] = true
}else {
var oldDate = new Date(merged[cleanFilePath].date)
var newDate = new Date(file.date)
var oldStatus = ''
if(merged[cleanFilePath][file.status]) {
oldStatus = file.status
}
// var newStatus = revision.status

// if draft > publish
if(typeof merged[cleanFilePath][file.status] === 'undefined' || merged[cleanFilePath][file.status] === null) {
merged[cleanFilePath][file.status] = true
}else if(newDate > oldDate && oldStatus === file.status) {
if(typeof merged[cleanFilePath] === 'undefined' || merged[cleanFilePath] === null) {
merged[cleanFilePath] = file
merged[cleanFilePath][file.status] = true
}else {
var oldDate = new Date(merged[cleanFilePath].date)
var newDate = new Date(file.date)
var oldStatus = ''
if(merged[cleanFilePath][file.status]) {
oldStatus = file.status
}
if(typeof merged[cleanFilePath][file.status] === 'undefined' || merged[cleanFilePath][file.status] === null) {
merged[cleanFilePath][file.status] = true
}else if(newDate > oldDate && oldStatus === file.status) {
merged[cleanFilePath][file.status] = true
}
}
}
})

// return merged
Array.prototype.forEach.call(Object.keys(merged), (key) => {
var merge = merged[key]
arMerged.push(merge)

// var publishedDate = (typeof merge.published !== 'undefined' && merge.published !== null) ? new Date(merge.published.date) : null
// var draftDate = (typeof merge.draft !== 'undefined' && merge.draft !== null) ? new Date(merge.draft.date) : null

// if(publishedDate !== null && draftDate !== null && publishedDate >= draftDate) {
// merge.draft = null
// }
// var revision = {
// fileUrl: path.join(merge.cleanFilePath.replace(/\.json/, `.${config.files.templates.extension}`)),
// path: merge.cleanFilePath,
// template: merge.template,
// published: merge.published,
// date: merge.date,
// draft: merge.draft
// }
// arMerged.push(revision)
})

return arMerged
Expand Down
5 changes: 4 additions & 1 deletion src/cli/models/Manager.js
Expand Up @@ -43,6 +43,7 @@ class Manager {
}

_init() {
this._loadTime = new TimeMesure('Loading Manager')
const pathTemplate = path.join(config.root, config.templates.url);
getSelectTemplateKeys(pathTemplate)
.then((whereKeys) => {
Expand All @@ -55,7 +56,9 @@ class Manager {
}

updateList() {
this._list = FileParser.getAllFilesWithMeta(this._whereKeys)

this._list = FileParser.getAllFilesWithKeys(this._whereKeys)
this._loadTime.duration()
// console.log('* * * * * * * * * * * * * * * * * * * * * * * * * * * * *')
// console.log('this._list[0]', this._list[0])

Expand Down

0 comments on commit dc5e0c6

Please sign in to comment.