|
1
|
|
|
import Handlebars from 'handlebars' |
|
2
|
|
|
import fse from 'fs-extra' |
|
3
|
|
|
import path from 'path' |
|
4
|
|
|
|
|
5
|
|
|
import { |
|
6
|
|
|
Plugins |
|
7
|
|
|
,fileUtils |
|
8
|
|
|
,Hooks |
|
9
|
|
|
} from '../../../' |
|
10
|
|
|
|
|
11
|
|
|
export default function abeImport (file, config, ctx) { |
|
12
|
|
|
file = Hooks.instance.trigger('beforeImport', file, config, ctx) |
|
13
|
|
|
|
|
14
|
|
|
config = JSON.parse(config) |
|
15
|
|
|
let intlData = config.intlData |
|
16
|
|
|
var defaultPartials = `${__dirname.replace(/\/$/,'')}/${config.defaultPartials.replace(/\/$/,'')}` |
|
17
|
|
|
var custom = (config.custom !== '') ? `${config.root.replace(/\/$/,'')}/${config.custom.replace(/\/$/,'')}` : defaultPartials |
|
18
|
|
|
var pathToPartial = `${custom}/${file}.html` |
|
19
|
|
|
try{ |
|
20
|
|
|
fse.statSync(pathToPartial) |
|
21
|
|
|
} |
|
22
|
|
|
catch(e){ |
|
23
|
|
|
pathToPartial = `${defaultPartials}/${file}.html` |
|
24
|
|
|
} |
|
25
|
|
|
if (fileUtils.isFile(pathToPartial)) { |
|
26
|
|
|
var html = fse.readFileSync(pathToPartial, 'utf8') |
|
27
|
|
|
}else { |
|
28
|
|
|
html = '' |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
var pluginsPartials = Plugins.instance.getPartials() |
|
32
|
|
|
Array.prototype.forEach.call(pluginsPartials, (pluginPartials) => { |
|
33
|
|
|
var checkFile = path.join(pluginPartials, `${file}.html`) |
|
34
|
|
|
if (fileUtils.isFile(checkFile)) { |
|
35
|
|
|
html += fse.readFileSync(checkFile, 'utf8') |
|
36
|
|
|
} |
|
37
|
|
|
}) |
|
38
|
|
|
html = Hooks.instance.trigger('afterImport', html, file, config, ctx) |
|
39
|
|
|
|
|
40
|
|
|
var template = Handlebars.compile(html) |
|
41
|
|
|
var res = new Handlebars.SafeString(template(ctx, {data: {intl: intlData}})) |
|
42
|
|
|
|
|
43
|
|
|
return res |
|
44
|
|
|
} |
|
45
|
|
|
|