|
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 |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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
|
|
|
} |