最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论