1
|
|
|
import Promise from 'bluebird' |
2
|
|
|
import path from 'path' |
3
|
|
|
import mkdirp from 'mkdirp' |
4
|
|
|
var fse = Promise.promisifyAll(require('fs-extra')) |
5
|
|
|
|
6
|
|
|
import { |
7
|
|
|
config, |
8
|
|
|
cmsData, |
9
|
|
|
coreUtils |
10
|
|
|
} from '../../' |
11
|
|
|
|
12
|
|
|
export function exist(pathFile) { |
13
|
|
|
try{ |
14
|
|
|
fse.statSync(pathFile) |
15
|
|
|
return true |
16
|
|
|
}catch(e){ |
17
|
|
|
return false |
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
export function changePath(pathEnv, change) { |
22
|
|
|
pathEnv = pathEnv.split(path.sep).join('/').replace(config.root, '').replace(/^\//, '').split('/') |
23
|
|
|
pathEnv[0] = change |
24
|
|
|
return path.join(config.root, pathEnv.join('/')) |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* This method checks that the path leads to a file and return the content as UTF-8 content |
29
|
|
|
* @param {string} path The path |
|
|
|
|
30
|
|
|
* @return {string} The content of the UTF-8 file |
31
|
|
|
*/ |
32
|
|
|
export function getContent(pathFile) { |
33
|
|
|
var res = null |
34
|
|
|
if(typeof pathFile !== 'undefined' && pathFile !== null && pathFile !== '') { |
35
|
|
|
if (exist(pathFile)) { |
36
|
|
|
res = fse.readFileSync(pathFile, 'utf8') |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
return res |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* synchronous fse walker to get folders with recursive option |
44
|
|
|
* @param {String} dirname dir path |
45
|
|
|
* @param {Boolean} recursive do we recurse in the subfolders |
46
|
|
|
* @param {String} filterExt extension or '' |
|
|
|
|
47
|
|
|
* @return {array} array of pathfiles |
48
|
|
|
*/ |
49
|
|
|
export function getFoldersSync(dirname, recursive = true) { |
50
|
|
|
let items = [] |
51
|
|
|
try{ |
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
|
|
|
let directory = {'path':pathFile, 'folders':[]} |
57
|
|
|
if (recursive) { |
58
|
|
|
directory.folders = coreUtils.file.getFoldersSync(pathFile, recursive) |
59
|
|
|
} |
60
|
|
|
items.push(directory) |
61
|
|
|
} |
62
|
|
|
}) |
63
|
|
|
|
64
|
|
|
return items |
65
|
|
|
} catch(e) { |
66
|
|
|
|
67
|
|
|
return items |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Promisified fse walker to get folders with recursive option |
73
|
|
|
* @param {String} dirname dir path |
74
|
|
|
* @param {Boolean} recursive do we recurse in the subfolders |
75
|
|
|
* @param {String} filterExt extension or '' |
|
|
|
|
76
|
|
|
* @return {array} array of pathfiles |
77
|
|
|
*/ |
78
|
|
|
export function getFoldersAsync(dirname, recursive = true) { |
79
|
|
|
let items = [] |
80
|
|
|
return fse.readdirAsync(dirname).map(function(fileName) { |
81
|
|
|
let pathFile = path.join(dirname, fileName) |
82
|
|
|
return fse.statAsync(pathFile).then(function(stat) { |
83
|
|
|
if (stat.isDirectory()) { |
84
|
|
|
let directory = {'path':pathFile, 'folders':[]} |
85
|
|
|
|
86
|
|
|
if (recursive) { |
87
|
|
|
return coreUtils.file.getFoldersAsync(pathFile, recursive).then(function(filesInDir) { |
88
|
|
|
directory.folders = filesInDir |
89
|
|
|
items.push(directory) |
90
|
|
|
}) |
91
|
|
|
} else { |
92
|
|
|
items.push(directory) |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
return |
|
|
|
|
96
|
|
|
}) |
97
|
|
|
}).then(function() { |
98
|
|
|
return items |
99
|
|
|
}) |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* synchronous fse walker with recursive and extension options |
104
|
|
|
* @param {String} dirname dir path |
105
|
|
|
* @param {Boolean} recursive do we recurse in the subfolders |
106
|
|
|
* @param {String} filterExt extension or '' |
107
|
|
|
* @return {array} array of pathfiles |
108
|
|
|
*/ |
109
|
|
|
export function getFilesSync(dirname, recursive = true, filterExt = '') { |
110
|
|
|
let items = [] |
111
|
|
|
try { |
112
|
|
|
fse.readdirSync(dirname).map(function(fileName) { |
113
|
|
|
let pathFile = path.join(dirname, fileName) |
114
|
|
|
let stat = fse.statSync(pathFile) |
115
|
|
|
if (stat.isFile()) { |
116
|
|
|
let extFile = path.extname(fileName) |
117
|
|
|
if (filterExt === '' || extFile === filterExt) { |
118
|
|
|
items.push(pathFile) |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
if (stat.isDirectory() && recursive) { |
122
|
|
|
let filesInDir = coreUtils.file.getFilesSync(pathFile, recursive, filterExt) |
123
|
|
|
items = items.concat(filesInDir) |
124
|
|
|
} |
125
|
|
|
}) |
126
|
|
|
|
127
|
|
|
return items |
128
|
|
|
} catch (e) { |
129
|
|
|
|
130
|
|
|
return items |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Promisified fse walker with recursive and extension options |
136
|
|
|
* @param {String} dirname dir path |
137
|
|
|
* @param {Boolean} recursive do we recurse in the subfolders |
138
|
|
|
* @param {String} filterExt extension or '' |
139
|
|
|
* @return {array} array of pathfiles |
140
|
|
|
*/ |
141
|
|
|
export function getFilesAsync(dirname, recursive = true, filterExt = '') { |
142
|
|
|
let items = [] |
143
|
|
|
|
144
|
|
|
return fse.lstatAsync(dirname).then(stat => { |
145
|
|
|
if (stat.isDirectory()) { |
146
|
|
|
return fse.readdirAsync(dirname).map(function(fileName) { |
147
|
|
|
let pathFile = path.join(dirname, fileName) |
148
|
|
|
return fse.statAsync(pathFile).then(function(stat) { |
149
|
|
|
if (stat.isFile()) { |
150
|
|
|
let extFile = path.extname(fileName) |
151
|
|
|
if (filterExt === '' || extFile === filterExt) { |
152
|
|
|
return items.push(pathFile) |
153
|
|
|
} |
154
|
|
|
return |
155
|
|
|
} |
156
|
|
|
if (recursive) { |
|
|
|
|
157
|
|
|
return coreUtils.file.getFilesAsync(pathFile, recursive, filterExt).then(function(filesInDir) { |
158
|
|
|
items = items.concat(filesInDir) |
159
|
|
|
}) |
160
|
|
|
} |
161
|
|
|
}) |
162
|
|
|
}).then(function() { |
163
|
|
|
return items |
164
|
|
|
}) |
165
|
|
|
} else { |
166
|
|
|
return items |
167
|
|
|
} |
168
|
|
|
}) |
169
|
|
|
.catch(function(e) { |
|
|
|
|
170
|
|
|
return items |
171
|
|
|
}) |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
export function addFolder(folderPath) { |
175
|
|
|
mkdirp(path.join(config.root, folderPath), function (err) { |
176
|
|
|
if (err) console.error(err) |
|
|
|
|
177
|
|
|
}) |
178
|
|
|
return folderPath |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
export function removeFolder(folderPath) { |
182
|
|
|
fse.remove(path.join(config.root, folderPath), function (err) { |
183
|
|
|
if (err) return console.error(err) |
|
|
|
|
184
|
|
|
}) |
185
|
|
|
return folderPath |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Return the date of the revision |
190
|
|
|
* @param {String} revisionPath url of the post revision |
191
|
|
|
* @return {Date} date Date object |
192
|
|
|
*/ |
193
|
|
|
export function getDate(revisionPath) { |
194
|
|
|
var date = cmsData.fileAttr.get(revisionPath).d |
195
|
|
|
|
196
|
|
|
if(typeof date === 'undefined' || date === null || date === '') { |
197
|
|
|
date = new Date() |
198
|
|
|
}else { |
199
|
|
|
date = new Date(date) |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return date |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* |
207
|
|
|
* @param {String} revisionPath url of the post revision |
208
|
|
|
* @return {Date} date Date object |
209
|
|
|
*/ |
210
|
|
|
export function addDateIsoToRevisionPath(revisionPath, type) { |
211
|
|
|
var dateISO |
212
|
|
|
var saveJsonFile = revisionPath |
213
|
|
|
|
214
|
|
|
if (type === 'publish') { |
215
|
|
|
return revisionPath |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
dateISO = type[0] + cmsData.revision.removeStatusAndDateFromFileName((new Date().toISOString())) |
219
|
|
|
|
220
|
|
|
if(dateISO) { |
221
|
|
|
saveJsonFile = cmsData.fileAttr.add(saveJsonFile, dateISO) |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return saveJsonFile |
225
|
|
|
} |