简体   繁体   English

如何遍历配置文件以在 Node 中有条件地生成文件

[英]How to loop through a config file to conditionally generate files in Node

Imagine that you have a config file like this one想象一下,你有一个像这样的配置文件

{
    environments: [
        {
            "name": "DEV",
            "host": "https://localhost",
            "port": "8080"
        },
        {
            "name": "PRE",
            "host": "whatever",
            "port": "whatever"
        }
    ]
}

how could I go through it to generate a file one by one considering that now my code generate all files at once考虑到现在我的代码一次生成所有文件,我怎么能通过它 go 一个一个地生成一个文件

I generate files this way我以这种方式生成文件

collection.js

fs.writeFileSync(argv.target+ '/' + title +'.collection.json', JSON.stringify(output))

And in the main file, named main.js.并在主文件中,命名为 main.js。 Here's they are generated all at once, but not one by one.这是它们一次生成的,但不是一一生成的。 I'd like to generate each collection having in mind the name of the environment in the config file我想生成每个集合,同时记住配置文件中的环境名称

require('./src/generator/collection.js')('01'+title+'_pre_backend',endpoints);
require('./src/generator/collection.js')('02'+title+'_pre_backend',endpoints);

Thanks in advance提前致谢

I am not sure If I clearly understand your question.我不确定我是否清楚地理解了你的问题。 Are you looking for some code like this:您是否正在寻找这样的代码:

config.environments.forEach(env => {
    require('./src/generator/collection.js')('01'+env.title+'_pre_backend',env.port);
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM