77
88import * as path from 'path' ;
99import { ComponentSet , RegistryAccess } from '@salesforce/source-deploy-retrieve' ;
10- import { ComponentLike } from '@salesforce/source-deploy-retrieve/lib/src/resolve/types' ;
1110import { fs , SfdxError , Logger } from '@salesforce/core' ;
1211
1312export type ManifestOption = {
@@ -38,64 +37,69 @@ export class ComponentSetBuilder {
3837 */
3938 public static async build ( options : ComponentSetOptions ) : Promise < ComponentSet > {
4039 const logger = Logger . childFromRoot ( 'createComponentSet' ) ;
41- const csAggregator : ComponentLike [ ] = [ ] ;
40+ let componentSet : ComponentSet ;
4241
4342 const { sourcepath, manifest, metadata, packagenames, apiversion, sourceapiversion } = options ;
4443 try {
4544 if ( sourcepath ) {
4645 logger . debug ( `Building ComponentSet from sourcepath: ${ sourcepath . toString ( ) } ` ) ;
46+ const fsPaths : string [ ] = [ ] ;
4747 sourcepath . forEach ( ( filepath ) => {
48- if ( fs . fileExistsSync ( filepath ) ) {
49- csAggregator . push ( ...ComponentSet . fromSource ( path . resolve ( filepath ) ) ) ;
50- } else {
48+ if ( ! fs . fileExistsSync ( filepath ) ) {
5149 throw new SfdxError ( `The sourcepath "${ filepath } " is not a valid source file path.` ) ;
5250 }
51+ fsPaths . push ( path . resolve ( filepath ) ) ;
5352 } ) ;
53+ componentSet = ComponentSet . fromSource ( { fsPaths } ) ;
5454 }
5555
5656 // Return empty ComponentSet and use packageNames in the library via `.retrieve` options
5757 if ( packagenames ) {
5858 logger . debug ( `Building ComponentSet for packagenames: ${ packagenames . toString ( ) } ` ) ;
59- csAggregator . push ( ... new ComponentSet ( [ ] ) ) ;
59+ componentSet ??= new ComponentSet ( ) ;
6060 }
6161
6262 // Resolve manifest with source in package directories.
6363 if ( manifest ) {
6464 logger . debug ( `Building ComponentSet from manifest: ${ manifest . manifestPath } ` ) ;
6565 const directoryPaths = options . manifest . directoryPaths ;
6666 logger . debug ( `Searching in packageDir: ${ directoryPaths . join ( ', ' ) } for matching metadata` ) ;
67- const compSet = await ComponentSet . fromManifest ( {
67+ componentSet = await ComponentSet . fromManifest ( {
6868 manifestPath : manifest . manifestPath ,
6969 resolveSourcePaths : options . manifest . directoryPaths ,
7070 forceAddWildcards : true ,
7171 } ) ;
72- csAggregator . push ( ...compSet ) ;
7372 }
7473
7574 // Resolve metadata entries with source in package directories.
7675 if ( metadata ) {
7776 logger . debug ( `Building ComponentSet from metadata: ${ metadata . metadataEntries . toString ( ) } ` ) ;
7877 const registry = new RegistryAccess ( ) ;
78+ const compSetFilter = new ComponentSet ( ) ;
79+ componentSet ??= new ComponentSet ( ) ;
7980
8081 // Build a Set of metadata entries
81- const filter = new ComponentSet ( ) ;
82- metadata . metadataEntries . forEach ( ( entry ) => {
83- const splitEntry = entry . split ( ':' ) ;
84- // try and get the type by name to ensure no typos or errors in type name
85- // matches toolbelt functionality
82+ metadata . metadataEntries . forEach ( ( rawEntry ) => {
83+ const splitEntry = rawEntry . split ( ':' ) ;
84+ // The registry will throw if it doesn't know what this type is.
8685 registry . getTypeByName ( splitEntry [ 0 ] ) ;
87- filter . add ( {
86+ const entry = {
8887 type : splitEntry [ 0 ] ,
8988 fullName : splitEntry . length === 1 ? '*' : splitEntry [ 1 ] ,
90- } ) ;
89+ } ;
90+ // Add to the filtered ComponentSet for resolved source paths,
91+ // and the unfiltered ComponentSet to build the correct manifest.
92+ compSetFilter . add ( entry ) ;
93+ componentSet . add ( entry ) ;
9194 } ) ;
9295
9396 const directoryPaths = options . metadata . directoryPaths ;
9497 logger . debug ( `Searching for matching metadata in directories: ${ directoryPaths . join ( ', ' ) } ` ) ;
95- const fromSource = ComponentSet . fromSource ( { fsPaths : directoryPaths , include : filter } ) ;
96- // If no matching metadata is found, default to the original component set
97- const finalized = fromSource . size > 0 ? fromSource : filter ;
98- csAggregator . push ( ...finalized ) ;
98+ const resolvedComponents = ComponentSet . fromSource ( { fsPaths : directoryPaths , include : compSetFilter } ) ;
99+ componentSet . forceIgnoredPaths = resolvedComponents . forceIgnoredPaths ;
100+ for ( const comp of resolvedComponents ) {
101+ componentSet . add ( comp ) ;
102+ }
99103 }
100104 } catch ( e ) {
101105 if ( ( e as Error ) . message . includes ( 'Missing metadata type definition in registry for id' ) ) {
@@ -108,8 +112,6 @@ export class ComponentSetBuilder {
108112 }
109113 }
110114
111- const componentSet = new ComponentSet ( csAggregator ) ;
112-
113115 // This is only for debug output of matched files based on the command flags.
114116 // It will log up to 20 file matches.
115117 if ( logger . debugEnabled && componentSet . size ) {
0 commit comments