1
0
Fork 0
forked from svrjs/svrjs

fix: fix bug with the extraction script

This commit is contained in:
Dorian Niemiec 2024-09-20 07:47:15 +02:00
parent 27c613f28e
commit 9e227bbec6
9 changed files with 274 additions and 314 deletions

View file

@ -123,6 +123,7 @@ The file structure for SVR.JS source code looks like this:
- out - contains SVR.JS zip archive
- src - contains SVR.JS source code
- index.js - entry point
- extraScripts - SVR.JS extra scripts (each script has a single file)
- handlers - handlers for servers
- middleware - built-in middleware for servers
- res - resources
@ -131,7 +132,6 @@ The file structure for SVR.JS source code looks like this:
- tests - Jest unit tests
- middleware - tests for middleware
- utils - unit tests for utility functions
- utils - SVR.JS utilities (each utility has a single file)
- commitlint.config.js - commitlint configuration
- esbuild.config.js - the build script
- eslint.config.js - ESLint configuration

View file

@ -23,10 +23,10 @@ function findAllDependencies(curList) {
__dirname +
"/node_modules/" +
dependency.replace(/\/\.\./g, "") +
"/package.json",
"/package.json"
)
.toString(),
).dependencies || {},
.toString()
).dependencies || {}
);
let noDupNewDepList = [];
newDeplist.forEach((dep) => {
@ -56,19 +56,19 @@ requiredDependencyList.forEach((dependency) => {
// Create the template functions using EJS
const layoutTemplate = ejs.compile(
fs.readFileSync(__dirname + "/templates/layout.ejs").toString(),
fs.readFileSync(__dirname + "/templates/layout.ejs").toString()
);
const testsTemplate = ejs.compile(
fs.readFileSync(__dirname + "/templates/tests.ejs").toString(),
fs.readFileSync(__dirname + "/templates/tests.ejs").toString()
);
const indexTemplate = ejs.compile(
fs.readFileSync(__dirname + "/templates/index.ejs").toString(),
fs.readFileSync(__dirname + "/templates/index.ejs").toString()
);
const licensesTemplate = ejs.compile(
fs.readFileSync(__dirname + "/templates/licenses.ejs").toString(),
fs.readFileSync(__dirname + "/templates/licenses.ejs").toString()
);
const licenseElementTemplate = ejs.compile(
fs.readFileSync(__dirname + "/templates/licenseElement.ejs").toString(),
fs.readFileSync(__dirname + "/templates/licenseElement.ejs").toString()
);
let licenseElements = "";
@ -81,9 +81,9 @@ dependencyList.forEach((dependency) => {
__dirname +
"/node_modules/" +
dependency.replace(/\/\.\./g, "") +
"/package.json",
"/package.json"
)
.toString(),
.toString()
);
licenseElements += licenseElementTemplate({
moduleName: packageJSON.name,
@ -91,7 +91,7 @@ dependencyList.forEach((dependency) => {
license: packageJSON.license,
description: packageJSON.description || "No description",
author: packageJSON.author ? packageJSON.author.name : packageJSON.author,
required: dependenciesAreRequired[dependency],
required: dependenciesAreRequired[dependency]
});
});
@ -101,16 +101,16 @@ const licensesPage = layoutTemplate({
content: licensesTemplate({
name: name,
version: version,
licenses: licenseElements,
}),
licenses: licenseElements
})
});
const testsPage = layoutTemplate({
title: name + " " + version + " Tests",
content: testsTemplate({
name: name,
version: version,
}),
version: version
})
});
const indexPage = layoutTemplate({
@ -119,8 +119,8 @@ const indexPage = layoutTemplate({
name: name,
version: version,
documentationURL: documentationURL,
changes: changes,
}),
changes: changes
})
});
// Remove the generated assets directory if exists, and create a new one.
@ -155,7 +155,7 @@ fs.writeFileSync(__dirname + "/generatedAssets/index.html", indexPage);
fs.writeFileSync(__dirname + "/generatedAssets/tests.html", testsPage);
fs.writeFileSync(
__dirname + "/generatedAssets/licenses/index.html",
licensesPage,
licensesPage
);
// Bundle the source and copy the assets using esbuild and esbuild-plugin-copy
@ -171,39 +171,43 @@ esbuild
resolveFrom: __dirname,
assets: {
from: ["./assets/**/*"],
to: ["./dist"],
to: ["./dist"]
},
globbyOptions: {
dot: true,
},
dot: true
}
}),
esbuildCopyPlugin.copy({
resolveFrom: __dirname,
assets: {
from: ["./generatedAssets/**/*"],
to: ["./dist"],
},
}),
],
to: ["./dist"]
}
})
]
})
.then(() => {
const utilFilesAndDirectories = fs.existsSync(__dirname + "/utils")
? fs.readdirSync(__dirname + "/utils")
const utilFilesAndDirectories = fs.existsSync(
__dirname + "/src/extraScripts"
)
? fs.readdirSync(__dirname + "/src/extraScripts")
: [];
const utilFiles = [];
utilFilesAndDirectories.forEach((entry) => {
if (fs.statSync(__dirname + "/utils/" + entry).isFile())
if (fs.statSync(__dirname + "/src/extraScripts/" + entry).isFile())
utilFiles.push(entry);
});
// Transpile utilities using esbuild
esbuild
.build({
entryPoints: utilFiles.map((filename) => "utils/" + filename),
bundle: false,
entryPoints: utilFiles.map(
(filename) => "src/extraScripts/" + filename
),
bundle: true,
outdir: "dist",
platform: "node",
target: "es2017",
target: "es2017"
})
.then(() => {
const archiveName =
@ -212,17 +216,14 @@ esbuild
".zip";
const output = fs.createWriteStream(__dirname + "/out/" + archiveName);
const archive = archiver("zip", {
zlib: { level: 9 },
zlib: { level: 9 }
});
archive.pipe(output);
// Add everything in the "dist" directory except for "svr.js" and "svr.compressed"
archive.glob("**/*", {
cwd: __dirname + "/dist",
ignore: [
"svr.js",
"svr.compressed"
],
ignore: ["svr.js", "svr.compressed"],
dot: true
});
@ -231,13 +232,13 @@ esbuild
.createReadStream(__dirname + "/dist/svr.js")
.pipe(
zlib.createGzip({
level: 9,
}),
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" },
'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.");',
{ name: "svr.js" }
);
archive.finalize();
})

View file

@ -6,7 +6,7 @@
"build": "node esbuild.config.js",
"cz": "cz",
"dev": "npm run build && npm run start",
"lint": "eslint --no-error-on-unmatched-pattern src/**/*.js src/*.js tests/**/*.test.js tests/**/*.js tests/*.test.js tests/*.js utils/**/*.js utils/*.js",
"lint": "eslint --no-error-on-unmatched-pattern src/**/*.js src/*.js tests/**/*.test.js tests/**/*.js tests/*.test.js tests/*.js",
"lint:fix": "npm run lint -- --fix",
"prepare": "husky",
"start": "node dist/svr.js",

View file

@ -0,0 +1,230 @@
//SVR.JS LOG VIEWER
const fs = require("fs");
const readline = require("readline");
const args = process.argv;
for (
let i =
process.argv[0].indexOf("node") > -1 || process.argv[0].indexOf("bun") > -1
? 2
: 1;
i < args.length;
i++
) {
if (
args[i] == "-h" ||
args[i] == "--help" ||
args[i] == "-?" ||
args[i] == "/h" ||
args[i] == "/?"
) {
console.log("SVR.JS log viewer usage:");
console.log("node logviewer.js [-h] [--help] [-?] [/h] [/?]");
console.log("-h -? /h /? --help -- Displays help");
process.exit(0);
} else {
console.log("Unrecognized argument: " + args[i]);
console.log("SVR.JS log viewer usage:");
console.log("node logviewer.js [-h] [--help] [-?] [/h] [/?]");
console.log("-h -? /h /? --help -- Displays help");
process.exit(1);
}
}
const logo = require("../res/logo.js");
for (let i = 0; i < logo.length; i++) {
console.log(logo[i]);
}
console.log("Welcome to SVR.JS log viewer");
if (!fs.existsSync(__dirname + "/log")) {
console.log("No log directory, exiting...");
process.exit(1);
}
function prompt(options) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: "logviewer> "
});
console.log("Options:");
for (let i = 0; i < options.length; i++) {
console.log("[" + i + "] - " + options[i].name);
}
rl.prompt();
rl.on("line", (line) => {
const op = line.trim();
if (op == "") {
rl.prompt();
return;
}
rl.close();
if (options[op]) {
options[op].callback();
} else {
console.log("Invalid option.");
prompt(options);
}
});
}
function viewLog(log) {
if (log[log.length - 1] == "") log.pop();
if (log[0] == "") log.shift();
for (let i = 0; i < log.length; i++) {
if (log[i].indexOf("SERVER REQUEST MESSAGE") != -1) {
log[i] =
log[i].replace(
"SERVER REQUEST MESSAGE",
"\x1b[34m\x1b[1mSERVER REQUEST MESSAGE\x1b[22m"
) + "\x1b[37m\x1b[0m";
} else if (log[i].indexOf("SERVER RESPONSE MESSAGE") != -1) {
log[i] =
log[i].replace(
"SERVER RESPONSE MESSAGE",
"\x1b[32m\x1b[1mSERVER RESPONSE MESSAGE\x1b[22m"
) + "\x1b[37m\x1b[0m";
} else if (log[i].indexOf("SERVER RESPONSE ERROR MESSAGE") != -1) {
log[i] =
log[i].replace(
"SERVER RESPONSE ERROR MESSAGE",
"\x1b[31m\x1b[1mSERVER RESPONSE ERROR MESSAGE\x1b[22m"
) + "\x1b[37m\x1b[0m";
} else if (log[i].indexOf("SERVER ERROR MESSAGE") != -1) {
log[i] =
log[i].replace(
"SERVER ERROR MESSAGE",
"\x1b[41m\x1b[1mSERVER ERROR MESSAGE\x1b[22m"
) + "\x1b[40m\x1b[0m";
} else if (log[i].indexOf("SERVER WARNING MESSAGE") != -1) {
log[i] =
log[i].replace(
"SERVER WARNING MESSAGE",
"\x1b[43m\x1b[1mSERVER WARNING MESSAGE\x1b[22m"
) + "\x1b[40m\x1b[0m";
} else if (log[i].indexOf("SERVER MESSAGE") != -1) {
log[i] = log[i].replace(
"SERVER MESSAGE",
"\x1b[1mSERVER MESSAGE\x1b[22m"
);
}
console.log(log[i]);
}
}
function viewMasterLogs() {
const logList = fs.readdirSync(__dirname + "/log");
let masterLogs = [];
for (var i = 0; i < logList.length; i++) {
if (logList[i].match(/^master-[0-9]+\.log$/)) {
masterLogs.push(logList[i]);
}
}
if (masterLogs.length == 0) {
console.log("No master log.");
return;
}
const latestLogFileName = masterLogs.sort().reverse()[0];
viewLog(
fs
.readFileSync(__dirname + "/log/" + latestLogFileName)
.toString()
.split("\n")
);
prompt(mainOptions);
}
function viewWorkerLogs() {
const logList = fs.readdirSync("log");
let masterLogs = [];
for (var i = 0; i < logList.length; i++) {
if (logList[i].match(/^worker-[0-9]+\.log$/)) {
masterLogs.push(logList[i]);
}
}
if (masterLogs.length == 0) {
console.log("No worker logs.");
return;
}
const latestLogFileNames = masterLogs.sort().reverse().slice(0, 5).reverse();
let log = [];
for (let i = 0; i < latestLogFileNames.length; i++) {
let rlog = fs
.readFileSync("log/" + latestLogFileNames[i])
.toString()
.split("\n");
if (rlog[rlog.length - 1] == "") rlog.pop();
if (rlog[0] == "") rlog.shift();
for (let j = 0; j < rlog.length; j++) {
log.push(rlog[j]);
}
}
log = log.sort();
viewLog(log);
prompt(mainOptions);
}
function viewFilteredWorkerLogs(filter) {
const logList = fs.readdirSync("log");
let masterLogs = [];
for (var i = 0; i < logList.length; i++) {
if (logList[i].match(/^worker-[0-9]+\.log$/)) {
masterLogs.push(logList[i]);
}
}
if (masterLogs.length == 0) {
console.log("No worker logs.");
return;
}
const latestLogFileNames = masterLogs.sort().reverse().slice(0, 20).reverse();
let log = [];
for (let i = 0; i < latestLogFileNames.length; i++) {
let rlog = fs
.readFileSync("log/" + latestLogFileNames[i])
.toString()
.split("\n");
if (rlog[rlog.length - 1] == "") rlog.pop();
if (rlog[0] == "") rlog.shift();
for (let j = 0; j < rlog.length; j++) {
if (rlog[j].indexOf(filter) != -1) log.push(rlog[j]);
}
}
log = log.sort();
viewLog(log);
prompt(mainOptions);
}
function viewFilteredWorkerLogsPrompt() {
var rl2 = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: "filter> "
});
console.log("Input filter:");
rl2.prompt();
rl2.on("line", (line) => {
rl2.close();
viewFilteredWorkerLogs(line);
});
}
var mainOptions = [
{ name: "View latest master log", callback: viewMasterLogs },
{ name: "View 5 latest worker logs", callback: viewWorkerLogs },
{
name: "View filtered worker logs (latest 20 logs)",
callback: viewFilteredWorkerLogsPrompt
},
{
name: "Exit log viewer",
callback: () => {
console.log("Bye!");
process.exit(0);
}
}
];
prompt(mainOptions);

