mirror of
https://github.com/gnh1201/welsonjs.git
synced 2024-11-26 07:21:43 +00:00
Add some code to add the .env support
This commit is contained in:
parent
9e623b645f
commit
94afeb406a
|
@ -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];
|
||||
|
|
60
app.js
60
app.js
|
@ -1,48 +1,15 @@
|
|||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// app.js
|
||||
//
|
||||
// Bootstrap code for running a javascript app in windows. Run as:
|
||||
//
|
||||
// cscript.js app.js <appname> <app arguments> ...
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// app.js
|
||||
// Bootstrap code for running a javascript app in windows. Run as:
|
||||
// cscript.js app.js <appname> <app arguments> ...
|
||||
//
|
||||
// Author: Namhyeon Go <abuse@catswords.net>
|
||||
// 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 <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) {
|
||||
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
|
||||
|
|
44
lib/file.js
44
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;
|
||||
|
|
19
lib/std.js
19
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;
|
||||
|
|
Loading…
Reference in New Issue
Block a user