Completed
Push — master ( 5e2b93...88fa0f )
by greg
01:45
created

file.js ➔ getFilesSync   B

Complexity

Conditions 2
Paths 8

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 8
dl 0
loc 24
rs 8.9713
nop 3

1 Function

Rating   Name   Duplication   Size   Complexity  
B file.js ➔ ... ➔ fse.map 0 14 6
1
import Promise from 'bluebird'
2
import path from 'path'
3
var fse = Promise.promisifyAll(require('fs-extra'))
4
5
import {
6
  config,
7
  coreUtils
8
} from '../../'
9
10
export function exist(pathFile) {
11
  try{
12
    fse.statSync(pathFile)
13
    return true
14
  }catch(e){
15
    return false
16
  }
17
}
18
19
export function changePath(pathEnv, change) {
20
  pathEnv = pathEnv.replace(config.root, '').replace(/^\//, '').split('/')
21
  pathEnv[0] = change
22
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
        items.push(pathFile)
56
        if (recursive) {
57
          let filesInDir = coreUtils.file.getFoldersSync(pathFile, recursive)
58
          items = items.concat(filesInDir)
59
        }
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
        items.push(pathFile)
84
        if (recursive) {
85
          return coreUtils.file.getFoldersAsync(pathFile, recursive).then(function(filesInDir) {
86
            items = items.concat(filesInDir)
87
          })
88
        }
89
      }
90
      return
0 ignored issues
show
Unused Code introduced by
This return has no effect and can be removed.
Loading history...
91
    })
92
  }).then(function() {
93
    return items
94
  })
95
}
96
97
/**
98
 * synchronous fse walker with recursive and extension options
99
 * @param  {String}  dirname   dir path
100
 * @param  {Boolean} recursive do we recurse in the subfolders
101
 * @param  {String}  filterExt extension or ''
102
 * @return {array}             array of pathfiles
103
 */
104
export function getFilesSync(dirname, recursive = true, filterExt = '') {
105
  let items = []
106
  try {
107
    fse.readdirSync(dirname).map(function(fileName) {
108
      let pathFile = path.join(dirname, fileName)
109
      let stat = fse.statSync(pathFile)
110
      if (stat.isFile()) {
111
        let extFile = path.extname(fileName)
112
        if (filterExt === '' || extFile === filterExt) {
113
          items.push(pathFile)
114
        }
115
      }
116
      if (stat.isDirectory() && recursive) {
117
        let filesInDir = coreUtils.file.getFilesSync(pathFile, recursive, filterExt)
118
        items = items.concat(filesInDir)
119
      }
120
    })
121
122
    return items
123
  } catch (e) {
124
125
    return items
126
  }
127
}
128
129
/**
130
 * Promisified fse walker with recursive and extension options
131
 * @param  {String}  dirname   dir path
132
 * @param  {Boolean} recursive do we recurse in the subfolders
133
 * @param  {String}  filterExt extension or ''
134
 * @return {array}             array of pathfiles
135
 */
136
export function getFilesAsync(dirname, recursive = true, filterExt = '') {
137
  let items = []
138
  return fse.readdirAsync(dirname).map(function(fileName) {
139
    let pathFile = path.join(dirname, fileName)
140
    return fse.statAsync(pathFile).then(function(stat) {
141
      if (stat.isFile()) {
142
        let extFile = path.extname(fileName)
143
        if (filterExt === '' || extFile === filterExt) {
144
          return items.push(pathFile)
145
        }
146
        return 
147
      }
148
      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...
149
        return coreUtils.file.getFilesAsync(pathFile, recursive, filterExt).then(function(filesInDir) {
150
          items = items.concat(filesInDir)
151
        })
152
      }
153
    })
154
  }).then(function() {
155
    return items
156
  })
157
}