forked from svrjs/svrjs
Lint out the build script
This commit is contained in:
parent
f074612dda
commit
9d31eac2f9
1 changed files with 134 additions and 79 deletions
|
@ -4,18 +4,30 @@ 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"));
|
||||||
const {name, version, documentationURL} = svrjsInfo;
|
const { name, version, documentationURL } = svrjsInfo;
|
||||||
|
|
||||||
// Function to find and add all dependencies into the dependencyList array.
|
// Function to find and add all dependencies into the dependencyList array.
|
||||||
function findAllDependencies(curList) {
|
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,21 +118,21 @@ 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.
|
||||||
if (fs.existsSync(__dirname + "/generatedAssets")) {
|
if (fs.existsSync(__dirname + "/generatedAssets")) {
|
||||||
if (fs.rmSync) fs.rmSync(__dirname + "/generatedAssets", {recursive: true});
|
if (fs.rmSync) fs.rmSync(__dirname + "/generatedAssets", { recursive: true });
|
||||||
else fs.rmdirSync(__dirname + "/generatedAssets", {recursive: true});
|
else fs.rmdirSync(__dirname + "/generatedAssets", { recursive: true });
|
||||||
}
|
}
|
||||||
fs.mkdirSync(__dirname + "/generatedAssets");
|
fs.mkdirSync(__dirname + "/generatedAssets");
|
||||||
|
|
||||||
// Remove the dist directory if exists, and create a new one.
|
// Remove the dist directory if exists, and create a new one.
|
||||||
if (fs.existsSync(__dirname + "/dist")) {
|
if (fs.existsSync(__dirname + "/dist")) {
|
||||||
if (fs.rmSync) fs.rmSync(__dirname + "/dist", {recursive: true});
|
if (fs.rmSync) fs.rmSync(__dirname + "/dist", { recursive: true });
|
||||||
else fs.rmdirSync(__dirname + "/dist", {recursive: true});
|
else fs.rmdirSync(__dirname + "/dist", { recursive: true });
|
||||||
}
|
}
|
||||||
fs.mkdirSync(__dirname + "/dist");
|
fs.mkdirSync(__dirname + "/dist");
|
||||||
fs.mkdirSync(__dirname + "/dist/log");
|
fs.mkdirSync(__dirname + "/dist/log");
|
||||||
|
@ -110,8 +141,8 @@ fs.mkdirSync(__dirname + "/dist/temp");
|
||||||
|
|
||||||
// Remove the out directory if exists, and create a new one.
|
// Remove the out directory if exists, and create a new one.
|
||||||
if (fs.existsSync(__dirname + "/out")) {
|
if (fs.existsSync(__dirname + "/out")) {
|
||||||
if (fs.rmSync) fs.rmSync(__dirname + "/out", {recursive: true});
|
if (fs.rmSync) fs.rmSync(__dirname + "/out", { recursive: true });
|
||||||
else fs.rmdirSync(__dirname + "/out", {recursive: true});
|
else fs.rmdirSync(__dirname + "/out", { recursive: true });
|
||||||
}
|
}
|
||||||
fs.mkdirSync(__dirname + "/out");
|
fs.mkdirSync(__dirname + "/out");
|
||||||
|
|
||||||
|
@ -121,65 +152,89 @@ 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
|
||||||
entryPoints: ["src/index.js"],
|
.build({
|
||||||
bundle: true,
|
entryPoints: ["src/index.js"],
|
||||||
outfile: "dist/svr.js",
|
bundle: true,
|
||||||
platform: "node",
|
outfile: "dist/svr.js",
|
||||||
target: "es2017",
|
|
||||||
plugins: [
|
|
||||||
esbuildCopyPlugin.copy({
|
|
||||||
resolveFrom: __dirname,
|
|
||||||
assets: {
|
|
||||||
from: ["./assets/**/*"],
|
|
||||||
to: ["./dist"],
|
|
||||||
},
|
|
||||||
globbyOptions: {
|
|
||||||
dot: true
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
esbuildCopyPlugin.copy({
|
|
||||||
resolveFrom: __dirname,
|
|
||||||
assets: {
|
|
||||||
from: ["./generatedAssets/**/*"],
|
|
||||||
to: ["./dist"],
|
|
||||||
}
|
|
||||||
})
|
|
||||||
],
|
|
||||||
}).then(() => {
|
|
||||||
const utilFilesAndDirectories = fs.existsSync(__dirname + "/utils") ? fs.readdirSync(__dirname + "/utils") : [];
|
|
||||||
const utilFiles = [];
|
|
||||||
utilFilesAndDirectories.forEach((entry) => {
|
|
||||||
if (fs.statSync(__dirname + "/utils/" + entry).isFile()) utilFiles.push(entry);
|
|
||||||
})
|
|
||||||
|
|
||||||
// Transpile utilities using esbuild
|
|
||||||
esbuild.build({
|
|
||||||
entryPoints: utilFiles.map((filename) => "utils/" + filename),
|
|
||||||
bundle: false,
|
|
||||||
outdir: "dist",
|
|
||||||
platform: "node",
|
platform: "node",
|
||||||
target: "es2017",
|
target: "es2017",
|
||||||
}).then(() => {
|
plugins: [
|
||||||
const archiveName = "svr.js." + version.toLowerCase().replace(/[^0-9a-z]+/g,".") + ".zip";
|
esbuildCopyPlugin.copy({
|
||||||
const output = fs.createWriteStream(__dirname + "/out/" + archiveName);
|
resolveFrom: __dirname,
|
||||||
const archive = archiver("zip", {
|
assets: {
|
||||||
zlib: { level: 9 } // Sets the compression level.
|
from: ["./assets/**/*"],
|
||||||
});
|
to: ["./dist"],
|
||||||
archive.pipe(output);
|
},
|
||||||
archive.directory(__dirname + "/dist/", false);
|
globbyOptions: {
|
||||||
const compressedSVRJSFileStream = fs.createReadStream(__dirname + "/dist/svr.js").pipe(zlib.createGzip({
|
dot: true,
|
||||||
level: 9
|
},
|
||||||
}));
|
}),
|
||||||
archive.append(compressedSVRJSFileStream, { name: "svr.compressed" });
|
esbuildCopyPlugin.copy({
|
||||||
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"});
|
resolveFrom: __dirname,
|
||||||
archive.finalize();
|
assets: {
|
||||||
}).catch((err) => {
|
from: ["./generatedAssets/**/*"],
|
||||||
throw err;
|
to: ["./dist"],
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
],
|
||||||
})
|
})
|
||||||
}).catch((err) => {
|
.then(() => {
|
||||||
throw err;
|
const utilFilesAndDirectories = fs.existsSync(__dirname + "/utils")
|
||||||
});
|
? fs.readdirSync(__dirname + "/utils")
|
||||||
|
: [];
|
||||||
|
const utilFiles = [];
|
||||||
|
utilFilesAndDirectories.forEach((entry) => {
|
||||||
|
if (fs.statSync(__dirname + "/utils/" + entry).isFile())
|
||||||
|
utilFiles.push(entry);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Transpile utilities using esbuild
|
||||||
|
esbuild
|
||||||
|
.build({
|
||||||
|
entryPoints: utilFiles.map((filename) => "utils/" + filename),
|
||||||
|
bundle: false,
|
||||||
|
outdir: "dist",
|
||||||
|
platform: "node",
|
||||||
|
target: "es2017",
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
const archiveName =
|
||||||
|
"svr.js." +
|
||||||
|
version.toLowerCase().replace(/[^0-9a-z]+/g, ".") +
|
||||||
|
".zip";
|
||||||
|
const output = fs.createWriteStream(__dirname + "/out/" + archiveName);
|
||||||
|
const archive = archiver("zip", {
|
||||||
|
zlib: { level: 9 },
|
||||||
|
});
|
||||||
|
archive.pipe(output);
|
||||||
|
archive.directory(__dirname + "/dist/", false);
|
||||||
|
|
||||||
|
// 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(
|
||||||
|
'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();
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
throw err;
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
throw err;
|
||||||
|
});
|
||||||
|
|
Reference in a new issue