javascript - Parse INI file in Node.js - Stack Overflow

I build a Node.js tool in order to change GCP config quickly. But I'm a bit stuck on the parsing o

I build a Node.js tool in order to change GCP config quickly. But I'm a bit stuck on the parsing of the config_nameOfConfig file. It looks like this :

[core]
account = [email protected]
project = project-example
disable_usage_reporting = False

[pute]
region = europe-west1-b

(This file is available in '/Users/nameOfUser/.config/gcloud')

And I want to convert it in an object like this :

const config = {
  account:"[email protected]",
  project:"project-example",
  disable_usage_reporting:false,
  region:"europe-west1-b",
};

I get the content of this file with the fs.readFileSync function who converts it into a string.

An idea ?

I build a Node.js tool in order to change GCP config quickly. But I'm a bit stuck on the parsing of the config_nameOfConfig file. It looks like this :

[core]
account = [email protected]
project = project-example
disable_usage_reporting = False

[pute]
region = europe-west1-b

(This file is available in '/Users/nameOfUser/.config/gcloud')

And I want to convert it in an object like this :

const config = {
  account:"[email protected]",
  project:"project-example",
  disable_usage_reporting:false,
  region:"europe-west1-b",
};

I get the content of this file with the fs.readFileSync function who converts it into a string.

An idea ?

Share Improve this question edited Oct 28, 2020 at 9:35 Thibault Walterspieler asked Sep 20, 2020 at 9:50 Thibault WalterspielerThibault Walterspieler 2,4222 gold badges17 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

This seems to be an ini file format. Unless you want to do the parsing yourself, you'd better just download a library to your project, for example:

npm install ini

Then in the code:

var ini = require('ini')
var fs = require('fs')
var config = ini.parse(fs.readFileSync('./config_nameOfConfig', 'utf-8'))

In config you should now have object containing the data in the file. You can console.log(config) to see how it exactly looks like.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745420278a4626933.html

相关推荐

  • javascript - Parse INI file in Node.js - Stack Overflow

    I build a Node.js tool in order to change GCP config quickly. But I'm a bit stuck on the parsing o

    13小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信