CodeLive: building Unlocked Packages with guest John Daniel
Last week on CodeLive special guest John Daniel and I took an existing codebase of Apex code and converted it to a DX unlocked package! Unable to join us? That’s ok the recording is available here. If you’re more of a reader than a viewer don’t fear — this post covers the highlights!
Our general ‘use-case’ was to take an existing bit of code (in this case Apex Promises) and convert the old metadata style repository to a source tracking style. We also configured the repository to build an unlocked package. This takes a handful of easy steps, most of which we demonstrated live! Let’s dive into exactly how this is done.
Kevin’s top tip: You can always append a -h flag to any sfdx command to see what command line params it accepts!
Create a new project, either with the VS Code extensions or the CLI with sfdx force:project:create --projectname MyAwesomeProject
Import your code. On the live stream we created new classes, and copy/pasted our code it. That makes sense, when you’re talking about a grand total of three classes. If your codebase is larger, though, you can use the handy Metadata api conversion tools built into sfdx. Use sfdx force:mdapi:convert -h for more information on how to use it!
At this point in our episode we made an entirely optional change to our codebase to separate our test classes from our regular classes. The key to remember is this: any and all folders underneath the folder you specify in the sfdx-project.json file’s packageDirectories/path key will be included in your package.
Next we needed to create a package. This command is only available via the command line. For our use case the command was: sfdx force:package:create -n ApexPromises -t Unlocked -d "Use the Promise pattern in Apex" -r force-app -e Those command line options like -n allow us to pass in our package name, etc. Use -h to see the full list of options available.
Next we created a package version using sfdx force:package:version:create -p ApexPromises -c -w 30 -x Note the -c -w 30 and -x flags. They calculate the code coverage( -c ), wait up to 30 minutes ( -w ) and bypass the requirement of a package password ( -x )
At this point, we have a package we can install using sfdx force:package:install -p <packageID>. However, this package is only available in staging environments.
To make our package available to production orgs we need to take one last step – promoting. To do this we used sfdx force:package:version:promote -p <packageID>