LoginSignup
29
23

More than 5 years have passed since last update.

ざっくりおぼえる sfdx コマンド

Last updated at Posted at 2017-12-12

この記事は Salesforce Platform Advent Calendar 2017 の12日目です。


sfdx コマンドって?

  • SalesforceDXで提供されている機能のひとつで、新しいコマンドラインツールです。
    • 組織に対する操作をコンソール上から行うことができます。
  • より詳しく学びたい方はTrailheadをやりましょう!!
    • (Trailheadやっとけ!で済むSalesforceはすごい)

下準備

sfdx コマンドのインストール

  • こちらからダウンロードしてインストールします。
  • NodeJSユーザな方は npm からのインストールも可能です。
    • npm install --global sfdx-cli

DevHub環境のセットアップ

  • Enterprise, Performance, Unlimited エディションの場合、DevHubの機能を有効にできます。
  • 個人など、DevHub環境を持っていない場合はこちらからトライアル組織を作成します。
  • DevHubを有効にすることで使い捨ての組織(Scratch Org)が作成できるようになります。
    • Scratch OrgはインスタントなSandbox環境です。
    • これを個人の開発環境やCI用の環境にしたりすることで、他の開発者に影響なく開発をすすめることが可能になります。
    • ただし、7日間を過ぎると削除されます。(詳細)

ざっくりおぼえる sfdx コマンド

本題です。
SalesforceDXでの開発で用いるコマンドをざっくり覚えましょう。

--help -h オプション ... ヘルプを表示する

  • コマンドの詳細について知りたい場合は --help または -h でヘルプを見れます。
  • force 以下のサブコマンドについても同様にオプションが使えます。
    • sfdx force:org:list --help など
$ sfdx --help
Usage: sfdx COMMAND

Help topics, type sfdx help TOPIC for more details:

 force    tools for the Salesforce developer
 plugins  manage plugins
 update   update CLI

$ sfdx force -h
Usage: sfdx force: [-v] [--json] [--loglevel <string>] [flags]

Flags:
 -v, --version        display the Salesforce API version
 --json               format output as json
 --loglevel LOGLEVEL  logging level for this command invocation (error*,trace,debug,info,warn,fatal)

Usage: sfdx force:COMMAND

Help topics, type sfdx help TOPIC for more details:

 force:alias        manage username aliases
 force:apex         work with Apex code
 force:auth         authorize an org for use with the Salesforce CLI
 force:config       configure the Salesforce CLI
 force:data         manipulate records in your org
 force:doc          display help for force commands
 force:lightning    create and test Lightning component bundles
 force:limits       view your org’s limits
 force:mdapi        retrieve and deploy metadata using Metadata API
 force:org          manage your Salesforce DX orgs
 force:package      install and uninstall first- and second-generation packages
 force:package1     develop first-generation managed and unmanaged packages
 force:package2     develop second-generation packages
 force:project      set up a Salesforce DX project
 force:schema       view standard and custom objects
 force:source       sync your project with your orgs
 force:user         perform user-related admin tasks
 force:visualforce  create and edit Visualforce files

sfdx force:doc:commands:list ... コマンドの一覧を表示する

  • やりたいことを実現するのにどんなコマンドを叩けばいいか分からない場合はこのコマンドを使いましょう。
  • sfdx force:doc:commands:display とすると、コマンドと一緒に詳細な説明が見られます。
$ sfdx force:doc:commands:list
=== Commands
  force:alias:list                   # list username aliases for the Salesforce CLI
  force:alias:set                    # set username aliases for the Salesforce CLI
  force:apex:class:create            # create an Apex class
  force:apex:execute                 # execute anonymous Apex code
  force:apex:log:get                 # fetch a debug log
  force:apex:log:list                # list debug logs
  force:apex:test:report             # display test results
  force:apex:test:run                # invoke Apex tests
  force:apex:trigger:create          # create an Apex trigger
  ~~~ (略) ~~~
  force:visualforce:component:create # create a Visualforce component
  force:visualforce:page:create      # create a Visualforce page

sfdx force:auth:web:login ... ブラウザを開いて組織にログインする

  • ログインが成功するとトークンが保存され、その組織に対して様々な操作をすることができるようになります。
  • -a [エイリアス] を使うと、組織に別名を付けることができ、長いユーザー名を覚える必要がないので合わせて使いましょう。
  • また、この操作はDevHubが有効でない組織でも利用可能です。
$ sfdx force:auth:web:login -a MyDevOrg
(ブラウザが開き、ログインページが表示される)

sfdx force:org:open ... 組織をブラウザで開く

  • -u [ユーザ名 or エイリアス] で開く組織を指定します。
  • 複数プロジェクトの組織を登録しておくと、このコマンドで組織間の行き来がしやすくなって便利です!
$ sfdx force:org:open -a MyDevOrg
(ブラウザが開き、組織の設定画面が開く)

sfdx force:project:create ... SalesforceDXプロジェクトの雛形を作成する

  • --projectname または -n でプロジェクト名を指定します。
  • このプロジェクト下にApexコードやメタデータなどが配置され、Gitなどで管理・共有していきます。
$ sfdx force:project:create --projectname sample-project
target dir = /path/to/projects
   create sample-project/sfdx-project.json
   create sample-project/README.md
   create sample-project/config/project-scratch-def.json
$ tree sample-project/
sample-project/
├── README.md
├── config
│   └── project-scratch-def.json
├── force-app
│   └── main
│       └── default
│           └── aura
└── sfdx-project.json

5 directories, 3 files

sfdx force:org:create ... 使い捨ての組織(Scratch Org)を作成する

  • -f オプションで組織作成に関する設定ファイルを指定します。(詳細)
  • -a オプションで組織のエイリアスを設定できます。
  • -v オプションでDevHubが有効な組織を指定します。
