Completed
Push — master ( faac82...5497ed )
by greg
01:57
created

file.js ➔ ... ➔ fse.catch   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
import Promise from 'bluebird'
2
import path from 'path'
3
import mkdirp from 'mkdirp'
4
var fse = Promise.promisifyAll(require('fs-extra'))
5
6
import {
7
  config,
8
  cmsData,
9
  coreUtils
10
} from '../../'
11
12
export function exist(pathFile) {
13
  try{
14
    fse.statSync(pathFile)
15
    return true
16
  }catch(e){
17
    return false
18
  }
19
}
20
21
export function changePath(pathEnv, change) {
22
  pathEnv = pathEnv.split(path.sep).join('/').replace(config.root, '').replace(/^\//, '').split('/')
23
  pathEnv[0] = change
24
  return path.join(config.root, pathEnv.join('/'))
25
}
26
27
/**
28
 * This method checks that the path leads to a file and return the content as UTF-8 content
29
 * @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...
30
 * @return {string}      The content of the UTF-8 file
31
 */
32
export function getContent(pathFile) {
33
  var res = null
34
  if(typeof pathFile !== 'undefined' && pathFile !== null && pathFile !== '') {
35
    if (exist(pathFile)) {
36
      res = fse.readFileSync(pathFile, 'utf8')
37
    }
38
  }
39
  return res
40
}
41
42
/**
43
 * synchronous fse walker to get folders with recursive option
44
 * @param  {String}  dirname   dir path
45
 * @param  {Boolean} recursive do we recurse in the subfolders
46
 * @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...
47
 * @return {array}             array of pathfiles
48
 */
49
export function getFoldersSync(dirname, recursive = true) {
50
  let items = []
51
  try{
52
    fse.readdirSync(dirname).map(function(fileName) {
53
      let pathFile = path.join(dirname, fileName)
54
      let stat = fse.statSync(pathFile)
55
      if (stat.isDirectory()) {
56
        let directory = {'path':pathFile, 'folders':[]}
57
        if (recursive) {
58
          directory.folders = coreUtils.file.getFoldersSync(pathFile, recursive)
59
        }
60
        items.push(directory)
61
      }
62
    })
63
64
    return items
65
  } catch(e) {
66
67
    return items
68
  }
69
}
70
71
/**
72
 * Promisified fse walker to get folders with recursive option
73
 * @param  {String}  dirname   dir path
74
 * @param  {Boolean} recursive do we recurse in the subfolders
75
 * @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...
76
 * @return {array}             array of pathfiles
77
 */
78
export function getFoldersAsync(dirname, recursive = true) {
79
  let items = []
80
  return fse.readdirAsync(dirname).map(function(fileName) {
81
    let pathFile = path.join(dirname, fileName)
82
    return fse.statAsync(pathFile).then(function(stat) {
83
      if (stat.isDirectory()) {
84
        let directory = {'path':pathFile, 'folders':[]}
85
        
86
        if (recursive) {
87
          return coreUtils.file.getFoldersAsync(pathFile, recursive).then(function(filesInDir) {
88
            directory.folders = filesInDir
89
            items.push(directory)
90
          })
91
        } else {
92
          items.push(directory)
93
        }
94
      }
95
      return
0 ignored issues
show
Unused Code introduced by
This return has no effect and can be removed.
Loading history...
96
    })
97
  }).then(function() {
98
    return items
99
  })
100
}
101
102
/**
103
 * synchronous fse walker with recursive and extension options
104
 * @param  {String}  dirname   dir path
105
 * @param  {Boolean} recursive do we recurse in the subfolders
106
 * @param  {String}  filterExt extension or ''
107
 * @return {array}             array of pathfiles
108
 */
109
export function getFilesSync(dirname, recursive = true, filterExt = '') {
110
  let items = []
111
  try {
112
    fse.readdirSync(dirname).map(function(fileName) {
113
      let pathFile = path.join(dirname, fileName)
114
      let stat = fse.statSync(pathFile)
115
      if (stat.isFile()) {
116
        let extFile = path.extname(fileName)
117
        if (filterExt === '' || extFile === filterExt) {
118
          items.push(pathFile)
119
        }
120
      }
121
      if (stat.isDirectory() && recursive) {
122
        let filesInDir = coreUtils.file.getFilesSync(pathFile, recursive, filterExt)
123
        items = items.concat(filesInDir)
124
      }
125
    })
126
127
    return items
128
  } catch (e) {
129
130
    return items
131
  }
132
}
133
134
/**
135
 * Promisified fse walker with recursive and extension options
136
 * @param  {String}  dirname   dir path
137
 * @param  {Boolean} recursive do we recurse in the subfolders
138
 * @param  {String}  filterExt extension or ''
139
 * @return {array}             array of pathfiles
140
 */
141
export function getFilesAsync(dirname, recursive = true, filterExt = '') {
142
  let items = []
143
144
  return fse.lstatAsync(dirname).then(stat => {
145
    if (stat.isDirectory()) {
146
      return fse.readdirAsync(dirname).map(function(fileName) {
147
        let pathFile = path.join(dirname, fileName)
148
        return fse.statAsync(pathFile).then(function(stat) {
149
          if (stat.isFile()) {
150
            let extFile = path.extname(fileName)
151
            if (filterExt === '' || extFile === filterExt) {
152
              return items.push(pathFile)
153
            }
154
            return 
155
          }
156
          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...
157
            return coreUtils.file.getFilesAsync(pathFile, recursive, filterExt).then(function(filesInDir) {
158
              items = items.concat(filesInDir)
159
            })
160
          }
161
        })
162
      }).then(function() {
163
        return items
164
      })
165
    } else {
166
      return items
167
    }
168
  })
169
  .catch(function(e) {
0 ignored issues
show
Unused Code introduced by
The parameter e 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...
170
    return items
171
  })
172
}
173
174
export function addFolder(folderPath) {
175
  mkdirp(path.join(config.root, folderPath), function (err) {
176
    if (err) console.error(err)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
177
  })
178
  return folderPath
179
}
180
181
export function removeFolder(folderPath) {
182
  fse.remove(path.join(config.root, folderPath), function (err) {
183
    if (err) return console.error(err)
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if err 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...
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
184
  })
185
  return folderPath
186
}
187
188
/**
189
 * Return the date of the revision
190
 * @param  {String}  revisionPath   url of the post revision
191
 * @return {Date}    date           Date object
192
 */
193
export function getDate(revisionPath) {
194
  var date = cmsData.fileAttr.get(revisionPath).d
195
196
  if(typeof date === 'undefined' || date === null || date === '') {
197
    date = new Date()
198
  }else {
199
    date = new Date(date)
200
  }
201
202
  return date
203
}
204
205
/**
206
 * 
207
 * @param  {String}  revisionPath   url of the post revision
208
 * @return {Date}    date           Date object
209
 */
210
export function addDateIsoToRevisionPath(revisionPath, type) {
211
  var dateISO
212
  var saveJsonFile = revisionPath
213
214
  if (type === 'publish') {
215
    return revisionPath
216
  }
217
  
218
  dateISO = type[0] + cmsData.revision.removeStatusAndDateFromFileName((new Date().toISOString()))
219
  
220
  if(dateISO) {
221
    saveJsonFile = cmsData.fileAttr.add(saveJsonFile, dateISO)
222
  }
223
224
  return saveJsonFile
225
}