forked from jarek-foksa/xel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
executable file
·139 lines (116 loc) · 3.88 KB
/
Copy pathbuild.js
File metadata and controls
executable file
·139 lines (116 loc) · 3.88 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env node
let Fs = require("fs");
let Fse = require("fs-extra");
let Path = require("path");
let Rollup = require("rollup");
let childProcess = require("child_process");
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
let build = () => {
return new Promise(async (resolve) => {
let bundle = await Rollup.rollup({
input: "./xel.js",
onwarn: (warning, warn) => {
if (warning.code !== "CIRCULAR_DEPENDENCY") {
warn(warning);
}
}
});
let xelMinJS = (await bundle.generate({format: "iife"})).output[0].code;
xelMinJS = xelMinJS.replace(
`XIconElement.DEFAULT_ICONSET = null;`,
"XIconElement.DEFAULT_ICONSET = svg`" + Fs.readFileSync(`${__dirname}/iconsets/default.svg`, "utf8") + "`;"
);
Fs.writeFileSync(`${__dirname}/xel.min.js`, xelMinJS, "utf8");
resolve();
});
};
let publishFirebase = () => {
return new Promise((resolve) => {
let manifest = JSON.parse(Fs.readFileSync(`${__dirname}/firebase.json`, "utf8"));
// Remove old files
{
if (Fs.existsSync(`${__dirname}/dist`)) {
Fse.removeSync(`${__dirname}/dist`);
}
}
// Rewrite temporarly firebase.json by changing "public" directory from "./" to "./dist"
// Also get rid of any redirect
{
let adjustedManifest = JSON.parse(JSON.stringify(manifest));
adjustedManifest.hosting.public = "./dist";
adjustedManifest.hosting.redirects = [];
Fs.writeFileSync(`${__dirname}/firebase.json`, JSON.stringify(adjustedManifest, null, 2), "utf8");
}
// Copy over files to "./dist/"
{
let paths = [
`index.html`,
`fallback.html`,
`xel.min.js`,
`docs`,
`iconsets`,
`themes`,
`node_modules/prismjs/prism.js`,
`node_modules/prismjs/themes/prism-coy.css`,
`node_modules/prismjs/themes/prism-tomorrow.css`
];
for (let path of paths) {
Fse.copySync(`${__dirname}/${path}`, `${__dirname}/dist/${path}`);
Fse.copySync(`${__dirname}/${path}`, `${__dirname}/dist/node_modules/xel/${path}`);
}
}
// Rewrite "dist/index.html"
{
let indexHTML = Fs.readFileSync(`${__dirname}/dist/index.html`, "utf8")
indexHTML = indexHTML.replace(`<script type="module" src="xel.js"></script>`, `<script src="xel.min.js"></script>`);
Fs.writeFileSync(`${__dirname}/dist/index.html`, indexHTML, "utf8");
}
// Upload files to the Firebase server
{
let firebaseProcess = childProcess.spawn("firebase", ["deploy"], {cwd: __dirname, stdio: "inherit"});
firebaseProcess.on("exit", (error) => {
if (error) {
console.log(error.toString());
}
// Restore firebase.json
Fs.writeFileSync(`${__dirname}/firebase.json`, JSON.stringify(manifest, null, 2), "utf8");
// Clean up
Fse.removeSync(`${__dirname}/dist`);
resolve();
});
}
});
};
let publishNpm = () => {
return new Promise((resolve) => {
let command = "npm";
let args = ["publish"];
let options = {cwd: __dirname, stdio: "inherit"};
let npmProcess = childProcess.spawn(command, args, options);
npmProcess.on("exit", (error) => {
if (error) {
console.log(error.toString());
}
resolve();
});
});
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
(async () => {
let [ , , arg1, ...otherArgs] = process.argv
await build();
if (arg1 === "--publish") {
if (otherArgs.length === 0) {
await publishFirebase();
await publishNpm();
}
else {
if (otherArgs.includes("firebase")) {
await publishFirebase();
}
if (otherArgs.includes("npm")) {
await publishNpm();
}
}
}
})();