sfdx-predeploy-hook-org-env 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # sfdx-predeploy-hook-org-env
2
+
3
+ > sfdx predeploy hook to export target org details as environment variables
4
+
5
+ This is useful for [Metadata String Replacements](https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_ws_string_replace.htm) before pushing/deploying to an org (especially Scratch Orgs).
6
+
7
+ Currently supported environment variables:
8
+
9
+ | Environment Variable | Description | Example |
10
+ | ---------------------------------- | --------------------------------------- | -------------------- |
11
+ | `SFDX_TARGET_ORG_USERNAME` | The username of the target org user | john.doe@example.com |
12
+ | `SFDX_TARGET_ORG_ID` | The id of the target org | 00D7g0000006RKmEAM |
13
+ | `SFDX_TARGET_ORG_USER_EMAIL` | The email of the target org user | john.doe@gmail.com |
14
+ | `SFDX_TARGET_ORG_USER_FIRSTNAME` | The first name of the target org user | John |
15
+ | `SFDX_TARGET_ORG_USER_LASTNAME` | The last name of the target org user | Doe |
16
+ | `SFDX_TARGET_ORG_USER_DISPLAYNAME` | The display name of the target org user | John Doe |
17
+ | `SFDX_TARGET_ORG_USER_ID` | The id of the target org user | 0058F000002RfcKQAS |
18
+
19
+ > **Note**
20
+ >
21
+ > This is a minimalistic sfdx plugin.
22
+ >
23
+ > Ideally sfdx supports dynamic Metadata String Replacements with custom Bash/Node.js scripts in the future.
24
+
25
+ ## Installation
26
+
27
+ ```console
28
+ sfdx plugins install sfdx-predeploy-hook-org-env
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ Make sure your `sfdx-project.json` contains some `replacements`.
34
+
35
+ Example:
36
+
37
+ ```json
38
+ {
39
+ "packageDirectories": [
40
+ {
41
+ "path": "force-app",
42
+ "default": true
43
+ }
44
+ ],
45
+ "sourceApiVersion": "57.0",
46
+ "replacements": [
47
+ {
48
+ "filename": "*.portal-meta.xml",
49
+ "stringToReplace": "john.doe@example.com",
50
+ "replaceWithEnv": "SFDX_TARGET_ORG_USERNAME"
51
+ },
52
+ {
53
+ "filename": "*.portal-meta.xml",
54
+ "stringToReplace": "john.doe@gmail.com",
55
+ "replaceWithEnv": "SFDX_TARGET_ORG_USER_EMAIL"
56
+ }
57
+ ]
58
+ }
59
+ ```
60
+
61
+ To preview the environment variables,
62
+ set the environment variable `DEBUG=sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy`
63
+ and perform a validation deployment to the target org.
64
+
65
+ **Example**
66
+
67
+ MacOS/Linux:
68
+
69
+ ```console
70
+ $ DEBUG=sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy sfdx force source deploy --checkonly -u mytargetorg -p force-app
71
+ ```
72
+
73
+ Windows PowerShell:
74
+
75
+ ```powershell
76
+ $env:DEBUG=sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy
77
+ sfdx force source deploy --checkonly -u mytargetorg -p force-app
78
+ ```
79
+
80
+ This will output something like:
81
+
82
+ ```
83
+ ...
84
+ sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy Setting environment variables for target org +2s
85
+ sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_ID="00D7g0000006RKmEAM"
86
+ sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_USERNAME="john.doe@example.com"
87
+ sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_USER_ID="0058F000002RfcKQAS"
88
+ sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_USER_EMAIL="john.doe@gmail.com"
89
+ sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_USER_FIRSTNAME="John"
90
+ sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_USER_LASTNAME="Doe"
91
+ sfdx:sfdx-predeploy-hook-org-env:hooks:predeploy SFDX_TARGET_ORG_USER_DISPLAYNAME="John Doe" +0ms
92
+ ...
93
+ ```
@@ -0,0 +1,4 @@
1
+ {
2
+ "version": "0.0.0-development",
3
+ "commands": {}
4
+ }
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "sfdx-predeploy-hook-org-env",
3
+ "version": "1.0.0",
4
+ "description": "sfdx predeploy hook to export target org details as environment variables",
5
+ "license": "MIT",
6
+ "files": [
7
+ "/src",
8
+ "/oclif.manifest.json"
9
+ ],
10
+ "oclif": {
11
+ "bin": "sfdx",
12
+ "hooks": {
13
+ "predeploy": "./src/hook.mjs"
14
+ }
15
+ },
16
+ "dependencies": {},
17
+ "peerDependencies": {
18
+ "@oclif/core": "2.x",
19
+ "@salesforce/core": "3.x"
20
+ },
21
+ "devDependencies": {
22
+ "@oclif/core": "2.6.2",
23
+ "@salesforce/core": "3.34.6"
24
+ },
25
+ "scripts": {
26
+ "test:e2e": "bash scripts/test-e2e.sh"
27
+ },
28
+ "keywords": [
29
+ "sfdx",
30
+ "sfdx-plugin",
31
+ "sfdx-hook",
32
+ "hook"
33
+ ]
34
+ }
package/src/hook.mjs ADDED
@@ -0,0 +1,24 @@
1
+ import { getTargetOrgFromHook } from "./target-org.mjs";
2
+
3
+ export async function hook(options) {
4
+ const org = await getTargetOrgFromHook(options);
5
+ const identity = await org.getConnection().identity();
6
+ const env = {
7
+ SFDX_TARGET_ORG_ID: identity.organization_id,
8
+ SFDX_TARGET_ORG_USERNAME: identity.username,
9
+ SFDX_TARGET_ORG_USER_ID: identity.user_id,
10
+ SFDX_TARGET_ORG_USER_EMAIL: identity.email,
11
+ SFDX_TARGET_ORG_USER_FIRSTNAME: identity.first_name,
12
+ SFDX_TARGET_ORG_USER_LASTNAME: identity.last_name,
13
+ SFDX_TARGET_ORG_USER_DISPLAYNAME: identity.display_name,
14
+ };
15
+ this.debug("Setting environment variables for target org");
16
+ this.debug(
17
+ Object.entries(env)
18
+ .map(([k, v]) => `${k}="${v}"`)
19
+ .join("\n")
20
+ );
21
+ for (const [k, v] of Object.entries(env)) {
22
+ process.env[k] = v;
23
+ }
24
+ }
@@ -0,0 +1,14 @@
1
+ import { Parser } from "@oclif/core";
2
+ import { Org } from "@salesforce/core";
3
+
4
+ export async function getTargetOrgFromHook(hookOptions) {
5
+ // dynamically parse the flags for the source deploy/push/delete commands
6
+ const { flags } = await Parser.parse(hookOptions.argv, hookOptions.Command);
7
+ const aliasOrUsername = flags["target-org"] ?? flags.targetusername;
8
+ const org = aliasOrUsername
9
+ ? // use the command line flag if given
10
+ await Org.create({ aliasOrUsername })
11
+ : // fallback to SFDX_DEFAULTUSERNAME environment variable or default org
12
+ await Org.create();
13
+ return org;
14
+ }