Skip to content

Commit 6dd7c70

Browse files
authored
fix: web support without cwd W-20175875 (#1647)
* fix: web support for treeContainer without cwd * fix: only apply cwd if provided
1 parent c717def commit 6dd7c70

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/resolve/treeContainers.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ const messages = Messages.loadMessages('@salesforce/source-deploy-retrieve', 'sd
3636
* Extend this base class to implement a custom container.
3737
*/
3838
export abstract class TreeContainer {
39+
protected cwd?: string;
40+
41+
/** 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) {
43+
this.cwd = cwd;
44+
}
3945
/**
4046
* Searches for a metadata component file in a container directory.
4147
*
@@ -46,14 +52,20 @@ export abstract class TreeContainer {
4652
*/
4753
public find(fileType: 'content' | 'metadataXml', name: string, directory: string): string | undefined {
4854
const fileName = this.readDirectory(directory).find((entry) => {
49-
const parsed = parseMetadataXml(join(directory, entry));
55+
const parsed = parseMetadataXml(this.getUpdatedFsPath(join(directory, entry)));
5056
const metaXmlCondition = fileType === 'metadataXml' ? !!parsed : !parsed;
5157
return baseName(entry) === name && metaXmlCondition;
5258
});
5359
if (fileName) {
54-
return join(directory, fileName);
60+
return this.getUpdatedFsPath(join(directory, fileName));
5561
}
5662
}
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+
5769
/**
5870
* Whether or not a file path exists in the container.
5971
*
@@ -104,31 +116,31 @@ export abstract class TreeContainer {
104116
export class NodeFSTreeContainer extends TreeContainer {
105117
public isDirectory(fsPath: SourcePath): boolean {
106118
// use stat instead of lstat to follow symlinks
107-
return statSync(fsPath).isDirectory();
119+
return statSync(this.getUpdatedFsPath(fsPath)).isDirectory();
108120
}
109121

110122
public exists(fsPath: SourcePath): boolean {
111-
return existsSync(fsPath);
123+
return existsSync(this.getUpdatedFsPath(fsPath));
112124
}
113125

114126
public readDirectory(fsPath: SourcePath): string[] {
115-
return readdirSync(fsPath);
127+
return readdirSync(this.getUpdatedFsPath(fsPath));
116128
}
117129

118130
public readFile(fsPath: SourcePath): Promise<Buffer> {
119131
// significant enough performance increase using sync instead of fs.promise version
120-
return Promise.resolve(readFileSync(fsPath));
132+
return Promise.resolve(readFileSync(this.getUpdatedFsPath(fsPath)));
121133
}
122134

123135
public readFileSync(fsPath: SourcePath): Buffer {
124-
return readFileSync(fsPath);
136+
return readFileSync(this.getUpdatedFsPath(fsPath));
125137
}
126138

127139
public stream(fsPath: SourcePath): Readable {
128140
if (!this.exists(fsPath)) {
129141
throw new Error(`File not found: ${fsPath}`);
130142
}
131-
return createReadStream(fsPath);
143+
return createReadStream(this.getUpdatedFsPath(fsPath));
132144
}
133145
}
134146

0 commit comments

Comments
 (0)