Skip to content

Commit b2d8938

Browse files
committed
fix: web support for treeContainer without cwd
1 parent 3707ec6 commit b2d8938

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/resolve/treeContainers.ts

Lines changed: 12 additions & 6 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 = process.cwd()) {
43+
this.cwd = cwd;
44+
}
3945
/**
4046
* Searches for a metadata component file in a container directory.
4147
*
@@ -104,31 +110,31 @@ export abstract class TreeContainer {
104110
export class NodeFSTreeContainer extends TreeContainer {
105111
public isDirectory(fsPath: SourcePath): boolean {
106112
// use stat instead of lstat to follow symlinks
107-
return statSync(fsPath).isDirectory();
113+
return statSync(join(this.cwd, fsPath)).isDirectory();
108114
}
109115

110116
public exists(fsPath: SourcePath): boolean {
111-
return existsSync(fsPath);
117+
return existsSync(join(this.cwd, fsPath));
112118
}
113119

114120
public readDirectory(fsPath: SourcePath): string[] {
115-
return readdirSync(fsPath);
121+
return readdirSync(join(this.cwd, fsPath));
116122
}
117123

118124
public readFile(fsPath: SourcePath): Promise<Buffer> {
119125
// significant enough performance increase using sync instead of fs.promise version
120-
return Promise.resolve(readFileSync(fsPath));
126+
return Promise.resolve(readFileSync(join(this.cwd, fsPath)));
121127
}
122128

123129
public readFileSync(fsPath: SourcePath): Buffer {
124-
return readFileSync(fsPath);
130+
return readFileSync(join(this.cwd, fsPath));
125131
}
126132

127133
public stream(fsPath: SourcePath): Readable {
128134
if (!this.exists(fsPath)) {
129135
throw new Error(`File not found: ${fsPath}`);
130136
}
131-
return createReadStream(fsPath);
137+
return createReadStream(join(this.cwd, fsPath));
132138
}
133139
}
134140

0 commit comments

Comments
 (0)