|
| 1 | +import { chmodSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' |
| 2 | +import { dirname, resolve } from 'node:path' |
| 3 | +import { fileURLToPath } from 'node:url' |
| 4 | +import { tmpdir } from 'node:os' |
| 5 | +import { parseArgs } from 'node:util' |
| 6 | + |
| 7 | +import { printLog, runMain } from './lib/executionUtils.ts' |
| 8 | +import { getSfLwcClientId, getSfLwcInstanceUrl, getSfLwcJwtPrivateKey, getSfLwcUsername } from './lib/secrets.ts' |
| 9 | +import { command } from './lib/command.ts' |
| 10 | + |
| 11 | +const repositoryRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..') |
| 12 | +const salesforceAppDir = resolve(repositoryRoot, 'test/apps/sf-lwc-app') |
| 13 | +const SALESFORCE_HOME_PATH = '/lightning/app/c__SF_LWC_App/page/home' |
| 14 | +const TARGET_ORG = 'sf-lwc-ci' |
| 15 | + |
| 16 | +runMain(() => { |
| 17 | + const { values, positionals } = parseArgs({ |
| 18 | + allowPositionals: true, |
| 19 | + options: { |
| 20 | + help: { |
| 21 | + type: 'boolean', |
| 22 | + short: 'h', |
| 23 | + }, |
| 24 | + }, |
| 25 | + }) |
| 26 | + |
| 27 | + if (values.help) { |
| 28 | + showUsageAndExit() |
| 29 | + } |
| 30 | + |
| 31 | + if (positionals.length !== 1) { |
| 32 | + throw new Error('Usage: node scripts/salesforce-lwc-app.ts <deploy-app|get-url>') |
| 33 | + } |
| 34 | + |
| 35 | + const commandName = positionals[0] |
| 36 | + |
| 37 | + switch (commandName) { |
| 38 | + // Deploy the app to the Salesforce org. To be done only when the app is updated. |
| 39 | + case 'deploy-app': |
| 40 | + deployApp() |
| 41 | + break |
| 42 | + // Get the authenticated URL of the app. |
| 43 | + case 'get-url': |
| 44 | + printSalesforceLwcUrl() |
| 45 | + break |
| 46 | + default: |
| 47 | + throw new Error(`Unknown command "${commandName ?? ''}". Expected: deploy-app|get-url`) |
| 48 | + } |
| 49 | +}) |
| 50 | + |
| 51 | +function showUsageAndExit() { |
| 52 | + console.log('Usage: node scripts/salesforce-lwc-app.ts <deploy-app|get-url>') |
| 53 | + process.exit(0) |
| 54 | +} |
| 55 | + |
| 56 | +function authenticate(targetOrg: string) { |
| 57 | + // Temporary directory holding the JWT private key for the duration of authentication. |
| 58 | + // Using a unique temp dir avoids collisions when multiple CI jobs run in parallel. |
| 59 | + const keyDirectory = mkdtempSync(resolve(tmpdir(), 'sf-lwc-jwt-')) |
| 60 | + const serverKeyPath = resolve(keyDirectory, 'server.key') |
| 61 | + |
| 62 | + try { |
| 63 | + writeFileSync(serverKeyPath, Buffer.from(getSfLwcJwtPrivateKey(), 'base64').toString('utf8'), { mode: 0o600 }) |
| 64 | + // writeFileSync mode can be masked by the process umask; chmodSync guarantees owner-only access. |
| 65 | + chmodSync(serverKeyPath, 0o600) |
| 66 | + |
| 67 | + printLog(`Authenticating Salesforce CLI alias ${targetOrg}...`) |
| 68 | + command`sf org login jwt --client-id ${getSfLwcClientId()} --jwt-key-file ${serverKeyPath} --username ${getSfLwcUsername()} --instance-url ${getSfLwcInstanceUrl()} --alias ${targetOrg}` |
| 69 | + .withCurrentWorkingDirectory(salesforceAppDir) |
| 70 | + .withLogs() |
| 71 | + .run() |
| 72 | + printLog(`Salesforce CLI authenticated as ${targetOrg}.`) |
| 73 | + } finally { |
| 74 | + rmSync(keyDirectory, { recursive: true, force: true }) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +function deployApp() { |
| 79 | + authenticate(TARGET_ORG) |
| 80 | + |
| 81 | + printLog(`Deploying Salesforce LWC app to ${TARGET_ORG}...`) |
| 82 | + command`sf project deploy start --target-org ${TARGET_ORG} --source-dir force-app --ignore-conflicts --concise` |
| 83 | + .withCurrentWorkingDirectory(salesforceAppDir) |
| 84 | + .withLogs() |
| 85 | + .run() |
| 86 | + printLog('Salesforce LWC app deployed.') |
| 87 | +} |
| 88 | + |
| 89 | +function printSalesforceLwcUrl(): void { |
| 90 | + const path = new URL(SALESFORCE_HOME_PATH, 'https://salesforce.local') |
| 91 | + |
| 92 | + authenticate(TARGET_ORG) |
| 93 | + |
| 94 | + command`sf org open --target-org ${TARGET_ORG} --path ${path.pathname}${path.search} --url-only` |
| 95 | + .withCurrentWorkingDirectory(salesforceAppDir) |
| 96 | + .withLogs() |
| 97 | + .run() |
| 98 | +} |
0 commit comments