Sub topic 2
Generating a project and tour of the standard layout
This chapter assumes you have the Salesforce CLI (sf) installed, know basic Salesforce concepts (orgs, metadata, Apex, Lightning web components), and are comfortable with a terminal and Git. You will generate a project and learn what each top-level file and folder does.
Generate a project
The sf project generate command creates a Salesforce DX project with the default template. It makes a new folder named after the project.
sf project generate --name my-project
cd my-project
ls -aThe ls -a flag shows hidden files such as .forceignore and .gitignore, which matter for the tour below.
The standard layout
The generated project looks like this:
my-project/
├── .forceignore
├── .gitignore
├── config/
│ └── project-scratch-def.json
├── force-app/
│ └── main/
│ └── default/
│ ├── classes/
│ ├── lwc/
│ └── objects/
├── scripts/
└── sfdx-project.jsonThe exact set of files can vary slightly between CLI versions. The roles below stay the same.
| Item | Purpose |
|---|---|
| `sfdx-project.json` | The project configuration file. The CLI reads it to learn where your source lives and which API version to use. |
| `force-app/` | The default package directory. Your metadata source lives here. |
| `config/project-scratch-def.json` | The definition file used when you create scratch orgs for this project. |
| `.forceignore` | Gitignore-style patterns that keep files out of deploys, retrieves, and source tracking. |
| `.gitignore` | Tells Git which files to leave out of version control. |
| `scripts/` | A place for helper scripts, such as Apex snippets or shell scripts. The CLI does not read it. |
Inside force-app
Under force-app/main/default/, metadata is stored in folders named for each metadata type. Apex classes go in classes/, Lightning web components in lwc/, and custom objects in objects/. Other types get their own folders, such as triggers/, flexipages/, or permissionsets/. The CLI creates a folder when it first needs to write that type.
The path main/default is a convention, not a requirement. What matters to the CLI is that the package directory listed in sfdx-project.json contains your source. Later chapters cover how source format maps to metadata and how to list several directories.
How the pieces fit together
Each file feeds a different step of the workflow. sfdx-project.json tells the CLI where to look. .forceignore filters what it finds. The scratch org definition shapes the org you deploy to.
Try the project against an org
The commands below create a scratch org from the definition file, then deploy and retrieve source. They require a Dev Hub org that you have already authorized.
sf org create scratch --definition-file config/project-scratch-def.json --alias my-scratch --set-default
sf project deploy start
sf project retrieve startsf project deploy start and sf project retrieve start act on the package directories from sfdx-project.json. You do not pass a path unless you want to limit the operation.
Common mistakes
- Running
sf project deploy startfrom a folder outside the project. The CLI cannot findsfdx-project.json. - Expecting
scripts/orconfig/to be deployed. They are not metadata, so the CLI ignores them during deploys. - Moving
force-appwithout updating thepathvalue insfdx-project.json.
Exercise
You run sf project deploy start from your home directory, one level above my-project, and it fails. What is the most likely cause, and how do you fix it?
Show answer
The CLI cannot find sfdx-project.json because it searches the current directory and its parents, not subfolders. Run cd my-project and run the command again.
Exercise
Which file would you edit to keep all files under a __tests__ folder out of deploys?
Show answer
.forceignore. Add a gitignore-style pattern such as **/__tests__/**.