mlog

備忘録とかメモとか

Firestoreセキュリティルール向けのビルド(ファイル結合)ツールを作ってみた!

Firestoreセキュリティルール向けのファイル結合ツールを作ってみたので、簡単に使い方などを紹介したいと思います。

作成したCLIツールは以下

Firestoreセキュリティルールに限らず、テキストファイルであれば何でも使えるので、ツール名はincludeとしています。

本記事ではFirestoreセキュリティルールとMarkdownの結合を例に、使い方を紹介したいと思います。

目次

インストール

yarnを使用する場合は、以下のコマンドでパッケージを追加してください。

yarn add --dev @mm0202/include

npmを使用する場合は以下のコマンドでパッケージを追加してください。

npm install --save-dev @mm0202/include

使用例

Firestoreセキュリティルール

入力ファイル

以下の5つのファイルを結合する場合を考えたいと思います。

// src/rules/index.rules
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    _include "functions.rules"

    match /someCollection/{someDocument} {
      allow read: if isAdmin();
      _include "someCollection/write.rules"
    }

    _include "someCollection2/index.rules"
  }
}
// src/rules/functions.rules
function isAdmin() {
  return request.auth.token.admin == true;
}
// src/rules/someCollection/write.rules
allow write: if isAdmin();
// src/rules/someCollection2/index.rules
match /someCollection2/{someDocument} {
  allow read: if isAdmin();
  _include "write.rules"
}
// src/rules/someCollection2/write.rules
allow write: if true;

ここで_include "[target file path]"の記述により結合するファイルを指定しています。

結合コマンド

上記のファイルを結合するには、以下のようなコマンドを実行してください。

yarn include -s src/rules/index.rules -w src/rules -o out/rules/output.rules

ここで、-sオプションでファイル結合のルートとなるファイルを指定しています。

-wでファイルの変更を監視するディレクトリを指定しています。

-oで出力先のファイル名を指定しています。

-wを指定しない場合は監視なしのビルドのみとなります。

-oを指定しない場合はターミナルに結合結果が表示されます。

※ その他のオプションについては、後述のオプションを参照してください。

出力結果

上記のコマンドを実行すると、以下のようなファイルが出力されます。

// out/rules/output.rules
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    function isAdmin() {
      return request.auth.token.admin == true;
    }

    match /someCollection/{someDocument} {
      allow read: if isAdmin();
      allow write: if isAdmin();
    }

    match /someCollection2/{someDocument} {
      allow read: if isAdmin();
      allow write: if true;
    }
  }
}

Markdown

入力ファイル

以下の2ファイルを結合する場合を考えたいと思います。

<!-- src/md/root.md -->
this is root

# subtext
    _include "./subtext/subtext1.md"

# subtext
> _include "./subtext/subtext1.md"
<!-- src/md/subtext/subtext1.md -->
this is subtext1
this is subtext1
this is subtext1
this is subtext1

結合コマンド

上記のファイルを結合するには、以下のようなコマンドを実行してください。

yarn include -s src/md/root.md -w src/md -o out/md/output.md

※ オプションについては、後述のオプションを参照してください。

出力結果

上記のコマンドを実行すると、以下のようなファイルが出力されます。

<!-- out/md/output.md -->
this is root

# subtext
    this is subtext1
    this is subtext1
    this is subtext1
    this is subtext1

# subtext
> this is subtext1
> this is subtext1
> this is subtext1
> this is subtext1

オプション

オプションを確認する場合は、以下のコマンドを実行してください。

yarn include -h

表示されるオプションは以下の通りです。

  -s, --src <path>                   src path
  -o, --out <path>                   output path
  -w, --watch <paths...>             watch paths
  -i, --min-interval <milliseconds>  min interval
  -h, --help                         help

-sオプションはファイル結合のルートとなるファイルを指定します。

-oオプションは出力ファイルを指定します。指定がない場合はターミナルに結合結果が表示されます。

-wオプションはファイル監視するディレクトリを指定します。(半角スペースつなぎで複数指定可。)

-iはwatchの最小インターバル時間(ミリ秒)を指定します。指定がない場合の最小インターバル時間は1000ミリ秒です。

まとめ

以上、Firestoreセキュリティルール向けのファイル結合ツールの使い方について簡単に紹介しました。

もしよければ、使ってみてください。使用感や感想などコメント大歓迎です!