$ sfdx force:org:create -f config/project-scratch-def.json -a MyScratchOrg -v MyDevOrg
Successfully created scratch org: 00DN0000000XXXXXX0, username: test-yyyyyyyyyy@zzzzzz_company.net

sfdx force:lightning:app:create ... Lightningアプリケーションの雛形を作成する

  • -n オプションでアプリケーション名を指定します。
  • -d オプションでファイルを展開するディレクトリを指定します。
    • Lightningのディレクトリは force-app/main/default/aura となるので基本的にはこちらを指定します。
$ sfdx force:lightning:app:create -n MyLightningApp -d forcce-app/main/default/aura
target dir = /path/to/projects/sample-project/force-app/main/default/aura
   create MyLightningApp/MyLightningApp.app
   create MyLightningApp/MyLightningApp.app-meta.xml
   create MyLightningApp/MyLightningAppController.js
   create MyLightningApp/MyLightningAppHelper.js
   create MyLightningApp/MyLightningApp.css
   create MyLightningApp/MyLightningAppRenderer.js
   create MyLightningApp/MyLightningApp.svg
   create MyLightningApp/MyLightningApp.auradoc
  • そのほか、Lightningアプリケーション以外にも雛形を作成するコマンドは複数あるので、目的に合わせたものを使いましょう。
  force:apex:class:create            # create an Apex class
  force:apex:trigger:create          # create an Apex trigger
  force:lightning:app:create         # create a Lightning app
  force:lightning:component:create   # create a Lightning component
  force:lightning:event:create       # create a Lightning event
  force:lightning:interface:create   # create a Lightning interface
  force:lightning:test:create        # create a Lightning test
  force:visualforce:component:create # create a Visualforce component
  force:visualforce:page:create      # create a Visualforce page

sfdx force:source:push ... 組織にソースコードやメタデータを反映する

  • ローカルで行った修正などを組織に反映します。
  • 下記の例は上記の MyLightningApp を組織に反映させたものです。
$ sfdx force:source:push
=== Pushed Source
STATE  FULL NAME                                   TYPE                  PROJECT PATH
─────  ──────────────────────────────────────────  ────────────────────  ──────────────────────────────────────────────────────────────────────
Add    MyLightningApp/MyLightningApp.app           AuraDefinitionBundle  force-app/main/default/aura/MyLightningApp/MyLightningApp.app
Add    MyLightningApp/MyLightningApp.app           AuraDefinitionBundle  force-app/main/default/aura/MyLightningApp/MyLightningApp.app-meta.xml
Add    MyLightningApp/MyLightningApp.auradoc       AuraDefinitionBundle  force-app/main/default/aura/MyLightningApp/MyLightningApp.auradoc
Add    MyLightningApp/MyLightningApp.css           AuraDefinitionBundle  force-app/main/default/aura/MyLightningApp/MyLightningApp.css
Add    MyLightningApp/MyLightningApp.svg           AuraDefinitionBundle  force-app/main/default/aura/MyLightningApp/MyLightningApp.svg
Add    MyLightningApp/MyLightningAppController.js  AuraDefinitionBundle  force-app/main/default/aura/MyLightningApp/MyLightningAppController.js
Add    MyLightningApp/MyLightningAppHelper.js      AuraDefinitionBundle  force-app/main/default/aura/MyLightningApp/MyLightningAppHelper.js
Add    MyLightningApp/MyLightningAppRenderer.js    AuraDefinitionBundle  force-app/main/default/aura/MyLightningApp/MyLightningAppRenderer.js

sfdx force:source:pull ... 組織からソースコードやメタデータを取得する

  • 組織のWebで編集したメタデータなどをローカルに反映します。
  • 下記の例はてきとうなオブジェクト サンプルオブジェクト (SampleObject__c) を作成した例です。
  • メタデータがローカルに反映されているのがわかります。
$ sfdx force:source:pull
=== Pulled Source
STATE  FULL NAME                        TYPE          PROJECT PATH
─────  ───────────────────────────────  ────────────  ──────────────────────────────────────────────────────────────────────────────
Add    SampleObject__c                  CustomObject  force-app/main/default/objects/SampleObject__c/SampleObject__c.object-meta.xml
Add    SampleObject__c-サンプルオブジェクトレイアウト  Layout        force-app/main/default/layouts/SampleObject__c-サンプルオブジェクトレイアウト.layout-meta.xml
Add    Admin                            Profile       force-app/main/default/profiles/Admin.profile-meta.xml
Add    Custom%3A Marketing Profile      Profile       force-app/main/default/profiles/Custom%3A Marketing Profile.profile-meta.xml
Add    Custom%3A Support Profile        Profile       force-app/main/default/profiles/Custom%3A Support Profile.profile-meta.xml
Add    Custom%3A Sales Profile          Profile       force-app/main/default/profiles/Custom%3A Sales Profile.profile-meta.xml
  • pushpull、ローカルのエディタとWeb組織(開発者コンソール)をうまく使って効率よく開発しましょう!

sfdx update ... sfdx コマンド自体を更新する

  • 定期的にアップデートして最新に追従しましょう。
$ sudo sfdx update
[sudo] user のパスワード:
sfdx-cli: Updating CLI from 6.0.13-a52f73c to 6.0.25-f21ab24... done
sfdx-cli: Updating CLI... ⣾
sfdx-cli: Updating plugins... done

おわりに

これらのコマンドが使えればSalesforceDXによる基本的な開発はできるようになるかと思います。
そのほか、テストの実行、テストデータの投入、本番環境への適用に向けたパッケージングなど、
ここでは言及しなかったものがありますがそれはまた別途…。

よい Developer Experience を!

29
23
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
29
23