Passed
Push — master ( cf6f6f...466923 )
by
unknown
02:28
created

revision.js ➔ deleteOlderRevisionByType   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 3
dl 0
loc 19
rs 9.4285
nop 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A revision.js ➔ ... ➔ files.forEach 0 9 3
1
import path from 'path'
2
import fse from 'fs-extra'
3
4
import {
5
  FileParser,
6
  fileUtils,
7
  config,
8
  cmsData,
9
  Manager
10
} from '../../'
11
12
export function getFilesRevision(urls, fileName) {
13
  var res = []
14
  var number = 1
15
  var tplUrl = FileParser.getFileDataFromUrl(fileName)
16
  fileName = fileName.split('/')
17
  fileName = fileName[fileName.length - 1]
18
  var publishDate = new Date()
19
  var json = null
20
21
  if(fileUtils.isFile(tplUrl.publish.json)) {
22
    json = FileParser.getJson(tplUrl.publish.json)
23
    if(typeof json !== 'undefined' && json !== null
24
      && typeof json[config.meta.name] !== 'undefined' && json[config.meta.name] !== null) {
25
      publishDate = new Date(json[config.meta.name].latest.date)
26
    }
27
  }
28
29
  var publishVersion = false
30
  urls.forEach(function (urlObj) {
31
    var fileData = cmsData.fileAttr.get(urlObj.cleanPath)
32
    if(fileData.s === 'd' && cmsData.fileAttr.delete(urlObj.cleanPath) == cmsData.fileAttr.delete(fileName)) {
33
      var currentDate = new Date(urlObj.date)
34
      if(currentDate.getTime() > publishDate.getTime()) {
35
        if(!publishVersion && typeof res[res.length - 1] !== 'undefined' && res[res.length - 1] !== null) {
36
          res[res.length - 1].publishedDate = 'same'
37
        }
38
        publishVersion = true
39
        urlObj.publishedDate = 'after'
40
        
41
      }else if(currentDate.getTime() === publishDate.getTime()) {
42
        urlObj.publishedDate = 'same'
43
        publishVersion = true
44
      }else {
45
        urlObj.publishedDate = 'before'
46
      }
47
      urlObj.version = number
48
      number = number + 1
49
50
      var tplUrlObj = FileParser.getFileDataFromUrl(urlObj.path)
51
      if(fileUtils.isFile(tplUrlObj.publish.json)) {
52
        var jsonObj = FileParser.getJson(tplUrlObj.publish.json)
53
        urlObj[config.meta.name] = jsonObj[config.meta.name]
54
      }
55
      res.push(urlObj)
56
    }
57
  })
58
  return res
59
}
60
61
/**
62
 * Create and array of doc file path containing the versions of this doc
63
 * @param  {String} path of a doc
0 ignored issues
show
Documentation introduced by
The parameter path does not exist. Did you maybe forget to remove this comment?
Loading history...
64
 * @return {Array} the versions of the doc
65
 */
66
export function getVersions(docPath) {
67
  var result = []
68
  var files = Manager.instance.getList()
69
  var dataFile = docPath.replace('.' + config.files.templates.extension, '.json')
70
71
  Array.prototype.forEach.call(files, (file) => {
72
    if (file.path.indexOf(dataFile) > -1) {
73
      result = file.revisions
74
    }
75
  })
76
  return result
77
}
78
79
/**
80
 * Return the revision from document html file path
81
 * if the docPath contains abe revision [status]-[date] will try to return this revision
82
 * else it will return the latest revision
83
 * or null
84
 * 
85
 * @param  {String} html path
0 ignored issues
show
Documentation introduced by
The parameter html does not exist. Did you maybe forget to remove this comment?
Loading history...
86
 * @return {Object} file revision | null
87
 */
