@@ -36,10 +36,10 @@ const messages = Messages.loadMessages('@salesforce/source-deploy-retrieve', 'sd
3636 * Extend this base class to implement a custom container.
3737 */
3838export abstract class TreeContainer {
39- protected cwd : string ;
39+ protected cwd ? : string ;
4040
4141 /** specify a cwd to use instead of the real cwd. Useful for tests and web environments where process.cwd is undefined */
42- public constructor ( cwd : string = process . cwd ( ) ) {
42+ public constructor ( cwd ? : string ) {
4343 this . cwd = cwd ;
4444 }
4545 /**
@@ -52,14 +52,20 @@ export abstract class TreeContainer {
5252 */
5353 public find ( fileType : 'content' | 'metadataXml' , name : string , directory : string ) : string | undefined {
5454 const fileName = this . readDirectory ( directory ) . find ( ( entry ) => {
55- const parsed = parseMetadataXml ( join ( directory , entry ) ) ;
55+ const parsed = parseMetadataXml ( this . getUpdatedFsPath ( join ( directory , entry ) ) ) ;
5656 const metaXmlCondition = fileType === 'metadataXml' ? ! ! parsed : ! parsed ;
5757 return baseName ( entry ) === name && metaXmlCondition ;
5858 } ) ;
5959 if ( fileName ) {
60- return join ( directory , fileName ) ;
60+ return this . getUpdatedFsPath ( join ( directory , fileName ) ) ;
6161 }
6262 }
63+
64+ /** if the container has cwd set, apply it to the path */
65+ protected getUpdatedFsPath ( fsPath : SourcePath ) : string {
66+ return this . cwd ? join ( this . cwd , fsPath ) : fsPath ;
67+ }
68+
6369 /**
6470 * Whether or not a file path exists in the container.
6571 *
@@ -110,31 +116,31 @@ export abstract class TreeContainer {
110116export class NodeFSTreeContainer extends TreeContainer {
111117 public isDirectory ( fsPath : SourcePath ) : boolean {
112118 // use stat instead of lstat to follow symlinks
113- return statSync ( join ( this . cwd , fsPath ) ) . isDirectory ( ) ;
119+ return statSync ( this . getUpdatedFsPath ( fsPath ) ) . isDirectory ( ) ;
114120 }
115121
116122 public exists ( fsPath : SourcePath ) : boolean {
117- return existsSync ( join ( this . cwd , fsPath ) ) ;
123+ return existsSync ( this . getUpdatedFsPath ( fsPath ) ) ;
118124 }
119125
120126 public readDirectory ( fsPath : SourcePath ) : string [ ] {
121- return readdirSync ( join ( this . cwd , fsPath ) ) ;
127+ return readdirSync ( this . getUpdatedFsPath ( fsPath ) ) ;
122128 }
123129
124130 public readFile ( fsPath : SourcePath ) : Promise < Buffer > {
125131 // significant enough performance increase using sync instead of fs.promise version
126- return Promise . resolve ( readFileSync ( join ( this . cwd , fsPath ) ) ) ;
132+ return Promise . resolve ( readFileSync ( this . getUpdatedFsPath ( fsPath ) ) ) ;
127133 }
128134
129135 public readFileSync ( fsPath : SourcePath ) : Buffer {
130- return readFileSync ( join ( this . cwd , fsPath ) ) ;
136+ return readFileSync ( this . getUpdatedFsPath ( fsPath ) ) ;
131137 }
132138
133139 public stream ( fsPath : SourcePath ) : Readable {
134140 if ( ! this . exists ( fsPath ) ) {
135141 throw new Error ( `File not found: ${ fsPath } ` ) ;
136142 }
137- return createReadStream ( join ( this . cwd , fsPath ) ) ;
143+ return createReadStream ( this . getUpdatedFsPath ( fsPath ) ) ;
138144 }
139145}
140146
0 commit comments