Completed
Push — master ( a05f1c...07f85d )
by greg
03:17
created

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

Complexity

Conditions 1
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 2
dl 0
loc 17
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
 * Promisified fse walker with recursive and extension options
45
 * @param  {String}  dirname   dir path
46
 * @param  {Boolean} recursive do we recurse in the subfolders
47
 * @param  {String}  filterExt extension or ''
48
 * @return {array}             array of pathfiles
49
 */
50
export function getFiles(dirname, recursive=true, filterExt = '') {
51
  let items = [];
52
  return fse.readdirAsync(dirname).map(function(fileName) {
53
    let pathFile = path.join(dirname, fileName)
54
    return fse.statAsync(pathFile).then(function(stat) {
55
      if (stat.isFile()) {
56
        let extFile = path.extname(fileName)
57
        if (filterExt === '' || extFile === filterExt) {
58
          return items.push(pathFile)
59
        }
60
        return 
61
      }
62
      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...
63
        return coreUtils.file.getFiles(pathFile, recursive, filterExt).then(function(filesInDir) {
64
            items = items.concat(filesInDir);
65
        })
66
      }
67
    })
68
  }).then(function() {
69
    return items
70
  })
71
}