npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sfdx-predeploy-hook-org-env

v1.0.3

Published

sfdx predeploy hook to export target org details as environment variables

Downloads

778

Readme

sfdx-predeploy-hook-org-env

sfdx predeploy hook to export target org details as environment variables

[!IMPORTANT]

This plugin currently only works with sfdx force:source:push and sfdx force:source:deploy.

The sf project deploy start command does not (yet) emit the predeploy hook.

Use Case

Example: When deploying Metadata of type Portal you'll need to provide an <admin> which is a username in the org. The Metadata String Replacements feature already supports replacing strings with values stored in a file or in an environment variable.

sfdx-project.json

{
  ...
  "replacements": [
    {
      "filename": "force-app/main/default/portals/Customer Portal.portal-meta.xml",
      "stringToReplace": "[email protected]",
      "replaceWithEnv": "ORG_USERNAME"
    }
  ]
}

Let's assume that for a Scratch Org we create, we simply want to make the default scratch org user the admin of the portal.

So in this example, we'll set the environment variable ORG_USERNAME, but first we need to get the default scratch org username from the org:

export ORG_USERNAME="$(node -pe 'JSON.parse(fs.readFileSync(0, "utf8")).result.username' < <(sfdx force:org:display -u my-target-org --json))"
sfdx force source deploy -p "force-app/main/default/portals/Customer Portal.portal-meta.xml" -u my-target-org

Although this works just fine, we need to remember to set this environment variable before deploying or pushing.

Note

For common things like username, email address, org id, it would be handy to have these environment variables with target specific values built-in.

And this is exactly what this sfdx plugin does!

Installation

sfdx plugins install sfdx-predeploy-hook-org-env

Usage

Once you've installed this sfdx plugin, you can immediately use the following environment variables mentioned for the Metadata String Replacements:

| Environment Variable | Description | Example | | ---------------------------------- | --------------------------------------- | -------------------- | | SFDX_TARGET_ORG_USERNAME | The username of the target org user | [email protected] | | SFDX_TARGET_ORG_ID | The id of the target org | 00D7g0000006RKmEAM | | SFDX_TARGET_ORG_USER_EMAIL | The email of the target org user | [email protected] | | SFDX_TARGET_ORG_USER_FIRSTNAME | The first name of the target org user | John | | SFDX_TARGET_ORG_USER_LASTNAME | The last name of the target org user | Doe | | SFDX_TARGET_ORG_USER_DISPLAYNAME | The display name of the target org user | John Doe | | SFDX_TARGET_ORG_USER_ID | The id of the target org user | 0058F000002RfcKQAS |

Note

Do you have ideas for other target specific values which could be useful?

Please get in touch by creating an issue.

Example:

Make sure your sfdx-project.json contains some replacements using one of the environment variables listed above.

sfdx-project.json:

{
  "packageDirectories": [
    {
      "path": "force-app",
      "default": true
    }
  ],
  "replacements": [
    {
      "filename": "force-app/main/default/portals/Customer Portal.portal-meta.xml",
      "stringToReplace": "[email protected]",
      "replaceWithEnv": "SFDX_TARGET_ORG_USERNAME"
    },
    {
      "filename": "force-app/main/default/portals/Customer Portal.portal-meta.xml",
      "stringToReplace": "[email protected]",
      "replaceWithEnv": "SFDX_TARGET_ORG_USER_EMAIL"
    }
  ],
  "sourceApiVersion": "57.0"
}

Now you can run sfdx force source push or sfdx force source deploy and the Metadata replacement will automatically use the dynamically generated environment variables for the given target org:

sfdx force source deploy -p "Portal:Customer Portal" -u my-target-org1
sfdx force source deploy -p "Portal:Customer Portal" -u my-target-org2
sfdx force source deploy -p "Portal:Customer Portal" -u my-target-org3

Debugging

To preview the environment variables, set the environment variable DEBUG to *:sfdx-predeploy-hook-org-env:* and perform a validation deployment to the target org.

Example

MacOS/Linux:

$ DEBUG="*:sfdx-predeploy-hook-org-env:*" sfdx force source deploy --checkonly -u mytargetorg -p force-app

Windows PowerShell:

$env:DEBUG="*:sfdx-predeploy-hook-org-env:*"
sfdx force source deploy --checkonly -u mytargetorg -p force-app

This will output something like:

...
sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy Setting environment variables for target org +2s
sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_ID="00D7g0000006RKmEAM"
sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_USERNAME="[email protected]"
sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_USER_ID="0058F000002RfcKQAS"
sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_USER_EMAIL="[email protected]"
sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_USER_FIRSTNAME="John"
sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_USER_LASTNAME="Doe"
sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_USER_DISPLAYNAME="John Doe" +0ms
...