View file

@ -0,0 +1 @@
require(__dirname + "/svr.js");

View file

@ -1,271 +0,0 @@
//SVR.JS LOG VIEWER
const fs = require("fs");
const readline = require("readline");
const args = process.argv;
for (
let i =
process.argv[0].indexOf("node") > -1 || process.argv[0].indexOf("bun") > -1
? 2
: 1;
i < args.length;
i++
) {
if (
args[i] == "-h" ||
args[i] == "--help" ||
args[i] == "-?" ||
args[i] == "/h" ||
args[i] == "/?"
) {
console.log("SVR.JS log viewer usage:");
console.log("node logviewer.js [-h] [--help] [-?] [/h] [/?]");
console.log("-h -? /h /? --help -- Displays help");
process.exit(0);
} else {
console.log("Unrecognized argument: " + args[i]);
console.log("SVR.JS log viewer usage:");
console.log("node logviewer.js [-h] [--help] [-?] [/h] [/?]");
console.log("-h -? /h /? --help -- Displays help");
process.exit(1);
}
}
const logo = [
"",
"",
"\x1b[0m \x1b[38;5;255m@@@@@@@@@\x1b[0m",
"\x1b[0m ,\x1b[38;5;255m@@@\x1b[38;5;254m&\x1b[38;5;041m/////////\x1b[38;5;254m@\x1b[38;5;255m@@@\x1b[0m,",
"\x1b[0m &\x1b[38;5;255m@@@\x1b[38;5;078m#\x1b[38;5;041m/////////////////\x1b[38;5;115m#\x1b[38;5;255m@@@\x1b[0m%",
"\x1b[0m \x1b[38;5;255m@@@@\x1b[38;5;041m///////////////////////////\x1b[38;5;255m@@@@\x1b[0m",
"\x1b[0m *\x1b[38;5;255m@@@\x1b[38;5;254m&\x1b[38;5;041m///////////////////////////////////\x1b[38;5;255m@@@@\x1b[0m*",
"\x1b[0m &\x1b[38;5;255m@@@\x1b[38;5;078m#\x1b[38;5;041m///////////////////////////////////////////\x1b[38;5;115m#\x1b[38;5;255m@@@\x1b[0m%",
"\x1b[0m \x1b[38;5;255m@@@@\x1b[38;5;041m/////////////////////////////////////////////////////\x1b[38;5;255m@@@@\x1b[0m",
"\x1b[0m /\x1b[38;5;255m@@@\x1b[38;5;254m@\x1b[38;5;041m/////////////////////////////////////////////////////////////\x1b[38;5;255m@@@@\x1b[0m,",
"\x1b[0m &\x1b[38;5;255m@@@\x1b[38;5;114m#\x1b[38;5;041m/////////////////////////////////////////////////////////////////////\x1b[38;5;115m#\x1b[38;5;255m@@@\x1b[0m%",
"\x1b[0m#\x1b[38;5;255m@\x1b[38;5;188m&\x1b[38;5;235m.\x1b[38;5;255m@@@@\x1b[38;5;041m/////////////////////////////////////////////////////////////////\x1b[38;5;255m@@@@\x1b[38;5;235m.\x1b[38;5;188m&\x1b[38;5;255m@",
"\x1b[0m#\x1b[38;5;255m@\x1b[38;5;188m&\x1b[38;5;235m.....\x1b[38;5;151m%\x1b[38;5;255m@@@\x1b[38;5;115m#\x1b[38;5;041m///////////////////////////////////////////////////////\x1b[38;5;151m%\x1b[38;5;255m@@@\x1b[38;5;109m#\x1b[38;5;235m.....\x1b[38;5;188m&\x1b[38;5;255m@",
"\x1b[0m#\x1b[38;5;255m@\x1b[38;5;188m&\x1b[38;5;235m.........\x1b[38;5;238m,\x1b[38;5;255m@@@@\x1b[38;5;041m///////////////////////////////////////////////\x1b[38;5;255m@@@@\x1b[38;5;236m,\x1b[38;5;235m.........\x1b[38;5;188m&\x1b[38;5;255m@",
"\x1b[0m#\x1b[38;5;255m@\x1b[38;5;188m&\x1b[38;5;235m..............\x1b[38;5;255m@@@@\x1b[38;5;041m//////////////////////////////////////\x1b[38;5;077m/\x1b[38;5;255m@@@@\x1b[38;5;235m..............\x1b[38;5;188m&\x1b[38;5;255m@",
"\x1b[0m#\x1b[38;5;255m@\x1b[38;5;188m&\x1b[38;5;235m..................\x1b[38;5;151m%\x1b[38;5;255m@@@\x1b[38;5;115m#\x1b[38;5;041m/////////////////////////////\x1b[38;5;151m%\x1b[38;5;255m@@@\x1b[38;5;248m#\x1b[38;5;235m..................\x1b[38;5;188m&\x1b[38;5;255m@",
"\x1b[0m#\x1b[38;5;255m@\x1b[38;5;188m&\x1b[38;5;235m......................\x1b[38;5;238m,\x1b[38;5;255m@@@\x1b[38;5;254m@\x1b[38;5;041m/////////////////////\x1b[38;5;255m@@@@\x1b[38;5;237m,\x1b[38;5;235m.............\x1b[38;5;041m///\x1b[38;5;236m.\x1b[38;5;235m.....\x1b[38;5;188m&\x1b[38;5;255m@",
"\x1b[0m#\x1b[38;5;255m@\x1b[38;5;254m&\x1b[38;5;235m...........................\x1b[38;5;255m@@@@\x1b[38;5;041m////////////\x1b[38;5;077m/\x1b[38;5;255m@@@@\x1b[38;5;235m................\x1b[38;5;041m/////\x1b[38;5;035m*\x1b[38;5;235m.....\x1b[38;5;188m&\x1b[38;5;255m@",
"\x1b[0m#\x1b[38;5;255m@\x1b[38;5;188m&\x1b[38;5;235m...............................\x1b[38;5;007m%\x1b[38;5;255m@@@\x1b[38;5;114m#\x1b[38;5;041m///\x1b[38;5;115m#\x1b[38;5;255m@@@\x1b[38;5;249m#\x1b[38;5;235m...................\x1b[38;5;041m/////\x1b[38;5;035m/\x1b[38;5;235m......\x1b[38;5;188m&\x1b[38;5;255m@",
"\x1b[0m#\x1b[38;5;255m@\x1b[38;5;188m&\x1b[38;5;235m...................................\x1b[38;5;237m,\x1b[38;5;255m@@@\x1b[38;5;237m,\x1b[38;5;235m.......................\x1b[38;5;041m///\x1b[38;5;035m/\x1b[38;5;235m........\x1b[38;5;188m&\x1b[38;5;255m@",
"\x1b[0m,\x1b[38;5;255m@@@\x1b[38;5;251m%\x1b[38;5;235m..................................\x1b[38;5;251m%\x1b[38;5;255m@\x1b[38;5;242m/\x1b[38;5;235m.........\x1b[38;5;035m*\x1b[38;5;041m///\x1b[38;5;035m/\x1b[38;5;235m....................\x1b[38;5;253m&\x1b[38;5;255m@@@",
"\x1b[0m \x1b[38;5;255m@@@@\x1b[38;5;235m..............................\x1b[38;5;251m%\x1b[38;5;255m@\x1b[38;5;242m/\x1b[38;5;235m........\x1b[38;5;041m/////\x1b[38;5;029m*\x1b[38;5;235m................\x1b[38;5;255m@@@@\x1b[0m",
"\x1b[0m \x1b[38;5;254m@\x1b[38;5;255m@@@\x1b[38;5;238m,\x1b[38;5;235m.........................\x1b[38;5;251m%\x1b[38;5;255m@\x1b[38;5;242m/\x1b[38;5;235m.......\x1b[38;5;041m/////\x1b[38;5;029m*\x1b[38;5;235m............\x1b[38;5;240m*\x1b[38;5;255m@@@\x1b[0m&",
"\x1b[0m *\x1b[38;5;255m@@@\x1b[38;5;251m%\x1b[38;5;235m.....................\x1b[38;5;251m%\x1b[38;5;255m@\x1b[38;5;242m/\x1b[38;5;235m.......\x1b[38;5;029m*\x1b[38;5;041m//\x1b[38;5;236m.\x1b[38;5;235m..........\x1b[38;5;252m&\x1b[38;5;255m@@@\x1b[0m/",
"\x1b[0m //\x1b[38;5;255m@@@@\x1b[38;5;235m.................\x1b[38;5;251m%\x1b[38;5;255m@\x1b[38;5;242m/\x1b[38;5;235m.................\x1b[38;5;255m@@@@\x1b[0m//",
"\x1b[0m //////////&\x1b[38;5;255m@@@\x1b[38;5;238m,\x1b[38;5;235m............\x1b[38;5;251m%\x1b[38;5;255m@\x1b[38;5;242m/\x1b[38;5;235m............\x1b[38;5;239m,\x1b[38;5;255m@@@\x1b[0m&//////////",
"\x1b[0m .//////////////////(\x1b[38;5;255m@@@\x1b[38;5;251m%\x1b[38;5;235m........\x1b[38;5;251m%\x1b[38;5;255m@\x1b[38;5;242m/\x1b[38;5;235m........\x1b[38;5;253m&\x1b[38;5;255m@@@\x1b[0m(//////////////////,",
"\x1b[0m ///////////////////////////\x1b[38;5;255m@@@@\x1b[38;5;235m....\x1b[38;5;251m%\x1b[38;5;255m@\x1b[38;5;242m/\x1b[38;5;235m....\x1b[38;5;255m@@@@\x1b[0m///////////////////////////",
"\x1b[0m ///////////////////////////////&\x1b[38;5;255m@@@\x1b[38;5;252m&\x1b[38;5;255m@\x1b[38;5;246m(\x1b[38;5;255m@@@\x1b[0m&///////////////////////////////",
"\x1b[0m /////////////////////////////////////////////////////////////////////",
"\x1b[0m .///////////////////////////////////////////////////////////.",
"\x1b[0m ///////////////////////////////////////////////////",
"\x1b[0m ///////////////////////////////////////////",
"\x1b[0m ./////////////////////////////////,",
"\x1b[0m /////////////////////////",
"\x1b[0m /////////////////",
"\x1b[0m ,///////,",
"",
"",
"\x1b[0m"
];
for (let i = 0; i < logo.length; i++) {
console.log(logo[i]);
}
console.log("Welcome to SVR.JS log viewer");
if (!fs.existsSync(__dirname + "/log")) {
console.log("No log directory, exiting...");
process.exit(1);
}
function prompt(options) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: "logviewer> "
});
console.log("Options:");
for (let i = 0; i < options.length; i++) {
console.log("[" + i + "] - " + options[i].name);
}
rl.prompt();
rl.on("line", (line) => {
const op = line.trim();
if (op == "") {
rl.prompt();
return;
}
rl.close();
if (options[op]) {
options[op].callback();
} else {
console.log("Invalid option.");
prompt(options);
}
});
}
function viewLog(log) {
if (log[log.length - 1] == "") log.pop();
if (log[0] == "") log.shift();
for (let i = 0; i < log.length; i++) {
if (log[i].indexOf("SERVER REQUEST MESSAGE") != -1) {
log[i] =
log[i].replace(
"SERVER REQUEST MESSAGE",
"\x1b[34m\x1b[1mSERVER REQUEST MESSAGE\x1b[22m"
) + "\x1b[37m\x1b[0m";
} else if (log[i].indexOf("SERVER RESPONSE MESSAGE") != -1) {
log[i] =
log[i].replace(
"SERVER RESPONSE MESSAGE",
"\x1b[32m\x1b[1mSERVER RESPONSE MESSAGE\x1b[22m"
) + "\x1b[37m\x1b[0m";
} else if (log[i].indexOf("SERVER RESPONSE ERROR MESSAGE") != -1) {
log[i] =
log[i].replace(
"SERVER RESPONSE ERROR MESSAGE",
"\x1b[31m\x1b[1mSERVER RESPONSE ERROR MESSAGE\x1b[22m"
) + "\x1b[37m\x1b[0m";
} else if (log[i].indexOf("SERVER ERROR MESSAGE") != -1) {
log[i] =
log[i].replace(
"SERVER ERROR MESSAGE",
"\x1b[41m\x1b[1mSERVER ERROR MESSAGE\x1b[22m"
) + "\x1b[40m\x1b[0m";
} else if (log[i].indexOf("SERVER WARNING MESSAGE") != -1) {
log[i] =
log[i].replace(
"SERVER WARNING MESSAGE",
"\x1b[43m\x1b[1mSERVER WARNING MESSAGE\x1b[22m"
) + "\x1b[40m\x1b[0m";
} else if (log[i].indexOf("SERVER MESSAGE") != -1) {
log[i] = log[i].replace(
"SERVER MESSAGE",
"\x1b[1mSERVER MESSAGE\x1b[22m"
);
}
console.log(log[i]);
}
}
function viewMasterLogs() {
const logList = fs.readdirSync(__dirname + "/log");
let masterLogs = [];
for (var i = 0; i < logList.length; i++) {
if (logList[i].match(/^master-[0-9]+\.log$/)) {
masterLogs.push(logList[i]);
}
}
if (masterLogs.length == 0) {
console.log("No master log.");
return;
}
const latestLogFileName = masterLogs.sort().reverse()[0];
viewLog(
fs
.readFileSync(__dirname + "/log/" + latestLogFileName)
.toString()
.split("\n")
);
prompt(mainOptions);
}
function viewWorkerLogs() {
const logList = fs.readdirSync("log");
let masterLogs = [];
for (var i = 0; i < logList.length; i++) {
if (logList[i].match(/^worker-[0-9]+\.log$/)) {
masterLogs.push(logList[i]);
}
}
if (masterLogs.length == 0) {
console.log("No worker logs.");
return;
}
const latestLogFileNames = masterLogs.sort().reverse().slice(0, 5).reverse();
let log = [];
for (let i = 0; i < latestLogFileNames.length; i++) {
let rlog = fs
.readFileSync("log/" + latestLogFileNames[i])
.toString()
.split("\n");
if (rlog[rlog.length - 1] == "") rlog.pop();
if (rlog[0] == "") rlog.shift();
for (let j = 0; j < rlog.length; j++) {
log.push(rlog[j]);
}
}
log = log.sort();
viewLog(log);
prompt(mainOptions);
}
function viewFilteredWorkerLogs(filter) {
const logList = fs.readdirSync("log");
let masterLogs = [];
for (var i = 0; i < logList.length; i++) {
if (logList[i].match(/^worker-[0-9]+\.log$/)) {
masterLogs.push(logList[i]);
}
}
if (masterLogs.length == 0) {
console.log("No worker logs.");
return;
}
const latestLogFileNames = masterLogs.sort().reverse().slice(0, 20).reverse();
let log = [];
for (let i = 0; i < latestLogFileNames.length; i++) {
let rlog = fs
.readFileSync("log/" + latestLogFileNames[i])
.toString()
.split("\n");
if (rlog[rlog.length - 1] == "") rlog.pop();
if (rlog[0] == "") rlog.shift();
for (let j = 0; j < rlog.length; j++) {
if (rlog[j].indexOf(filter) != -1) log.push(rlog[j]);
}
}
log = log.sort();
viewLog(log);
prompt(mainOptions);
}
function viewFilteredWorkerLogsPrompt() {
var rl2 = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: "filter> "
});
console.log("Input filter:");
rl2.prompt();
rl2.on("line", (line) => {
rl2.close();
viewFilteredWorkerLogs(line);
});
}
var mainOptions = [
{ name: "View latest master log", callback: viewMasterLogs },
{ name: "View 5 latest worker logs", callback: viewWorkerLogs },
{
name: "View filtered worker logs (latest 20 logs)",
callback: viewFilteredWorkerLogsPrompt
},
{
name: "Exit log viewer",
callback: () => {
console.log("Bye!");
process.exit(0);
}
}
];
prompt(mainOptions);

View file

@ -1 +0,0 @@
require("./svr.js");