npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@twogate/boilerplate

v1.2.2

Published

A tool that replaces any placeholder text in a input file in bulk.

Downloads

144

Readme

Boilerplate CLI

テンプレートファイル中の任意のプレースホルダーテキストを一括で置き換えるツール。

※ root / sudo 権限では動作させないこと

Replacement definition (置換定義ファイル)

使うには、置換定義ファイルを用意します:

startSigil: "{{"         # 置き換えプレースホルダー開始sigil
endSigil: "}}"           # 置き換えプレースホルダー終了sigil
templateDir: ./templates # 入力側ディレクトリのベース
outDir: ./               # 出力側ディレクトリのベース

replaces:
    -   template: "./config.template.xml" # 入力 (templateDir + template)
        out: "./config.xml"               # 出力 (outDir + out)
        placeholders:                     # 置き換えるプレースホルダー: 置き換え先の値
            name: ZOC
            CFBundleDisplayName: ZOC
            APP_NAME: ZOC

    -   template: "./GoogleService-Info.template.plist"
        out: "./GoogleService-Info.plist"
        placeholders:    
            CLIENT_ID: xxx.apps.googleusercontent.com
            REVERSED_CLIENT_ID: 
            API_KEY: AIzaxxxxxxxxxxxx
            GCM_SENDER_ID: 123
            BUNDLE_ID: com.example.zoc
            PROJECT_ID: xxx.appspot.com
            GOOGLE_APP_ID: 1:123123123:ios:123123123
            DATABASE_URL: https://xxxxxxxx.firebaseio.com
            mobilesdk_app_id: xxx
            package_name: com.example.zoc

以下は replaces[].outreplaces[].placeholders を省略した形式です。

startSigil: "{{"
endSigil: "}}"
templateDir: .
outDir: ../../tmp

replaces:
    # out を書いていないので ../../tmp/depth1/d1-move-only.txt に出力され、placeholders がないため単純にコピーだけされる
    - template: "depth1/d1-move-only.txt"

    # out を書いていないので ../../tmp/path/to/template.txt に出力される
    # {{ ENV }} のプレースホルダーは development に置換される
    - template: "path/to/template.txt"
      placeholders:
          ENV: development

⚠️ やりがちなミス ⚠️

こうすべきところを

startSigil: "{{"
endSigil: "}}"
templateDir: .
outDir: .

replaces:
    - template: "1.png"
      out: out.png

こう書いてしまう

startSigil: "{{"
endSigil: "}}"
templateDir: .
outDir: .

replaces:
    - template: "1.png"
    - out: out.png # ❌ 間違い!

間違いがあると、Replacement Definition Syntax Error を表示して異常終了します。

Sigil

シジルは各プレースホルダーの前後につけるものです。 例えば {{ }} というシジルで PLACEHOLDER というプレースホルダーを定義して、それをテンプレートに埋め込む場合 {{PLACEHOLDER}} を埋め込みます。

その場合、置換定義ファイルは:

startSigil: "{{"
endSigil: "}}"
# ...
replaces: 
    # ...
    placeholders:
        PLACEHOLDER: '...'

のように定義します。

Path resolution

パスの定義は、

  • テンプレート
    • templateDirreplaces[].template
  • 出力先
    • outDirreplaces[].out

と解決されます。

基本的に共通する部分を templateDir または outDir にくくり出して、差異がある部分だけ各 replacestemplate または out で定義すると良いでしょう。

全てが相対パスだった場合、置換定義ファイルから見た相対パスで解決されます。

置換定義ファイルとテンプレート、出力先の相対的位置を固定しておけば、どこのカレントワーキングディレクトリにいても置換定義ファイルの定義通りのパスが使われます。

なお、 outDir は省略することができ、省略した場合は template と同じような構造のディレクトリが作られ、その中に出力されます。

Placeholder

プレースホルダーは startSigil + \s*? + placeholderText + \s*? + endSigil のような正規表現置換で行われているので、{{PLACEHOLDER}} だけでなく {{ PLACEHOLDER }} のような表記も可能です。

なお、startSigil endSigil とプレースホルダーテキストには正規表現は利用できません。

replaces[].placeholders を書かなかった場合、そのままコピーされます。

実行

置換定義ファイルを書いたら、そのパスを指定して実行します。

npx @twogate/boilerplate path/to/replacement.yml

第二引数としてパスを渡すと、そのパスを出力先(outDir)とします。

npx @twogate/boilerplate path/to/replacement.yml path/to/outDir