2021-10-10 11:02:25 +02:00
|
|
|
const { execSync } = require("child_process");
|
2021-10-07 18:21:01 +02:00
|
|
|
const path = require("path");
|
2021-12-21 11:56:33 +01:00
|
|
|
const fs = require("fs");
|
2022-08-06 15:08:19 +02:00
|
|
|
const { argv, stdout, exit } = require("process");
|
2022-08-19 18:53:25 +02:00
|
|
|
const { execIn, parts, getDirs, walk, sanitizeVarName } = require("./utils");
|
2021-10-07 18:21:01 +02:00
|
|
|
|
2022-08-19 18:53:25 +02:00
|
|
|
if (argv.includes("help")) {
|
2022-08-06 15:08:19 +02:00
|
|
|
console.log(`Fosscord build script help:
|
|
|
|
Arguments:
|
|
|
|
clean Cleans up previous builds
|
|
|
|
verbose Enable verbose logging
|
|
|
|
logerrors Log build errors to console
|
2022-08-06 15:24:12 +02:00
|
|
|
pretty-errors Pretty-print build errors
|
2022-08-19 18:53:25 +02:00
|
|
|
silent No output to console or files.
|
|
|
|
propagate-err Exit script with error code if build fails.`);
|
2022-08-06 15:08:19 +02:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2022-08-19 18:53:25 +02:00
|
|
|
let steps = 5,
|
|
|
|
i = 0;
|
2021-12-21 21:49:58 +01:00
|
|
|
if (argv.includes("clean")) steps++;
|
2021-10-07 18:21:01 +02:00
|
|
|
|
2021-10-10 17:55:48 +02:00
|
|
|
const verbose = argv.includes("verbose") || argv.includes("v");
|
2022-08-06 15:32:28 +02:00
|
|
|
const logerr = argv.includes("logerrors");
|
|
|
|
const pretty = argv.includes("pretty-errors");
|
2022-08-06 15:24:12 +02:00
|
|
|
const silent = argv.includes("silent");
|
|
|
|
|
2022-08-19 18:53:25 +02:00
|
|
|
if (silent) console.error = console.log = function () {};
|
2021-10-07 18:21:01 +02:00
|
|
|
|
2021-10-15 00:44:19 +02:00
|
|
|
if (argv.includes("clean")) {
|
2021-12-21 21:49:58 +01:00
|
|
|
console.log(`[${++i}/${steps}] Cleaning...`);
|
2022-08-15 06:22:22 +02:00
|
|
|
let d = "dist";
|
|
|
|
if (fs.existsSync(d)) {
|
|
|
|
fs.rmSync(d, { recursive: true });
|
|
|
|
if (verbose) console.log(`Deleted ${d}!`);
|
|
|
|
}
|
2021-10-07 18:21:01 +02:00
|
|
|
}
|
|
|
|
|
2022-08-15 06:22:22 +02:00
|
|
|
console.log(`[${++i}/${steps}] Compiling src files ...`);
|
2021-10-08 10:43:43 +02:00
|
|
|
|
2022-08-19 18:53:25 +02:00
|
|
|
let buildFlags = "";
|
|
|
|
if (pretty) buildFlags += "--pretty ";
|
2021-10-07 18:21:01 +02:00
|
|
|
|
2022-08-19 18:53:25 +02:00
|
|
|
console.log(`[${++i}/${steps}] Building plugin index...`);
|
|
|
|
let pluginDir = path.join(__dirname, "..", "src", "plugins");
|
|
|
|
let output = 'import { Plugin } from "util/plugin";\n';
|
|
|
|
|
|
|
|
const dirs = fs.readdirSync(pluginDir).filter((x) => {
|
|
|
|
try {
|
|
|
|
fs.readdirSync(path.join(pluginDir, x));
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
2022-08-15 06:22:22 +02:00
|
|
|
}
|
2022-08-19 18:53:25 +02:00
|
|
|
});
|
|
|
|
dirs.forEach((x) => {
|
|
|
|
let pluginManifest = require(path.join(pluginDir, x, "plugin.json"));
|
|
|
|
output += `import * as ${sanitizeVarName(x)} from "./${x}/${pluginManifest.mainClass}";\n`;
|
|
|
|
});
|
|
|
|
output += `\nexport const PluginIndex: any = {\n`;
|
|
|
|
dirs.forEach((x) => {
|
|
|
|
output += ` "${x}": new ${sanitizeVarName(x)}.default(),\n`; //ctor test: '${path.resolve(path.join(pluginDir, x))}', require('./${x}/plugin.json')
|
|
|
|
});
|
|
|
|
output += `};`;
|
|
|
|
|
|
|
|
fs.writeFileSync(path.join(__dirname, "..", "src", "plugins", "PluginIndex.ts"), output);
|
|
|
|
|
|
|
|
if (!argv.includes("copyonly")) {
|
|
|
|
console.log(`[${++i}/${steps}] Compiling source code...`);
|
|
|
|
|
|
|
|
let buildFlags = "";
|
|
|
|
if (pretty) buildFlags += "--pretty ";
|
|
|
|
|
|
|
|
try {
|
|
|
|
execSync(
|
|
|
|
'node "' +
|
|
|
|
path.join(__dirname, "..", "node_modules", "typescript", "lib", "tsc.js") +
|
|
|
|
'" -p "' +
|
|
|
|
path.join(__dirname, "..") +
|
|
|
|
'" ' +
|
|
|
|
buildFlags,
|
|
|
|
{
|
|
|
|
cwd: path.join(__dirname, ".."),
|
|
|
|
shell: true,
|
|
|
|
env: process.env,
|
|
|
|
encoding: "utf8"
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
if (verbose || logerr) {
|
|
|
|
error.stdout.split(/\r?\n/).forEach((line) => {
|
|
|
|
let _line = line.replace("dist/", "", 1);
|
|
|
|
if (!pretty && _line.includes(".ts(")) {
|
|
|
|
//reformat file path for easy jumping
|
|
|
|
_line = _line.replace("(", ":", 1).replace(",", ":", 1).replace(")", "", 1);
|
|
|
|
}
|
|
|
|
console.error(_line);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
console.error(`Build failed! Please check build.log for info!`);
|
|
|
|
if (!silent) {
|
|
|
|
if (pretty) fs.writeFileSync("build.log.ansi", error.stdout);
|
|
|
|
fs.writeFileSync(
|
|
|
|
"build.log",
|
|
|
|
error.stdout.replaceAll(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, "")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
throw error;
|
2022-08-06 15:32:28 +02:00
|
|
|
}
|
2022-08-19 18:53:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`[${++i}/${steps}] Copying plugin data...`);
|
|
|
|
let pluginFiles = walk(pluginDir).filter((x) => !x.endsWith(".ts"));
|
|
|
|
pluginFiles.forEach((x) => {
|
|
|
|
fs.copyFileSync(x, x.replace("src", "dist"));
|
|
|
|
});
|