Completed
Push — master ( 2c560f...469e75 )
by
unknown
01:58
created

revision.js ➔ getStatusAndDateToFileName   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 9
rs 9.6666
nop 2
1
import {
2
  FileParser,
3
  fileUtils,
4
  config,
5
  cmsData,
6
  Manager
7
} from '../../'
8
9
export function getFilesRevision(urls, fileName) {
10
  var res = []
11
  var number = 1
12
  var tplUrl = FileParser.getFileDataFromUrl(fileName)
13
  fileName = fileName.split('/')
14
  fileName = fileName[fileName.length - 1]
15
  var publishDate = new Date()
16
  var json = null
17
18
  if(fileUtils.isFile(tplUrl.publish.json)) {
19
    json = FileParser.getJson(tplUrl.publish.json)
20
    if(typeof json !== 'undefined' && json !== null
21
      && typeof json[config.meta.name] !== 'undefined' && json[config.meta.name] !== null) {
22
      publishDate = new Date(json[config.meta.name].latest.date)
23
    }
24
  }
25
26
  var publishVersion = false
27
  urls.forEach(function (urlObj) {
28
    var fileData = cmsData.fileAttr.get(urlObj.cleanPath)
29
    if(fileData.s === 'd' && cmsData.fileAttr.delete(urlObj.cleanPath) == cmsData.fileAttr.delete(fileName)) {
30
      var currentDate = new Date(urlObj.date)
31
      if(currentDate.getTime() > publishDate.getTime()) {
32
        if(!publishVersion && typeof res[res.length - 1] !== 'undefined' && res[res.length - 1] !== null) {
33
          res[res.length - 1].publishedDate = 'same'
34
        }
35
        publishVersion = true
36
        urlObj.publishedDate = 'after'
37
        
38
      }else if(currentDate.getTime() === publishDate.getTime()) {
39
        urlObj.publishedDate = 'same'
40
        publishVersion = true
41
      }else {
42
        urlObj.publishedDate = 'before'
43
      }
44
      urlObj.version = number
45
      number = number + 1
46
47
      var tplUrlObj = FileParser.getFileDataFromUrl(urlObj.path)
48
      if(fileUtils.isFile(tplUrlObj.publish.json)) {
49
        var jsonObj = FileParser.getJson(tplUrlObj.publish.json)
50
        urlObj[config.meta.name] = jsonObj[config.meta.name]
51
      }
52
      res.push(urlObj)
53
    }
54
  })
55
  return res
56
}
57
58
/**
59
 * Create and array of doc file path containing the versions of this doc
60
 * @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...
61
 * @return {Array} the versions of the doc
62
 */
63
export function getVersions(docPath) {
64
  var result = []
65
  var files = Manager.instance.getList()
66
  var dataFile = docPath.replace('.' + config.files.templates.extension, '.json')
67
68
  Array.prototype.forEach.call(files, (file) => {
69
    if (file.path.indexOf(dataFile) > -1) {
70
      result = file.revisions
71
    }
72
  })
73
  return result
74
}
75
76
/**
77
 * Return the revision from document html file path
78
 * if the docPath contains abe revision [status]-[date] will try to return this revision
79
 * else it will return the latest revision
80
 * or null
81
 * 
82
 * @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...
83
 * @return {Object} file revision | null
84
 */
85
export function getDocumentRevision(docPath) {
86
  var result = null
87
  var documentPath = docPath
88
  var latest = true
89
90
  if(cmsData.fileAttr.test(documentPath)){
91
    latest = false
92
    documentPath = cmsData.fileAttr.delete(documentPath)
93
  }
94
  var revisions = getVersions(documentPath)
95
  if (latest && revisions.length >= 0) {
96
    result = revisions[0]
97
  }else if (!latest) {
98
    Array.prototype.forEach.call(revisions, (revision) => {
99
      if (revision.html === docPath) {
100
        result = revision
101
      }
102
    })
103
    if (result === null && revisions.length >= 0) {
104
      result = revisions[0]
105
    }
106
  }
107
  return result
108
}
109
110
export function getStatusAndDateToFileName(date, file) {
0 ignored issues
show
Unused Code introduced by
The parameter file is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
111
  var res = date.substring(0, 4) + '-'
112
            + date.substring(4, 6) + '-'
113
            + date.substring(6, 11) + ':'
114
            + date.substring(11, 13) + ':'
115
            + date.substring(13, 15) + '.'
116
            + date.substring(15, 19)
117
  return res
118
}
119
120
export function removeStatusAndDateFromFileName(date) {
121
  return date.replace(/[-:\.]/g, '')
122
}