1
0
Fork 0
forked from svrjs/svrjs

feat: add options related to disabling configuration file saving, and use them in the "dev" script

This commit is contained in:
Dorian Niemiec 2024-11-09 13:31:31 +01:00
parent 8e1ea9e5bc
commit 5a0ea4007e
4 changed files with 81 additions and 64 deletions

View file

@ -59,5 +59,6 @@
"disableTrailingSlashRedirects": false, "disableTrailingSlashRedirects": false,
"environmentVariables": {}, "environmentVariables": {},
"allowDoubleSlashes": false, "allowDoubleSlashes": false,
"optOutOfStatisticsServer": false "optOutOfStatisticsServer": false,
"disableConfigurationSaving": false
} }

View file

@ -6,7 +6,7 @@
"build": "npm run clean && NODE_ENV=production node esbuild.config.js", "build": "npm run clean && NODE_ENV=production node esbuild.config.js",
"cz": "cz", "cz": "cz",
"clean": "rimraf dist && rimraf out && rimraf generatedAssets", "clean": "rimraf dist && rimraf out && rimraf generatedAssets",
"dev": "npm run clean && concurrently \"NODE_ENV=development node esbuild.config.js\" \"wait-on dist/svr.js && nodemon dist/svr.js --stdout-notty\"", "dev": "npm run clean && concurrently \"NODE_ENV=development node esbuild.config.js\" \"wait-on dist/svr.js && nodemon dist/svr.js --stdout-notty --no-save-config\"",
"lint": "eslint --no-error-on-unmatched-pattern src/**/*.js src/*.js tests/**/*.test.js tests/**/*.js tests/*.test.js tests/*.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", "lint:fix": "npm run lint -- --fix",
"prepare": "husky", "prepare": "husky",

View file

@ -117,7 +117,7 @@ if (process.versions) process.versions.svrjs = version; // Inject SVR.JS into pr
function printUsage() { function printUsage() {
console.log(`${name} usage:`); console.log(`${name} usage:`);
console.log( console.log(
"node svr.js [-h] [--help] [-?] [/h] [/?] [--secure] [--reset] [--clean] [--disable-mods] [--single-threaded] [--stdout-notty] [-v] [--version]" "node svr.js [-h] [--help] [-?] [/h] [/?] [--secure] [--reset] [--clean] [--disable-mods] [--single-threaded] [--stdout-notty] [--no-save-config] [-v] [--version]"
); );
console.log("-h -? /h /? --help -- Displays help"); console.log("-h -? /h /? --help -- Displays help");
console.log("--clean -- Cleans up files created by " + name); console.log("--clean -- Cleans up files created by " + name);
@ -130,6 +130,7 @@ function printUsage() {
console.log( console.log(
"--stdout-notty -- Enable stdout even when stdout is not a TTY. May decrease the performace" "--stdout-notty -- Enable stdout even when stdout is not a TTY. May decrease the performace"
); );
console.log("--no-save-config -- Don't save configuration file");
console.log("-v --version -- Display server version"); console.log("-v --version -- Display server version");
} }
@ -137,6 +138,7 @@ let exiting = false;
let forceSecure = false; let forceSecure = false;
let disableMods = false; let disableMods = false;
let stdoutNoTTY = false; let stdoutNoTTY = false;
let noSaveConfig = false;
// Handle command line arguments // Handle command line arguments
const args = process.argv; const args = process.argv;
@ -190,6 +192,8 @@ for (
process.singleThreaded = true; process.singleThreaded = true;
} else if (args[i] == "--stdout-notty") { } else if (args[i] == "--stdout-notty") {
stdoutNoTTY = true; stdoutNoTTY = true;
} else if (args[i] == "--no-save-config") {
noSaveConfig = true;
} else { } else {
console.log(`Unrecognized argument: ${args[i]}`); console.log(`Unrecognized argument: ${args[i]}`);
printUsage(); printUsage();
@ -335,6 +339,11 @@ if (process.serverConfig.allowPostfixDoubleSlashes === undefined)
process.serverConfig.allowPostfixDoubleSlashes = false; process.serverConfig.allowPostfixDoubleSlashes = false;
if (process.serverConfig.optOutOfStatisticsServer === undefined) if (process.serverConfig.optOutOfStatisticsServer === undefined)
process.serverConfig.optOutOfStatisticsServer = false; process.serverConfig.optOutOfStatisticsServer = false;
if (process.serverConfig.disableConfigurationSaving === undefined)
process.serverConfig.disableConfigurationSaving = false;
// Don't save configuration if disableConfigurationSaving option is set to true
if (process.serverConfig.disableConfigurationSaving) noSaveConfig = true;
// Compatiblity for very old SVR.JS mods // Compatiblity for very old SVR.JS mods
process.serverConfig.version = version; process.serverConfig.version = version;
@ -1692,6 +1701,8 @@ function saveConfig() {
configJSONobj.allowDoubleSlashes = false; configJSONobj.allowDoubleSlashes = false;
if (configJSONobj.optOutOfStatisticsServer === undefined) if (configJSONobj.optOutOfStatisticsServer === undefined)
configJSONobj.optOutOfStatisticsServer = false; configJSONobj.optOutOfStatisticsServer = false;
if (configJSONobj.disableConfigurationSaving === undefined)
configJSONobj.disableConfigurationSaving = false;
fs.writeFileSync( fs.writeFileSync(
process.dirname + "/config.json", process.dirname + "/config.json",
@ -1984,6 +1995,7 @@ function start(init) {
let workersToFork = 1; let workersToFork = 1;
if (cluster.isPrimary === undefined) { if (cluster.isPrimary === undefined) {
if (!noSaveConfig) {
setInterval(() => { setInterval(() => {
try { try {
saveConfig(); saveConfig();
@ -1992,7 +2004,9 @@ function start(init) {
throw new Error(err); throw new Error(err);
} }
}, 300000); }, 300000);
}
} else if (cluster.isPrimary) { } else if (cluster.isPrimary) {
if (!noSaveConfig) {
setInterval(() => { setInterval(() => {
let allWorkers = Object.keys(cluster.workers); let allWorkers = Object.keys(cluster.workers);
let goodWorkers = []; let goodWorkers = [];
@ -2048,6 +2062,7 @@ function start(init) {
}); });
}, 300000); }, 300000);
} }
}
if (!cluster.isPrimary && cluster.isPrimary !== undefined) { if (!cluster.isPrimary && cluster.isPrimary !== undefined) {
process.on("message", (line) => { process.on("message", (line) => {
@ -2435,7 +2450,7 @@ if (cluster.isPrimary || cluster.isPrimary === undefined) {
process.on("exit", (code) => { process.on("exit", (code) => {
try { try {
if (!configJSONRErr && !configJSONPErr) { if (!configJSONRErr && !configJSONPErr && !noSaveConfig) {
saveConfig(); saveConfig();
} }
} catch (err) { } catch (err) {

View file

@ -73,7 +73,8 @@
"disableTrailingSlashRedirects": false, "disableTrailingSlashRedirects": false,
"environmentVariables": {}, "environmentVariables": {},
"allowDoubleSlashes": false, "allowDoubleSlashes": false,
"optOutOfStatisticsServer": false "optOutOfStatisticsServer": false,
"disableConfigurationSaving": false
}</pre> }</pre>
</code> </code>
<p>Changes:</p> <p>Changes:</p>