|
1
|
|
|
import path from 'path' |
|
2
|
|
|
|
|
3
|
|
|
import { |
|
4
|
|
|
cmsData, |
|
5
|
|
|
Page, |
|
6
|
|
|
cmsOperations, |
|
7
|
|
|
cmsTemplates, |
|
8
|
|
|
coreUtils, |
|
9
|
|
|
config, |
|
10
|
|
|
abeExtend, |
|
11
|
|
|
Manager |
|
12
|
|
|
} from '../../' |
|
13
|
|
|
|
|
14
|
|
|
export function draft(filePath, json, workflow = 'draft') { |
|
15
|
|
|
var p = new Promise((resolve) => { |
|
16
|
|
|
abeExtend.hooks.instance.trigger('beforeDraft', json, filePath) |
|
17
|
|
|
|
|
18
|
|
|
var revisionPath = path.join(config.root, config.data.url, filePath.replace(`.${config.files.templates.extension}`, '.json')) |
|
19
|
|
|
revisionPath = coreUtils.file.addDateIsoToRevisionPath(revisionPath, workflow) |
|
20
|
|
|
var date = coreUtils.file.getDate(revisionPath) |
|
21
|
|
|
cmsData.metas.add(json, workflow, date) |
|
22
|
|
|
|
|
23
|
|
|
var template = cmsTemplates.template.getTemplate(json.abe_meta.template) |
|
24
|
|
|
|
|
25
|
|
|
cmsData.source.getDataList(path.dirname(json.abe_meta.link), template, json) |
|
26
|
|
|
.then(() => { |
|
27
|
|
|
|
|
28
|
|
|
json['abe_meta'].complete = cmsData.utils.getPercentOfRequiredTagsFilled(template, json) |
|
29
|
|
|
|
|
30
|
|
|
// var page = new Page(json.abe_meta.template, template, json, true) |
|
31
|
|
|
var result |
|
32
|
|
|
if (!cmsOperations.save.saveJson(revisionPath, json)) { |
|
33
|
|
|
result = { |
|
34
|
|
|
success: 0, |
|
35
|
|
|
error: 'cannot json save file' |
|
36
|
|
|
} |
|
37
|
|
|
}else { |
|
38
|
|
|
Manager.instance.updatePostInList(revisionPath) |
|
39
|
|
|
result = { |
|
40
|
|
|
success: 1, |
|
41
|
|
|
json: json |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
resolve(result) |
|
45
|
|
|
}) |
|
46
|
|
|
}) |
|
47
|
|
|
|
|
48
|
|
|
return p |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
export function publish(filePath, json) { |
|
52
|
|
|
var p = new Promise((resolve) => { |
|
53
|
|
|
abeExtend.hooks.instance.trigger('beforePublish', json, filePath) |
|
54
|
|
|
|
|
55
|
|
|
var revisionPath = path.join(config.root, config.data.url, filePath.replace(`.${config.files.templates.extension}`, '.json')) |
|
56
|
|
|
var postPath = path.join(config.root, config.publish.url, filePath) |
|
57
|
|
|
// revisionPath = coreUtils.file.addDateIsoToRevisionPath(revisionPath, workflow) |
|
58
|
|
|
cmsData.metas.add(json, 'publish') |
|
59
|
|
|
|
|
60
|
|
|
var template = cmsTemplates.template.getTemplate(json.abe_meta.template) |
|
61
|
|
|
|
|
62
|
|
|
cmsData.source.getDataList(path.dirname(json.abe_meta.link), template, json) |
|
63
|
|
|
.then(() => { |
|
64
|
|
|
json['abe_meta'].complete = cmsData.utils.getPercentOfRequiredTagsFilled(template, json) |
|
65
|
|
|
|
|
66
|
|
|
var page = new Page(json.abe_meta.template, template, json, true) |
|
67
|
|
|
|
|
68
|
|
|
var result |
|
69
|
|
|
if (!cmsOperations.save.saveHtml(postPath, page.html)) { |
|
70
|
|
|
result = { |
|
71
|
|
|
success: 0, |
|
72
|
|
|
error: 'cannot html save file' |
|
73
|
|
|
} |
|
74
|
|
|
}else { |
|
75
|
|
|
if (!cmsOperations.save.saveJson(revisionPath, json)) { |
|
76
|
|
|
result = { |
|
77
|
|
|
success: 0, |
|
78
|
|
|
error: 'cannot json save file' |
|
79
|
|
|
} |
|
80
|
|
|
}else { |
|
81
|
|
|
Manager.instance.updatePostInList(revisionPath) |
|
82
|
|
|
result = { |
|
83
|
|
|
success: 1, |
|
84
|
|
|
json: json |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
resolve(result) |
|
89
|
|
|
}) |
|
90
|
|
|
}) |
|
91
|
|
|
|
|
92
|
|
|
return p |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
export function unpublish(filePath) { |
|
96
|
|
|
abeExtend.hooks.instance.trigger('beforeUnpublish', filePath) |
|
97
|
|
|
|
|
98
|
|
|
var p = new Promise((resolve, reject) => { |
|
99
|
|
|
var revisionPath = path.join(config.root, config.data.url, filePath.replace(`.${config.files.templates.extension}`, '.json')) |
|
100
|
|
|
var postPath = path.join(config.root, config.publish.url, filePath) |
|
101
|
|
|
if(coreUtils.file.exist(revisionPath)) { |
|
102
|
|
|
var json = JSON.parse(JSON.stringify(cmsData.file.get(revisionPath))) |
|
103
|
|
|
if(json.abe_meta.publish != null) { |
|
104
|
|
|
delete json.abe_meta.publish |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
var p = draft( |
|
108
|
|
|
filePath, |
|
109
|
|
|
json, |
|
110
|
|
|
'draft' |
|
111
|
|
|
) |
|
112
|
|
|
|
|
113
|
|
|
p.then((result) => { |
|
114
|
|
|
cmsOperations.remove.removeFile(revisionPath, postPath) |
|
115
|
|
|
abeExtend.hooks.instance.trigger('afterUnpublish', revisionPath, postPath) |
|
116
|
|
|
var newRevisionPath = path.join(config.root, config.data.url, result.json.abe_meta.latest.abeUrl.replace(`.${config.files.templates.extension}`, '.json')) |
|
117
|
|
|
Manager.instance.updatePostInList(newRevisionPath) |
|
118
|
|
|
resolve(result) |
|
119
|
|
|
}).catch(function(e) { |
|
120
|
|
|
console.error('[ERROR] unpublish', e) |
|
121
|
|
|
reject() |
|
122
|
|
|
}) |
|
123
|
|
|
} |
|
124
|
|
|
}) |
|
125
|
|
|
|
|
126
|
|
|
return p |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
export function reject(filePath, json, workflow) { |
|
130
|
|
|
abeExtend.hooks.instance.trigger('beforeReject', filePath) |
|
131
|
|
|
|
|
132
|
|
|
var rejectToWorkflow |
|
133
|
|
|
var found = false |
|
134
|
|
|
Array.prototype.forEach.call(config.users.workflow, (flow) => { |
|
135
|
|
|
if (workflow === flow) { |
|
136
|
|
|
found = true |
|
137
|
|
|
} |
|
138
|
|
|
if (!found) { |
|
139
|
|
|
rejectToWorkflow = flow |
|
140
|
|
|
} |
|
141
|
|
|
}) |
|
142
|
|
|
if (!found) { |
|
143
|
|
|
rejectToWorkflow = 'draft' |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
var p = new Promise((resolve) => { |
|
147
|
|
|
if(json.abe_meta.publish != null) { |
|
148
|
|
|
delete json.abe_meta.publish |
|
149
|
|
|
} |
|
150
|
|
|
var p2 = draft( |
|
151
|
|
|
filePath, |
|
152
|
|
|
json, |
|
153
|
|
|
rejectToWorkflow |
|
|
|
|
|
|
154
|
|
|
) |
|
155
|
|
|
p2.then((result) => { |
|
156
|
|
|
abeExtend.hooks.instance.trigger('afterReject', result) |
|
157
|
|
|
resolve(result) |
|
158
|
|
|
}).catch(function(e) { |
|
159
|
|
|
console.error('[ERROR] reject.js', e) |
|
160
|
|
|
}) |
|
161
|
|
|
}) |
|
162
|
|
|
|
|
163
|
|
|
return p |
|
164
|
|
|
} |