@salesforce/ui-bundle-template-feature-react-search 11.3.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/LICENSE.txt +82 -0
- package/README.md +692 -0
- package/dist/.forceignore +15 -0
- package/dist/.husky/pre-commit +4 -0
- package/dist/.prettierignore +11 -0
- package/dist/.prettierrc +17 -0
- package/dist/CHANGELOG.md +3499 -0
- package/dist/README.md +28 -0
- package/dist/config/project-scratch-def.json +13 -0
- package/dist/eslint.config.js +7 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/.forceignore +15 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/.graphqlrc.yml +2 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/.prettierignore +9 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/.prettierrc +11 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/CHANGELOG.md +10 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/README.md +75 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/codegen.yml +95 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/components.json +18 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/e2e/app.spec.ts +17 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/eslint.config.js +169 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/feature-react-search.uibundle-meta.xml +7 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/index.html +12 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/package.json +76 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/playwright.config.ts +24 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/scripts/get-graphql-schema.mjs +71 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/scripts/rewrite-e2e-assets.mjs +23 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/api/graphqlClient.ts +44 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/app.tsx +17 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/appLayout.tsx +83 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/assets/icons/book.svg +3 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/assets/icons/copy.svg +4 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/assets/icons/rocket.svg +3 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/assets/icons/star.svg +3 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/assets/images/codey-1.png +0 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/assets/images/codey-2.png +0 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/assets/images/codey-3.png +0 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/assets/images/vibe-codey.svg +194 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/alerts/status-alert.tsx +52 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/layouts/card-layout.tsx +29 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/alert.tsx +76 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/avatar.tsx +109 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/badge.tsx +48 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/breadcrumb.tsx +109 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/button.tsx +67 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/calendar.tsx +232 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/card.tsx +103 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/checkbox.tsx +32 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/collapsible.tsx +33 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/datePicker.tsx +127 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/dialog.tsx +162 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/dropdown-menu.tsx +257 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/field.tsx +237 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/index.ts +109 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/input.tsx +19 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/label.tsx +22 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/pagination.tsx +132 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/popover.tsx +89 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/select.tsx +193 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/separator.tsx +26 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/skeleton.tsx +14 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/sonner.tsx +20 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/spinner.tsx +16 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/table.tsx +114 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/components/ui/tabs.tsx +88 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/api/distinctValuesService.ts +84 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/api/searchService.ts +71 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/Search.tsx +160 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/SearchResults.tsx +77 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/SourceSection.tsx +167 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/controls/PaginationControls.tsx +115 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/controls/ScopeSelector.tsx +55 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/controls/SearchBar.tsx +55 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/controls/SortControl.tsx +62 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/filters/ActiveFilters.tsx +61 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/filters/DefaultFilterPanel.tsx +122 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/filters/FilterContext.tsx +70 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/filters/inputs/BooleanFilter.tsx +50 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/filters/inputs/DateRangeFilter.tsx +50 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/filters/inputs/FilterFieldWrapper.tsx +26 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/filters/inputs/MultiSelectFilter.tsx +47 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/filters/inputs/NumericRangeFilter.tsx +78 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/filters/inputs/SelectFilter.tsx +46 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/filters/inputs/TextFilter.tsx +52 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/results/DefaultResultRow.tsx +109 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/config.json +82 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/hooks/useAsyncData.ts +56 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/hooks/useDistinctValues.ts +59 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/hooks/useSearch.ts +442 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/index.ts +80 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/loadConfig.ts +14 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/queryBuilder.ts +156 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/types.ts +169 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/utils/debounce.ts +17 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/utils/fieldUtils.ts +14 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/utils/filterUtils.ts +272 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/features/search/utils/sortUtils.ts +34 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/hooks/useAsyncData.ts +67 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/lib/utils.ts +6 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/navigationMenu.tsx +80 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/pages/Home.tsx +11 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/pages/NotFound.tsx +18 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/router-utils.tsx +35 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/routes.tsx +22 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/src/styles/global.css +135 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/tsconfig.json +45 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/tsconfig.node.json +13 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/ui-bundle.json +7 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/vite-env.d.ts +4 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/vite.config.ts +106 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/vitest-env.d.ts +2 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/vitest.config.ts +11 -0
- package/dist/force-app/main/default/uiBundles/feature-react-search/vitest.setup.ts +1 -0
- package/dist/jest.config.js +6 -0
- package/dist/package-lock.json +9995 -0
- package/dist/package.json +44 -0
- package/dist/scripts/apex/hello.apex +10 -0
- package/dist/scripts/gitignore-templates.json +4 -0
- package/dist/scripts/graphql-search.sh +191 -0
- package/dist/scripts/org-setup-config-schema.mjs +96 -0
- package/dist/scripts/org-setup.config.json +5 -0
- package/dist/scripts/org-setup.mjs +1392 -0
- package/dist/scripts/sf-project-setup.mjs +103 -0
- package/dist/scripts/soql/account.soql +6 -0
- package/dist/scripts/validate-org-setup-config.mjs +38 -0
- package/dist/sfdx-project.json +12 -0
- package/package.json +51 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/__inherit__appLayout.tsx +9 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/components/ui/__inherit__alert.tsx +39 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/components/ui/__inherit__button.tsx +45 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/components/ui/__inherit__checkbox.tsx +8 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/components/ui/__inherit__input.tsx +5 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/components/ui/__inherit__label.tsx +8 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/components/ui/__inherit__pagination.tsx +47 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/components/ui/__inherit__select.tsx +57 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/components/ui/__inherit__skeleton.tsx +5 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/api/distinctValuesService.ts +84 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/api/searchService.ts +71 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/Search.tsx +160 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/SearchResults.tsx +77 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/SourceSection.tsx +167 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/controls/PaginationControls.tsx +115 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/controls/ScopeSelector.tsx +55 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/controls/SearchBar.tsx +55 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/controls/SortControl.tsx +62 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/filters/ActiveFilters.tsx +61 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/filters/DefaultFilterPanel.tsx +122 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/filters/FilterContext.tsx +70 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/filters/inputs/BooleanFilter.tsx +50 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/filters/inputs/DateRangeFilter.tsx +50 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/filters/inputs/FilterFieldWrapper.tsx +26 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/filters/inputs/MultiSelectFilter.tsx +47 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/filters/inputs/NumericRangeFilter.tsx +78 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/filters/inputs/SelectFilter.tsx +46 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/filters/inputs/TextFilter.tsx +52 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/components/results/DefaultResultRow.tsx +109 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/config.json +82 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/hooks/useAsyncData.ts +56 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/hooks/useDistinctValues.ts +59 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/hooks/useSearch.ts +442 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/index.ts +80 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/loadConfig.ts +14 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/queryBuilder.ts +156 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/types.ts +169 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/utils/debounce.ts +17 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/utils/fieldUtils.ts +14 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/utils/filterUtils.ts +272 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/features/search/utils/sortUtils.ts +34 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/pages/Home.tsx +11 -0
- package/src/force-app/main/default/uiBundles/feature-react-search/src/routes.tsx +10 -0
|
@@ -0,0 +1,1392 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* One-command setup: login, deploy, optional permset/data, GraphQL schema/codegen, UI bundle build.
|
|
4
|
+
* Use this script to make setup easier for each app generated from this template.
|
|
5
|
+
*
|
|
6
|
+
* Usage:
|
|
7
|
+
* node scripts/org-setup.mjs --target-org <alias> # interactive step picker (all selected)
|
|
8
|
+
* node scripts/org-setup.mjs --target-org <alias> --yes # skip picker, run all steps
|
|
9
|
+
* node scripts/org-setup.mjs --target-org afv5 --skip-login
|
|
10
|
+
* node scripts/org-setup.mjs --target-org afv5 --skip-data --skip-ui-bundle-build
|
|
11
|
+
* node scripts/org-setup.mjs --target-org myorg --ui-bundle-name my-app
|
|
12
|
+
*
|
|
13
|
+
* Steps (in order):
|
|
14
|
+
* 1. login — sf org login web only if org not already connected (skip with --skip-login)
|
|
15
|
+
* 2. uiBundle — (all UI bundles) npm install && npm run build so dist exists for deploy (skip with --skip-ui-bundle-build)
|
|
16
|
+
* 3. deploy — sf project deploy start --target-org <alias> (requires dist for entity deployment)
|
|
17
|
+
* 4. permset — assign permsets per org-setup.config.json (skip with --skip-permset; override via --permset-name)
|
|
18
|
+
* 5. data — prepare unique fields + sf data import tree (skipped if no data dir/plan)
|
|
19
|
+
* 6. graphql — (in UI bundle) npm run graphql:schema then npm run graphql:codegen
|
|
20
|
+
* 7. dev — (in UI bundle) npm run dev — launch dev server (skip with --skip-dev)
|
|
21
|
+
*
|
|
22
|
+
* Permset assignment config (scripts/org-setup.config.json):
|
|
23
|
+
* {
|
|
24
|
+
* "permsetAssignments": {
|
|
25
|
+
* "defaultAssignee": "skip",
|
|
26
|
+
* "assignments": {
|
|
27
|
+
* "My_Permset": { "assignee": "currentUser" },
|
|
28
|
+
* "Guest_Permset": { "assignee": "guestUser" },
|
|
29
|
+
* "Internal_Only": { "assignee": "skip" }
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
* }
|
|
33
|
+
* Assignee values: "currentUser", "guestUser", or "skip". For "guestUser" the
|
|
34
|
+
* site is derived from the single networks/<siteName>.network-meta.xml the app
|
|
35
|
+
* ships — it is not restated per assignment.
|
|
36
|
+
* Unlisted permsets resolve to "defaultAssignee" (default "skip").
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
import { spawnSync, spawn as nodeSpawn } from 'node:child_process';
|
|
40
|
+
import { resolve, dirname } from 'node:path';
|
|
41
|
+
import { fileURLToPath } from 'node:url';
|
|
42
|
+
import { readdirSync, existsSync, readFileSync, writeFileSync, unlinkSync } from 'node:fs';
|
|
43
|
+
|
|
44
|
+
import { validateConfig } from './org-setup-config-schema.mjs';
|
|
45
|
+
|
|
46
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
47
|
+
const ROOT = resolve(__dirname, '..');
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Thrown by step runners (run/runAsync) when a subprocess fails. The per-step
|
|
51
|
+
* orchestration in main() catches it, records the failure in the result ledger,
|
|
52
|
+
* and either aborts (fail-fast steps) or continues (skippable steps).
|
|
53
|
+
*/
|
|
54
|
+
class StepError extends Error {}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* npm strips .gitignore from published packages — generate them on first run.
|
|
58
|
+
* Templates are stored in scripts/gitignore-templates.json (generated at build
|
|
59
|
+
* time from the actual .gitignore files) so the content lives in one place.
|
|
60
|
+
* The JSON may not exist in git-cloned distributions where .gitignore is
|
|
61
|
+
* already present, so loading is best-effort.
|
|
62
|
+
*/
|
|
63
|
+
function loadGitignoreTemplates() {
|
|
64
|
+
const templatesPath = resolve(__dirname, 'gitignore-templates.json');
|
|
65
|
+
if (!existsSync(templatesPath)) return null;
|
|
66
|
+
try {
|
|
67
|
+
return JSON.parse(readFileSync(templatesPath, 'utf8'));
|
|
68
|
+
} catch {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function ensureGitignore(dir, content) {
|
|
74
|
+
if (!content) return;
|
|
75
|
+
const gitignorePath = resolve(dir, '.gitignore');
|
|
76
|
+
if (!existsSync(gitignorePath)) {
|
|
77
|
+
writeFileSync(gitignorePath, content, 'utf8');
|
|
78
|
+
console.log(`Created .gitignore in ${dir}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function resolveSfdxSource() {
|
|
83
|
+
const sfdxPath = resolve(ROOT, 'sfdx-project.json');
|
|
84
|
+
if (!existsSync(sfdxPath)) {
|
|
85
|
+
console.error('Error: sfdx-project.json not found at project root.');
|
|
86
|
+
process.exit(1);
|
|
87
|
+
}
|
|
88
|
+
const sfdxProject = JSON.parse(readFileSync(sfdxPath, 'utf8'));
|
|
89
|
+
const pkgDir = sfdxProject?.packageDirectories?.[0]?.path;
|
|
90
|
+
if (!pkgDir) {
|
|
91
|
+
console.error('Error: No packageDirectories[].path found in sfdx-project.json.');
|
|
92
|
+
process.exit(1);
|
|
93
|
+
}
|
|
94
|
+
return resolve(ROOT, pkgDir, 'main', 'default');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const SFDX_SOURCE = resolveSfdxSource();
|
|
98
|
+
const UIBUNDLES_DIR = resolve(SFDX_SOURCE, 'uiBundles');
|
|
99
|
+
const DATA_DIR = resolve(SFDX_SOURCE, 'data');
|
|
100
|
+
const DATA_PLAN = resolve(SFDX_SOURCE, 'data/data-plan.json');
|
|
101
|
+
|
|
102
|
+
function parseArgs() {
|
|
103
|
+
const args = process.argv.slice(2);
|
|
104
|
+
let targetOrg = null;
|
|
105
|
+
let uiBundleName = null;
|
|
106
|
+
/** If non-empty, only these names are assigned; otherwise all discovered from the project. */
|
|
107
|
+
const permsetNamesExplicit = [];
|
|
108
|
+
let yes = false;
|
|
109
|
+
const flags = {
|
|
110
|
+
skipLogin: false,
|
|
111
|
+
skipDeploy: false,
|
|
112
|
+
skipPermset: false,
|
|
113
|
+
skipRole: false,
|
|
114
|
+
skipData: false,
|
|
115
|
+
skipGraphql: false,
|
|
116
|
+
skipUIBundleBuild: false,
|
|
117
|
+
skipSelfReg: false,
|
|
118
|
+
skipDev: false,
|
|
119
|
+
};
|
|
120
|
+
for (let i = 0; i < args.length; i++) {
|
|
121
|
+
if (args[i] === '--target-org' && args[i + 1]) {
|
|
122
|
+
targetOrg = args[++i];
|
|
123
|
+
} else if (args[i] === '--ui-bundle-name' && args[i + 1]) {
|
|
124
|
+
uiBundleName = args[++i];
|
|
125
|
+
} else if (args[i] === '--permset-name' && args[i + 1]) {
|
|
126
|
+
permsetNamesExplicit.push(args[++i]);
|
|
127
|
+
} else if (args[i] === '--skip-login') flags.skipLogin = true;
|
|
128
|
+
else if (args[i] === '--skip-deploy') flags.skipDeploy = true;
|
|
129
|
+
else if (args[i] === '--skip-permset') flags.skipPermset = true;
|
|
130
|
+
else if (args[i] === '--skip-role') flags.skipRole = true;
|
|
131
|
+
else if (args[i] === '--skip-data') flags.skipData = true;
|
|
132
|
+
else if (args[i] === '--skip-self-reg') flags.skipSelfReg = true;
|
|
133
|
+
else if (args[i] === '--skip-graphql') flags.skipGraphql = true;
|
|
134
|
+
else if (args[i] === '--skip-ui-bundle-build') flags.skipUIBundleBuild = true;
|
|
135
|
+
else if (args[i] === '--skip-dev') flags.skipDev = true;
|
|
136
|
+
else if (args[i] === '--yes' || args[i] === '-y') yes = true;
|
|
137
|
+
else if (args[i] === '--help' || args[i] === '-h') {
|
|
138
|
+
console.log(`
|
|
139
|
+
Setup CLI — one-command setup for apps in this project
|
|
140
|
+
|
|
141
|
+
Usage:
|
|
142
|
+
node scripts/org-setup.mjs --target-org <alias> [options]
|
|
143
|
+
|
|
144
|
+
Required:
|
|
145
|
+
--target-org <alias> Target Salesforce org alias (e.g. myorg)
|
|
146
|
+
|
|
147
|
+
Options:
|
|
148
|
+
--ui-bundle-name <name> UI bundle folder name under uiBundles/ (default: auto-detect)
|
|
149
|
+
--permset-name <name> Assign only this permission set (repeatable). Default: all sets under permissionsets/
|
|
150
|
+
--skip-login Skip login step (login is auto-skipped if org is already connected)
|
|
151
|
+
--skip-deploy Do not deploy metadata
|
|
152
|
+
--skip-permset Do not assign permission set
|
|
153
|
+
--skip-data Do not prepare data or run data import
|
|
154
|
+
--skip-graphql Do not fetch schema or run GraphQL codegen
|
|
155
|
+
--skip-ui-bundle-build Do not npm install / build the UI bundle
|
|
156
|
+
--skip-dev Do not launch the dev server at the end
|
|
157
|
+
-y, --yes Skip interactive step picker; run all enabled steps immediately
|
|
158
|
+
-h, --help Show this help
|
|
159
|
+
|
|
160
|
+
Permset config (scripts/org-setup.config.json):
|
|
161
|
+
Control per-permset assignment via a config file. Example:
|
|
162
|
+
{
|
|
163
|
+
"permsetAssignments": {
|
|
164
|
+
"defaultAssignee": "skip",
|
|
165
|
+
"assignments": {
|
|
166
|
+
"My_Permset": { "assignee": "currentUser" },
|
|
167
|
+
"Guest_Permset": { "assignee": "guestUser" },
|
|
168
|
+
"Internal_Only": { "assignee": "skip" }
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
Assignee values: "currentUser", "guestUser", or "skip". For "guestUser" the site
|
|
173
|
+
is derived from the single networks/<siteName>.network-meta.xml the app ships.
|
|
174
|
+
Unlisted permsets resolve to "defaultAssignee" (default "skip").
|
|
175
|
+
`);
|
|
176
|
+
process.exit(0);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (!targetOrg) {
|
|
180
|
+
console.error('Error: --target-org <alias> is required.');
|
|
181
|
+
process.exit(1);
|
|
182
|
+
}
|
|
183
|
+
return { targetOrg, uiBundleName, permsetNamesExplicit, yes, ...flags };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function discoverAllUIBundleDirs(uiBundleName) {
|
|
187
|
+
if (!existsSync(UIBUNDLES_DIR)) {
|
|
188
|
+
console.error(`Error: uiBundles directory not found: ${UIBUNDLES_DIR}`);
|
|
189
|
+
process.exit(1);
|
|
190
|
+
}
|
|
191
|
+
const entries = readdirSync(UIBUNDLES_DIR, { withFileTypes: true });
|
|
192
|
+
const dirs = entries.filter((e) => e.isDirectory() && !e.name.startsWith('.'));
|
|
193
|
+
if (dirs.length === 0) {
|
|
194
|
+
console.error(`Error: No UI bundle folder found under ${UIBUNDLES_DIR}`);
|
|
195
|
+
process.exit(1);
|
|
196
|
+
}
|
|
197
|
+
if (uiBundleName) {
|
|
198
|
+
const requested = dirs.find((d) => d.name === uiBundleName);
|
|
199
|
+
if (!requested) {
|
|
200
|
+
console.error(`Error: UI bundle directory not found: ${uiBundleName}`);
|
|
201
|
+
process.exit(1);
|
|
202
|
+
}
|
|
203
|
+
return [resolve(UIBUNDLES_DIR, requested.name)];
|
|
204
|
+
}
|
|
205
|
+
return dirs.map((d) => resolve(UIBUNDLES_DIR, d.name));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function discoverUIBundleDir(uiBundleName) {
|
|
209
|
+
const all = discoverAllUIBundleDirs(uiBundleName);
|
|
210
|
+
if (all.length > 1 && !uiBundleName) {
|
|
211
|
+
console.log(`Multiple UI bundles found; using first: ${all[0].split(/[/\\]/).pop()}`);
|
|
212
|
+
}
|
|
213
|
+
return all[0];
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** API names from permissionsets/*.permissionset-meta.xml in the first package directory. */
|
|
217
|
+
function discoverPermissionSetNames() {
|
|
218
|
+
const dir = resolve(SFDX_SOURCE, 'permissionsets');
|
|
219
|
+
if (!existsSync(dir)) return [];
|
|
220
|
+
const names = [];
|
|
221
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
222
|
+
if (!entry.isFile()) continue;
|
|
223
|
+
const m = entry.name.match(/^(.+)\.permissionset-meta\.xml$/);
|
|
224
|
+
if (m) names.push(m[1]);
|
|
225
|
+
}
|
|
226
|
+
return names.sort();
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const CONFIG_PATH = resolve(__dirname, 'org-setup.config.json');
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Read + validate org-setup.config.json ONCE, against the shared zod schema
|
|
233
|
+
* (the same `validateConfig` the build/CI gate uses). Exits non-zero with the
|
|
234
|
+
* precise zod issues if the config is invalid — before any step runs.
|
|
235
|
+
*
|
|
236
|
+
* Returns the validated config object (zod defaults applied), or an empty
|
|
237
|
+
* object when the file is absent (every section is optional).
|
|
238
|
+
*
|
|
239
|
+
* This is the single source of truth: loadPermsetConfig / loadRoleConfig /
|
|
240
|
+
* loadSelfRegConfig all read from the object it returns, so they no longer
|
|
241
|
+
* parse or defensively swallow errors.
|
|
242
|
+
*/
|
|
243
|
+
function loadValidatedConfig() {
|
|
244
|
+
if (!existsSync(CONFIG_PATH)) return {};
|
|
245
|
+
const result = validateConfig(readFileSync(CONFIG_PATH, 'utf8'), CONFIG_PATH);
|
|
246
|
+
if (!result.ok) {
|
|
247
|
+
console.error('Invalid org-setup.config.json:');
|
|
248
|
+
for (const err of result.errors) console.error(` - ${err}`);
|
|
249
|
+
process.exit(1);
|
|
250
|
+
}
|
|
251
|
+
return result.data;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Derive the site name from the single networks/<siteName>.network-meta.xml the
|
|
256
|
+
* app ships. An app ships exactly one site, so the site name is derivable from
|
|
257
|
+
* deployed metadata rather than restated per-assignment (spec §5.2). Returns
|
|
258
|
+
* null when there is no networks dir or no .network-meta.xml file.
|
|
259
|
+
*/
|
|
260
|
+
function deriveSiteName() {
|
|
261
|
+
const networksDir = resolve(SFDX_SOURCE, 'networks');
|
|
262
|
+
if (!existsSync(networksDir)) return null;
|
|
263
|
+
const files = readdirSync(networksDir)
|
|
264
|
+
.filter((f) => f.endsWith('.network-meta.xml'))
|
|
265
|
+
.sort();
|
|
266
|
+
if (files.length === 0) return null;
|
|
267
|
+
// An app ships exactly one site; if a developer added a second, derivation is
|
|
268
|
+
// ambiguous — fail loudly rather than silently bind to an arbitrary site.
|
|
269
|
+
if (files.length > 1) {
|
|
270
|
+
throw new StepError(
|
|
271
|
+
`cannot derive guest site: multiple network metadata files found in ${networksDir} (${files.join(', ')}); guestUser assignment requires exactly one`,
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
return files[0].replace(/\.network-meta\.xml$/, '');
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Permset assignment configuration, read from the already-validated config.
|
|
279
|
+
*
|
|
280
|
+
* Config shape:
|
|
281
|
+
* {
|
|
282
|
+
* "permsetAssignments": {
|
|
283
|
+
* "defaultAssignee": "skip",
|
|
284
|
+
* "assignments": {
|
|
285
|
+
* "My_Permset": { "assignee": "currentUser" },
|
|
286
|
+
* "My_Guest_Permset": { "assignee": "guestUser" },
|
|
287
|
+
* "Internal_Only": { "assignee": "skip" }
|
|
288
|
+
* }
|
|
289
|
+
* }
|
|
290
|
+
* }
|
|
291
|
+
*
|
|
292
|
+
* Assignee values:
|
|
293
|
+
* "currentUser" — assign to the user running the script
|
|
294
|
+
* "skip" — do not assign this permset
|
|
295
|
+
* "guestUser" — resolve the site guest user automatically (site derived from
|
|
296
|
+
* the single networks/<siteName>.network-meta.xml the app ships)
|
|
297
|
+
*
|
|
298
|
+
* Unlisted permsets resolve to `defaultAssignee` (default "skip").
|
|
299
|
+
*
|
|
300
|
+
* Returns { defaultAssignee: string, assignments: Record<string, { assignee: string }> }
|
|
301
|
+
*/
|
|
302
|
+
function loadPermsetConfig(config) {
|
|
303
|
+
const section = config.permsetAssignments;
|
|
304
|
+
if (!section) return { defaultAssignee: 'skip', assignments: {} };
|
|
305
|
+
return {
|
|
306
|
+
defaultAssignee: section.defaultAssignee,
|
|
307
|
+
assignments: section.assignments,
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Resolve the effective assignment config for a given permset name. */
|
|
312
|
+
function resolveAssignment(permsetName, permsetConfig) {
|
|
313
|
+
const override = permsetConfig.assignments[permsetName];
|
|
314
|
+
if (!override) return { assignee: permsetConfig.defaultAssignee };
|
|
315
|
+
return { assignee: override.assignee };
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Role assignment config, read from the already-validated config.
|
|
320
|
+
*
|
|
321
|
+
* Config shape:
|
|
322
|
+
* { "role": { "assignee": "currentUser", "roleName": "Admin" } }
|
|
323
|
+
*
|
|
324
|
+
* Returns null if no "role" section exists in config (the step is hidden).
|
|
325
|
+
*/
|
|
326
|
+
function loadRoleConfig(config) {
|
|
327
|
+
const section = config.role;
|
|
328
|
+
if (!section) return null;
|
|
329
|
+
return {
|
|
330
|
+
assignee: section.assignee,
|
|
331
|
+
roleName: section.roleName,
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Self-registration config, read from the already-validated config. The site is
|
|
337
|
+
* NOT stored here — it is derived from the single
|
|
338
|
+
* networks/<siteName>.network-meta.xml the app ships (spec §5.2), exactly like
|
|
339
|
+
* the guestUser permset path. deriveSiteName() is called lazily inside the
|
|
340
|
+
* selfReg step body so its "multiple network files" StepError is recorded in
|
|
341
|
+
* the ledger rather than escaping config load.
|
|
342
|
+
*
|
|
343
|
+
* Config shape:
|
|
344
|
+
* {
|
|
345
|
+
* "selfRegistration": {
|
|
346
|
+
* "selfRegProfile": "myapp Profile",
|
|
347
|
+
* "accountName": "My Self-Reg Account"
|
|
348
|
+
* }
|
|
349
|
+
* }
|
|
350
|
+
*
|
|
351
|
+
* Returns null if no "selfRegistration" section exists in config (the step is hidden).
|
|
352
|
+
*/
|
|
353
|
+
function loadSelfRegConfig(config) {
|
|
354
|
+
const section = config.selfRegistration;
|
|
355
|
+
if (!section) return null;
|
|
356
|
+
return {
|
|
357
|
+
selfRegProfile: section.selfRegProfile,
|
|
358
|
+
accountName: section.accountName,
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Ensure the self-registration profile is listed in networkMemberGroups.
|
|
364
|
+
* This must happen BEFORE the initial deploy so that the profile is a recognised
|
|
365
|
+
* site member when subsequent steps (selfRegProfile, selfRegistration=true) are deployed.
|
|
366
|
+
*/
|
|
367
|
+
function ensureNetworkMemberProfile(selfRegConfig, siteName) {
|
|
368
|
+
const { selfRegProfile } = selfRegConfig;
|
|
369
|
+
if (!siteName || !selfRegProfile) return;
|
|
370
|
+
|
|
371
|
+
const networkXmlPath = resolve(SFDX_SOURCE, 'networks', `${siteName}.network-meta.xml`);
|
|
372
|
+
if (!existsSync(networkXmlPath)) {
|
|
373
|
+
console.log(` Network metadata not found: ${networkXmlPath}; skipping member group update.`);
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
const xml = readFileSync(networkXmlPath, 'utf8');
|
|
377
|
+
|
|
378
|
+
// Check if profile is already in networkMemberGroups
|
|
379
|
+
const profileEscaped = selfRegProfile.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
380
|
+
const profileRegex = new RegExp(`<profile>\\s*${profileEscaped}\\s*</profile>`);
|
|
381
|
+
if (profileRegex.test(xml)) {
|
|
382
|
+
console.log(` Profile "${selfRegProfile}" already in networkMemberGroups; no update needed.`);
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// Add the profile to networkMemberGroups
|
|
387
|
+
const updatedXml = xml.replace(
|
|
388
|
+
/(<networkMemberGroups>)/,
|
|
389
|
+
`$1\n <profile>${selfRegProfile}</profile>`
|
|
390
|
+
);
|
|
391
|
+
writeFileSync(networkXmlPath, updatedXml);
|
|
392
|
+
console.log(` Added profile "${selfRegProfile}" to networkMemberGroups in ${siteName}.network-meta.xml`);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Enable self-registration for an Experience Cloud network.
|
|
397
|
+
*
|
|
398
|
+
* 1. Modify the network metadata XML to set selfRegistration=true and add selfRegProfile.
|
|
399
|
+
* 2. Re-deploy the modified network metadata.
|
|
400
|
+
* 3. Create an Account record (idempotent).
|
|
401
|
+
* 4. Create a NetworkSelfRegistration record linking the Account to the Network (idempotent).
|
|
402
|
+
*/
|
|
403
|
+
function enableSelfRegistration(selfRegConfig, siteName, targetOrg) {
|
|
404
|
+
const { selfRegProfile, accountName } = selfRegConfig;
|
|
405
|
+
|
|
406
|
+
// 1. Modify network metadata XML
|
|
407
|
+
const networkXmlPath = resolve(SFDX_SOURCE, 'networks', `${siteName}.network-meta.xml`);
|
|
408
|
+
if (!existsSync(networkXmlPath)) {
|
|
409
|
+
throw new StepError(`network metadata not found: ${networkXmlPath}`);
|
|
410
|
+
}
|
|
411
|
+
const xml = readFileSync(networkXmlPath, 'utf8');
|
|
412
|
+
|
|
413
|
+
// Skip network modification and deploy if self-registration is already configured
|
|
414
|
+
const alreadyEnabled = /<selfRegistration>true<\/selfRegistration>/.test(xml);
|
|
415
|
+
const alreadyHasProfile = /<selfRegProfile>/.test(xml);
|
|
416
|
+
if (alreadyEnabled || alreadyHasProfile) {
|
|
417
|
+
console.log(` Network "${siteName}" already has self-registration configured; skipping metadata update and deploy.`);
|
|
418
|
+
} else {
|
|
419
|
+
// Set selfRegistration to true and add selfRegProfile
|
|
420
|
+
let updatedXml = xml.replace(
|
|
421
|
+
/<selfRegistration>false<\/selfRegistration>/,
|
|
422
|
+
'<selfRegistration>true</selfRegistration>'
|
|
423
|
+
);
|
|
424
|
+
updatedXml = updatedXml.replace(
|
|
425
|
+
/(\s*)(<selfRegistration>)/,
|
|
426
|
+
`$1<selfRegProfile>${selfRegProfile}</selfRegProfile>\n$1$2`
|
|
427
|
+
);
|
|
428
|
+
|
|
429
|
+
writeFileSync(networkXmlPath, updatedXml);
|
|
430
|
+
console.log(` Updated ${siteName}.network-meta.xml: selfRegistration=true, selfRegProfile=${selfRegProfile}`);
|
|
431
|
+
|
|
432
|
+
// Re-deploy only the network file
|
|
433
|
+
const deployResult = spawnSync('sf', [
|
|
434
|
+
'project', 'deploy', 'start',
|
|
435
|
+
'--target-org', targetOrg,
|
|
436
|
+
'--source-dir', networkXmlPath,
|
|
437
|
+
], { cwd: ROOT, stdio: 'inherit', shell: true, timeout: 120000 });
|
|
438
|
+
if (deployResult.status !== 0) {
|
|
439
|
+
throw new StepError(`failed to deploy updated network metadata (exit ${deployResult.status ?? 1})`);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// 3. Create Account (idempotent)
|
|
444
|
+
const acctQuery = `SELECT Id FROM Account WHERE Name = '${accountName.replace(/'/g, "\\'")}' LIMIT 1`;
|
|
445
|
+
const acctQueryResult = spawnSync('sf', [
|
|
446
|
+
'data', 'query',
|
|
447
|
+
'--query', acctQuery,
|
|
448
|
+
'--target-org', targetOrg,
|
|
449
|
+
'--json',
|
|
450
|
+
], { cwd: ROOT, encoding: 'utf8' });
|
|
451
|
+
let accountId = null;
|
|
452
|
+
if (acctQueryResult.status === 0) {
|
|
453
|
+
try {
|
|
454
|
+
const json = JSON.parse(acctQueryResult.stdout);
|
|
455
|
+
accountId = json.result?.records?.[0]?.Id || null;
|
|
456
|
+
} catch { /* proceed to create */ }
|
|
457
|
+
}
|
|
458
|
+
if (accountId) {
|
|
459
|
+
console.log(` Account "${accountName}" already exists (${accountId}); skipping creation.`);
|
|
460
|
+
} else {
|
|
461
|
+
const createResult = spawnSync('sf', [
|
|
462
|
+
'data', 'create', 'record',
|
|
463
|
+
'--sobject', 'Account',
|
|
464
|
+
'--values', `Name='${accountName}'`,
|
|
465
|
+
'--target-org', targetOrg,
|
|
466
|
+
'--json',
|
|
467
|
+
], { cwd: ROOT, encoding: 'utf8' });
|
|
468
|
+
if (createResult.status !== 0) {
|
|
469
|
+
if (createResult.stderr) console.error(createResult.stderr);
|
|
470
|
+
throw new StepError(`failed to create Account "${accountName}"`);
|
|
471
|
+
}
|
|
472
|
+
try {
|
|
473
|
+
const json = JSON.parse(createResult.stdout);
|
|
474
|
+
accountId = json.result?.id;
|
|
475
|
+
console.log(` Created Account "${accountName}" (${accountId}).`);
|
|
476
|
+
} catch {
|
|
477
|
+
throw new StepError('failed to parse Account creation result');
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// 4. Query Network Id
|
|
482
|
+
const netQuery = `SELECT Id FROM Network WHERE Name = '${siteName}'`;
|
|
483
|
+
const netResult = spawnSync('sf', [
|
|
484
|
+
'data', 'query',
|
|
485
|
+
'--query', netQuery,
|
|
486
|
+
'--target-org', targetOrg,
|
|
487
|
+
'--json',
|
|
488
|
+
], { cwd: ROOT, encoding: 'utf8' });
|
|
489
|
+
let networkId = null;
|
|
490
|
+
if (netResult.status === 0) {
|
|
491
|
+
try {
|
|
492
|
+
const json = JSON.parse(netResult.stdout);
|
|
493
|
+
networkId = json.result?.records?.[0]?.Id || null;
|
|
494
|
+
} catch { /* fall through */ }
|
|
495
|
+
}
|
|
496
|
+
if (!networkId) {
|
|
497
|
+
throw new StepError(`could not find Network "${siteName}" in org`);
|
|
498
|
+
}
|
|
499
|
+
console.log(` Found Network "${siteName}" (${networkId}).`);
|
|
500
|
+
|
|
501
|
+
// 5. Create NetworkSelfRegistration (idempotent)
|
|
502
|
+
const nsrQuery = `SELECT Id FROM NetworkSelfRegistration WHERE NetworkId = '${networkId}'`;
|
|
503
|
+
const nsrResult = spawnSync('sf', [
|
|
504
|
+
'data', 'query',
|
|
505
|
+
'--query', nsrQuery,
|
|
506
|
+
'--target-org', targetOrg,
|
|
507
|
+
'--json',
|
|
508
|
+
], { cwd: ROOT, encoding: 'utf8' });
|
|
509
|
+
let nsrExists = false;
|
|
510
|
+
if (nsrResult.status === 0) {
|
|
511
|
+
try {
|
|
512
|
+
const json = JSON.parse(nsrResult.stdout);
|
|
513
|
+
nsrExists = (json.result?.records?.length || 0) > 0;
|
|
514
|
+
} catch { /* proceed to create */ }
|
|
515
|
+
}
|
|
516
|
+
if (nsrExists) {
|
|
517
|
+
console.log(' NetworkSelfRegistration record already exists; skipping.');
|
|
518
|
+
} else {
|
|
519
|
+
const tmpApex = resolve(ROOT, '.tmp-setup-selfreg.apex');
|
|
520
|
+
const apex = [
|
|
521
|
+
`Account acct = [SELECT Id FROM Account WHERE Id = '${accountId}' LIMIT 1];`,
|
|
522
|
+
`NetworkSelfRegistration nsr = new NetworkSelfRegistration();`,
|
|
523
|
+
`nsr.AccountId = acct.Id;`,
|
|
524
|
+
`nsr.NetworkId = '${networkId}';`,
|
|
525
|
+
`insert nsr;`,
|
|
526
|
+
`System.debug('NSR_CREATED:' + nsr.Id);`,
|
|
527
|
+
].join('\n');
|
|
528
|
+
writeFileSync(tmpApex, apex);
|
|
529
|
+
const apexResult = spawnSync('sf', [
|
|
530
|
+
'apex', 'run', '--target-org', targetOrg, '--file', tmpApex,
|
|
531
|
+
], { cwd: ROOT, stdio: 'pipe', shell: true, timeout: 60000 });
|
|
532
|
+
const apexOut = apexResult.stdout?.toString() || '';
|
|
533
|
+
if (existsSync(tmpApex)) unlinkSync(tmpApex);
|
|
534
|
+
if (apexResult.status !== 0 && !apexOut.includes('Compiled successfully')) {
|
|
535
|
+
process.stderr.write(apexResult.stderr?.toString() || apexOut);
|
|
536
|
+
throw new StepError('failed to create NetworkSelfRegistration record');
|
|
537
|
+
}
|
|
538
|
+
const nsrMatch = apexOut.match(/NSR_CREATED:(\w+)/);
|
|
539
|
+
if (nsrMatch) {
|
|
540
|
+
console.log(` Created NetworkSelfRegistration (${nsrMatch[1]}).`);
|
|
541
|
+
} else {
|
|
542
|
+
console.log(' NetworkSelfRegistration creation executed.');
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Assign a role to the current user so that Experience Cloud self-registration
|
|
549
|
+
* works correctly.
|
|
550
|
+
*/
|
|
551
|
+
function assignRoleToCurrentUser(roleName, targetOrg) {
|
|
552
|
+
const roleQuery = `SELECT Id FROM UserRole WHERE Name = '${roleName}'`;
|
|
553
|
+
const roleResult = spawnSync('sf', [
|
|
554
|
+
'data', 'query',
|
|
555
|
+
'--query', roleQuery,
|
|
556
|
+
'--target-org', targetOrg,
|
|
557
|
+
'--json',
|
|
558
|
+
], { cwd: ROOT, encoding: 'utf8' });
|
|
559
|
+
if (roleResult.status !== 0) {
|
|
560
|
+
if (roleResult.stderr) console.error(roleResult.stderr);
|
|
561
|
+
throw new StepError(`failed to query role "${roleName}" in org`);
|
|
562
|
+
}
|
|
563
|
+
let roleId;
|
|
564
|
+
try {
|
|
565
|
+
const json = JSON.parse(roleResult.stdout);
|
|
566
|
+
const records = json.result?.records;
|
|
567
|
+
if (!records || records.length === 0) {
|
|
568
|
+
throw new StepError(`role "${roleName}" not found in org`);
|
|
569
|
+
}
|
|
570
|
+
roleId = records[0].Id;
|
|
571
|
+
} catch (err) {
|
|
572
|
+
if (err instanceof StepError) throw err;
|
|
573
|
+
throw new StepError(`failed to parse role query result for "${roleName}"`);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
const orgResult = spawnSync('sf', [
|
|
577
|
+
'org', 'display',
|
|
578
|
+
'--target-org', targetOrg,
|
|
579
|
+
'--json',
|
|
580
|
+
], { cwd: ROOT, encoding: 'utf8' });
|
|
581
|
+
if (orgResult.status !== 0) {
|
|
582
|
+
throw new StepError('failed to resolve current user from org');
|
|
583
|
+
}
|
|
584
|
+
let username;
|
|
585
|
+
try {
|
|
586
|
+
const json = JSON.parse(orgResult.stdout);
|
|
587
|
+
username = json.result?.username;
|
|
588
|
+
if (!username) {
|
|
589
|
+
throw new StepError('could not determine current username from org display');
|
|
590
|
+
}
|
|
591
|
+
} catch (err) {
|
|
592
|
+
if (err instanceof StepError) throw err;
|
|
593
|
+
throw new StepError('failed to parse org display result');
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
const userQuery = `SELECT Id, UserRoleId FROM User WHERE Username = '${username}'`;
|
|
597
|
+
const userResult = spawnSync('sf', [
|
|
598
|
+
'data', 'query',
|
|
599
|
+
'--query', userQuery,
|
|
600
|
+
'--target-org', targetOrg,
|
|
601
|
+
'--json',
|
|
602
|
+
], { cwd: ROOT, encoding: 'utf8' });
|
|
603
|
+
if (userResult.status === 0) {
|
|
604
|
+
try {
|
|
605
|
+
const json = JSON.parse(userResult.stdout);
|
|
606
|
+
const userRecord = json.result?.records?.[0];
|
|
607
|
+
if (userRecord?.UserRoleId) {
|
|
608
|
+
console.log(` User ${username} already has a role assigned; skipping to avoid overriding.`);
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
} catch { /* continue */ }
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
const updateResult = spawnSync('sf', [
|
|
615
|
+
'data', 'update', 'record',
|
|
616
|
+
'--sobject', 'User',
|
|
617
|
+
'--where', `Username='${username}'`,
|
|
618
|
+
'--values', `UserRoleId='${roleId}'`,
|
|
619
|
+
'--target-org', targetOrg,
|
|
620
|
+
'--json',
|
|
621
|
+
], { cwd: ROOT, encoding: 'utf8' });
|
|
622
|
+
if (updateResult.status === 0) {
|
|
623
|
+
console.log(` Role "${roleName}" assigned to ${username}.`);
|
|
624
|
+
} else {
|
|
625
|
+
const out = (updateResult.stderr?.toString() || '') + (updateResult.stdout?.toString() || '');
|
|
626
|
+
if (out) console.error(out);
|
|
627
|
+
throw new StepError(`failed to assign role "${roleName}" to ${username}`);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* Query the org for a guest user whose profile name matches the given site name.
|
|
633
|
+
*/
|
|
634
|
+
function resolveGuestUsername(siteName, targetOrg) {
|
|
635
|
+
const query = `SELECT Username FROM User WHERE Profile.Name LIKE '%${siteName}%' AND UserType = 'Guest'`;
|
|
636
|
+
const result = spawnSync('sf', [
|
|
637
|
+
'data', 'query',
|
|
638
|
+
'--query', query,
|
|
639
|
+
'--target-org', targetOrg,
|
|
640
|
+
'--json',
|
|
641
|
+
], { cwd: ROOT, encoding: 'utf8' });
|
|
642
|
+
if (result.status !== 0) {
|
|
643
|
+
console.error(` Failed to query guest user for site "${siteName}".`);
|
|
644
|
+
if (result.stderr) console.error(result.stderr);
|
|
645
|
+
return null;
|
|
646
|
+
}
|
|
647
|
+
try {
|
|
648
|
+
const json = JSON.parse(result.stdout);
|
|
649
|
+
const records = json.result?.records;
|
|
650
|
+
if (!records || records.length === 0) {
|
|
651
|
+
console.error(` No guest user found for site "${siteName}".`);
|
|
652
|
+
return null;
|
|
653
|
+
}
|
|
654
|
+
return records[0].Username;
|
|
655
|
+
} catch {
|
|
656
|
+
console.error(` Failed to parse guest user query result for site "${siteName}".`);
|
|
657
|
+
return null;
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
function isOrgConnected(targetOrg) {
|
|
662
|
+
const result = spawnSync('sf', ['org', 'display', '--target-org', targetOrg, '--json'], {
|
|
663
|
+
cwd: ROOT,
|
|
664
|
+
stdio: 'pipe',
|
|
665
|
+
shell: true,
|
|
666
|
+
});
|
|
667
|
+
return result.status === 0;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
function apexLiteral(value) {
|
|
671
|
+
if (value === null || value === undefined) return 'null';
|
|
672
|
+
if (typeof value === 'boolean') return String(value);
|
|
673
|
+
if (typeof value === 'number') return String(value);
|
|
674
|
+
const s = String(value);
|
|
675
|
+
if (/^\d{4}-\d{2}-\d{2}$/.test(s)) return `Date.valueOf('${s}')`;
|
|
676
|
+
if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/.test(s)) {
|
|
677
|
+
const dt = s.replace('T', ' ').replace(/\.\d+/, '').replace('Z', '');
|
|
678
|
+
return `DateTime.valueOf('${dt}')`;
|
|
679
|
+
}
|
|
680
|
+
return "'" + s.replace(/\\/g, '\\\\').replace(/'/g, "\\'") + "'";
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
function buildApexInsert(sobject, records, refIds) {
|
|
684
|
+
const lines = [
|
|
685
|
+
'Database.DMLOptions dmlOpts = new Database.DMLOptions();',
|
|
686
|
+
'dmlOpts.DuplicateRuleHeader.allowSave = true;',
|
|
687
|
+
`List<${sobject}> recs = new List<${sobject}>();`,
|
|
688
|
+
];
|
|
689
|
+
for (const rec of records) {
|
|
690
|
+
lines.push(`{ ${sobject} r = new ${sobject}();`);
|
|
691
|
+
for (const [key, val] of Object.entries(rec)) {
|
|
692
|
+
if (key === 'attributes') continue;
|
|
693
|
+
lines.push(`r.put('${key}', ${apexLiteral(val)});`);
|
|
694
|
+
}
|
|
695
|
+
lines.push('recs.add(r); }');
|
|
696
|
+
}
|
|
697
|
+
lines.push('Database.SaveResult[] results = Database.insert(recs, dmlOpts);');
|
|
698
|
+
const refArray = refIds.map((r) => `'${r}'`).join(',');
|
|
699
|
+
lines.push(`String[] refs = new String[]{${refArray}};`);
|
|
700
|
+
lines.push('for (Integer i = 0; i < results.size(); i++) {');
|
|
701
|
+
lines.push(" if (results[i].isSuccess()) System.debug('REF:' + refs[i] + ':' + results[i].getId());");
|
|
702
|
+
lines.push(" else System.debug('ERR:' + refs[i] + ':' + results[i].getErrors()[0].getMessage());");
|
|
703
|
+
lines.push('}');
|
|
704
|
+
return lines.join('\n');
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* Interactive multi-select: arrow keys navigate, space toggles, 'a' toggles all, enter confirms.
|
|
709
|
+
* Returns a boolean[] matching the input order. Falls through immediately when stdin is not a TTY.
|
|
710
|
+
*/
|
|
711
|
+
async function promptSteps(steps) {
|
|
712
|
+
if (!process.stdin.isTTY) return steps.map((s) => s.enabled);
|
|
713
|
+
|
|
714
|
+
// `selected` stays indexed by ORIGINAL step order (so the caller's
|
|
715
|
+
// selections[i] → stepDefs[i] mapping is unchanged); unavailable steps remain
|
|
716
|
+
// false. Only available steps are shown and navigable — unavailable steps are
|
|
717
|
+
// hidden entirely rather than rendered greyed-out.
|
|
718
|
+
const selected = steps.map((s) => s.enabled);
|
|
719
|
+
const visible = steps.map((s, i) => ({ step: s, index: i })).filter(({ step }) => step.available);
|
|
720
|
+
let cursor = 0;
|
|
721
|
+
const RST = '\x1B[0m';
|
|
722
|
+
const CYAN = '\x1B[36m';
|
|
723
|
+
const GREEN = '\x1B[32m';
|
|
724
|
+
|
|
725
|
+
/** Strip ANSI escape sequences to get visible character count. */
|
|
726
|
+
function visibleLength(str) {
|
|
727
|
+
return str.replace(/\x1B\[[0-9;]*m/g, '').length;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
/** Count how many terminal rows a set of lines occupies (accounting for wrapping). */
|
|
731
|
+
function terminalRows(lines) {
|
|
732
|
+
const cols = process.stdout.columns || 80;
|
|
733
|
+
let rows = 0;
|
|
734
|
+
for (const line of lines) {
|
|
735
|
+
const len = visibleLength(line);
|
|
736
|
+
rows += len === 0 ? 1 : Math.ceil(len / cols);
|
|
737
|
+
}
|
|
738
|
+
return rows;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
function render() {
|
|
742
|
+
return visible.map(({ step, index }, row) => {
|
|
743
|
+
const ptr = row === cursor ? `${CYAN}❯${RST}` : ' ';
|
|
744
|
+
const chk = selected[index] ? `${GREEN}●${RST}` : '○';
|
|
745
|
+
return `${ptr} ${chk} ${step.label}`;
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
let prevRows = 0;
|
|
750
|
+
|
|
751
|
+
return new Promise((resolve) => {
|
|
752
|
+
process.stdin.setRawMode(true);
|
|
753
|
+
process.stdin.resume();
|
|
754
|
+
process.stdin.setEncoding('utf8');
|
|
755
|
+
process.stdout.write('\x1B[?25l');
|
|
756
|
+
console.log('\nSelect steps (↑↓ move, space toggle, a all, enter confirm):\n');
|
|
757
|
+
const initialLines = render();
|
|
758
|
+
prevRows = terminalRows(initialLines);
|
|
759
|
+
process.stdout.write(initialLines.join('\n') + '\n');
|
|
760
|
+
|
|
761
|
+
function redraw() {
|
|
762
|
+
process.stdout.write(`\x1B[${prevRows}A`);
|
|
763
|
+
const lines = render();
|
|
764
|
+
for (const line of lines) process.stdout.write(`\x1B[2K${line}\n`);
|
|
765
|
+
prevRows = terminalRows(lines);
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
process.stdin.on('data', (key) => {
|
|
769
|
+
if (key === '\x03') {
|
|
770
|
+
process.stdout.write('\x1B[?25h\n');
|
|
771
|
+
process.exit(0);
|
|
772
|
+
}
|
|
773
|
+
if (key === '\r' || key === '\n') {
|
|
774
|
+
process.stdout.write('\x1B[?25h');
|
|
775
|
+
process.stdin.setRawMode(false);
|
|
776
|
+
process.stdin.pause();
|
|
777
|
+
process.stdin.removeAllListeners('data');
|
|
778
|
+
console.log();
|
|
779
|
+
resolve(selected);
|
|
780
|
+
return;
|
|
781
|
+
}
|
|
782
|
+
// Note: `cursor` indexes `visible`; `selected` is indexed by ORIGINAL step
|
|
783
|
+
// order. Map through visible[cursor].index before touching `selected`.
|
|
784
|
+
if (key === ' ') {
|
|
785
|
+
const { index } = visible[cursor];
|
|
786
|
+
selected[index] = !selected[index];
|
|
787
|
+
redraw();
|
|
788
|
+
return;
|
|
789
|
+
}
|
|
790
|
+
if (key === 'a') {
|
|
791
|
+
const allOn = visible.every(({ index }) => selected[index]);
|
|
792
|
+
for (const { index } of visible) selected[index] = !allOn;
|
|
793
|
+
redraw();
|
|
794
|
+
return;
|
|
795
|
+
}
|
|
796
|
+
if (key === '\x1B[A' || key === 'k') {
|
|
797
|
+
cursor = Math.max(0, cursor - 1);
|
|
798
|
+
redraw();
|
|
799
|
+
} else if (key === '\x1B[B' || key === 'j') {
|
|
800
|
+
cursor = Math.min(visible.length - 1, cursor + 1);
|
|
801
|
+
redraw();
|
|
802
|
+
}
|
|
803
|
+
});
|
|
804
|
+
});
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
function run(name, cmd, args, opts = {}) {
|
|
808
|
+
const { cwd = ROOT, optional = false } = opts;
|
|
809
|
+
console.log('\n---', name, '---');
|
|
810
|
+
const result = spawnSync(cmd, args, {
|
|
811
|
+
cwd,
|
|
812
|
+
stdio: 'inherit',
|
|
813
|
+
shell: true,
|
|
814
|
+
...(opts.env && { env: opts.env }),
|
|
815
|
+
...(opts.timeout && { timeout: opts.timeout }),
|
|
816
|
+
});
|
|
817
|
+
if (result.status !== 0 && !optional) {
|
|
818
|
+
throw new StepError(`${name} (exit ${result.status ?? 1})`);
|
|
819
|
+
}
|
|
820
|
+
return result;
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
/** Promise-based spawn for parallel execution. Always uses stdio: 'pipe'. */
|
|
824
|
+
function spawnAsync(cmd, args, opts = {}) {
|
|
825
|
+
return new Promise((resolve, reject) => {
|
|
826
|
+
const proc = nodeSpawn(cmd, args, {
|
|
827
|
+
cwd: opts.cwd || ROOT,
|
|
828
|
+
stdio: 'pipe',
|
|
829
|
+
shell: true,
|
|
830
|
+
...(opts.timeout && { timeout: opts.timeout }),
|
|
831
|
+
});
|
|
832
|
+
let stdout = '';
|
|
833
|
+
let stderr = '';
|
|
834
|
+
proc.stdout.on('data', (d) => { stdout += d.toString(); });
|
|
835
|
+
proc.stderr.on('data', (d) => { stderr += d.toString(); });
|
|
836
|
+
proc.on('close', (code) => resolve({ status: code, stdout, stderr }));
|
|
837
|
+
proc.on('error', reject);
|
|
838
|
+
});
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
/** Async version of run() for parallel steps. Captures output and prints on failure. */
|
|
842
|
+
async function runAsync(name, cmd, args, opts = {}) {
|
|
843
|
+
const { cwd = ROOT, optional = false } = opts;
|
|
844
|
+
const result = await spawnAsync(cmd, args, { cwd, ...(opts.timeout && { timeout: opts.timeout }) });
|
|
845
|
+
if (result.status !== 0 && !optional) {
|
|
846
|
+
if (result.stdout) process.stdout.write(result.stdout);
|
|
847
|
+
if (result.stderr) process.stderr.write(result.stderr);
|
|
848
|
+
throw new StepError(`${name} (exit ${result.status ?? 1})`);
|
|
849
|
+
}
|
|
850
|
+
return result;
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
/**
|
|
854
|
+
* In-memory result ledger. Each selected step records exactly one outcome:
|
|
855
|
+
* ok — the step ran and succeeded
|
|
856
|
+
* skipped — the step was not selected, or had no config section (intentional)
|
|
857
|
+
* failed — the step ran and failed (with a human-readable reason)
|
|
858
|
+
* The end-of-run summary is rendered from this ledger and the process exits
|
|
859
|
+
* non-zero whenever any step is `failed` (fail-fast or skippable alike).
|
|
860
|
+
*
|
|
861
|
+
* @typedef {{ key: string, label: string, status: 'ok'|'skipped'|'failed', reason?: string, failFast?: boolean }} StepResult
|
|
862
|
+
*/
|
|
863
|
+
const results = [];
|
|
864
|
+
function recordOk(step) {
|
|
865
|
+
results.push({ key: step.key, label: step.label, status: 'ok', failFast: step.failFast });
|
|
866
|
+
}
|
|
867
|
+
function recordSkipped(step, reason) {
|
|
868
|
+
results.push({ key: step.key, label: step.label, status: 'skipped', reason, failFast: step.failFast });
|
|
869
|
+
}
|
|
870
|
+
function recordFailed(step, reason) {
|
|
871
|
+
results.push({ key: step.key, label: step.label, status: 'failed', reason, failFast: step.failFast });
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
/** True if any step in the ledger failed. */
|
|
875
|
+
function finalExitCode() {
|
|
876
|
+
return results.some((r) => r.status === 'failed') ? 1 : 0;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
const SUMMARY_GLYPH = { ok: '✔', skipped: '–', failed: '✖' };
|
|
880
|
+
|
|
881
|
+
/**
|
|
882
|
+
* Render the end-of-run summary. Always called — on success, on a fail-fast
|
|
883
|
+
* abort (partial: only steps reached so far are present), and on a clean finish
|
|
884
|
+
* that had skippable failures. Failed rows are listed last so the real problem
|
|
885
|
+
* is the last thing the developer sees.
|
|
886
|
+
*/
|
|
887
|
+
function printSummary(targetOrg) {
|
|
888
|
+
const ordered = [
|
|
889
|
+
...results.filter((r) => r.status !== 'failed'),
|
|
890
|
+
...results.filter((r) => r.status === 'failed'),
|
|
891
|
+
];
|
|
892
|
+
const pad = Math.max(0, ...ordered.map((r) => r.key.length));
|
|
893
|
+
console.log(`\nSetup summary (target org: ${targetOrg})`);
|
|
894
|
+
for (const r of ordered) {
|
|
895
|
+
const glyph = SUMMARY_GLYPH[r.status] ?? ' ';
|
|
896
|
+
const key = r.key.padEnd(pad);
|
|
897
|
+
let line = ` ${glyph} ${key} ${r.status}`;
|
|
898
|
+
if (r.reason) line += ` (${r.reason})`;
|
|
899
|
+
if (r.status === 'failed') line += r.failFast ? ' [fail-fast — aborted]' : ' [skippable — continued]';
|
|
900
|
+
console.log(line);
|
|
901
|
+
}
|
|
902
|
+
const failures = results.filter((r) => r.status === 'failed').length;
|
|
903
|
+
if (failures > 0) {
|
|
904
|
+
console.log(`Setup completed with ${failures} failure(s). Exiting 1.`);
|
|
905
|
+
} else {
|
|
906
|
+
console.log('Setup complete.');
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
/**
|
|
911
|
+
* Run one step body, recording its outcome in the ledger and honoring the
|
|
912
|
+
* step's fixed `failFast` classification. The body either completes (→ ok),
|
|
913
|
+
* throws a StepError (→ failed), or throws something unexpected (→ failed,
|
|
914
|
+
* with the raw message). On a fail-fast failure this prints the partial
|
|
915
|
+
* summary and exits non-zero immediately; on a skippable failure it records
|
|
916
|
+
* and returns so the run continues.
|
|
917
|
+
*/
|
|
918
|
+
async function runStep(step, targetOrg, body) {
|
|
919
|
+
try {
|
|
920
|
+
await body();
|
|
921
|
+
recordOk(step);
|
|
922
|
+
} catch (err) {
|
|
923
|
+
const reason = err instanceof StepError ? err.message : (err?.message ?? String(err));
|
|
924
|
+
recordFailed(step, reason);
|
|
925
|
+
console.error(`\nStep "${step.key}" failed: ${reason}`);
|
|
926
|
+
if (step.failFast) {
|
|
927
|
+
printSummary(targetOrg);
|
|
928
|
+
process.exit(1);
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
async function main() {
|
|
934
|
+
// Ensure .gitignore files exist (npm strips them from published packages).
|
|
935
|
+
const gitignoreTemplates = loadGitignoreTemplates();
|
|
936
|
+
if (gitignoreTemplates) {
|
|
937
|
+
ensureGitignore(ROOT, gitignoreTemplates.sfdx);
|
|
938
|
+
if (existsSync(UIBUNDLES_DIR)) {
|
|
939
|
+
for (const entry of readdirSync(UIBUNDLES_DIR, { withFileTypes: true })) {
|
|
940
|
+
if (entry.isDirectory() && !entry.name.startsWith('.')) {
|
|
941
|
+
ensureGitignore(resolve(UIBUNDLES_DIR, entry.name), gitignoreTemplates.webapp);
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
const {
|
|
948
|
+
targetOrg,
|
|
949
|
+
uiBundleName,
|
|
950
|
+
permsetNamesExplicit,
|
|
951
|
+
yes,
|
|
952
|
+
skipLogin: argSkipLogin,
|
|
953
|
+
skipDeploy: argSkipDeploy,
|
|
954
|
+
skipPermset: argSkipPermset,
|
|
955
|
+
skipRole: argSkipRole,
|
|
956
|
+
skipSelfReg: argSkipSelfReg,
|
|
957
|
+
skipData: argSkipData,
|
|
958
|
+
skipGraphql: argSkipGraphql,
|
|
959
|
+
skipUIBundleBuild: argSkipUIBundleBuild,
|
|
960
|
+
skipDev: argSkipDev,
|
|
961
|
+
} = parseArgs();
|
|
962
|
+
|
|
963
|
+
const permsetNames =
|
|
964
|
+
permsetNamesExplicit.length > 0 ? permsetNamesExplicit : discoverPermissionSetNames();
|
|
965
|
+
const permsetStepLabel =
|
|
966
|
+
permsetNames.length === 0
|
|
967
|
+
? 'Permset — (none under permissionsets/)'
|
|
968
|
+
: permsetNames.length <= 3
|
|
969
|
+
? `Permset — assign ${permsetNames.join(', ')}`
|
|
970
|
+
: `Permset — assign ${permsetNames.length} permission sets`;
|
|
971
|
+
|
|
972
|
+
// Validate org-setup.config.json ONCE, before any step runs (spec §5.2). The
|
|
973
|
+
// three load*Config helpers read from this already-validated object.
|
|
974
|
+
const config = loadValidatedConfig();
|
|
975
|
+
|
|
976
|
+
const hasDataPlan = existsSync(DATA_PLAN) && existsSync(DATA_DIR);
|
|
977
|
+
const roleConfig = loadRoleConfig(config);
|
|
978
|
+
const hasRoleConfig = roleConfig !== null;
|
|
979
|
+
const selfRegConfig = loadSelfRegConfig(config);
|
|
980
|
+
const hasSelfRegConfig = selfRegConfig !== null;
|
|
981
|
+
|
|
982
|
+
// failFast is a fixed, implementer-owned classification (spec §5.2) — NOT
|
|
983
|
+
// user-configurable. A fail-fast failure aborts the run immediately; a
|
|
984
|
+
// skippable failure is recorded and the run continues. The exit code is
|
|
985
|
+
// non-zero on any failure regardless of class.
|
|
986
|
+
const stepDefs = [
|
|
987
|
+
{ key: 'login', label: 'Login — org authentication', enabled: !argSkipLogin, available: true, failFast: true },
|
|
988
|
+
{ key: 'uiBundleBuild', label: 'UI Bundle Build — npm install + build (pre-deploy)', enabled: !argSkipUIBundleBuild, available: true, failFast: true },
|
|
989
|
+
{ key: 'deploy', label: 'Deploy — sf project deploy start', enabled: !argSkipDeploy, available: true, failFast: true },
|
|
990
|
+
{ key: 'permset', label: permsetStepLabel, enabled: !argSkipPermset, available: true, failFast: false },
|
|
991
|
+
{ key: 'role', label: `Role — assign "${roleConfig?.roleName ?? '?'}" to current user`, enabled: !argSkipRole && hasRoleConfig, available: hasRoleConfig, failFast: false },
|
|
992
|
+
{ key: 'selfReg', label: 'Self-Registration — enable for site', enabled: !argSkipSelfReg && hasSelfRegConfig, available: hasSelfRegConfig, failFast: false },
|
|
993
|
+
{ key: 'data', label: 'Data — delete + import records via Apex', enabled: !argSkipData && hasDataPlan, available: hasDataPlan, failFast: true },
|
|
994
|
+
{ key: 'graphql', label: 'GraphQL — schema introspect + codegen', enabled: !argSkipGraphql, available: true, failFast: true },
|
|
995
|
+
{ key: 'dev', label: 'Dev — launch dev server', enabled: !argSkipDev, available: true, failFast: false },
|
|
996
|
+
];
|
|
997
|
+
|
|
998
|
+
const selections = yes ? stepDefs.map((s) => s.enabled) : await promptSteps(stepDefs);
|
|
999
|
+
const on = {};
|
|
1000
|
+
stepDefs.forEach((s, i) => {
|
|
1001
|
+
on[s.key] = selections[i];
|
|
1002
|
+
});
|
|
1003
|
+
|
|
1004
|
+
const skipLogin = !on.login;
|
|
1005
|
+
const skipUIBundleBuild = !on.uiBundleBuild;
|
|
1006
|
+
const skipDeploy = !on.deploy;
|
|
1007
|
+
const skipPermset = !on.permset;
|
|
1008
|
+
const skipRole = !on.role;
|
|
1009
|
+
const skipSelfReg = !on.selfReg;
|
|
1010
|
+
const skipData = !on.data;
|
|
1011
|
+
const skipGraphql = !on.graphql;
|
|
1012
|
+
const skipDev = !on.dev;
|
|
1013
|
+
|
|
1014
|
+
const needsUIBundle = !skipUIBundleBuild || !skipGraphql || !skipDev;
|
|
1015
|
+
const uiBundleDir = needsUIBundle ? discoverUIBundleDir(uiBundleName) : null;
|
|
1016
|
+
const doData = !skipData;
|
|
1017
|
+
|
|
1018
|
+
console.log('Setup — target org:', targetOrg, '| UI bundle:', uiBundleDir ?? '(none)');
|
|
1019
|
+
console.log(
|
|
1020
|
+
'Steps: login=%s deploy=%s permset=%s role=%s selfReg=%s data=%s graphql=%s uiBundle=%s dev=%s',
|
|
1021
|
+
!skipLogin,
|
|
1022
|
+
!skipDeploy,
|
|
1023
|
+
!skipPermset,
|
|
1024
|
+
!skipRole,
|
|
1025
|
+
!skipSelfReg,
|
|
1026
|
+
doData,
|
|
1027
|
+
!skipGraphql,
|
|
1028
|
+
!skipUIBundleBuild,
|
|
1029
|
+
!skipDev
|
|
1030
|
+
);
|
|
1031
|
+
|
|
1032
|
+
const loginStep = stepDefs.find((s) => s.key === 'login');
|
|
1033
|
+
if (!skipLogin) {
|
|
1034
|
+
await runStep(loginStep, targetOrg, async () => {
|
|
1035
|
+
if (isOrgConnected(targetOrg)) {
|
|
1036
|
+
console.log('\n--- Login ---');
|
|
1037
|
+
console.log(`Org ${targetOrg} is already authenticated; skipping browser login.`);
|
|
1038
|
+
} else {
|
|
1039
|
+
// Login is fail-fast (spec §5.2): drop the previous { optional: true } so a
|
|
1040
|
+
// failed browser login records `failed`, prints the summary, and aborts.
|
|
1041
|
+
run('Login (browser)', 'sf', ['org', 'login', 'web', '--alias', targetOrg]);
|
|
1042
|
+
}
|
|
1043
|
+
});
|
|
1044
|
+
} else {
|
|
1045
|
+
recordSkipped(loginStep, 'not selected');
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
// Ensure the self-reg profile is in networkMemberGroups before deploy so that
|
|
1049
|
+
// subsequent selfRegProfile / selfRegistration updates don't fail. This is
|
|
1050
|
+
// best-effort prep: if the site can't be derived (no network file, or the
|
|
1051
|
+
// ambiguous multi-network case where deriveSiteName throws), skip the prep
|
|
1052
|
+
// silently here — the selfReg step records that derivation failure as a
|
|
1053
|
+
// skippable StepError, so it stays in the ledger instead of aborting pre-deploy.
|
|
1054
|
+
if (!skipDeploy && selfRegConfig) {
|
|
1055
|
+
let preDeploySiteName = null;
|
|
1056
|
+
try {
|
|
1057
|
+
preDeploySiteName = deriveSiteName();
|
|
1058
|
+
} catch {
|
|
1059
|
+
// ambiguous derivation — surfaced by the selfReg step below
|
|
1060
|
+
}
|
|
1061
|
+
if (preDeploySiteName) {
|
|
1062
|
+
console.log('\n--- Ensure network member profile (pre-deploy) ---');
|
|
1063
|
+
ensureNetworkMemberProfile(selfRegConfig, preDeploySiteName);
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
// Build all UI Bundles before deploy so dist exists for entity deployment
|
|
1068
|
+
const uiBundleBuildStep = stepDefs.find((s) => s.key === 'uiBundleBuild');
|
|
1069
|
+
let preDeployBundlesBuilt = false;
|
|
1070
|
+
if (!skipUIBundleBuild) {
|
|
1071
|
+
if (!skipDeploy) {
|
|
1072
|
+
await runStep(uiBundleBuildStep, targetOrg, () => {
|
|
1073
|
+
const allUIBundleDirs = discoverAllUIBundleDirs(uiBundleName);
|
|
1074
|
+
for (const dir of allUIBundleDirs) {
|
|
1075
|
+
const name = dir.split(/[/\\]/).pop();
|
|
1076
|
+
run(`UI Bundle install (${name})`, 'npm', ['install'], { cwd: dir });
|
|
1077
|
+
run(`UI Bundle build (${name})`, 'npm', ['run', 'build'], { cwd: dir });
|
|
1078
|
+
}
|
|
1079
|
+
});
|
|
1080
|
+
preDeployBundlesBuilt = true;
|
|
1081
|
+
}
|
|
1082
|
+
// When skipDeploy, the bundle build happens in the GraphQL section below;
|
|
1083
|
+
// its outcome is recorded there.
|
|
1084
|
+
} else {
|
|
1085
|
+
recordSkipped(uiBundleBuildStep, 'not selected');
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
const deployStep = stepDefs.find((s) => s.key === 'deploy');
|
|
1089
|
+
if (!skipDeploy) {
|
|
1090
|
+
await runStep(deployStep, targetOrg, () => {
|
|
1091
|
+
run('Deploy metadata', 'sf', ['project', 'deploy', 'start', '--target-org', targetOrg], {
|
|
1092
|
+
timeout: 180000,
|
|
1093
|
+
});
|
|
1094
|
+
});
|
|
1095
|
+
} else {
|
|
1096
|
+
recordSkipped(deployStep, 'not selected');
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
const permsetStep = stepDefs.find((s) => s.key === 'permset');
|
|
1100
|
+
if (!skipPermset) {
|
|
1101
|
+
await runStep(permsetStep, targetOrg, async () => {
|
|
1102
|
+
const permsetConfig = loadPermsetConfig(config);
|
|
1103
|
+
if (permsetNames.length === 0) {
|
|
1104
|
+
console.log('\n--- Assign permission sets ---');
|
|
1105
|
+
console.log('No permission sets found under permissionsets/ and none passed via --permset-name; skipping.');
|
|
1106
|
+
return;
|
|
1107
|
+
}
|
|
1108
|
+
console.log('\n--- Assign permission sets ---');
|
|
1109
|
+
|
|
1110
|
+
// Resolve assignments (guest user lookups etc.) then run all sf assign calls in parallel.
|
|
1111
|
+
//
|
|
1112
|
+
// A guest-user resolution failure (no derivable site, or no guest user yet)
|
|
1113
|
+
// is collected — NOT thrown mid-loop. Throwing here would unwind the whole
|
|
1114
|
+
// runStep body before Promise.all and silently drop every assignment already
|
|
1115
|
+
// queued (e.g. a currentUser permset that sorts ahead of a guestUser one and
|
|
1116
|
+
// never depended on the site at all). Resolvable assignments still run; the
|
|
1117
|
+
// combined failure is thrown at the end so the step is still recorded failed.
|
|
1118
|
+
const assignmentJobs = [];
|
|
1119
|
+
const resolutionFailures = [];
|
|
1120
|
+
for (const permsetName of permsetNames) {
|
|
1121
|
+
const assignment = resolveAssignment(permsetName, permsetConfig);
|
|
1122
|
+
if (assignment.assignee === 'skip') {
|
|
1123
|
+
console.log(`Permission set "${permsetName}" — skipped (config).`);
|
|
1124
|
+
continue;
|
|
1125
|
+
}
|
|
1126
|
+
let effectiveUsername = null;
|
|
1127
|
+
if (assignment.assignee === 'guestUser') {
|
|
1128
|
+
// Site name is derived from the single network metadata file the app
|
|
1129
|
+
// ships (spec §5.2) — never restated per-assignment.
|
|
1130
|
+
const siteName = deriveSiteName();
|
|
1131
|
+
if (!siteName) {
|
|
1132
|
+
console.error(`Permission set "${permsetName}" — assignee is "guestUser" but no networks/<siteName>.network-meta.xml was found to derive the site; skipping.`);
|
|
1133
|
+
resolutionFailures.push(`${permsetName} (no network metadata to derive site)`);
|
|
1134
|
+
continue;
|
|
1135
|
+
}
|
|
1136
|
+
effectiveUsername = resolveGuestUsername(siteName, targetOrg);
|
|
1137
|
+
if (!effectiveUsername) {
|
|
1138
|
+
console.error(`Permission set "${permsetName}" — could not resolve guest user for site "${siteName}"; skipping.`);
|
|
1139
|
+
resolutionFailures.push(`${permsetName} (could not resolve guest user for site "${siteName}")`);
|
|
1140
|
+
continue;
|
|
1141
|
+
}
|
|
1142
|
+
console.log(` Resolved guest user for site "${siteName}": ${effectiveUsername}`);
|
|
1143
|
+
}
|
|
1144
|
+
assignmentJobs.push({ permsetName, effectiveUsername });
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
// Run all permset assignment calls in parallel.
|
|
1148
|
+
const assignResults = await Promise.all(assignmentJobs.map(async ({ permsetName, effectiveUsername }) => {
|
|
1149
|
+
const sfArgs = ['org', 'assign', 'permset', '--name', permsetName, '--target-org', targetOrg];
|
|
1150
|
+
if (effectiveUsername) {
|
|
1151
|
+
sfArgs.push('--on-behalf-of', effectiveUsername);
|
|
1152
|
+
}
|
|
1153
|
+
const assigneeLabel = effectiveUsername || 'current user';
|
|
1154
|
+
const result = await spawnAsync('sf', sfArgs);
|
|
1155
|
+
return { permsetName, assigneeLabel, result };
|
|
1156
|
+
}));
|
|
1157
|
+
|
|
1158
|
+
const failures = [];
|
|
1159
|
+
for (const { permsetName, assigneeLabel, result } of assignResults) {
|
|
1160
|
+
if (result.status === 0) {
|
|
1161
|
+
console.log(`Permission set "${permsetName}" assigned to ${assigneeLabel}.`);
|
|
1162
|
+
} else {
|
|
1163
|
+
const out = (result.stderr || '') + (result.stdout || '');
|
|
1164
|
+
if (out.includes('Duplicate') && out.includes('PermissionSet')) {
|
|
1165
|
+
console.log(`Permission set "${permsetName}" already assigned to ${assigneeLabel}; skipping.`);
|
|
1166
|
+
} else if (out.includes('not found') && out.includes('target org')) {
|
|
1167
|
+
console.log(`Permission set "${permsetName}" not in org; skipping.`);
|
|
1168
|
+
} else {
|
|
1169
|
+
if (result.stdout) process.stdout.write(result.stdout);
|
|
1170
|
+
if (result.stderr) process.stderr.write(result.stderr);
|
|
1171
|
+
failures.push(`${permsetName} (exit ${result.status ?? 1})`);
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
const allFailures = [...resolutionFailures, ...failures];
|
|
1176
|
+
if (allFailures.length > 0) {
|
|
1177
|
+
throw new StepError(`failed to assign permission set(s): ${allFailures.join(', ')}`);
|
|
1178
|
+
}
|
|
1179
|
+
});
|
|
1180
|
+
} else {
|
|
1181
|
+
recordSkipped(permsetStep, 'not selected');
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
const roleStep = stepDefs.find((s) => s.key === 'role');
|
|
1185
|
+
if (!skipRole) {
|
|
1186
|
+
console.log('\n--- Assign role ---');
|
|
1187
|
+
// Config shape (assignee === 'currentUser', non-empty roleName) is guaranteed
|
|
1188
|
+
// by the schema, so there is nothing to re-check here — the step either ran
|
|
1189
|
+
// (ok) or its assignment threw (failed). No manual recordSkipped inference.
|
|
1190
|
+
await runStep(roleStep, targetOrg, () => {
|
|
1191
|
+
assignRoleToCurrentUser(roleConfig.roleName, targetOrg);
|
|
1192
|
+
});
|
|
1193
|
+
} else {
|
|
1194
|
+
recordSkipped(roleStep, roleStep.available ? 'not selected' : 'no config');
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
const selfRegStep = stepDefs.find((s) => s.key === 'selfReg');
|
|
1198
|
+
if (!skipSelfReg) {
|
|
1199
|
+
console.log('\n--- Enable self-registration ---');
|
|
1200
|
+
// Config shape (selfRegProfile, accountName) is guaranteed by the schema.
|
|
1201
|
+
// The site is derived inside the step body so a "multiple network files"
|
|
1202
|
+
// StepError is recorded in the ledger rather than escaping. No manual
|
|
1203
|
+
// recordSkipped inference.
|
|
1204
|
+
await runStep(selfRegStep, targetOrg, () => {
|
|
1205
|
+
const siteName = deriveSiteName();
|
|
1206
|
+
if (!siteName) {
|
|
1207
|
+
throw new StepError(
|
|
1208
|
+
'self-registration is configured but no networks/<siteName>.network-meta.xml was found to derive the site',
|
|
1209
|
+
);
|
|
1210
|
+
}
|
|
1211
|
+
enableSelfRegistration(selfRegConfig, siteName, targetOrg);
|
|
1212
|
+
});
|
|
1213
|
+
} else {
|
|
1214
|
+
recordSkipped(selfRegStep, selfRegStep.available ? 'not selected' : 'no config');
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
const dataStep = stepDefs.find((s) => s.key === 'data');
|
|
1218
|
+
if (doData) {
|
|
1219
|
+
await runStep(dataStep, targetOrg, () => {
|
|
1220
|
+
// Prepare data for uniqueness (run before import so repeat imports don't conflict).
|
|
1221
|
+
// Per-app data normalization (reference remapping, unique-field mangling) ships with
|
|
1222
|
+
// the app's seed data as data/prepare-import-unique-fields.js — this script stays
|
|
1223
|
+
// object-agnostic and simply runs it if present.
|
|
1224
|
+
const prepareScript = resolve(DATA_DIR, 'prepare-import-unique-fields.js');
|
|
1225
|
+
if (existsSync(prepareScript)) {
|
|
1226
|
+
run('Prepare data (unique fields)', 'node', [prepareScript, '--data-dir', DATA_DIR], {
|
|
1227
|
+
cwd: ROOT,
|
|
1228
|
+
});
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
// Delete existing records so every run inserts the full dataset without duplicate conflicts.
|
|
1232
|
+
// Reverse plan order ensures children are removed before parents (FK safety).
|
|
1233
|
+
console.log('\n--- Clean existing data for fresh import ---');
|
|
1234
|
+
const planEntries = JSON.parse(readFileSync(DATA_PLAN, 'utf8'));
|
|
1235
|
+
const sobjectsReversed = [...planEntries.map((e) => e.sobject)].reverse();
|
|
1236
|
+
const tmpApex = resolve(ROOT, '.tmp-setup-delete.apex');
|
|
1237
|
+
for (const sobject of sobjectsReversed) {
|
|
1238
|
+
const apexCode = [
|
|
1239
|
+
'try {',
|
|
1240
|
+
` List<SObject> recs = Database.query('SELECT Id FROM ${sobject} LIMIT 10000');`,
|
|
1241
|
+
' if (!recs.isEmpty()) {',
|
|
1242
|
+
' Database.delete(recs, false);',
|
|
1243
|
+
' Database.emptyRecycleBin(recs);',
|
|
1244
|
+
' }',
|
|
1245
|
+
'} catch (Exception e) {',
|
|
1246
|
+
' // non-deletable records (e.g. Contact linked to Case) are skipped via allOrNone=false',
|
|
1247
|
+
'}',
|
|
1248
|
+
].join('\n');
|
|
1249
|
+
writeFileSync(tmpApex, apexCode);
|
|
1250
|
+
spawnSync('sf', ['apex', 'run', '--target-org', targetOrg, '--file', tmpApex], {
|
|
1251
|
+
cwd: ROOT,
|
|
1252
|
+
stdio: 'pipe',
|
|
1253
|
+
shell: true,
|
|
1254
|
+
timeout: 60000,
|
|
1255
|
+
});
|
|
1256
|
+
console.log(` ${sobject}: cleaned`);
|
|
1257
|
+
}
|
|
1258
|
+
if (existsSync(tmpApex)) unlinkSync(tmpApex);
|
|
1259
|
+
|
|
1260
|
+
// Import via Anonymous Apex with Database.DMLOptions.duplicateRuleHeader.allowSave = true.
|
|
1261
|
+
// This bypasses both duplicate-rule blocks AND matching-service timeouts that the REST
|
|
1262
|
+
// API headers (Sforce-Duplicate-Rule-Action) cannot override.
|
|
1263
|
+
console.log('\n--- Data import tree ---');
|
|
1264
|
+
const refMap = new Map();
|
|
1265
|
+
const APEX_CHAR_LIMIT = 25000;
|
|
1266
|
+
const APEX_MAX_BATCH = 200;
|
|
1267
|
+
|
|
1268
|
+
for (const entry of planEntries) {
|
|
1269
|
+
for (const file of entry.files) {
|
|
1270
|
+
const data = JSON.parse(readFileSync(resolve(DATA_DIR, file), 'utf8'));
|
|
1271
|
+
const records = data.records || [];
|
|
1272
|
+
|
|
1273
|
+
for (const rec of records) {
|
|
1274
|
+
for (const key of Object.keys(rec)) {
|
|
1275
|
+
if (key === 'attributes') continue;
|
|
1276
|
+
const val = rec[key];
|
|
1277
|
+
if (typeof val === 'string' && val.startsWith('@')) {
|
|
1278
|
+
const actual = refMap.get(val.slice(1));
|
|
1279
|
+
if (actual) {
|
|
1280
|
+
rec[key] = actual;
|
|
1281
|
+
} else if (refMap.size > 0) {
|
|
1282
|
+
console.warn(` Warning: unresolved ref ${val} in ${file}`);
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
let imported = 0;
|
|
1289
|
+
const sampleRec = records[0] || {};
|
|
1290
|
+
const fieldsPerRec = Object.keys(sampleRec).filter((k) => k !== 'attributes').length;
|
|
1291
|
+
const estCharsPerRec = 40 + fieldsPerRec * 55;
|
|
1292
|
+
const batchSize = Math.min(APEX_MAX_BATCH, Math.max(5, Math.floor(APEX_CHAR_LIMIT / estCharsPerRec)));
|
|
1293
|
+
for (let i = 0; i < records.length; i += batchSize) {
|
|
1294
|
+
const batch = records.slice(i, i + batchSize);
|
|
1295
|
+
const refIds = batch.map((r) => r.attributes?.referenceId || `_idx${i}`);
|
|
1296
|
+
const apex = buildApexInsert(entry.sobject, batch, refIds);
|
|
1297
|
+
writeFileSync(tmpApex, apex);
|
|
1298
|
+
const apexResult = spawnSync(
|
|
1299
|
+
'sf',
|
|
1300
|
+
['apex', 'run', '--target-org', targetOrg, '--file', tmpApex],
|
|
1301
|
+
{ cwd: ROOT, stdio: 'pipe', shell: true, timeout: 120000 }
|
|
1302
|
+
);
|
|
1303
|
+
const apexOut = apexResult.stdout?.toString() || '';
|
|
1304
|
+
const apexErr = apexResult.stderr?.toString() || '';
|
|
1305
|
+
if (apexResult.status !== 0 && !apexOut.includes('Compiled successfully')) {
|
|
1306
|
+
process.stderr.write(apexErr || apexOut);
|
|
1307
|
+
throw new StepError(`${entry.sobject}: apex execution failed`);
|
|
1308
|
+
}
|
|
1309
|
+
const okMatches = [...apexOut.matchAll(/\|DEBUG\|REF:([^:\n]+):(\w+)/g)];
|
|
1310
|
+
const errMatches = [...apexOut.matchAll(/\|DEBUG\|ERR:([^:\n]+):([^\n]+)/g)];
|
|
1311
|
+
if (errMatches.length) {
|
|
1312
|
+
for (const m of errMatches.slice(0, 5)) {
|
|
1313
|
+
console.error(` ${m[1]}: ${m[2].trim()}`);
|
|
1314
|
+
}
|
|
1315
|
+
if (errMatches.length > 5) console.error(` ... and ${errMatches.length - 5} more`);
|
|
1316
|
+
throw new StepError(`data import tree (${entry.sobject}) — ${errMatches.length} record error(s)`);
|
|
1317
|
+
}
|
|
1318
|
+
if (entry.saveRefs) {
|
|
1319
|
+
for (const m of okMatches) refMap.set(m[1], m[2]);
|
|
1320
|
+
}
|
|
1321
|
+
imported += okMatches.length;
|
|
1322
|
+
}
|
|
1323
|
+
console.log(` ${entry.sobject}: imported ${imported} records`);
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
if (existsSync(tmpApex)) unlinkSync(tmpApex);
|
|
1327
|
+
});
|
|
1328
|
+
} else {
|
|
1329
|
+
recordSkipped(dataStep, dataStep.available ? 'not selected' : 'no data plan');
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
const graphqlStep = stepDefs.find((s) => s.key === 'graphql');
|
|
1333
|
+
if (!skipGraphql) {
|
|
1334
|
+
await runStep(graphqlStep, targetOrg, () => {
|
|
1335
|
+
run('UI Bundle npm install', 'npm', ['install'], { cwd: uiBundleDir });
|
|
1336
|
+
run('GraphQL schema (introspect)', 'npm', ['run', 'graphql:schema'], {
|
|
1337
|
+
cwd: uiBundleDir,
|
|
1338
|
+
env: { ...process.env, SF_TARGET_ORG: targetOrg },
|
|
1339
|
+
});
|
|
1340
|
+
run('GraphQL codegen', 'npm', ['run', 'graphql:codegen'], { cwd: uiBundleDir });
|
|
1341
|
+
run('UI Bundle build (post-codegen)', 'npm', ['run', 'build'], { cwd: uiBundleDir });
|
|
1342
|
+
});
|
|
1343
|
+
} else {
|
|
1344
|
+
recordSkipped(graphqlStep, 'not selected');
|
|
1345
|
+
if (!skipUIBundleBuild && skipDeploy && !preDeployBundlesBuilt) {
|
|
1346
|
+
// The pre-deploy build never ran (deploy was skipped); build here and
|
|
1347
|
+
// record the uiBundleBuild outcome that the pre-deploy branch would have.
|
|
1348
|
+
await runStep(uiBundleBuildStep, targetOrg, () => {
|
|
1349
|
+
run('UI Bundle npm install', 'npm', ['install'], { cwd: uiBundleDir });
|
|
1350
|
+
run('UI Bundle build', 'npm', ['run', 'build'], { cwd: uiBundleDir });
|
|
1351
|
+
});
|
|
1352
|
+
preDeployBundlesBuilt = true;
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
// When uiBundleBuild was selected but the dedicated pre-deploy build never ran
|
|
1357
|
+
// (deploy skipped and graphql selected), the build happened inside the graphql
|
|
1358
|
+
// step — which is fail-fast, so reaching here means it succeeded. Record ok.
|
|
1359
|
+
if (!skipUIBundleBuild && !preDeployBundlesBuilt && !results.some((r) => r.key === 'uiBundleBuild')) {
|
|
1360
|
+
recordOk(uiBundleBuildStep);
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
const devStep = stepDefs.find((s) => s.key === 'dev');
|
|
1364
|
+
if (!skipDev) {
|
|
1365
|
+
// dev is skippable and terminal: a failure here is a local runtime issue
|
|
1366
|
+
// (port in use, tooling), not a broken setup. Print the summary BEFORE the
|
|
1367
|
+
// (blocking, long-lived) dev server starts so the developer sees the setup
|
|
1368
|
+
// outcome up front; the server then runs in the foreground until Ctrl+C.
|
|
1369
|
+
recordOk(devStep);
|
|
1370
|
+
printSummary(targetOrg);
|
|
1371
|
+
console.log('\n--- Launching dev server (Ctrl+C to stop) ---\n');
|
|
1372
|
+
const devResult = run('Dev server', 'npm', ['run', 'dev'], { cwd: uiBundleDir, optional: true });
|
|
1373
|
+
if (devResult.status !== 0) {
|
|
1374
|
+
// Replace the optimistic `ok` with a skippable failure and reprint.
|
|
1375
|
+
const row = results.find((r) => r.key === 'dev');
|
|
1376
|
+
row.status = 'failed';
|
|
1377
|
+
row.reason = 'dev server failed to start — setup itself completed; check local runtime';
|
|
1378
|
+
printSummary(targetOrg);
|
|
1379
|
+
}
|
|
1380
|
+
process.exit(finalExitCode());
|
|
1381
|
+
} else {
|
|
1382
|
+
recordSkipped(devStep, 'not selected');
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
printSummary(targetOrg);
|
|
1386
|
+
process.exit(finalExitCode());
|
|
1387
|
+
}
|
|
1388
|
+
|
|
1389
|
+
main().catch((err) => {
|
|
1390
|
+
console.error(err);
|
|
1391
|
+
process.exit(1);
|
|
1392
|
+
});
|