Completed
Push — master ( 430c18...52d1c6 )
by
unknown
54s
created

file.js ➔ addDateIsoToRevisionPath   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
c 0
b 0
f 0
nc 3
dl 0
loc 16
rs 9.4285
nop 2
1
import Promise from 'bluebird'
2
import path from 'path'
3
var fse = Promise.promisifyAll(require('fs-extra'))
4
5
import {
6
  config,
7
  cmsData,
8
  coreUtils
9
} from '../../'
10
11
export function exist(pathFile) {
12
  try{
13
    fse.statSync(pathFile)
14
    return true
15
  }catch(e){
16
    return false
17
  }
18
}
19
20
export function changePath(pathEnv, change) {
21
  pathEnv = pathEnv.split(path.sep).join('/').replace(config.root, '').replace(/^\//, '').split('/')
22
  pathEnv[0] = change
23
  return path.join(config.root, pathEnv.join('/'))
24
}
25
26
/**
27
 * This method checks that the path leads to a file and return the content as UTF-8 content
28
 * @param  {string} path The path
0 ignored issues
show
Documentation introduced by
The parameter path does not exist. Did you maybe forget to remove this comment?
Loading history...
29
 * @return {string}      The content of the UTF-8 file
30
 */
31
export function getContent(pathFile) {
32
  var res = null
33
  if(typeof pathFile !== 'undefined' && pathFile !== null && pathFile !== '') {
34
    if (exist(pathFile)) {
35
      res = fse.readFileSync(pathFile, 'utf8')
36
    }
37
  }
38
  return res
39
}
40
41
/**
42
 * synchronous fse walker to get folders with recursive option
43
 * @param  {String}  dirname   dir path
44
 * @param  {Boolean} recursive do we recurse in the subfolders
45
 * @param  {String}  filterExt extension or ''
0 ignored issues
show
Documentation introduced by
The parameter filterExt does not exist. Did you maybe forget to remove this comment?
Loading history...
46
 * @return {array}             array of pathfiles
47
 */
48
export function getFoldersSync(dirname, recursive = true) {
49
  let items = []
50
  try{
51
    fse.readdirSync(dirname).map(function(fileName) {
52
      let pathFile = path.join(dirname, fileName)
53
      let stat = fse.statSync(pathFile)
54
      if (stat.isDirectory()) {
55
        let directory = {'path':pathFile, 'folders':[]}
56
        if (recursive) {
57
          directory.folders = coreUtils.file.getFoldersSync(pathFile, recursive)
58
        }
59
        items.push(directory)
60
      }
61
    })
62
63
    return items
64
  } catch(e) {
65
66
    return items
67
  }
68
}
69
70
/**
71
 * Promisified fse walker to get folders with recursive option
72
 * @param  {String}  dirname   dir path
73
 * @param  {Boolean} recursive do we recurse in the subfolders
74
 * @param  {String}  filterExt extension or ''
0 ignored issues
show
Documentation introduced by
The parameter filterExt does not exist. Did you maybe forget to remove this comment?
Loading history...
75
 * @return {array}             array of pathfiles
76
 */
77
export function getFoldersAsync(dirname, recursive = true) {
78
  let items = []
79
  return fse.readdirAsync(dirname).map(function(fileName) {
80
    let pathFile = path.join(dirname, fileName)
81
    return fse.statAsync(pathFile).then(function(stat) {
82
      if (stat.isDirectory()) {
83
        let directory = {'path':pathFile, 'folders':[]}
84
        
85
        if (recursive) {
86
          return coreUtils.file.getFoldersAsync(pathFile, recursive).then(function(filesInDir) {
87
            directory.folders = filesInDir
88
            items.push(directory)
89
          })
90
        } else {
91
          items.push(directory)
92
        }
93
      }
94
      return
0 ignored issues
show
Unused Code introduced by
This return has no effect and can be removed.
Loading history...
95
    })
96
  }).then(function() {
97
    return items
98
  })
99
}
100
101
/**
102
 * synchronous fse walker with recursive and extension options
103
 * @param  {String}  dirname   dir path
104
 * @param  {Boolean} recursive do we recurse in the subfolders
105
 * @param  {String}  filterExt extension or ''
106
 * @return {array}             array of pathfiles
107
 */
108
export function getFilesSync(dirname, recursive = true, filterExt = '') {
109
  let items = []
110
  try {
111
    fse.readdirSync(dirname).map(function(fileName) {
112
      let pathFile = path.join(dirname, fileName)
113
      let stat = fse.statSync(pathFile)
114
      if (stat.isFile()) {
115
        let extFile = path.extname(fileName)
116
        if (filterExt === '' || extFile === filterExt) {
117
          items.push(pathFile)
118
        }
119
      }
120
      if (stat.isDirectory() && recursive) {
121
        let filesInDir = coreUtils.file.getFilesSync(pathFile, recursive, filterExt)
122
        items = items.concat(filesInDir)
123
      }
124
    })
125
126
    return items
127
  } catch (e) {
128
129
    return items
130
  }
131
}
132
133
/**
134
 * Promisified fse walker with recursive and extension options
135
 * @param  {String}  dirname   dir path
136
 * @param  {Boolean} recursive do we recurse in the subfolders
137
 * @param  {String}  filterExt extension or ''
138
 * @return {array}             array of pathfiles
139
 */
140
export function getFilesAsync(dirname, recursive = true, filterExt = '') {
141
  let items = []
142
  return fse.readdirAsync(dirname).map(function(fileName) {
143
    let pathFile = path.join(dirname, fileName)
144
    return fse.statAsync(pathFile).then(function(stat) {
145
      if (stat.isFile()) {
146
        let extFile = path.extname(fileName)
147
        if (filterExt === '' || extFile === filterExt) {
148
          return items.push(pathFile)
149
        }
150
        return 
151
      }
152
      if (recursive) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if recursive is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
153
        return coreUtils.file.getFilesAsync(pathFile, recursive, filterExt).then(function(filesInDir) {
154
          items = items.concat(filesInDir)
155
        })
156
      }
157
    })
158
  }).then(function() {
159
    return items
160
  })
161
}
162
163
/**
164
 * Return the date of the revision
165
 * @param  {String}  revisionPath   url of the post revision
166
 * @return {Date}    date           Date object
167
 */
168
export function getDate(revisionPath) {
169
  var date = cmsData.fileAttr.get(revisionPath).d
170
171
  if(typeof date === 'undefined' || date === null || date === '') {
172
    date = new Date()
173
  }else {
174
    date = new Date(date)
175
  }
176
177
  return date
178
}
179
180
/**
181
 * 
182
 * @param  {String}  revisionPath   url of the post revision
183
 * @return {Date}    date           Date object
184
 */
185
export function addDateIsoToRevisionPath(revisionPath, type) {
186
  var dateISO
187
  var saveJsonFile = revisionPath
188
189
  if (type === 'publish') {
190
    return revisionPath
191
  }
192
  
193
  dateISO = type[0] + cmsData.revision.removeStatusAndDateFromFileName((new Date().toISOString()))
194
  
195
  if(dateISO) {
196
    saveJsonFile = cmsData.fileAttr.add(saveJsonFile, dateISO)
197
  }
198
199
  return saveJsonFile
200
}