|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Salesforce, Inc. |
| 3 | + * SPDX-License-Identifier: Apache-2 |
| 4 | + * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +import {Args, Flags} from '@oclif/core'; |
| 7 | +import {BaseCommand} from '@salesforce/b2c-tooling-sdk/cli'; |
| 8 | +import {t} from '../../../i18n/index.js'; |
| 9 | + |
| 10 | +async function openBrowser(url: string): Promise<void> { |
| 11 | + try { |
| 12 | + const open = await import('open'); |
| 13 | + await open.default(url); |
| 14 | + } catch { |
| 15 | + // If open fails, the URL will still be printed to console |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +export default class SlasClientOpen extends BaseCommand<typeof SlasClientOpen> { |
| 20 | + static args = { |
| 21 | + clientId: Args.string({ |
| 22 | + description: 'SLAS client ID to open in the admin UI', |
| 23 | + required: true, |
| 24 | + }), |
| 25 | + }; |
| 26 | + |
| 27 | + static description = t('commands.slas.client.open.description', 'Open the SLAS Admin UI for a client'); |
| 28 | + |
| 29 | + static examples = [ |
| 30 | + '<%= config.bin %> <%= command.id %> my-client-id --tenant-id abcd_123', |
| 31 | + '<%= config.bin %> <%= command.id %> my-client-id --tenant-id abcd_123 --short-code kv7kzm78', |
| 32 | + ]; |
| 33 | + |
| 34 | + static flags = { |
| 35 | + ...BaseCommand.baseFlags, |
| 36 | + 'tenant-id': Flags.string({ |
| 37 | + description: 'SLAS tenant ID (organization ID)', |
| 38 | + env: 'SFCC_TENANT_ID', |
| 39 | + required: true, |
| 40 | + }), |
| 41 | + 'short-code': Flags.string({ |
| 42 | + description: 'SCAPI short code', |
| 43 | + env: 'SFCC_SHORTCODE', |
| 44 | + }), |
| 45 | + }; |
| 46 | + |
| 47 | + async run(): Promise<{url: string}> { |
| 48 | + const {'tenant-id': tenantId, 'short-code': shortCodeFlag} = this.flags; |
| 49 | + const {clientId} = this.args; |
| 50 | + |
| 51 | + const shortCode = shortCodeFlag ?? this.resolvedConfig.shortCode; |
| 52 | + |
| 53 | + if (!shortCode) { |
| 54 | + this.error( |
| 55 | + t( |
| 56 | + 'error.shortCodeRequired', |
| 57 | + 'SCAPI short code required. Provide --short-code, set SFCC_SHORTCODE, or configure short-code in dw.json.', |
| 58 | + ), |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + const url = `https://${shortCode}.api.commercecloud.salesforce.com/shopper/auth-admin/v1/ui/client?clientId=${encodeURIComponent(clientId)}&tenantId=${encodeURIComponent(tenantId)}`; |
| 63 | + |
| 64 | + this.log(t('commands.slas.client.open.opening', 'Opening SLAS Admin UI...')); |
| 65 | + this.log(url); |
| 66 | + |
| 67 | + await openBrowser(url); |
| 68 | + |
| 69 | + return {url}; |
| 70 | + } |
| 71 | +} |
0 commit comments