0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Salesforce CLI のsfdxプラグインを作成する

Posted at

はじめに

Salesforce CLIはコマンドラインからSalesforce開発に必要な操作を実行できるツールです。
プラグインで機能が拡張することができます。

Salesforce CLIにはsfdxとsfの実行ファイルが存在し、使用できるプラグインも別になります。
今後はsfに統合されていくので、プラグインを自作するときはsfでプラグインを作成した方が良いです。
ですが、現在sfのプラグインではretireveやdeployをhookすることができません(Spring'23)。
メタデータをretrieveしたときにフォーマットするようなプラグインを作成するときはsfdxのプラグインを作成する必要があります。

sfdxプラグインの作成(hook)

例として、retrieveをhookして改行コードをLFに自動変換するプラグインを作成していきます。

環境

準備

  1. 下記のsfdxプラグインのリポジトリをクローンします。
  2. 依存関係をインストールします。
    • yarn install
  3. 今回はpostretrieveのhookのみ使用するので、package.jsonのoclif.hooksから下記を削除します。
    package.json
    "hooks": {
    -   "predeploy": "./lib/hooks/predeploy/metadataReplaceDeploy",
        "postretrieve": "./lib/hooks/postretrieve/metadataReplaceRetrieve"
    }
    

作成

src/hooks/postretrieve/metadataReplaceRetrieve.tsを下記の内容に置き換えます。

metadataReplaceRetrieve.ts
import { readFileSync, writeFileSync } from 'fs';
import { Command, Hook } from '@oclif/config';
import { FileResponse } from '@salesforce/source-deploy-retrieve';

type HookFunction = (this: Hook.Context, options: HookOptions) => any;

type HookOptions = {
  Command: Command.Class;
  argv: string[];
  commandId: string;
  result?: FileResponse[];
};

type LineBreakCode = '\n' | '\r\n';

const TARGET_TYPES = [
  'ApexClass', 'ApexTrigger', 'ApexPage', 'LightningComponentBundle', 'AuraDefinitionBundle'
];

const DEFAULT_LINE_BREAK_CODE = '\n';

export const hook: HookFunction = async function (options) {
  if (options.result) {
    for (const fileResponse of options.result) {
      const { type, filePath } = fileResponse;
      if (TARGET_TYPES.includes(type) && filePath) {
        console.log(`target: ${type} ${filePath}`);
        const inputData: string = readFileSync(filePath, 'utf-8');
        const outputData: string = convertLineBreaks(inputData, DEFAULT_LINE_BREAK_CODE);
        writeFileSync(filePath, outputData);
      } else {
        console.log(`not target: ${type} ${filePath}`);
      }
    }
  }
};

const convertLineBreaks = (text: string, lineBreakCode: LineBreakCode): string => {
  return text.replace(/\r\n|\r|\n/g, lineBreakCode);
}

sfdx force:source:retrievesfdx force:source:pullが実行された時にこのファイルのhookメソッドが実行されます。

postretrieveのoptions.resultの型はFileResponseです。これはhookするイベントによって異なります。詳細はこちら

FileResponseのtypeの種類はこちら

実行

  1. 作成したツールをコンパイルします。
    • yarn prepack
  2. Salesforce CLIにリンクします。
    • sfdx plugins:link
  3. Salesforceのプロジェクトフォルダでretrieveすると、改行コードがLFに自動で変換されます。
    • sfdx force:source:retrieve -m ApexClass

補足

  • リンクされているsfdxプラグインを確認する場合はsfdx pluginsを実行します。
  • sfdxプラグインをアンインストールする場合はsfdx plugins:unlink プラグイン名を実行します。
  • コマンドの初めにDEBUG=*をつけて実行するとデバッグがコンソールに表示されます。
    • DEBUG=* sfdx force:source:retrieve -m ApexClass

参考

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?