LoginSignup
3
3

More than 3 years have passed since last update.

Node.jsでiniファイルを操る 「properties-reader」

Last updated at Posted at 2020-10-11

properties-readerとは?

Node.js上でiniファイルを読み込めるようにしてくれるmodule。
(正確には、iniファイル形式 で書かれていれば拡張子はなんでもOK)

何のために使うか?

Electronとかで実行ファイル形式に変換した際、あとから変更になった既定値(ファイルパスとか)のために わざわざソース編集して再コンパイル...なんてやってられないよ! って人が使うもの。

実際作成したソースを「コンパイルするのに30分かかります!」なんてのはざらで、ちょっとした変更の度に再コンパイル→検証→再コンパイル...ってのはかなりの手間。

そんな中でiniファイルやpropertiesファイルとしてパラメータもたせられたら便利じゃないですか???便利ですよね!!!

上記環境のような中でNodejs使ってるよ!って方は 今すぐproperties-readerの使用を検討してはいかがでしょうか?

使い方

インストール方法

npm install properties-reader

使用するためのおまじない

index.js
// properties-readerをimport
const propertiesReader = require('properties-reader');

// iniファイルをimport
const properties = propertiesReader('./test.ini');

また、今回使用するiniファイルは下記の通り。

test.ini
[hoge]
fuga=hogefuga

[foo]
yeah=fooyeah
test2.ini
[test]
aaa=bbb

指定のパラメータのみ取得

index.js
// iniファイルから任意のパラメータを取得
const hf = properties.get('hoge.fuga'); // == hogehuga
const fy = properties.get('foo.yeah');  // == fooyeah
const hfPath = properties.path().hoge.fuga; // == hogehuga /syntax sugar

全パラメータ取得

index.js
// iniファイルから全パラメータを取得
properties.each((key, value) => {
    console.log(`${key}: ${value}`);
});
実行結果
hoge.fuga: hogefuga
foo.yeah: fooyeah

複数のiniファイルをまとめて取得

index.js
// 既に取得したiniに他のiniを追加
properties.append("./test2.ini");

// iniファイルから全パラメータを取得
properties.each((key, value) => {
    console.log(`${key}: ${value}`);
});
実行結果
hoge.fuga: hogefuga
foo.yeah: fooyeah
test.aaa: bbb

文字列から取得

index.js
// 文字列を読み込み
properties.read('ai.kaki = aikaki \n ue.kuke = uekuke');
properties.set('o.ko', 'oko');  // 読み込みたいパラメータが1つの場合はsetでもOK

// iniファイルから全パラメータを取得
properties.each((key, value) => {
    console.log(`${key}: ${value}`);
});
実行結果
hoge.fuga: hogefuga
foo.yeah: fooyeah
test.aaa: bbb
ai.kaki: aikaki
ue.kuke: uekuke
o.ko: oko

要素数取得

index.js
const propertiesCount = properties.length;

パラメータの保存

index.js
const props = propertiesReader('./test.ini');
console.log(props.get('hoge.fuga'));
props.set('hoge.fuga', 'hogehogefugafuga');

props.save('./test.ini')
    .then(data => {
        // save成功時処理
    }, err => {
        // save失敗時処理
        console.log(err);
    });

console.log(props.get('hoge.fuga'));
test.ini
[hoge]
fuga=hogehogefugafuga
[foo]
yeah=fooyeah

注意点

パラメータの上書き

既に存在するパラメータを指定してreadとかsetとかappendとかしちゃうと上書きされちゃうので注意。(read, set はまずないと思うがappendはありそう)

index.js
// パラメータの上書き
properties.each((key, value) => {
    console.log(`${key}: ${value}`);
});
console.log("");
properties.append("./test3.ini");
properties.each((key, value) => {
    console.log(`${key}: ${value}`);
});
console.log("");
test3.ini
[hoge]
fuga=hogehogehoge
[foo]
yeah=fooyeah
実行結果
hoge.fuga: hogefuga

hoge.fuga: hogehogehoge

参考

公式: https://www.npmjs.com/package/properties-reader
また、上記を試してみたい方は keito-damaのgithub よりclone作ってみてください。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3