55 * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66 */
77
8- import fs from 'node:fs' ;
98import path from 'node:path' ;
109import { SfCommand , Flags } from '@salesforce/sf-plugins-core' ;
11- import { Messages } from '@salesforce/core' ;
10+ import { Messages , SfProject } from '@salesforce/core' ;
1211import { cmpDev } from '@lwrjs/api' ;
12+ import { ComponentUtils } from '../../../shared/componentUtils.js' ;
1313import { PromptUtils } from '../../../shared/promptUtils.js' ;
1414
1515Messages . importMessagesDirectoryFromMetaUrl ( import . meta. url ) ;
1616const messages = Messages . loadMessages ( '@salesforce/plugin-lightning-dev' , 'lightning.dev.component' ) ;
1717
18- // TODO support other module directories
19- const MODULES_DIR = path . resolve ( path . join ( 'force-app' , 'main' , 'default' , 'lwc' ) ) ;
20-
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-
3318export default class LightningDevComponent extends SfCommand < void > {
3419 public static readonly summary = messages . getMessage ( 'summary' ) ;
3520 public static readonly description = messages . getMessage ( 'description' ) ;
@@ -47,27 +32,35 @@ export default class LightningDevComponent extends SfCommand<void> {
4732 } ;
4833
4934 public async run ( ) : Promise < void > {
35+ const project = await SfProject . resolve ( ) ;
5036 const { flags } = await this . parse ( LightningDevComponent ) ;
5137
5238 let name = flags . name ;
5339 if ( ! name ) {
54- const dirs = getDirectories ( path . resolve ( MODULES_DIR ) ) ;
40+ const dirs = await ComponentUtils . getComponentPaths ( project ) ;
5541 if ( ! dirs ) {
5642 throw new Error ( messages . getMessage ( 'error.directory' ) ) ;
5743 }
5844
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 ( / < m a s t e r L a b e l > ( .* ?) < \/ m a s t e r L a b e l > / ) ;
63- const description = xmlContent . match ( / < d e s c r i p t i o n > ( .* ?) < \/ d e s c r i p t i o n > / ) ;
64-
65- return {
66- name : dir ,
67- label : label ? label [ 1 ] : '' ,
68- description : description ? description [ 1 ] : '' ,
69- } ;
70- } ) ;
45+ const components = (
46+ await Promise . all (
47+ dirs . map ( async ( dir ) => {
48+ const xml = await ComponentUtils . getComponentMetadata ( dir ) ;
49+ if ( ! xml ) {
50+ return undefined ;
51+ }
52+
53+ const componentName = path . basename ( dir ) ;
54+ const label = ComponentUtils . componentNameToTitleCase ( componentName ) ;
55+
56+ return {
57+ name : componentName ,
58+ label : xml . LightningComponentBundle . masterLabel ?? label ,
59+ description : xml . LightningComponentBundle . description ?? '' ,
60+ } ;
61+ } )
62+ )
63+ ) . filter ( ( component ) => ! ! component ) ;
7164
7265 name = await PromptUtils . promptUserToSelectComponent ( components ) ;
7366 if ( ! name ) {
0 commit comments