config.ts
:
export const data = 'Config';
process.ts
:
import { data } from "./config.ts";
const calculated = data + ' is used.'
export {calculated}
main.ts
:
import { calculated } from "./process.ts";
console.log(calculated)
> deno run main.ts
Config is used.
Now, I want to be able to choose configuration via command-line argument. That is:
> deno run main.ts config1
Config1 is used.
> deno run main.ts config2
Config2 is used.
> deno run main.ts config3
Config3 is used.
Is this possible?
To solve the static import
and Uncaught ReferenceError: Cannot access 'configToUse' before initialization
, I use dynamic imports and wrap everything inside functions. The code becomes:
process.ts
:
async function calculate(configToUse) {
const pathToConfig = './' + configToUse + '.ts'
const config = await import(pathToConfig)
return config.data + ' is used.'
}
export {calculate}
main.ts
:
import { calculate } from "./process.ts";
const configToUse = Deno.args[0]
calculate(configToUse).then((result) => {
console.log(result)
})