如何选择配置文件作为命令行参数并在控制台中显示结果,同时保持模块化?

问题描述 投票:0回答:1

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.

现在,我希望能够通过命令行参数选择配置。那就是:

> deno run main.ts config1
Config1 is used.
> deno run main.ts config2
Config2 is used.
> deno run main.ts config3
Config3 is used.

这可能吗?

javascript configuration command-line-arguments
1个回答
0
投票

为了解决静态

import
Uncaught ReferenceError: Cannot access 'configToUse' before initialization
,我使用 动态导入 并将所有内容包装在函数内。代码变为:

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)
})
© www.soinside.com 2019 - 2024. All rights reserved.