1
|
|
|
import path from 'path' |
2
|
|
|
import mkdirp from 'mkdirp' |
3
|
|
|
import fse from 'fs-extra' |
4
|
|
|
import moment from 'moment' |
5
|
|
|
|
6
|
|
|
import { |
7
|
|
|
abeExtend, |
8
|
|
|
coreUtils, |
9
|
|
|
cmsData, |
10
|
|
|
config |
11
|
|
|
} from '../../' |
12
|
|
|
|
13
|
|
|
export function getAbeMeta(fileObject, json) { |
14
|
|
|
const fileStatusIsPublish = cmsData.fileAttr.get(fileObject.cleanPath) |
15
|
|
|
|
16
|
|
|
if(json.abe_meta.latest.date != null) { |
17
|
|
|
fileObject.date = json.abe_meta.latest.date |
18
|
|
|
fileObject.cleanDate = moment(json.abe_meta.latest.date).format('YYYY/MM/DD HH:mm:ss') |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
if(json.abe_meta != null) { |
22
|
|
|
if (json.abe_meta.status === 'publish') { |
23
|
|
|
fileObject.htmlPath = path.join(config.root, config.publish.url, path.join('/', fileObject.filePath.replace(/\.json/, `.${config.files.templates.extension}`))) |
24
|
|
|
} |
25
|
|
|
var date = null |
26
|
|
|
if (json.abe_meta.latest.date !== null) { |
27
|
|
|
date = json.abe_meta.latest.date |
28
|
|
|
} else if (json.abe_meta.date !== null) { |
29
|
|
|
date = json.abe_meta.date |
30
|
|
|
} |
31
|
|
|
fileObject.abe_meta = { |
32
|
|
|
date: date, |
33
|
|
|
type: (json.abe_meta.type != null) ? json.abe_meta.type : null, |
34
|
|
|
link: (json.abe_meta.link != null) ? json.abe_meta.link : null, |
35
|
|
|
template: (json.abe_meta.template != null) ? json.abe_meta.template : null, |
36
|
|
|
cleanName: (json.abe_meta.cleanName != null) ? json.abe_meta.cleanName : null, |
37
|
|
|
cleanFilename: (json.abe_meta.cleanFilename != null) ? json.abe_meta.cleanFilename : null, |
38
|
|
|
status: (json.abe_meta.status != null) ? json.abe_meta.status : null |
39
|
|
|
} |
40
|
|
|
if(fileStatusIsPublish.s != null && json.abe_meta.status === 'publish') { |
41
|
|
|
fileObject.abe_meta.status = 'draft' |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return fileObject |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
export function getAllWithKeys(withKeys) { |
49
|
|
|
const pathData = path.join(config.root, config.data.url) |
50
|
|
|
const extension = '.json' |
51
|
|
|
const files = coreUtils.file.getFilesSync(pathData, true, extension) |
52
|
|
|
|
53
|
|
|
let filesArr = [] |
54
|
|
|
|
55
|
|
|
Array.prototype.forEach.call(files, (pathFile) => { |
56
|
|
|
const json = cmsData.file.get(pathFile) |
57
|
|
|
let fileObject = cmsData.file.getFileObject(pathFile) |
58
|
|
|
fileObject = cmsData.file.getAbeMeta(fileObject, json) |
59
|
|
|
|
60
|
|
|
Array.prototype.forEach.call(withKeys, (key) => { |
61
|
|
|
var keyFirst = key.split('.')[0] |
62
|
|
|
fileObject[keyFirst] = json[keyFirst] |
63
|
|
|
}) |
64
|
|
|
|
65
|
|
|
filesArr.push(fileObject) |
66
|
|
|
}) |
67
|
|
|
|
68
|
|
|
var merged = cmsData.revision.getFilesMerged(filesArr) |
69
|
|
|
abeExtend.hooks.instance.trigger('afterGetAllFiles', merged) |
70
|
|
|
|
71
|
|
|
return merged |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
export function get(pathJson) { |
75
|
|
|
let json |
76
|
|
|
pathJson = abeExtend.hooks.instance.trigger('beforeGetJson', pathJson) |
77
|
|
|
|
78
|
|
|
try { |
79
|
|
|
var stat = fse.statSync(pathJson) |
80
|
|
|
if (stat) { |
81
|
|
|
json = fse.readJsonSync(pathJson) |
82
|
|
|
} |
83
|
|
|
}catch(e) { |
84
|
|
|
json = {} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
json = abeExtend.hooks.instance.trigger('afterGetJson', json) |
|
|
|
|
88
|
|
|
return json |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
export function fromUrl(url) { |
92
|
|
|
var res = { |
93
|
|
|
root: '', |
94
|
|
|
draft: { |
95
|
|
|
dir: '', |
96
|
|
|
file: '', |
97
|
|
|
path: '' |
98
|
|
|
}, |
99
|
|
|
publish: { |
100
|
|
|
dir: '', |
101
|
|
|
file: '', |
102
|
|
|
link: '', |
103
|
|
|
path: '', |
104
|
|
|
json: '' |
105
|
|
|
}, |
106
|
|
|
json: { |
107
|
|
|
path: '', |
108
|
|
|
file: '' |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if(url != null) { |
113
|
|
|
const dir = path.dirname(url).replace(config.root, '') |
114
|
|
|
const filename = path.basename(url) |
115
|
|
|
const pathDraft = path.join(config.root,config.draft.url,path.sep) |
116
|
|
|
const pathPublish = path.join(config.root,config.publish.url,path.sep) |
117
|
|
|
|
118
|
|
|
let link = url.replace(pathDraft, '') |
119
|
|
|
link = link.replace(pathPublish, '') |
120
|
|
|
link = link.replace(new RegExp('\\' + path.sep, 'g'), '/') |
121
|
|
|
link = cmsData.fileAttr.delete('/'+link) |
122
|
|
|
|
123
|
|
|
let publish = config.publish.url |
124
|
|
|
let data = config.data.url |
125
|
|
|
|
126
|
|
|
res.root = config.root |
127
|
|
|
|
128
|
|
|
res.json.dir = coreUtils.file.changePath(dir, data) |
129
|
|
|
res.publish.dir = coreUtils.file.changePath(dir, publish) |
130
|
|
|
res.publish.json = res.json.dir |
131
|
|
|
|
132
|
|
|
res.publish.file = cmsData.fileAttr.delete(filename) |
133
|
|
|
res.publish.link = link |
134
|
|
|
res.json.file = filename.replace(`.${config.files.templates.extension}`, '.json') |
135
|
|
|
res.publish.json = path.join(res.json.dir, cmsData.fileAttr.delete(res.json.file)) |
136
|
|
|
|
137
|
|
|
res.publish.path = path.join(res.publish.dir, res.publish.file) |
138
|
|
|
res.json.path = path.join(res.json.dir, res.json.file) |
139
|
|
|
} |
140
|
|
|
return res |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
export function getFilesByType(pathFile, type = null) { |
144
|
|
|
const extension = '.' + config.files.templates.extension |
145
|
|
|
let result = [] |
146
|
|
|
|
147
|
|
|
try { |
148
|
|
|
var directory = fse.lstatSync(pathFile) |
149
|
|
|
if (!directory.isDirectory()) { |
150
|
|
|
mkdirp.sync(pathFile) |
151
|
|
|
} |
152
|
|
|
} catch (e) { |
153
|
|
|
mkdirp.sync(pathFile) |
154
|
|
|
} |
155
|
|
|
const files = coreUtils.file.getFilesSync(pathFile, true, extension) |
156
|
|
|
|
157
|
|
|
Array.prototype.forEach.call(files, (file) => { |
158
|
|
|
var val = cmsData.fileAttr.get(file).s |
159
|
|
|
if(type === null || val === type) result.push(file) |
|
|
|
|
160
|
|
|
}) |
161
|
|
|
|
162
|
|
|
return result |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
export function getFileObject(pathFile) { |
166
|
|
|
const pathData = path.join(config.root, config.data.url) |
167
|
|
|
const extension = '.json' |
168
|
|
|
const templateExtension = '.' + config.files.templates.extension |
169
|
|
|
|
170
|
|
|
const name = path.basename(pathFile) |
171
|
|
|
const relativePath = pathFile.replace(pathData + path.sep, '') |
172
|
|
|
|
173
|
|
|
const parentName = cmsData.fileAttr.delete(name) |
174
|
|
|
const parentRelativePath = cmsData.fileAttr.delete(pathFile).replace(pathData + path.sep, '') |
175
|
|
|
|
176
|
|
|
const fileData = cmsData.fileAttr.get(name) |
177
|
|
|
|
178
|
|
|
let date |
179
|
|
|
if (fileData.d) { |
180
|
|
|
date = fileData.d |
181
|
|
|
}else { |
182
|
|
|
const stat = fse.statSync(pathFile) |
183
|
|
|
date = stat.mtime |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
const fileDate = moment(date) |
187
|
|
|
const duration = moment.duration(moment(fileDate).diff(new Date())).humanize(true) |
188
|
|
|
|
189
|
|
|
const htmlUrl = path.join('/', relativePath.replace(extension, templateExtension)) |
190
|
|
|
|
191
|
|
|
let fileObject = { |
192
|
|
|
'name': name, |
193
|
|
|
'path': pathFile, |
194
|
|
|
'cleanPath': relativePath, |
195
|
|
|
'filePath': relativePath, |
196
|
|
|
'date': date, |
197
|
|
|
'cleanDate': fileDate.format('YYYY/MM/DD HH:mm:ss'), |
198
|
|
|
'duration': duration, |
199
|
|
|
'cleanName': parentName, |
200
|
|
|
'parentRelativePath': parentRelativePath, |
201
|
|
|
'html': htmlUrl |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return fileObject |
205
|
|
|
} |
206
|
|
|
|