1
0
Fork 0
forked from svrjs/svrjs

Lint out the build script

This commit is contained in:
Dorian Niemiec 2024-08-26 09:33:02 +02:00
parent f074612dda
commit 9d31eac2f9

View file

@ -4,7 +4,8 @@ const fs = require("fs");
const zlib = require("zlib"); const zlib = require("zlib");
const ejs = require("ejs"); const ejs = require("ejs");
const archiver = require("archiver"); const archiver = require("archiver");
const dependencies = JSON.parse(fs.readFileSync(__dirname + "/package.json")).dependencies || {}; const dependencies =
JSON.parse(fs.readFileSync(__dirname + "/package.json")).dependencies || {};
const requiredDependencyList = Object.keys(dependencies); const requiredDependencyList = Object.keys(dependencies);
let dependencyList = Object.keys(dependencies); let dependencyList = Object.keys(dependencies);
const svrjsInfo = JSON.parse(fs.readFileSync(__dirname + "/svrjs.json")); const svrjsInfo = JSON.parse(fs.readFileSync(__dirname + "/svrjs.json"));
@ -15,7 +16,18 @@ function findAllDependencies(curList) {
// If no curList parameter is specified, use dependencyList. // If no curList parameter is specified, use dependencyList.
if (!curList) curList = dependencyList; if (!curList) curList = dependencyList;
curList.forEach((dependency) => { curList.forEach((dependency) => {
const newDeplist = Object.keys(JSON.parse(fs.readFileSync(__dirname + "/node_modules/" + dependency.replace(/\/\.\./g,"") + "/package.json").toString()).dependencies || {}); const newDeplist = Object.keys(
JSON.parse(
fs
.readFileSync(
__dirname +
"/node_modules/" +
dependency.replace(/\/\.\./g, "") +
"/package.json",
)
.toString(),
).dependencies || {},
);
let noDupNewDepList = []; let noDupNewDepList = [];
newDeplist.forEach((dep) => { newDeplist.forEach((dep) => {
// Ignore duplicates // Ignore duplicates
@ -43,24 +55,43 @@ requiredDependencyList.forEach((dependency) => {
}); });
// Create the template functions using EJS // Create the template functions using EJS
const layoutTemplate = ejs.compile(fs.readFileSync(__dirname + "/templates/layout.ejs").toString()); const layoutTemplate = ejs.compile(
const testsTemplate = ejs.compile(fs.readFileSync(__dirname + "/templates/tests.ejs").toString()); fs.readFileSync(__dirname + "/templates/layout.ejs").toString(),
const indexTemplate = ejs.compile(fs.readFileSync(__dirname + "/templates/index.ejs").toString()); );
const licensesTemplate = ejs.compile(fs.readFileSync(__dirname + "/templates/licenses.ejs").toString()); const testsTemplate = ejs.compile(
const licenseElementTemplate = ejs.compile(fs.readFileSync(__dirname + "/templates/licenseElement.ejs").toString()); fs.readFileSync(__dirname + "/templates/tests.ejs").toString(),
);
const indexTemplate = ejs.compile(
fs.readFileSync(__dirname + "/templates/index.ejs").toString(),
);
const licensesTemplate = ejs.compile(
fs.readFileSync(__dirname + "/templates/licenses.ejs").toString(),
);
const licenseElementTemplate = ejs.compile(
fs.readFileSync(__dirname + "/templates/licenseElement.ejs").toString(),
);
let licenseElements = ""; let licenseElements = "";
// Generate the licenses list in HTML // Generate the licenses list in HTML
dependencyList.forEach((dependency) => { dependencyList.forEach((dependency) => {
const packageJSON = JSON.parse(fs.readFileSync(__dirname + "/node_modules/" + dependency.replace(/\/\.\./g,"") + "/package.json").toString()); const packageJSON = JSON.parse(
fs
.readFileSync(
__dirname +
"/node_modules/" +
dependency.replace(/\/\.\./g, "") +
"/package.json",
)
.toString(),
);
licenseElements += licenseElementTemplate({ licenseElements += licenseElementTemplate({
moduleName: packageJSON.name, moduleName: packageJSON.name,
name: name, name: name,
license: packageJSON.license, license: packageJSON.license,
description: packageJSON.description || "No description", description: packageJSON.description || "No description",
author: packageJSON.author ? packageJSON.author.name : packageJSON.author, author: packageJSON.author ? packageJSON.author.name : packageJSON.author,
required: dependenciesAreRequired[dependency] required: dependenciesAreRequired[dependency],
}); });
}); });
@ -70,16 +101,16 @@ const licensesPage = layoutTemplate({
content: licensesTemplate({ content: licensesTemplate({
name: name, name: name,
version: version, version: version,
licenses: licenseElements licenses: licenseElements,
}) }),
}); });
const testsPage = layoutTemplate({ const testsPage = layoutTemplate({
title: name + " " + version + " Tests", title: name + " " + version + " Tests",
content: testsTemplate({ content: testsTemplate({
name: name, name: name,
version: version version: version,
}) }),
}); });
const indexPage = layoutTemplate({ const indexPage = layoutTemplate({
@ -87,8 +118,8 @@ const indexPage = layoutTemplate({
content: indexTemplate({ content: indexTemplate({
name: name, name: name,
version: version, version: version,
documentationURL: documentationURL documentationURL: documentationURL,
}) }),
}); });
// Remove the generated assets directory if exists, and create a new one. // Remove the generated assets directory if exists, and create a new one.
@ -121,10 +152,14 @@ fs.mkdirSync(__dirname + "/generatedAssets/licenses");
// Write to HTML files // Write to HTML files
fs.writeFileSync(__dirname + "/generatedAssets/index.html", indexPage); fs.writeFileSync(__dirname + "/generatedAssets/index.html", indexPage);
fs.writeFileSync(__dirname + "/generatedAssets/tests.html", testsPage); fs.writeFileSync(__dirname + "/generatedAssets/tests.html", testsPage);
fs.writeFileSync(__dirname + "/generatedAssets/licenses/index.html", licensesPage); fs.writeFileSync(
__dirname + "/generatedAssets/licenses/index.html",
licensesPage,
);
// Bundle the source and copy the assets using esbuild and esbuild-plugin-copy // Bundle the source and copy the assets using esbuild and esbuild-plugin-copy
esbuild.build({ esbuild
.build({
entryPoints: ["src/index.js"], entryPoints: ["src/index.js"],
bundle: true, bundle: true,
outfile: "dist/svr.js", outfile: "dist/svr.js",
@ -138,48 +173,68 @@ esbuild.build({
to: ["./dist"], to: ["./dist"],
}, },
globbyOptions: { globbyOptions: {
dot: true dot: true,
} },
}), }),
esbuildCopyPlugin.copy({ esbuildCopyPlugin.copy({
resolveFrom: __dirname, resolveFrom: __dirname,
assets: { assets: {
from: ["./generatedAssets/**/*"], from: ["./generatedAssets/**/*"],
to: ["./dist"], to: ["./dist"],
} },
}) }),
], ],
}).then(() => { })
const utilFilesAndDirectories = fs.existsSync(__dirname + "/utils") ? fs.readdirSync(__dirname + "/utils") : []; .then(() => {
const utilFilesAndDirectories = fs.existsSync(__dirname + "/utils")
? fs.readdirSync(__dirname + "/utils")
: [];
const utilFiles = []; const utilFiles = [];
utilFilesAndDirectories.forEach((entry) => { utilFilesAndDirectories.forEach((entry) => {
if (fs.statSync(__dirname + "/utils/" + entry).isFile()) utilFiles.push(entry); if (fs.statSync(__dirname + "/utils/" + entry).isFile())
}) utilFiles.push(entry);
});
// Transpile utilities using esbuild // Transpile utilities using esbuild
esbuild.build({ esbuild
.build({
entryPoints: utilFiles.map((filename) => "utils/" + filename), entryPoints: utilFiles.map((filename) => "utils/" + filename),
bundle: false, bundle: false,
outdir: "dist", outdir: "dist",
platform: "node", platform: "node",
target: "es2017", target: "es2017",
}).then(() => { })
const archiveName = "svr.js." + version.toLowerCase().replace(/[^0-9a-z]+/g,".") + ".zip"; .then(() => {
const archiveName =
"svr.js." +
version.toLowerCase().replace(/[^0-9a-z]+/g, ".") +
".zip";
const output = fs.createWriteStream(__dirname + "/out/" + archiveName); const output = fs.createWriteStream(__dirname + "/out/" + archiveName);
const archive = archiver("zip", { const archive = archiver("zip", {
zlib: { level: 9 } // Sets the compression level. zlib: { level: 9 },
}); });
archive.pipe(output); archive.pipe(output);
archive.directory(__dirname + "/dist/", false); archive.directory(__dirname + "/dist/", false);
const compressedSVRJSFileStream = fs.createReadStream(__dirname + "/dist/svr.js").pipe(zlib.createGzip({
level: 9 // Create a stream for the "svr.compressed" file
})); const compressedSVRJSFileStream = fs
.createReadStream(__dirname + "/dist/svr.js")
.pipe(
zlib.createGzip({
level: 9,
}),
);
archive.append(compressedSVRJSFileStream, { name: "svr.compressed" }); archive.append(compressedSVRJSFileStream, { name: "svr.compressed" });
archive.append('const zlib = require("zlib");\nconst fs = require("fs");\nconst tar = require("tar");\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.");', {name: "svr.js"}); archive.append(
'const zlib = require("zlib");\nconst fs = require("fs");\nconst tar = require("tar");\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.");',
{ name: "svr.js" },
);
archive.finalize(); archive.finalize();
}).catch((err) => {
throw err;
}) })
}).catch((err) => { .catch((err) => {
throw err;
});
})
.catch((err) => {
throw err; throw err;
}); });