2024-08-23 17:05:10 +02:00
|
|
|
const esbuild = require("esbuild");
|
|
|
|
const esbuildCopyPlugin = require("esbuild-plugin-copy");
|
|
|
|
const fs = require("fs");
|
2024-08-26 09:21:14 +02:00
|
|
|
const zlib = require("zlib");
|
2024-08-23 17:05:10 +02:00
|
|
|
const ejs = require("ejs");
|
2024-08-23 17:18:18 +02:00
|
|
|
const archiver = require("archiver");
|
2024-08-26 09:33:02 +02:00
|
|
|
const dependencies =
|
|
|
|
JSON.parse(fs.readFileSync(__dirname + "/package.json")).dependencies || {};
|
2024-08-23 17:05:10 +02:00
|
|
|
const requiredDependencyList = Object.keys(dependencies);
|
|
|
|
let dependencyList = Object.keys(dependencies);
|
2024-08-24 17:13:59 +02:00
|
|
|
const svrjsInfo = JSON.parse(fs.readFileSync(__dirname + "/svrjs.json"));
|
2024-08-27 10:49:36 +02:00
|
|
|
const { name, version, documentationURL, changes } = svrjsInfo;
|
2024-08-23 17:05:10 +02:00
|
|
|
|
|
|
|
// Function to find and add all dependencies into the dependencyList array.
|
|
|
|
function findAllDependencies(curList) {
|
|
|
|
// If no curList parameter is specified, use dependencyList.
|
|
|
|
if (!curList) curList = dependencyList;
|
|
|
|
curList.forEach((dependency) => {
|
2024-08-26 09:33:02 +02:00
|
|
|
const newDeplist = Object.keys(
|
|
|
|
JSON.parse(
|
|
|
|
fs
|
|
|
|
.readFileSync(
|
|
|
|
__dirname +
|
|
|
|
"/node_modules/" +
|
|
|
|
dependency.replace(/\/\.\./g, "") +
|
2024-09-20 06:37:14 +02:00
|
|
|
"/package.json"
|
2024-08-26 09:33:02 +02:00
|
|
|
)
|
2024-09-20 06:37:14 +02:00
|
|
|
.toString()
|
|
|
|
).dependencies || {}
|
2024-08-26 09:33:02 +02:00
|
|
|
);
|
2024-08-23 17:05:10 +02:00
|
|
|
let noDupNewDepList = [];
|
|
|
|
newDeplist.forEach((dep) => {
|
|
|
|
// Ignore duplicates
|
|
|
|
if (dependencyList.indexOf(dep) == -1) {
|
|
|
|
noDupNewDepList.push(dep);
|
|
|
|
dependencyList.push(dep);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Call findAllDependencies for the dependency list.
|
|
|
|
findAllDependencies(noDupNewDepList);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get list of all dependencies
|
|
|
|
findAllDependencies();
|
|
|
|
dependencyList = dependencyList.sort();
|
|
|
|
|
|
|
|
// Create and populate an object, where whenever the dependencies are required are listed.
|
|
|
|
let dependenciesAreRequired = {};
|
|
|
|
dependencyList.forEach((dependency) => {
|
|
|
|
dependenciesAreRequired[dependency] = false;
|
|
|
|
});
|
|
|
|
requiredDependencyList.forEach((dependency) => {
|
|
|
|
dependenciesAreRequired[dependency] = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create the template functions using EJS
|
2024-08-26 09:33:02 +02:00
|
|
|
const layoutTemplate = ejs.compile(
|
2024-09-20 06:37:14 +02:00
|
|
|
fs.readFileSync(__dirname + "/templates/layout.ejs").toString()
|
2024-08-26 09:33:02 +02:00
|
|
|
);
|
|
|
|
const testsTemplate = ejs.compile(
|
2024-09-20 06:37:14 +02:00
|
|
|
fs.readFileSync(__dirname + "/templates/tests.ejs").toString()
|
2024-08-26 09:33:02 +02:00
|
|
|
);
|
|
|
|
const indexTemplate = ejs.compile(
|
2024-09-20 06:37:14 +02:00
|
|
|
fs.readFileSync(__dirname + "/templates/index.ejs").toString()
|
2024-08-26 09:33:02 +02:00
|
|
|
);
|
|
|
|
const licensesTemplate = ejs.compile(
|
2024-09-20 06:37:14 +02:00
|
|
|
fs.readFileSync(__dirname + "/templates/licenses.ejs").toString()
|
2024-08-26 09:33:02 +02:00
|
|
|
);
|
|
|
|
const licenseElementTemplate = ejs.compile(
|
2024-09-20 06:37:14 +02:00
|
|
|
fs.readFileSync(__dirname + "/templates/licenseElement.ejs").toString()
|
2024-08-26 09:33:02 +02:00
|
|
|
);
|
2024-08-23 17:05:10 +02:00
|
|
|
|
|
|
|
let licenseElements = "";
|
|
|
|
|
|
|
|
// Generate the licenses list in HTML
|
|
|
|
dependencyList.forEach((dependency) => {
|
2024-08-26 09:33:02 +02:00
|
|
|
const packageJSON = JSON.parse(
|
|
|
|
fs
|
|
|
|
.readFileSync(
|
|
|
|
__dirname +
|
|
|
|
"/node_modules/" +
|
|
|
|
dependency.replace(/\/\.\./g, "") +
|
2024-09-20 06:37:14 +02:00
|
|
|
"/package.json"
|
2024-08-26 09:33:02 +02:00
|
|
|
)
|
2024-09-20 06:37:14 +02:00
|
|
|
.toString()
|
2024-08-26 09:33:02 +02:00
|
|
|
);
|
2024-08-23 17:05:10 +02:00
|
|
|
licenseElements += licenseElementTemplate({
|
2024-08-24 17:13:59 +02:00
|
|
|
moduleName: packageJSON.name,
|
|
|
|
name: name,
|
2024-08-23 17:05:10 +02:00
|
|
|
license: packageJSON.license,
|
|
|
|
description: packageJSON.description || "No description",
|
|
|
|
author: packageJSON.author ? packageJSON.author.name : packageJSON.author,
|
2024-09-20 06:37:14 +02:00
|
|
|
required: dependenciesAreRequired[dependency]
|
2024-08-23 17:05:10 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Generate pages
|
|
|
|
const licensesPage = layoutTemplate({
|
2024-08-24 17:13:59 +02:00
|
|
|
title: name + " " + version + " Licenses",
|
2024-08-23 17:05:10 +02:00
|
|
|
content: licensesTemplate({
|
2024-08-24 17:13:59 +02:00
|
|
|
name: name,
|
2024-08-23 17:05:10 +02:00
|
|
|
version: version,
|
2024-09-20 06:37:14 +02:00
|
|
|
licenses: licenseElements
|
|
|
|
})
|
2024-08-23 17:05:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const testsPage = layoutTemplate({
|
2024-08-24 17:13:59 +02:00
|
|
|
title: name + " " + version + " Tests",
|
2024-08-23 17:05:10 +02:00
|
|
|
content: testsTemplate({
|
2024-08-24 17:13:59 +02:00
|
|
|
name: name,
|
2024-09-20 06:37:14 +02:00
|
|
|
version: version
|
|
|
|
})
|
2024-08-23 17:05:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const indexPage = layoutTemplate({
|
2024-08-24 17:13:59 +02:00
|
|
|
title: name + " " + version,
|
2024-08-23 17:05:10 +02:00
|
|
|
content: indexTemplate({
|
2024-08-24 17:13:59 +02:00
|
|
|
name: name,
|
|
|
|
version: version,
|
2024-08-26 09:33:02 +02:00
|
|
|
documentationURL: documentationURL,
|
2024-09-20 06:37:14 +02:00
|
|
|
changes: changes
|
|
|
|
})
|
2024-08-23 17:05:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Remove the generated assets directory if exists, and create a new one.
|
|
|
|
if (fs.existsSync(__dirname + "/generatedAssets")) {
|
2024-08-26 09:33:02 +02:00
|
|
|
if (fs.rmSync) fs.rmSync(__dirname + "/generatedAssets", { recursive: true });
|
|
|
|
else fs.rmdirSync(__dirname + "/generatedAssets", { recursive: true });
|
2024-08-23 17:05:10 +02:00
|
|
|
}
|
|
|
|
fs.mkdirSync(__dirname + "/generatedAssets");
|
|
|
|
|
|
|
|
// Remove the dist directory if exists, and create a new one.
|
|
|
|
if (fs.existsSync(__dirname + "/dist")) {
|
2024-08-26 09:33:02 +02:00
|
|
|
if (fs.rmSync) fs.rmSync(__dirname + "/dist", { recursive: true });
|
|
|
|
else fs.rmdirSync(__dirname + "/dist", { recursive: true });
|
2024-08-23 17:05:10 +02:00
|
|
|
}
|
|
|
|
fs.mkdirSync(__dirname + "/dist");
|
2024-08-24 19:43:02 +02:00
|
|
|
fs.mkdirSync(__dirname + "/dist/log");
|
|
|
|
fs.mkdirSync(__dirname + "/dist/mods");
|
|
|
|
fs.mkdirSync(__dirname + "/dist/temp");
|
2024-08-23 17:05:10 +02:00
|
|
|
|
2024-08-23 17:18:18 +02:00
|
|
|
// Remove the out directory if exists, and create a new one.
|
|
|
|
if (fs.existsSync(__dirname + "/out")) {
|
2024-08-26 09:33:02 +02:00
|
|
|
if (fs.rmSync) fs.rmSync(__dirname + "/out", { recursive: true });
|
|
|
|
else fs.rmdirSync(__dirname + "/out", { recursive: true });
|
2024-08-23 17:18:18 +02:00
|
|
|
}
|
|
|
|
fs.mkdirSync(__dirname + "/out");
|
|
|
|
|
2024-08-23 17:05:10 +02:00
|
|
|
// Create a licenses directory
|
|
|
|
fs.mkdirSync(__dirname + "/generatedAssets/licenses");
|
|
|
|
|
|
|
|
// Write to HTML files
|
|
|
|
fs.writeFileSync(__dirname + "/generatedAssets/index.html", indexPage);
|
|
|
|
fs.writeFileSync(__dirname + "/generatedAssets/tests.html", testsPage);
|
2024-08-26 09:33:02 +02:00
|
|
|
fs.writeFileSync(
|
|
|
|
__dirname + "/generatedAssets/licenses/index.html",
|
2024-09-20 06:37:14 +02:00
|
|
|
licensesPage
|
2024-08-26 09:33:02 +02:00
|
|
|
);
|
2024-08-23 17:05:10 +02:00
|
|
|
|
|
|
|
// Bundle the source and copy the assets using esbuild and esbuild-plugin-copy
|
2024-08-26 09:33:02 +02:00
|
|
|
esbuild
|
|
|
|
.build({
|
|
|
|
entryPoints: ["src/index.js"],
|
|
|
|
bundle: true,
|
|
|
|
outfile: "dist/svr.js",
|
2024-08-26 08:24:29 +02:00
|
|
|
platform: "node",
|
|
|
|
target: "es2017",
|
2024-08-26 09:33:02 +02:00
|
|
|
plugins: [
|
|
|
|
esbuildCopyPlugin.copy({
|
|
|
|
resolveFrom: __dirname,
|
|
|
|
assets: {
|
|
|
|
from: ["./assets/**/*"],
|
2024-09-20 06:37:14 +02:00
|
|
|
to: ["./dist"]
|
2024-08-26 09:33:02 +02:00
|
|
|
},
|
|
|
|
globbyOptions: {
|
2024-09-20 06:37:14 +02:00
|
|
|
dot: true
|
|
|
|
}
|
2024-08-26 09:33:02 +02:00
|
|
|
}),
|
|
|
|
esbuildCopyPlugin.copy({
|
|
|
|
resolveFrom: __dirname,
|
|
|
|
assets: {
|
|
|
|
from: ["./generatedAssets/**/*"],
|
2024-09-20 06:37:14 +02:00
|
|
|
to: ["./dist"]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
]
|
2024-08-26 09:33:02 +02:00
|
|
|
})
|
|
|
|
.then(() => {
|
2024-09-20 06:37:14 +02:00
|
|
|
const utilFilesAndDirectories = fs.existsSync(
|
|
|
|
__dirname + "/src/extraScripts"
|
|
|
|
)
|
2024-09-20 06:27:28 +02:00
|
|
|
? fs.readdirSync(__dirname + "/src/extraScripts")
|
2024-08-26 09:33:02 +02:00
|
|
|
: [];
|
|
|
|
const utilFiles = [];
|
|
|
|
utilFilesAndDirectories.forEach((entry) => {
|
2024-09-20 06:27:28 +02:00
|
|
|
if (fs.statSync(__dirname + "/src/extraScripts/" + entry).isFile())
|
2024-08-26 09:33:02 +02:00
|
|
|
utilFiles.push(entry);
|
2024-08-26 08:24:29 +02:00
|
|
|
});
|
2024-08-26 09:33:02 +02:00
|
|
|
|
|
|
|
// Transpile utilities using esbuild
|
|
|
|
esbuild
|
|
|
|
.build({
|
2024-09-20 06:37:14 +02:00
|
|
|
entryPoints: utilFiles.map(
|
|
|
|
(filename) => "src/extraScripts/" + filename
|
|
|
|
),
|
2024-09-20 06:22:04 +02:00
|
|
|
bundle: true,
|
2024-08-26 09:33:02 +02:00
|
|
|
outdir: "dist",
|
|
|
|
platform: "node",
|
2024-09-20 06:37:14 +02:00
|
|
|
target: "es2017"
|
2024-08-26 09:33:02 +02:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
const archiveName =
|
|
|
|
"svr.js." +
|
|
|
|
version.toLowerCase().replace(/[^0-9a-z]+/g, ".") +
|
|
|
|
".zip";
|
|
|
|
const output = fs.createWriteStream(__dirname + "/out/" + archiveName);
|
|
|
|
const archive = archiver("zip", {
|
2024-09-20 06:37:14 +02:00
|
|
|
zlib: { level: 9 }
|
2024-08-26 09:33:02 +02:00
|
|
|
});
|
|
|
|
archive.pipe(output);
|
2024-08-27 10:38:26 +02:00
|
|
|
|
|
|
|
// Add everything in the "dist" directory except for "svr.js" and "svr.compressed"
|
2024-08-30 08:06:09 +02:00
|
|
|
archive.glob("**/*", {
|
|
|
|
cwd: __dirname + "/dist",
|
2024-09-20 06:37:14 +02:00
|
|
|
ignore: ["svr.js", "svr.compressed"],
|
2024-08-30 08:06:09 +02:00
|
|
|
dot: true
|
2024-08-27 10:38:26 +02:00
|
|
|
});
|
2024-08-26 09:33:02 +02:00
|
|
|
|
|
|
|
// Create a stream for the "svr.compressed" file
|
|
|
|
const compressedSVRJSFileStream = fs
|
|
|
|
.createReadStream(__dirname + "/dist/svr.js")
|
|
|
|
.pipe(
|
|
|
|
zlib.createGzip({
|
2024-09-20 06:37:14 +02:00
|
|
|
level: 9
|
|
|
|
})
|
2024-08-26 09:33:02 +02:00
|
|
|
);
|
|
|
|
archive.append(compressedSVRJSFileStream, { name: "svr.compressed" });
|
|
|
|
archive.append(
|
2024-09-20 06:35:17 +02:00
|
|
|
'const zlib = require("zlib");\nconst fs = require("fs");\nconsole.log("Deleting SVR.JS stub...");\nfs.unlinkSync("svr.js");\nconsole.log("Decompressing SVR.JS...");\nconst script = zlib.gunzipSync(fs.readFileSync("svr.compressed"));\nfs.unlinkSync("svr.compressed");\nfs.writeFileSync("svr.js",script);\nconsole.log("Restart SVR.JS to get server interface.");',
|
2024-09-20 06:37:14 +02:00
|
|
|
{ name: "svr.js" }
|
2024-08-26 09:33:02 +02:00
|
|
|
);
|
|
|
|
archive.finalize();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
throw err;
|
|
|
|
});
|
2024-08-26 08:24:29 +02:00
|
|
|
})
|
2024-08-26 09:33:02 +02:00
|
|
|
.catch((err) => {
|
|
|
|
throw err;
|
|
|
|
});
|