2019-09-15 12:44:20 +05:30
|
|
|
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 13:25:30 +05:30
|
|
|
const debug = false;
|
2019-09-28 13:15:01 +05:30
|
|
|
const offline = true;
|
2019-09-15 15:52:43 +05:30
|
|
|
|
2019-09-15 12:44:20 +05:30
|
|
|
async function build() {
|
2019-09-15 15:52:43 +05:30
|
|
|
// get version number
|
|
|
|
const version = JSON.parse(await fs.readFile(path.join(projectDir, "package.json"), "utf8")).version;
|
2019-09-15 12:44:20 +05:30
|
|
|
// clear target dir
|
|
|
|
await removeDirIfExists(targetDir);
|
|
|
|
await fs.mkdir(targetDir);
|
2019-09-15 18:02:12 +05:30
|
|
|
|
2020-03-24 03:16:31 +05:30
|
|
|
await buildHtml(version);
|
2019-09-15 18:02:12 +05:30
|
|
|
await buildJs();
|
|
|
|
await buildCss();
|
2019-09-28 13:15:01 +05:30
|
|
|
if (offline) {
|
|
|
|
await buildOffline(version);
|
|
|
|
}
|
2019-09-15 18:02:12 +05:30
|
|
|
|
|
|
|
console.log(`built brawl ${version} successfully`);
|
|
|
|
}
|
|
|
|
|
2020-03-24 03:16:31 +05:30
|
|
|
async function buildHtml(version) {
|
2019-09-15 12:44:20 +05:30
|
|
|
// 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 18:02:12 +05:30
|
|
|
doc("script#main").replaceWith(
|
2019-09-15 12:44:20 +05:30
|
|
|
`<script type="text/javascript" src="brawl.js"></script>` +
|
2019-09-15 18:02:12 +05:30
|
|
|
`<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 23:53:37 +05:30
|
|
|
removeOrEnableScript(doc("script#service-worker"), offline);
|
2020-03-24 03:16:31 +05:30
|
|
|
|
|
|
|
const versionScript = doc("script#version");
|
|
|
|
versionScript.attr("type", "text/javascript");
|
|
|
|
let vSource = versionScript.contents().text();
|
|
|
|
vSource = vSource.replace(`"%%VERSION%%"`, `"${version}"`);
|
|
|
|
versionScript.text(vSource);
|
|
|
|
|
2019-09-28 13:15:01 +05:30
|
|
|
if (offline) {
|
|
|
|
doc("html").attr("manifest", "manifest.appcache");
|
|
|
|
doc("head").append(`<link rel="manifest" href="manifest.json">`);
|
|
|
|
}
|
2019-09-15 12:44:20 +05:30
|
|
|
await fs.writeFile(path.join(targetDir, "index.html"), doc.html(), "utf8");
|
2019-09-15 18:02:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
async function buildJs() {
|
2019-09-15 12:44:20 +05:30
|
|
|
// 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 18:02:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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-14 03:24:11 +05:30
|
|
|
display: "fullscreen",
|
2019-09-15 18:02:12 +05:30
|
|
|
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 12:44:20 +05:30
|
|
|
// 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]);
|
2020-08-04 22:32:34 +05:30
|
|
|
const result = await cssBundler.process(preCss, {from: cssMainFile});
|
|
|
|
await fs.writeFile(path.join(targetDir, "brawl.css"), result.css, "utf8");
|
2019-09-15 18:02:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function removeOrEnableScript(scriptNode, enable) {
|
|
|
|
if (enable) {
|
|
|
|
scriptNode.attr("type", "text/javascript");
|
|
|
|
} else {
|
|
|
|
scriptNode.remove();
|
|
|
|
}
|
2019-09-15 12:44:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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 15:52:43 +05:30
|
|
|
if (err.code !== "ENOENT") {
|
|
|
|
throw err;
|
|
|
|
}
|
2019-09-15 12:44:20 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
build().catch(err => console.error(err));
|