Completed
Pull Request — master (#66)
by
unknown
01:50
created

file.js ➔ addFolder   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A file.js ➔ ... ➔ mkdirp 0 3 2
1
import Promise from 'bluebird'
2
import path from 'path'
3
import mkdirp from 'mkdirp'
4
import execPromise from 'child-process-promise'
5
var fse = Promise.promisifyAll(require('fs-extra'))
6
7
import {
8
  config,
9
  cmsData,
10
  coreUtils
11
} from '../../'
12
13
export function exist(pathFile) {
14
  try{
15
    fse.statSync(pathFile)
16
    return true
17
  }catch(e){
18
    return false
19
  }
20
}
21
22
export function changePath(pathEnv, change) {
23
  pathEnv = pathEnv.split(path.sep).join('/').replace(config.root, '').replace(/^\//, '').split('/')
24
  pathEnv[0] = change
25
  return path.join(config.root, pathEnv.join('/'))
26
}
27
28
/**
29
 * This method checks that the path leads to a file and return the content as UTF-8 content
30
 * @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...
31
 * @return {string}      The content of the UTF-8 file
32
 */
33
export function getContent(pathFile) {
34
  var res = null
35
  if(typeof pathFile !== 'undefined' && pathFile !== null && pathFile !== '') {
36
    if (exist(pathFile)) {
37
      res = fse.readFileSync(pathFile, 'utf8')
38
    }
39
  }
40
  return res
41
}
42
43
/**
44
 * synchronous fse walker to get folders with recursive option
45
 * @param  {String}  dirname   dir path
46
 * @param  {Boolean} recursive do we recurse in the subfolders
47
 * @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...
48
 * @return {array}             array of pathfiles
49
 */
50
export function getFoldersSync(dirname, recursive = true) {
51
  let items = []
52
  try{
53
    fse.readdirSync(dirname).map(function(fileName) {
54
      let pathFile = path.join(dirname, fileName)
55
      let stat = fse.statSync(pathFile)
56
      if (stat.isDirectory()) {
57
        let directory = {'path':pathFile, 'folders':[]}
58
        if (recursive) {
59
          directory.folders = coreUtils.file.getFoldersSync(pathFile, recursive)
60
        }
61
        items.push(directory)
62
      }
63
    })
64
65
    return items
66
  } catch(e) {
67
68
    return items
69
  }
70
}
71
72
/**
73
 * Promisified fse walker to get folders with recursive option
74
 * @param  {String}  dirname   dir path
75
 * @param  {Boolean} recursive do we recurse in the subfolders
76
 * @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...
77
 * @return {array}             array of pathfiles
78
 */
79
export function getFoldersAsync(dirname, recursive = true) {
80
  let items = []
81
  return fse.readdirAsync(dirname).map(function(fileName) {
82
    let pathFile = path.join(dirname, fileName)
83
    return fse.statAsync(pathFile).then(function(stat) {
84
      if (stat.isDirectory()) {
85
        let directory = {'path':pathFile, 'folders':[]}
86
        
87
        if (recursive) {
88
          return coreUtils.file.getFoldersAsync(pathFile, recursive).then(function(filesInDir) {
89
            directory.folders = filesInDir
90
            items.push(directory)
91
          })
92
        } else {
93
          items.push(directory)
94
        }
95
      }
96
      return
0 ignored issues
show
Unused Code introduced by
This return has no effect and can be removed.
Loading history...
97
    })
98
  }).then(function() {
99
    return items
100
  })
101
}
102
103
/**
104
 * synchronous fse walker with recursive and extension options
105
 * @param  {String}  dirname   dir path
106
 * @param  {Boolean} recursive do we recurse in the subfolders
107
 * @param  {String}  filterExt extension or ''
108
 * @return {array}             array of pathfiles
109
 */
110
export function getFilesSync(dirname, recursive = true, filterExt = '') {
111
  let items = []
112
  try {
113
    fse.readdirSync(dirname).map(function(fileName) {
114
      let pathFile = path.join(dirname, fileName)
115
      let stat = fse.statSync(pathFile)
116
      if (stat.isFile()) {
117
        let extFile = path.extname(fileName)
118
        if (filterExt === '' || extFile === filterExt) {
119
          items.push(pathFile)
120
        }
121
      }
122
      if (stat.isDirectory() && recursive) {
123
        let filesInDir = coreUtils.file.getFilesSync(pathFile, recursive, filterExt)
124
        items = items.concat(filesInDir)
125
      }
126
    })
127
128
    return items
129
  } catch (e) {
130
131
    return items
132
  }
133
}
134
135
/**
136
 * Promisified fse walker with recursive and extension options
137
 * @param  {String}  dirname   dir path
138
 * @param  {Boolean} recursive do we recurse in the subfolders
139
 * @param  {String}  filterExt extension or ''
140
 * @return {array}             array of pathfiles
141
 */
142
export function getFilesAsync(dirname, recursive = true, filterExt = '') {
143
  let items = []
144
  return fse.readdirAsync(dirname).map(function(fileName) {
145
    let pathFile = path.join(dirname, fileName)
146
    return fse.statAsync(pathFile).then(function(stat) {
147
      if (stat.isFile()) {
148
        let extFile = path.extname(fileName)
149
        if (filterExt === '' || extFile === filterExt) {
150
          return items.push(pathFile)
151
        }
152
        return 
153
      }
154
      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...
155
        return coreUtils.file.getFilesAsync(pathFile, recursive, filterExt).then(function(filesInDir) {
156
          items = items.concat(filesInDir)
157
        })
158
      }
159
    })
160
  }).then(function() {
161
    return items
162
  })
163
}
164
165
export function addFolder(folderPath) {
166
  mkdirp(path.join(config.root, folderPath), function (err) {
167
    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...
168
  })
169
  return folderPath
170
}
171
172
export function removeFolder(folderPath) {
173
  fse.remove(path.join(config.root, folderPath), function (err) {
174
    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...
175
  })
176
  return folderPath
177
}
178
179
/**
180
 * Return the date of the revision
181
 * @param  {String}  revisionPath   url of the post revision
182
 * @return {Date}    date           Date object
183
 */
184
export function getDate(revisionPath) {
185
  var date = cmsData.fileAttr.get(revisionPath).d
186
187
  if(typeof date === 'undefined' || date === null || date === '') {
188
    date = new Date()
189
  }else {
190
    date = new Date(date)
191
  }
192
193
  return date
194
}
195
196
/**
197
 * 
198
 * @param  {String}  revisionPath   url of the post revision
199
 * @return {Date}    date           Date object
200
 */
201
export function addDateIsoToRevisionPath(revisionPath, type) {
202
  var dateISO
203
  var saveJsonFile = revisionPath
204
205
  if (type === 'publish') {
206
    return revisionPath
207
  }
208
  
209
  dateISO = type[0] + cmsData.revision.removeStatusAndDateFromFileName((new Date().toISOString()))
210
  
211
  if(dateISO) {
212
    saveJsonFile = cmsData.fileAttr.add(saveJsonFile, dateISO)
213
  }
214
215
  return saveJsonFile
216
}