88
export function getDocumentRevision(docPath) {
89
  var result = null
90
  var documentPath = docPath
91
  var latest = true
92
93
  if(cmsData.fileAttr.test(documentPath)){
94
    latest = false
95
    documentPath = cmsData.fileAttr.delete(documentPath)
96
  }
97
  var revisions = getVersions(documentPath)
98
  if (latest && revisions.length >= 0) {
99
    result = revisions[0]
100
  }else if (!latest) {
101
    Array.prototype.forEach.call(revisions, (revision) => {
102
      if (revision.html === docPath) {
103
        result = revision
104
      }
105
    })
106
    if (result === null && revisions.length >= 0) {
107
      result = revisions[0]
108
    }
109
  }
110
  return result
111
}
112
113
export function getStatusAndDateToFileName(date) {
114
  var res = date.substring(0, 4) + '-'
115
            + date.substring(4, 6) + '-'
116
            + date.substring(6, 11) + ':'
117
            + date.substring(11, 13) + ':'
118
            + date.substring(13, 15) + '.'
119
            + date.substring(15, 19)
120
  return res
121
}
122
123
export function removeStatusAndDateFromFileName(date) {
124
  return date.replace(/[-:\.]/g, '')
125
}
126
127
export function filePathInfos(pathFolder) {
128
  var pathArr = pathFolder.split('/')
129
  var name = pathArr[pathArr.length - 1]
130
131
  var rootArr = config.root.split('/')
132
  var website = rootArr[pathArr.length - 1]
133
  return {
134
    'name': name,
135
    'path': pathFolder,
136
    'website': website,
137
    'cleanPath': pathFolder.replace(config.root, '').replace(/\/$/, ''),
138
    'type': 'folder'
139
  }
140
}
141
142
export function getFilesMerged(files) {
143
  var merged = {}
144
  var arMerged = []
145
  
146
  Array.prototype.forEach.call(files, (file) => {
147
    var cleanFilePath = file.cleanFilePath
148
149
    var fileStatusIsPublish = cmsData.fileAttr.get(file.cleanPath)
150
    if(typeof fileStatusIsPublish.s !== 'undefined' && fileStatusIsPublish.s !== null && file.abe_meta.status === 'publish') {
151
      file.abe_meta.status = 'draft'
152
    }
153
154
    file.html = path.join('/', file.filePath.replace(/\.json/, `.${config.files.templates.extension}`))
155
    if (file.abe_meta.status === 'publish') {
156
      file.htmlPath = path.join(config.root, config.publish.url, path.join('/', file.filePath.replace(/\.json/, `.${config.files.templates.extension}`)))
157
    }else {
158
      file.htmlPath = path.join(config.root, config.draft.url, path.join('/', file.filePath.replace(/\.json/, `.${config.files.templates.extension}`)))
159
    }
160
161
    if(typeof merged[cleanFilePath] === 'undefined' || merged[cleanFilePath] === null) {
162
      merged[cleanFilePath] = {
163
        name: cmsData.fileAttr.delete(file.name)
164
        , path: cmsData.fileAttr.delete(file.path)
165
        , html: cmsData.fileAttr.delete(path.join('/', file.filePath.replace(/\.json/, `.${config.files.templates.extension}`)))
166
        , htmlPath: path.join(config.root, config.publish.url, path.join('/', cmsData.fileAttr.delete(file.filePath.replace(/\.json/, `.${config.files.templates.extension}`))))
167
        , cleanPathName: file.cleanPathName
168
        , cleanPath: file.cleanPath
169
        , cleanName: file.cleanName
170
        , cleanNameNoExt: file.cleanNameNoExt
171
        , cleanFilePath: file.cleanFilePath
172
        , filePath: cmsData.fileAttr.delete(file.filePath)
173
        , revisions: []
174
      }
175
    }
176
177
    merged[cleanFilePath].revisions.push(JSON.parse(JSON.stringify(file)))
178
  })
179
180
  // return merged
181
  Array.prototype.forEach.call(Object.keys(merged), (key) => {
182
    var revisions = merged[key].revisions
183
    revisions.sort(FileParser.predicatBy('date', -1))
184
    if(typeof revisions[0] !== 'undefined' && revisions[0] !== null) {
185
      merged[key].date = revisions[0].date
186
    }
187
188
    Array.prototype.forEach.call(revisions, (revision) => {
189
      
190
      var status = revision.abe_meta.status
191
192
      if (status === 'publish') {
193
        merged[key][status] = revision
194
      }else {
195
        merged[key][status] = {}
196
      }
197
      merged[key][status].path = revision.path
198
      merged[key][status].html = revision.html
199
      merged[key][status].htmlPath = revision.htmlPath
200
      merged[key][status].date = new Date(revision.date)
201
      merged[key][status].link = revision.abe_meta.link
202
    })
203
204
    merged[key].revisions = revisions
205
206
    merged[key].date = revisions[0].date
207
    merged[key].cleanDate = revisions[0].cleanDate
208
    merged[key].duration = revisions[0].duration
209
    merged[key].abe_meta = revisions[0].abe_meta
210
211
    arMerged.push(merged[key])
212
  })
213
214
  return arMerged
215
}
216
217
export function deleteOlderRevisionByType(fileName, type) {
218
  var folder = fileName.split('/')
219
  var file = folder.pop()
220
  var extension = file.replace(/.*?\./, '')
221
  folder = folder.join('/')
222
  var stat = fse.statSync(folder)
223
  if(stat){
224
    var files = FileParser.getFiles(folder, true, 1, new RegExp('\\.' + extension))
225
    files.forEach(function (fileItem) {
226
      var fname = cmsData.fileAttr.delete(fileItem.cleanPath)
227
      var ftype = cmsData.fileAttr.get(fileItem.cleanPath).s
228
      if(fname === file && ftype === type){
229
        var fileDraft = fileItem.path.replace(/-abe-./, '-abe-d')
230
        FileParser.removeFile(fileItem.path, FileParser.changePathEnv(fileItem.path, config.data.url).replace(new RegExp('\\.' + extension), '.json'))
231
        FileParser.removeFile(fileDraft, FileParser.changePathEnv(fileDraft, config.data.url).replace(new RegExp('\\.' + extension), '.json'))
232
      }
233
    })
234
  }
235
}