Completed
Push — master ( 16cfcf...a20d6f )
by greg
01:44
created

file.js ➔ ... ➔ fse.map   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 14
rs 9.4285
nop 1
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
  return false
0 ignored issues
show
introduced by
This code is unreachable and can thus be removed without consequences.
Loading history...
19
}
20
21
export function changePath(pathEnv, change) {
22
  pathEnv = pathEnv.replace(config.root, '').replace(/^\//, '').split('/')
23
  pathEnv[0] = change
24
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
  fse.readdirSync(dirname).map(function(fileName) {
53
    let pathFile = path.join(dirname, fileName)
54
    let stat = fse.statSync(pathFile)
55
    if (stat.isDirectory()) {
56
      items.push(pathFile)
57
      if (recursive) {
58
        let filesInDir = coreUtils.file.getFoldersSync(pathFile, recursive)
59
        items = items.concat(filesInDir)
60
      }
61
    }
62
  })
63
64
  return items
65
}
66
67
/**
68
 * Promisified fse walker to get folders with recursive option
69
 * @param  {String}  dirname   dir path
70
 * @param  {Boolean} recursive do we recurse in the subfolders
71
 * @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...
72
 * @return {array}             array of pathfiles
73
 */
74
export function getFoldersAsync(dirname, recursive = true) {
75
  let items = []
76
  return fse.readdirAsync(dirname).map(function(fileName) {
77
    let pathFile = path.join(dirname, fileName)
78
    return fse.statAsync(pathFile).then(function(stat) {
79
      if (stat.isDirectory()) {
80
        items.push(pathFile)
81
        if (recursive) {
82
          return coreUtils.file.getFoldersAsync(pathFile, recursive, filterExt).then(function(filesInDir) {
0 ignored issues
show
Bug introduced by
The variable filterExt seems to be never declared. If this is a global, consider adding a /** global: filterExt */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
83
            items = items.concat(filesInDir)
84
          })
85
        }
86
      }
87
      return
0 ignored issues
show
Unused Code introduced by
This return has no effect and can be removed.
Loading history...
88
    })
89
  }).then(function() {
90
    return items
91
  })
92
}
93
94
/**
95
 * synchronous fse walker with recursive and extension options
96
 * @param  {String}  dirname   dir path
97
 * @param  {Boolean} recursive do we recurse in the subfolders
98
 * @param  {String}  filterExt extension or ''
99
 * @return {array}             array of pathfiles
100
 */
101
export function getFilesSync(dirname, recursive = true, filterExt = '') {
102
  let items = []
103
  fse.readdirSync(dirname).map(function(fileName) {
104
    let pathFile = path.join(dirname, fileName)
105
    let stat = fse.statSync(pathFile)
106
    if (stat.isFile()) {
107
      let extFile = path.extname(fileName)
108
      if (filterExt === '' || extFile === filterExt) {
109
        items.push(pathFile)
110
      }
111
    }
112
    if (stat.isDirectory() && recursive) {
113
      let filesInDir = coreUtils.file.getFilesSync(pathFile, recursive, filterExt)
114
      items = items.concat(filesInDir)
115
    }
116
  })
117
118
  return items
119
}
120
121
/**
122
 * Promisified fse walker with recursive and extension options
123
 * @param  {String}  dirname   dir path
124
 * @param  {Boolean} recursive do we recurse in the subfolders
125
 * @param  {String}  filterExt extension or ''
126
 * @return {array}             array of pathfiles
127
 */
128
export function getFilesAsync(dirname, recursive = true, filterExt = '') {
129
  let items = []
130
  return fse.readdirAsync(dirname).map(function(fileName) {
131
    let pathFile = path.join(dirname, fileName)
132
    return fse.statAsync(pathFile).then(function(stat) {
133
      if (stat.isFile()) {
134
        let extFile = path.extname(fileName)
135
        if (filterExt === '' || extFile === filterExt) {
136
          return items.push(pathFile)
137
        }
138
        return 
139
      }
140
      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...
141
        return coreUtils.file.getFiles(pathFile, recursive, filterExt).then(function(filesInDir) {
142
          items = items.concat(filesInDir)
143
        })
144
      }
145
    })
146
  }).then(function() {
147
    return items
148
  })
149
}