diff --git a/WelsonJS.Toolkit/WelsonJS.Service/ServiceMain.cs b/WelsonJS.Toolkit/WelsonJS.Service/ServiceMain.cs index a3298b1..ba0250e 100644 --- a/WelsonJS.Toolkit/WelsonJS.Service/ServiceMain.cs +++ b/WelsonJS.Toolkit/WelsonJS.Service/ServiceMain.cs @@ -261,7 +261,7 @@ namespace WelsonJS.Service { _args = new string[] { - $"--user-variables-file={userVariablesHandler.GetEnvFilePath()}", + $"--env-file={userVariablesHandler.GetEnvFilePath()}", "--user-interactive" }; } @@ -269,7 +269,7 @@ namespace WelsonJS.Service { _args = new string[] { - $"--user-variables-file={userVariablesHandler.GetEnvFilePath()}" + $"--env-file={userVariablesHandler.GetEnvFilePath()}" }; } startArguments = new string[args.Length + _args.Length]; diff --git a/app.js b/app.js index baca85b..2e2165b 100644 --- a/app.js +++ b/app.js @@ -1,48 +1,15 @@ -////////////////////////////////////////////////////////////////////////////////// -// -// app.js -// -// Bootstrap code for running a javascript app in windows. Run as: -// -// cscript.js app.js ... -// -///////////////////////////////////////////////////////////////////////////////// +// app.js +// Bootstrap code for running a javascript app in windows. Run as: +// cscript.js app.js ... +// +// Author: Namhyeon Go +// Repository: https://github.com/gnh1201/welsonjs +// Report abuse: abuse@catswords.net +// Latest news: [ActivityPub @catswords_oss@catswords.social](https://catswords.social/@catswords_oss) +// Join our team: https://teams.live.com/l/community/FEACHncAhq8ldnojAI +// "use strict"; -///////////////////////////////////////////////////////////////////////////////// -// -// Author: Namhyeon Go -// Repository: https://github.com/gnh1201/welsonjs -// Report abuse: abuse@catswords.net -// Latest news: ActivityPub @catswords_oss@catswords.social -// Join our team: https://teams.live.com/l/community/FEACHncAhq8ldnojAI -// -///////////////////////////////////////////////////////////////////////////////// - -///////////////////////////////////////////////////////////////////////////////// -// Bootstrap code, basic module loading functionality -///////////////////////////////////////////////////////////////////////////////// - -// -// The module loaded is run inside a function, with one argument, global which -// points to the global context. So global.FN is the same as FN (as long as a -// version of FN does not exist in local scope). -// -// The module should return its interface at the end of the script. The basic -// pattern for a module is:- -// -// var module = { ... }; -// return module; -// -// Or:- -// -// return function() { -// } -// -// The appname argument causes .js to be loaded. The interface returned -// must define main = function(args) {}, which is called once the module is -// loaded. -// var exit = function(status) { console.error("Exit", status, "caused"); @@ -239,8 +206,8 @@ function require(pathname) { var T = null; var pos = FN.indexOf('://'); - - if (pos > -1) { // from a remote server + if (pos > -1) { + // load script from a remote server if (["http", "https"].indexOf(FN.substring(0, pos)) > -1) { require._addScriptProvider(function(url) { try { @@ -263,7 +230,8 @@ function require(pathname) { i++; } } - } else { // from a local server + } else { + // load script from a local server var _filename = (function(fs, path) { var filepaths = [ FN, // default diff --git a/lib/file.js b/lib/file.js index 62d5a0f..1b9cfd8 100644 --- a/lib/file.js +++ b/lib/file.js @@ -5,7 +5,7 @@ // ///////////////////////////////////////////////////////////////////////////////// -var LIB = require("lib/std"); +var STD = require("lib/std"); var PipeIPC = require("lib/pipe-ipc"); ///////////////////////////////////////////////////////////////////////////////// @@ -197,6 +197,44 @@ function rotateFile(FN, content, numOfLines, charset) { return result; } +// Function to load and parse the .env file +var loadEnvFromFile = function(envFilePath, callback) { + try { + // Read the file content using PipeIPC.CdoCharset.CdoUTF_8 encoding + var envString = readFile(envFilePath, PipeIPC.CdoCharset.CdoUTF_8); + + // Parse the environment variables + var envConfig = parseEnv(envString); + + console.log('Environment variables loaded from ' + envFilePath); + + // Call the callback function if provided + if (typeof callback === "function") { + try { + callback(envConfig); + } catch (e) { + console.error('Callback error:', e.message); + } + } + } catch (e) { + console.error('Error reading environment file:', envFilePath, e.message); + } +}; + +// Function to find --env-file argument in the args array and load the environment file +var loadEnvFromArgs = function(args, callback) { + var envFileArg = args.find(function(x) { + return x.startsWith('--env-file='); + }); + + if (envFileArg) { + var envFilePath = envFileArg.split('=')[1]; + loadEnvFromFile(envFilePath, callback); + } else { + console.warn('No --env-file argument provided.'); + } +}; + exports.fileExists = fileExists; exports.folderExists = folderExists; exports.fileGet = fileGet; @@ -210,10 +248,12 @@ exports.deleteFile = deleteFile; exports.includeFile = includeFile; exports.appendFile = appendFile; exports.rotateFile = rotateFile; +exports.loadEnvFromFile = loadEnvFromFile; +exports.loadEnvFromArgs = loadEnvFromArgs; exports.CdoCharset = PipeIPC.CdoCharset; -exports.VERSIONINFO = "File IO Library (file.js) version 0.2.12"; +exports.VERSIONINFO = "File IO Library (file.js) version 0.2.13"; exports.AUTHOR = "abuse@catswords.net"; exports.global = global; exports.require = global.require; diff --git a/lib/std.js b/lib/std.js index 44ed863..a47ea4c 100644 --- a/lib/std.js +++ b/lib/std.js @@ -267,6 +267,23 @@ function confirm(message) { } } +function parseEnv(s) { + var envConfig = {}; + var lines = s.split('\n'); + + for (var i = 0; i < lines.length; i++) { + var line = lines[i].replace(/^\s+|\s+$/g, ''); + if (line && line.indexOf('=') !== -1 && line.charAt(0) !== '#') { + var parts = line.split('='); + var key = parts[0].replace(/^\s+|\s+$/g, ''); + var value = parts[1].replace(/^\s+|\s+$/g, '').replace(/^"(.*)"$/, '$1'); + envConfig[key] = value; + } + } + + return envConfig; +}; + // Standard Event Object function StdEvent(type, options) { this.defaultPrevented = false; @@ -534,7 +551,7 @@ exports.Storage = StdStorage; exports.alert = alert; exports.confirm = confirm; -exports.VERSIONINFO = "WelsonJS Standard Library (std.js) version 0.8.10"; +exports.VERSIONINFO = "WelsonJS Standard Library (std.js) version 0.8.11"; exports.AUTHOR = "abuse@catswords.net"; exports.global = global; exports.require = global.require;