Completed
Push — master ( cf6f6f...cf6f6f )
by
unknown
01:49
created

revision.js ➔ getFilesMerged   A

Complexity

Conditions 1
Paths 12

Size

Total Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 12
dl 0
loc 74
rs 9.0335
nop 1

1 Function

Rating   Name   Duplication   Size   Complexity  
C revision.js ➔ ... ➔ ??? 0 33 7

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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