diff --git a/index.js b/index.js index dcfec48..a161036 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ try { } } +const nullish = (value, cond) => ((undefined === value) || (null === value)) ? cond() : value; const awsLambdaTaskRoot = process.env.LAMBDA_TASK_ROOT; const awsHandler = process.env._HANDLER; const FUNCTION_EXPR = /^([^.]*)\.(.*)$/; @@ -52,12 +53,15 @@ if (awsLambdaTaskRoot && awsHandler) { } delete require.cache[file]; -const mainExports = __jymfony.autoload.classLoader.loadFile(file, null); +const mainExports = __jymfony.autoload.classLoader.loadFile(file); +if (mainExports.__esModule) { + exportName = 'default'; +} -let runtime = process.env.APP_RUNTIME ?? Jymfony.Component.Runtime.JymfonyRuntime; +let runtime = nullish(process.env.APP_RUNTIME, () => Jymfony.Component.Runtime.JymfonyRuntime); const reflection = new ReflectionClass(runtime); -runtime = reflection.newInstance(Object.assign({ project_dir: dirname(file) }, globalThis.APP_RUNTIME_OPTIONS ?? {})); +runtime = reflection.newInstance(Object.assign({ project_dir: dirname(file) }, nullish(globalThis.APP_RUNTIME_OPTIONS, () => ({})))); let [ app, args ] = runtime.getResolver(null === exportName ? mainExports : mainExports[exportName]).resolve(); (async function () { diff --git a/src/JymfonyRuntime.js b/src/JymfonyRuntime.js index d5b9e5d..570c758 100644 --- a/src/JymfonyRuntime.js +++ b/src/JymfonyRuntime.js @@ -6,6 +6,20 @@ const ConsoleOutput = Jymfony.Component.Console.Output.ConsoleOutput; const GenericRuntime = Jymfony.Component.Runtime.GenericRuntime; const JymfonyErrorHandler = Jymfony.Component.Runtime.Internal.JymfonyErrorHandler; +function getInput(options) { + const input = new ArgvInput(); + const env = input.getParameterOption([ '--env', '-e' ], null, true); + if (null !== env) { + process.env[options.env_var_name] = env; + } + + if (input.hasParameterOption('--no-debug', true)) { + process.env[options.debug_var_name] = '0'; + } + + return input; +} + /** * Knows the basic conventions to run Jymfony apps. * @@ -65,7 +79,7 @@ export default class JymfonyRuntime extends GenericRuntime { } const prodEnvs = options.prod_envs ?? [ 'prod' ]; - const input = JymfonyRuntime.#getInput(options); + const input = getInput(options); if ( !(options.disable_dotenv ?? !ReflectionClass.exists('Jymfony.Component.Dotenv.Dotenv')) && !!options.project_dir @@ -151,18 +165,4 @@ export default class JymfonyRuntime extends GenericRuntime { return self; } - - static #getInput(options) { - const input = new ArgvInput(); - const env = input.getParameterOption([ '--env', '-e' ], null, true); - if (null !== env) { - process.env[options.env_var_name] = env; - } - - if (input.hasParameterOption('--no-debug', true)) { - process.env[options.debug_var_name] = '0'; - } - - return input; - } }