|
1
|
|
|
import path from 'path' |
|
2
|
|
|
import { |
|
3
|
|
|
fileUtils, |
|
4
|
|
|
FileParser, |
|
5
|
|
|
Util, |
|
6
|
|
|
cleanSlug, |
|
7
|
|
|
cmsTemplate, |
|
8
|
|
|
cmsOperations, |
|
9
|
|
|
config, |
|
10
|
|
|
Hooks, |
|
11
|
|
|
cmsData, |
|
12
|
|
|
Manager |
|
13
|
|
|
} from '../../' |
|
14
|
|
|
|
|
15
|
|
|
var create = function(template, pathCreate, name, req, forceJson = {}, duplicate = false) { |
|
16
|
|
|
var p = new Promise((resolve, reject) => { |
|
17
|
|
|
Hooks.instance.trigger('beforeCreate', template, pathCreate, name, req, forceJson) |
|
18
|
|
|
|
|
19
|
|
|
var templatePath = fileUtils.getTemplatePath(template.replace(config.root, '')) |
|
20
|
|
|
var filePath = path.join(pathCreate, name) |
|
21
|
|
|
filePath = cleanSlug(filePath) |
|
22
|
|
|
filePath = fileUtils.getFilePath(filePath) |
|
23
|
|
|
|
|
24
|
|
|
if(templatePath !== null && filePath !== null) { |
|
25
|
|
|
var tplUrl = FileParser.getFileDataFromUrl(filePath) |
|
26
|
|
|
|
|
27
|
|
|
if(!fileUtils.isFile(tplUrl.json.path)) { |
|
28
|
|
|
var json = (forceJson) ? forceJson : {} |
|
29
|
|
|
var tpl = templatePath |
|
30
|
|
|
var text = cmsTemplate.template.getTemplate(tpl) |
|
31
|
|
|
if (duplicate) { |
|
32
|
|
|
json = cmsData.removeDuplicateAttr(text, json) |
|
33
|
|
|
} |
|
34
|
|
|
text = Util.removeDataList(text) |
|
35
|
|
|
var resHook = Hooks.instance.trigger('beforeFirstSave', filePath, req.query, json, text) |
|
36
|
|
|
filePath = resHook.filePath |
|
37
|
|
|
json = resHook.json |
|
38
|
|
|
text = resHook.text |
|
39
|
|
|
|
|
40
|
|
|
Hooks.instance.trigger('afterCreate', json, text, pathCreate, name, req, forceJson) |
|
41
|
|
|
cmsOperations.save.save(filePath, template, json, text, 'draft', null, 'draft') |
|
42
|
|
|
.then((resSave) => { |
|
43
|
|
|
Manager.instance.updateList() |
|
44
|
|
|
filePath = resSave.htmlPath |
|
45
|
|
|
tplUrl = FileParser.getFileDataFromUrl(filePath) |
|
46
|
|
|
resolve(resSave.json) |
|
47
|
|
|
}).catch(function(e) { |
|
48
|
|
|
reject() |
|
49
|
|
|
console.error(e) |
|
50
|
|
|
}) |
|
51
|
|
|
}else { |
|
52
|
|
|
json = FileParser.getJson(tplUrl.json.path) |
|
53
|
|
|
resolve(json, tplUrl.json.path) |
|
54
|
|
|
} |
|
55
|
|
|
}else { |
|
56
|
|
|
reject() |
|
57
|
|
|
} |
|
58
|
|
|
}).catch(function(e) { |
|
59
|
|
|
console.error(e) |
|
60
|
|
|
}) |
|
61
|
|
|
|
|
62
|
|
|
return p |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
export default create |