2019-09-15 09:14:20 +02:00
|
|
|
import cheerio from "cheerio";
|
|
|
|
import fsRoot from "fs";
|
|
|
|
const fs = fsRoot.promises;
|
|
|
|
import path from "path";
|
|
|
|
import rollup from 'rollup';
|
|
|
|
import postcss from "postcss";
|
|
|
|
import postcssImport from "postcss-import";
|
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
import { dirname } from 'path';
|
|
|
|
|
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
const __dirname = dirname(__filename);
|
|
|
|
const projectDir = path.join(__dirname, "../");
|
|
|
|
const targetDir = path.join(projectDir, "target");
|
|
|
|
|
2019-11-22 08:55:30 +01:00
|
|
|
const debug = false;
|
2019-09-28 09:45:01 +02:00
|
|
|
const offline = true;
|
2019-09-15 12:22:43 +02:00
|
|
|
|
2019-09-15 09:14:20 +02:00
|
|
|
async function build() {
|
2019-09-15 12:22:43 +02:00
|
|
|
// get version number
|
|
|
|
const version = JSON.parse(await fs.readFile(path.join(projectDir, "package.json"), "utf8")).version;
|
2019-09-15 09:14:20 +02:00
|
|
|
// clear target dir
|
|
|
|
await removeDirIfExists(targetDir);
|
|
|
|
await fs.mkdir(targetDir);
|
2019-09-15 14:32:12 +02:00
|
|
|
|
|
|
|
await buildHtml();
|
|
|
|
await buildJs();
|
|
|
|
await buildCss();
|
2019-09-28 09:45:01 +02:00
|
|
|
if (offline) {
|
|
|
|
await buildOffline(version);
|
|
|
|
}
|
2019-09-15 14:32:12 +02:00
|
|
|
|
|
|
|
console.log(`built brawl ${version} successfully`);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function buildHtml() {
|
2019-09-15 09:14:20 +02:00
|
|
|
// transform html file
|
|
|
|
const devHtml = await fs.readFile(path.join(projectDir, "index.html"), "utf8");
|
|
|
|
const doc = cheerio.load(devHtml);
|
|
|
|
doc("link[rel=stylesheet]").attr("href", "brawl.css");
|
2019-09-15 14:32:12 +02:00
|
|
|
doc("script#main").replaceWith(
|
2019-09-15 09:14:20 +02:00
|
|
|
`<script type="text/javascript" src="brawl.js"></script>` +
|
2019-09-15 14:32:12 +02:00
|
|
|
`<script type="text/javascript">main(document.body);</script>`);
|
|
|
|
removeOrEnableScript(doc("script#phone-debug-pre"), debug);
|
|
|
|
removeOrEnableScript(doc("script#phone-debug-post"), debug);
|
2019-10-12 20:23:37 +02:00
|
|
|
removeOrEnableScript(doc("script#service-worker"), offline);
|
2019-09-28 09:45:01 +02:00
|
|
|
if (offline) {
|
|
|
|
doc("html").attr("manifest", "manifest.appcache");
|
|
|
|
doc("head").append(`<link rel="manifest" href="manifest.json">`);
|
|
|
|
}
|
2019-09-15 09:14:20 +02:00
|
|
|
await fs.writeFile(path.join(targetDir, "index.html"), doc.html(), "utf8");
|
2019-09-15 14:32:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function buildJs() {
|
2019-09-15 09:14:20 +02:00
|
|
|
// create js bundle
|
|
|
|
const rollupConfig = {
|
|
|
|
input: 'src/main.js',
|
|
|
|
output: {
|
|
|
|
file: path.join(targetDir, "brawl.js"),
|
|
|
|
format: 'iife',
|
|
|
|
name: 'main'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const bundle = await rollup.rollup(rollupConfig);
|
|
|
|
await bundle.write(rollupConfig);
|
2019-09-15 14:32:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function buildOffline(version) {
|
|
|
|
// write offline availability
|
|
|
|
const offlineFiles = ["brawl.js", "brawl.css", "index.html", "icon-192.png"];
|
|
|
|
|
|
|
|
// write appcache manifest
|
|
|
|
const manifestLines = [
|
|
|
|
`CACHE MANIFEST`,
|
|
|
|
`# v${version}`,
|
|
|
|
`NETWORK`,
|
|
|
|
`"*"`,
|
|
|
|
`CACHE`,
|
|
|
|
];
|
|
|
|
manifestLines.push(...offlineFiles);
|
|
|
|
const manifest = manifestLines.join("\n") + "\n";
|
|
|
|
await fs.writeFile(path.join(targetDir, "manifest.appcache"), manifest, "utf8");
|
|
|
|
// write service worker
|
|
|
|
let swSource = await fs.readFile(path.join(projectDir, "src/service-worker.template.js"), "utf8");
|
|
|
|
swSource = swSource.replace(`"%%VERSION%%"`, `"${version}"`);
|
|
|
|
swSource = swSource.replace(`"%%FILES%%"`, JSON.stringify(offlineFiles));
|
|
|
|
await fs.writeFile(path.join(targetDir, "sw.js"), swSource, "utf8");
|
|
|
|
// write web manifest
|
|
|
|
const webManifest = {
|
|
|
|
name: "Brawl Chat",
|
|
|
|
short_name: "Brawl",
|
2020-03-13 22:54:11 +01:00
|
|
|
display: "fullscreen",
|
2019-09-15 14:32:12 +02:00
|
|
|
start_url: "index.html",
|
|
|
|
icons: [{"src": "icon-192.png", "sizes": "192x192", "type": "image/png"}],
|
|
|
|
};
|
|
|
|
await fs.writeFile(path.join(targetDir, "manifest.json"), JSON.stringify(webManifest), "utf8");
|
|
|
|
// copy icon
|
|
|
|
let icon = await fs.readFile(path.join(projectDir, "icon.png"));
|
|
|
|
await fs.writeFile(path.join(targetDir, "icon-192.png"), icon);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function buildCss() {
|
2019-09-15 09:14:20 +02:00
|
|
|
// create css bundle
|
|
|
|
const cssMainFile = path.join(projectDir, "src/ui/web/css/main.css");
|
|
|
|
const preCss = await fs.readFile(cssMainFile, "utf8");
|
|
|
|
const cssBundler = postcss([postcssImport]);
|
|
|
|
const postCss = await cssBundler.process(preCss, {from: cssMainFile});
|
|
|
|
await fs.writeFile(path.join(targetDir, "brawl.css"), postCss, "utf8");
|
2019-09-15 14:32:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function removeOrEnableScript(scriptNode, enable) {
|
|
|
|
if (enable) {
|
|
|
|
scriptNode.attr("type", "text/javascript");
|
|
|
|
} else {
|
|
|
|
scriptNode.remove();
|
|
|
|
}
|
2019-09-15 09:14:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function removeDirIfExists(targetDir) {
|
|
|
|
try {
|
|
|
|
const files = await fs.readdir(targetDir);
|
|
|
|
await Promise.all(files.map(filename => fs.unlink(path.join(targetDir, filename))));
|
|
|
|
await fs.rmdir(targetDir);
|
|
|
|
} catch (err) {
|
2019-09-15 12:22:43 +02:00
|
|
|
if (err.code !== "ENOENT") {
|
|
|
|
throw err;
|
|
|
|
}
|
2019-09-15 09:14:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
build().catch(err => console.error(err));
|