|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | +import { SfCommand, Flags } from '@salesforce/sf-plugins-core'; |
| 8 | +import { Messages } from '@salesforce/core'; |
| 9 | +import { cmpDev } from '@lwrjs/api'; |
| 10 | + |
| 11 | +Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); |
| 12 | +const messages = Messages.loadMessages('@salesforce/plugin-lightning-dev', 'lightning.dev.component'); |
| 13 | + |
| 14 | +export default class LightningDevComponent extends SfCommand<void> { |
| 15 | + public static readonly summary = messages.getMessage('summary'); |
| 16 | + public static readonly description = messages.getMessage('description'); |
| 17 | + public static readonly examples = messages.getMessages('examples'); |
| 18 | + |
| 19 | + public static readonly flags = { |
| 20 | + 'target-org': Flags.optionalOrg(), // Don't necessarily require org unless we need to proxy |
| 21 | + name: Flags.string({ |
| 22 | + char: 'n', |
| 23 | + summary: messages.getMessage('flags.name.summary'), |
| 24 | + required: true, |
| 25 | + }), |
| 26 | + namespace: Flags.string({ |
| 27 | + char: 's', |
| 28 | + summary: messages.getMessage('flags.namespace.summary'), |
| 29 | + default: 'c', |
| 30 | + }), |
| 31 | + attributes: Flags.string({ |
| 32 | + char: 'a', |
| 33 | + summary: messages.getMessage('flags.attributes.summary'), |
| 34 | + }), |
| 35 | + }; |
| 36 | + |
| 37 | + public async run(): Promise<void> { |
| 38 | + const { flags } = await this.parse(LightningDevComponent); |
| 39 | + |
| 40 | + const connection = flags['target-org']?.getConnection(); |
| 41 | + |
| 42 | + let attributes = {}; |
| 43 | + if (flags.attributes) { |
| 44 | + try { |
| 45 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 46 | + attributes = JSON.parse(flags.attributes); |
| 47 | + } catch (e) { |
| 48 | + throw new Error(messages.getMessage('error.invalid.attributes')); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + await cmpDev({ |
| 53 | + componentName: flags.name, |
| 54 | + namespace: flags.namespace, |
| 55 | + attributes, |
| 56 | + mode: 'dev', |
| 57 | + siteDir: '', // TODO fix |
| 58 | + port: 3000, |
| 59 | + open: true, |
| 60 | + logLevel: 'info', |
| 61 | + authToken: connection?.accessToken ?? '', |
| 62 | + }); |
| 63 | + } |
| 64 | +} |
0 commit comments