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
|
|
|
* 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 '' |
|
|
|
|
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 '' |
|
|
|
|
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) { |
|
|
|
|
83
|
|
|
items = items.concat(filesInDir) |
84
|
|
|
}) |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
return |
|
|
|
|
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) { |
|
|
|
|
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
|
|
|
} |