Configuring Sitecore CLI

Setting up Sitecore CLI is an essential step to streamline your Sitecore development workflow. Here’s a guide to configuring Sitecore CLI for your project:

1. Install .NET SDK: First, make sure you have .NET SDK version 4.8 installed on your machine to match the requirements for Sitecore 10.3. You can refer to the Sitecore Compatibility Table for Sitecore XP 9.0 and later to ensure compatibility. Sitecore Compatibility Table

2. Install Sitecore Management Services: Download and install Sitecore Management Services into your Sitecore instance.

3. Open the Command Line: Navigate to your project directory and open the Command Prompt (CMD) or your terminal.

4. Install Sitecore CLI: Run the following commands in your CMD or terminal:

dotnet new tool-manifest
dotnet nuget add source -n Sitecore https://nuget.sitecore.com/resources/v3/index.json
dotnet tool install Sitecore.CLI

5. Initialize Your Project: After installing Sitecore CLI, initiate your project with the following command within your project folder:

dotnet sitecore init

This command sets up the project configuration and creates a sitecore.json file in your project directory.

6. Configure Project Structure: In your sitecore.json file, you can specify your project’s module and plugin configurations. Customize it according to your project’s needs. Here’s a sample sitecore.json structure:

{
  "$schema": "./.sitecore/schemas/RootConfigurationFile.schema.json",
  "modules": [
    "./serialization/*/*.module.json"
  ],
  "plugins": [
    "Sitecore.DevEx.Extensibility.Serialization@4.2.0",
    "Sitecore.DevEx.Extensibility.Publishing@4.2.0",
    "Sitecore.DevEx.Extensibility.Indexing@4.2.0",
    "Sitecore.DevEx.Extensibility.ResourcePackage@4.2.0"
  ],
  "serialization": {
    "defaultMaxRelativeItemPathLength": 100,
    "defaultModuleRelativeSerializationPath": "serialization",
    "removeOrphansForRoles": true,
    "continueOnItemFailure": false,
    "excludedFields": []
  }
}

7. Create Module Folders: Organize your project by creating module folders within a “serialization” directory. Typically, you’ll have Feature, Project, and Foundation folders if you’re following the Helix architecture. Customize this structure as needed. For example, you can create a “Feature” folder and add a .module.json file inside it:

{
    "namespace": "[YOUR NAMESPACE NAME]",
    "description": "[DESCRIPTION OF THE MODULE]",
    "tags": ["content"],
    "items": {
        "includes": [
            {
                "name": "SCSSolution.SiteContent",
                "path": "/sitecore/Templates/Feature"
            }
        ]
    }
}

8. Connect to a Sitecore Instance: Use the following command to connect to your Sitecore instance. Replace <Sitecore identity server> and <Sitecore instance> with your actual server URLs:

dotnet sitecore login --authority https://<Sitecore identity server> --cm https://<Sitecore instance> --allow-write true

This will open your Sitecore instance’s login screen in a browser tab. Log in and allow access.

After Succefull login you will see the message in your terminal :

9. Pull Updates from Sitecore: To retrieve updated items from your Sitecore instance, run the following command:

dotnet sitecore ser pull

This command will pull all the templates from your Feature folder and generate corresponding .yaml files.

With these steps completed, you’re all set to streamline your Sitecore development with Sitecore CLI. Happy coding! 😊

Sitecore JSS 10.3 Leprechaun Setup

Setting Up Leprechaun in Sitecore JSS 10.3

Published by Vadherravi on August 26, 2023

In this article, we’ll walk you through the process of setting up Leprechaun in your development environment and explore how it can help you generate types for your Next.js application in a Sitecore JSS project. But before diving into the setup, let’s clarify what Leprechaun is and why it’s essential in the context of Sitecore JSS.

What is Leprechaun?

Leprechaun is a code generator specifically designed for Sitecore templates. It reads serialized YAML files from Sitecore serialized items and is a command-line tool for generating models for Sitecore JSS projects.

Why Use Leprechaun?

When you’re working with a Next.js application in Sitecore JSS, creating types or interfaces for field data can be a time-consuming task. Leprechaun streamlines this process by automatically generating the necessary TypeScript types for a particular template. This significantly reduces the effort required for setting up your Next.js application.

Prerequisites for Using Leprechaun

Before you get started with Leprechaun, you’ll need to have the following prerequisites in place:

  1. Sitecore CLI Setup: Ensure that you have the Sitecore CLI properly configured in your environment: Sitecore CLI
  2. Serialized Sitecore Templates: Leprechaun relies on the .yaml files generated from serialized Sitecore templates. Make sure you have these files available.

Now, let’s dive into the steps to set up Leprechaun.

Setting Up Leprechaun

Step 1 – Install Leprechaun in Your Project

To install Leprechaun in your project, follow these steps:

  • Open a PowerShell window in administrator mode.
  • Navigate to your project directory.
  • Run the following command:
  dotnet tool install --no-cache Leprechaun.Cli

You’ll receive a success message indicating that the installation was successful. You can now invoke the tool using commands like ‘dotnet tool run leprechaun’ or ‘dotnet leprechaun.’

Step 2 – Download the Code Generator File

Download the code generator file (.csx) from the following link and save it in your project directory:

Code Generator File

Step 3 – Configure the module.json File

In your module.json file, provide Leprechaun configuration. Ensure that the ‘@extends’ value matches your leprechaun.config file configuration name. Here’s an example of what this configuration may look like:

{
  "namespace": "Training.jss",
  "items": {
    "includes": [
      {
        "name": "feature-templates", 
        "path": "/sitecore/templates/Feature/next-jss"
      }
    ],
    }
  },
"leprechaun": {
      "configuration": {
        "@extends": "Training.jss.Base", // Same name as in your leprechaun.config file
        "@name": "Feature.feature-templates" // Define where you want to save your TypeScript file.
      }
}

Step 4 – Setup leprechaun.config File

After installing Leprechaun, you need to download or copy the code from the leprechaun.config file provided in this link. Ensure that your leprechaun.config file and your sitecore.json file are in the same level.

You’ll need to configure the ‘configurations’ node in the leprechaun.config file. Here’s what a section of that configuration might look like:

<configurations import="./serialization/Feature/*.module.json">
    <configuration name="Training.jss.Base" abstract="true">
        <codeGenerator
        scripts="./CodeGen/JssTypeScript.csx"
        outputFile="$(configDirectory)\models\$(layer).$(module).Model.ts" />

        <templatePredicate type="Leprechaun.InputProviders.Sitecore.Filters.SitecoreTemplatePredicate, Leprechaun.InputProviders.Sitecore"
        rootNamespace="$(layer).$(module)" singleInstance="true">
        <include name="feature-templates" path="/sitecore/templates/Feature/next-jss"/>
        </templatePredicate>
    </configuration>
</configurations>

You will also find another <templatePredicate> under the <Defaultss> node that you need to comment out.

Make sure to match the ‘@extends’ value in the and in <Include node> also name should same as “name”: “feature-templates” in module.json file .module file, provide the script name you downloaded in Step 2, , the specify the output directory where your code will be saved, and use the same ‘name’ as in your .module file.

Step 5 – Run the Leprechaun Command

Finally, open your PowerShell window in administrator mode, navigate to your project directory, and run the following command:

dotnet leprechaun -c "C:\Demo\Demo\Leprechaun.config"

Replace the path with the actual location of your leprechaun.config file.

Congratulations! Leprechaun is now set up and ready to generate the required code. You should see a message indicating that the code generator has loaded and successfully generated files from your templates. 🎉