|
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 fs from 'node:fs'; |
| 9 | +import path from 'node:path'; |
8 | 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 |
|
15 | | -export type LightningDevComponentResult = { |
16 | | - path: string; |
17 | | -}; |
| 18 | +// TODO support other module directories |
| 19 | +const MODULES_DIR = path.resolve(path.join('force-app', 'main', 'default', 'lwc')); |
18 | 20 |
|
19 | | -export default class LightningDevComponent extends SfCommand<LightningDevComponentResult> { |
| 21 | +function getDirectories(filePath: string): string[] { |
| 22 | + try { |
| 23 | + const items = fs.readdirSync(filePath); |
| 24 | + |
| 25 | + const directories = items.filter((item) => fs.statSync(path.join(filePath, item)).isDirectory()); |
| 26 | + |
| 27 | + return directories; |
| 28 | + } catch (error) { |
| 29 | + return []; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +export default class LightningDevComponent extends SfCommand<void> { |
20 | 34 | public static readonly summary = messages.getMessage('summary'); |
21 | 35 | public static readonly description = messages.getMessage('description'); |
22 | 36 | public static readonly examples = messages.getMessages('examples'); |
23 | 37 |
|
24 | 38 | public static readonly flags = { |
25 | 39 | name: Flags.string({ |
26 | 40 | summary: messages.getMessage('flags.name.summary'), |
27 | | - description: messages.getMessage('flags.name.description'), |
28 | 41 | char: 'n', |
29 | | - required: false, |
| 42 | + requiredOrDefaulted: false, |
30 | 43 | }), |
31 | 44 | // TODO should this be required or optional? |
32 | 45 | // We don't technically need this if your components are simple / don't need any data from your org |
33 | | - 'target-org': Flags.requiredOrg(), |
| 46 | + 'target-org': Flags.optionalOrg(), |
34 | 47 | }; |
35 | 48 |
|
36 | | - public async run(): Promise<LightningDevComponentResult> { |
| 49 | + public async run(): Promise<void> { |
37 | 50 | const { flags } = await this.parse(LightningDevComponent); |
38 | 51 |
|
39 | | - const name = flags.name ?? 'world'; |
40 | | - this.log(`preview component: ${name}`); |
| 52 | + let name = flags.name; |
| 53 | + if (!name) { |
| 54 | + const dirs = getDirectories(path.resolve(MODULES_DIR)); |
| 55 | + if (!dirs) { |
| 56 | + throw new Error(messages.getMessage('error.directory')); |
| 57 | + } |
| 58 | + |
| 59 | + const components = dirs.map((dir) => { |
| 60 | + const xmlPath = path.resolve(path.join(MODULES_DIR, dir, `${dir}.js-meta.xml`)); |
| 61 | + const xmlContent = fs.readFileSync(xmlPath, 'utf-8'); |
| 62 | + const label = xmlContent.match(/<masterLabel>(.*?)<\/masterLabel>/); |
| 63 | + const description = xmlContent.match(/<description>(.*?)<\/description>/); |
| 64 | + |
| 65 | + return { |
| 66 | + name: dir, |
| 67 | + label: label ? label[1] : '', |
| 68 | + description: description ? description[1] : '', |
| 69 | + }; |
| 70 | + }); |
| 71 | + |
| 72 | + name = await PromptUtils.promptUserToSelectComponent(components); |
| 73 | + if (!name) { |
| 74 | + throw new Error(messages.getMessage('error.component')); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + this.log('Starting application on port 3000...'); |
| 79 | + |
| 80 | + const port = parseInt(process.env.PORT ?? '3000', 10); |
41 | 81 |
|
42 | | - // TODO implement me |
43 | | - // eslint-disable-next-line @typescript-eslint/no-unsafe-call |
44 | 82 | await cmpDev({ |
45 | | - componentName: name, |
| 83 | + mode: 'dev', |
| 84 | + port, |
| 85 | + name: `c/${name}`, |
46 | 86 | }); |
47 | | - |
48 | | - return { |
49 | | - path: '', |
50 | | - }; |
51 | 87 | } |
52 | 88 | } |
0 commit comments