Add some code to add the .env support
Some checks are pending
CodeQL / Analyze (csharp) (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run

This commit is contained in:
Namhyeon Go 2024-10-14 04:38:18 +09:00
parent 9e623b645f
commit 94afeb406a
4 changed files with 76 additions and 51 deletions

View File

@ -261,7 +261,7 @@ namespace WelsonJS.Service
{ {
_args = new string[] _args = new string[]
{ {
$"--user-variables-file={userVariablesHandler.GetEnvFilePath()}", $"--env-file={userVariablesHandler.GetEnvFilePath()}",
"--user-interactive" "--user-interactive"
}; };
} }
@ -269,7 +269,7 @@ namespace WelsonJS.Service
{ {
_args = new string[] _args = new string[]
{ {
$"--user-variables-file={userVariablesHandler.GetEnvFilePath()}" $"--env-file={userVariablesHandler.GetEnvFilePath()}"
}; };
} }
startArguments = new string[args.Length + _args.Length]; startArguments = new string[args.Length + _args.Length];

60
app.js
View File

@ -1,48 +1,15 @@
////////////////////////////////////////////////////////////////////////////////// // app.js
// // Bootstrap code for running a javascript app in windows. Run as:
// app.js // cscript.js app.js <appname> <app arguments> ...
// //
// Bootstrap code for running a javascript app in windows. Run as: // Author: Namhyeon Go <abuse@catswords.net>
// // Repository: https://github.com/gnh1201/welsonjs
// cscript.js app.js <appname> <app arguments> ... // 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"; "use strict";
/////////////////////////////////////////////////////////////////////////////////
//
// Author: Namhyeon Go <abuse@catswords.net>
// 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 <appname>.js to be loaded. The interface returned
// must define main = function(args) {}, which is called once the module is
// loaded.
//
var exit = function(status) { var exit = function(status) {
console.error("Exit", status, "caused"); console.error("Exit", status, "caused");
@ -239,8 +206,8 @@ function require(pathname) {
var T = null; var T = null;
var pos = FN.indexOf('://'); var pos = FN.indexOf('://');
if (pos > -1) {
if (pos > -1) { // from a remote server // load script from a remote server
if (["http", "https"].indexOf(FN.substring(0, pos)) > -1) { if (["http", "https"].indexOf(FN.substring(0, pos)) > -1) {
require._addScriptProvider(function(url) { require._addScriptProvider(function(url) {
try { try {
@ -263,7 +230,8 @@ function require(pathname) {
i++; i++;
} }
} }
} else { // from a local server } else {
// load script from a local server
var _filename = (function(fs, path) { var _filename = (function(fs, path) {
var filepaths = [ var filepaths = [
FN, // default FN, // default

View File

@ -5,7 +5,7 @@
// //
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
var LIB = require("lib/std"); var STD = require("lib/std");
var PipeIPC = require("lib/pipe-ipc"); var PipeIPC = require("lib/pipe-ipc");
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
@ -197,6 +197,44 @@ function rotateFile(FN, content, numOfLines, charset) {
return result; 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.fileExists = fileExists;
exports.folderExists = folderExists; exports.folderExists = folderExists;
exports.fileGet = fileGet; exports.fileGet = fileGet;
@ -210,10 +248,12 @@ exports.deleteFile = deleteFile;
exports.includeFile = includeFile; exports.includeFile = includeFile;
exports.appendFile = appendFile; exports.appendFile = appendFile;
exports.rotateFile = rotateFile; exports.rotateFile = rotateFile;
exports.loadEnvFromFile = loadEnvFromFile;
exports.loadEnvFromArgs = loadEnvFromArgs;
exports.CdoCharset = PipeIPC.CdoCharset; 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.AUTHOR = "abuse@catswords.net";
exports.global = global; exports.global = global;
exports.require = global.require; exports.require = global.require;

View File

@ -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 // Standard Event Object
function StdEvent(type, options) { function StdEvent(type, options) {
this.defaultPrevented = false; this.defaultPrevented = false;
@ -534,7 +551,7 @@ exports.Storage = StdStorage;
exports.alert = alert; exports.alert = alert;
exports.confirm = confirm; 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.AUTHOR = "abuse@catswords.net";
exports.global = global; exports.global = global;
exports.require = global.require; exports.require = global.require;