-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (90 loc) · 2.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const fs = require("fs");
const path = require("path");
const archiver = require("archiver");
const cheerio = require("cheerio");
var appDir = path.resolve("./");
var metadataPath = path.resolve("metadata.json");
class DexTemplatePlugin {
constructor(generatePreview = true){
this.preview = generatePreview;
}
apply(compiler) {
compiler.hooks.done.tap("Bindings Plugin", stats => {
if (stats.compilation.compiler.watchMode) return;
if (!fs.existsSync(metadataPath)) {
console.log("Missing metadata.json");
return;
}
var metadata = require(metadataPath);
fs.copyFileSync(
metadataPath,
path.join(compiler.options.output.path, "metadata.json")
);
let indexPath = path.join(compiler.options.output.path, "index.html");
let html = fs.readFileSync(indexPath, "utf8");
try {
//let metadata = JSON.parse(fs.readFileSync("metadata.json", "utf8"));
let bindings = metadata.bindings;
const $ = cheerio.load(html);
let bindingContainer = $("<div>");
bindingContainer.attr("dex-template", "");
$("head").append(bindingContainer);
bindings.forEach(binding => {
if ($(`#${binding.id}`).length === 0)
bindingContainer.append(
`<span id=${binding.id} hidden>{{${binding.id}}}</span>`
);
});
fs.writeFileSync(indexPath, $.html());
html = $.html();
if (fs.existsSync(metadata.icon)) {
fs.copyFileSync(
metadata.icon,
path.join(compiler.options.output.path, metadata.icon)
);
}
metadata.preview.forEach(icon => {
if (fs.existsSync(icon)) {
fs.copyFileSync(
icon,
path.join(compiler.options.output.path, icon)
);
}
});
bindings.forEach(binding => {
html = html.replace(`{{${binding.id}}}`, binding.value);
});
if(this.preview){
let previewPath = indexPath.replace(".html", ".preview.html");
fs.writeFileSync(previewPath, html);
if (!fs.existsSync(path.join(appDir, "builds"))) {
fs.mkdirSync(path.join(appDir, "builds"));
}
}
var zip = fs.createWriteStream(
path.join(
appDir,
"builds",
`${metadata.name}-${metadata.version}.dextpl`
)
);
var archive = archiver("zip");
zip.on("close", function() {
console.log(archive.pointer() + " total bytes");
console.log(
"archiver has been finalized and the output file descriptor has closed."
);
});
archive.on("error", function(err) {
throw err;
});
archive.pipe(zip);
archive.directory(compiler.options.output.path, false);
archive.finalize();
} catch (e) {
console.log(e);
}
});
}
}
module.exports = DexTemplatePlugin;