How to improve our development process when scripting?

Content Hub enthousiast Logo


One of the challenges I face within the Content Hub is writing a good and solid script. I've been developing code for over a decade now and I do like my favourite development environment. I really like the Visual Studio IDE that helps me program my solutions. But currently, I can't use the power of Visual Studio (VS) when writing scripts for the Content Hub. I've tried coding in the VS with the SDK and then copy/paste the code over the Content Hub, but that really feels like a stupid thing to do. 

So how can do better?

The latest version of the CLI preview can really help us out here. Sitecore has added support for Scripting in their latest version. Although it's still a preview release, it can show the future way of work for us developers.

IMPORTANT

This is a preview release. The goal of the preview is to enable customers and partners to provide feedback on features before they are finalized. As such, some features are in an interim state that is subject to change.

Since the scripting module is still in preview, we need to execute the following command in order to retrieve the help of scripting:

ch-cli preview scripting --help

This will give us the following result:

CLI preview scripting command

The first thing we need to do is create a separate folder that we would like to work in. In my case, I'm going to work in the C:\Temp folder. In the Temp folder, we will execute the initialize command, This will initialize the script working folder with the files needed to make IntelliSense work in VS Code.

Now that we've initialized the folder, we can open the folder in Visual Studio Code. As You can see, the command has given us a couple of files. In the references folder, you can find the different script types. For each script type, you will find a reference file that specifies the properties that are available to you. For example the Action type. It contains an MClient and a Context,

Visual Studio Code

Let pull our first existing script out of the Content Hub. The pulling of a script is done by the name of the script. Surround the name with double quotes, otherwise the script will throw an error on the name. For example sync the script CMP - Create public links for linked assets with the command:
ch-cli preview scripting --pull "CMP - Create public links for linked assets"

When you now check your Visual Studio Code instance, you will see that a new file named CMP - Create public links for linked assets.csx has been created. Open the file to see it VS Code. It should be something similar to the image below, depending on your theme and such.

CMP - Create public links for linked assets.csx

As you can see the script on your local machine isn't exactly the same as it is in the Content Hub. The reason for this is actually quite simple. In order for IntelliSense to work, they need a reference to the underlying classes. By loading the references the IntelliSense knows what to display.

So, now that we have pulled the script we can trigger the command to watch for changes on our local machine. We can activate the watch with the command:
ch-cli preview scripting --watch

When you make a change to a script and save it, the watch will find this change and start syncing it with your Content Hub instance. There it will be compiled and published right away. 

This is how it looks.

ch-cli preview scripting --watch

And there you have it. From local development to publishing the script in the Content Hub. This will greatly help you write better scripts.

The biggest limitation I currently came across, is the fact that you can only pull existing scripts. The creation of a new script is not (yet) possible with the CLI. Perhaps still will happen in the future.

Want to read more about CLI? Check out the link.