|
5 | 5 | * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
6 | 6 | */ |
7 | 7 |
|
8 | | -import { SfCommand } from '@salesforce/sf-plugins-core'; |
| 8 | +import fs from 'node:fs'; |
| 9 | +import path from 'node:path'; |
| 10 | +import { SfCommand, Flags } from '@salesforce/sf-plugins-core'; |
9 | 11 | import { Messages } from '@salesforce/core'; |
10 | 12 | import { cmpDev } from '@lwrjs/api'; |
| 13 | +import { PromptUtils } from '../../../shared/promptUtils.js'; |
11 | 14 |
|
12 | 15 | Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); |
13 | 16 | const messages = Messages.loadMessages('@salesforce/plugin-lightning-dev', 'lightning.dev.component'); |
14 | 17 |
|
| 18 | +const MODULES_DIR = './force-app/main/default/lwc'; |
| 19 | + |
| 20 | +function getDirectories(filePath: string): string[] { |
| 21 | + try { |
| 22 | + const items = fs.readdirSync(filePath); |
| 23 | + |
| 24 | + const directories = items.filter((item) => fs.statSync(path.join(filePath, item)).isDirectory()); |
| 25 | + |
| 26 | + return directories; |
| 27 | + } catch (error) { |
| 28 | + return []; |
| 29 | + } |
| 30 | +} |
| 31 | + |
15 | 32 | export default class LightningDevComponent extends SfCommand<void> { |
16 | 33 | public static readonly summary = messages.getMessage('summary'); |
17 | 34 | public static readonly description = messages.getMessage('description'); |
18 | 35 | public static readonly examples = messages.getMessages('examples'); |
19 | 36 |
|
| 37 | + public static readonly flags = { |
| 38 | + name: Flags.string({ |
| 39 | + summary: messages.getMessage('flags.name.summary'), |
| 40 | + char: 'n', |
| 41 | + requiredOrDefaulted: false, |
| 42 | + }), |
| 43 | + }; |
| 44 | + |
20 | 45 | public async run(): Promise<void> { |
| 46 | + const { flags } = await this.parse(LightningDevComponent); |
| 47 | + |
| 48 | + let name = flags.name; |
| 49 | + if (!name) { |
| 50 | + const dirs = getDirectories(path.resolve(MODULES_DIR)); |
| 51 | + if (!dirs) { |
| 52 | + throw new Error(messages.getMessage('error.directory')); |
| 53 | + } |
| 54 | + |
| 55 | + const components = dirs.map((dir) => { |
| 56 | + const xmlPath = path.resolve(path.join(MODULES_DIR, dir, `${dir}.js-meta.xml`)); |
| 57 | + const xmlContent = fs.readFileSync(xmlPath, 'utf-8'); |
| 58 | + const label = xmlContent.match(/<masterLabel>(.*?)<\/masterLabel>/); |
| 59 | + const description = xmlContent.match(/<description>(.*?)<\/description>/); |
| 60 | + |
| 61 | + return { |
| 62 | + name: dir, |
| 63 | + label: label ? label[1] : '', |
| 64 | + description: description ? description[1] : '', |
| 65 | + }; |
| 66 | + }); |
| 67 | + |
| 68 | + name = await PromptUtils.promptUserToSelectComponent(components); |
| 69 | + if (!name) { |
| 70 | + throw new Error(messages.getMessage('error.component')); |
| 71 | + } |
| 72 | + } |
| 73 | + |
21 | 74 | this.log('Starting application on port 3000...'); |
22 | 75 |
|
23 | 76 | const port = parseInt(process.env.PORT ?? '3000', 10); |
24 | 77 |
|
25 | 78 | await cmpDev({ |
26 | 79 | mode: 'dev', |
27 | 80 | port, |
| 81 | + name: `c/${name}`, |
28 | 82 | }); |
29 | 83 | } |
30 | 84 | } |
0 commit comments