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-09-15 15:52:43 +05:30
|
|
|
const jsPostfix = `
|
|
|
|
window.DEBUG = true;
|
|
|
|
let buf = "";
|
|
|
|
console.error = (...params) => {
|
|
|
|
const lastLines = "...\\n" + buf.split("\\n").slice(-10).join("\\n");
|
|
|
|
// buf = buf + "ERR " + params.join(" ") + "\\n";
|
|
|
|
// const location = new Error().stack.split("\\n")[2];
|
|
|
|
alert(params.join(" ") +"\\n...\\n" + lastLines);
|
|
|
|
};
|
|
|
|
console.log = console.info = console.warn = (...params) => {
|
|
|
|
buf = buf + params.join(" ") + "\\n";
|
|
|
|
};
|
|
|
|
`;
|
|
|
|
|
|
|
|
const jsSuffix = `
|
|
|
|
setTimeout(() => {
|
|
|
|
const showlogs = document.getElementById("showlogs");
|
|
|
|
showlogs.addEventListener("click", () => {
|
|
|
|
const lastLines = "...\\n" + buf.split("\\n").slice(-20).join("\\n");
|
|
|
|
alert(lastLines);
|
|
|
|
}, true);
|
|
|
|
showlogs.innerText = "Show last 20 log lines";
|
|
|
|
}, 10000);
|
|
|
|
`;
|
|
|
|
|
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);
|
|
|
|
// 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 15:52:43 +05:30
|
|
|
// doc("html").attr("manifest", "manifest.appcache");
|
2019-09-15 12:44:20 +05:30
|
|
|
doc("script").replaceWith(
|
|
|
|
`<script type="text/javascript" src="brawl.js"></script>` +
|
2019-09-15 15:52:43 +05:30
|
|
|
`<script type="text/javascript">${jsPostfix} main(document.body); ${jsSuffix}</script>`);
|
2019-09-15 12:44:20 +05:30
|
|
|
await fs.writeFile(path.join(targetDir, "index.html"), doc.html(), "utf8");
|
|
|
|
// 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);
|
|
|
|
// 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 15:52:43 +05:30
|
|
|
// write appcache manifest
|
|
|
|
const manifestLines = [
|
|
|
|
`CACHE MANIFEST`,
|
|
|
|
`# v${version}`,
|
|
|
|
"brawl.js",
|
|
|
|
"brawl.css",
|
|
|
|
"index.html",
|
|
|
|
""
|
|
|
|
];
|
|
|
|
await fs.writeFile(path.join(targetDir, "manifest.appcache"), manifestLines.join("\n"), "utf8");
|
|
|
|
console.log(`built brawl ${version} successfully`);
|
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));
|