-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathtreeContainers.ts
322 lines (290 loc) · 11.2 KB
/
treeContainers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
/* eslint-disable class-methods-use-this */
import { join, dirname, basename, normalize, sep } from 'node:path';
import { Readable } from 'node:stream';
import { statSync, existsSync, readdirSync, createReadStream, promises, readFileSync } from 'graceful-fs';
import JSZip from 'jszip';
import { Messages, SfError } from '@salesforce/core';
import { isString } from '@salesforce/ts-types';
import { baseName, parseMetadataXml } from '../utils/path';
import type { SourcePath } from '../common/types';
import type { VirtualDirectory } from './types';
Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/source-deploy-retrieve', 'sdr');
/**
* A container for interacting with a file system. Operations such as component resolution,
* conversion, and packaging perform I/O against `TreeContainer` abstractions.
*
* Extend this base class to implement a custom container.
*/
export abstract class TreeContainer {
/**
* Searches for a metadata component file in a container directory.
*
* @param fileType - The type of component file
* @param name - The name of the file without a suffix
* @param directory - The directory to search in
* @returns The first path that meets the criteria, or `undefined` if none were found
*/
public find(fileType: 'content' | 'metadataXml', name: string, directory: string): string | undefined {
const fileName = this.readDirectory(directory).find((entry) => {
const parsed = parseMetadataXml(join(directory, entry));
const metaXmlCondition = fileType === 'metadataXml' ? !!parsed : !parsed;
return baseName(entry) === name && metaXmlCondition;
});
if (fileName) {
return join(directory, fileName);
}
}
/**
* Whether or not a file path exists in the container.
*
* @param fsPath - File path to test
* @returns `true` if the path exists
*/
public abstract exists(fsPath: SourcePath): boolean;
/**
* Whether or not a file path is a directory in the container.
*
* @param fsPath - File path to test
* @returns `true` if the path is to a directory
*/
public abstract isDirectory(fsPath: SourcePath): boolean;
/**
* Reads the contents of a directory in the container.
*
* @param fsPath Path to directory
* @returns An array of file and directory names in the directory
*/
public abstract readDirectory(fsPath: SourcePath): string[];
/**
* Reads the contents of a file.
*
* @param fsPath
* @returns A buffer of the file contents
*/
public abstract readFile(fsPath: SourcePath): Promise<Buffer>;
/**
* Reads the contents of a file synchronously.
*
* @param fsPath
* @returns A buffer of the file contents
*/
public abstract readFileSync(fsPath: SourcePath): Buffer;
/**
* Creates a readable stream of a file's contents.
*
* @param fsPath - File path to create a readable stream from
* @returns A readable stream
*/
public abstract stream(fsPath: SourcePath): Readable;
}
/**
* A {@link TreeContainer} that wraps the NodeJS `fs` module.
*/
export class NodeFSTreeContainer extends TreeContainer {
public isDirectory(fsPath: SourcePath): boolean {
// use stat instead of lstat to follow symlinks
return statSync(fsPath).isDirectory();
}
public exists(fsPath: SourcePath): boolean {
return existsSync(fsPath);
}
public readDirectory(fsPath: SourcePath): string[] {
return readdirSync(fsPath);
}
public readFile(fsPath: SourcePath): Promise<Buffer> {
return promises.readFile(fsPath);
}
public readFileSync(fsPath: SourcePath): Buffer {
return readFileSync(fsPath);
}
public stream(fsPath: SourcePath): Readable {
if (!this.exists(fsPath)) {
throw new Error(`File not found: ${fsPath}`);
}
return createReadStream(fsPath);
}
}
/**
* A {@link TreeContainer} that performs I/O without unzipping it to the disk first.
*/
export class ZipTreeContainer extends TreeContainer {
private zip: JSZip;
private constructor(zip: JSZip) {
super();
this.zip = zip;
}
public static async create(buffer: Buffer): Promise<ZipTreeContainer> {
const zip = await JSZip.loadAsync(buffer, { createFolders: true });
return new ZipTreeContainer(zip);
}
public exists(fsPath: string): boolean {
return !!this.match(fsPath);
}
public isDirectory(fsPath: string): boolean {
const resolvedPath = this.match(fsPath);
if (resolvedPath) {
return this.ensureDirectory(resolvedPath);
}
throw new SfError(messages.getMessage('error_path_not_found', [fsPath]), 'LibraryError');
}
public readDirectory(fsPath: string): string[] {
const resolvedPath = this.match(fsPath);
if (resolvedPath && this.ensureDirectory(resolvedPath)) {
// Remove trailing path sep if it exists. JSZip always adds them for directories but
// when comparing we call `dirname()` which does not include them.
const dirPath = resolvedPath.endsWith('/') ? resolvedPath.slice(0, -1) : resolvedPath;
return Object.keys(this.zip.files)
.filter((filePath) => dirname(filePath) === dirPath)
.map((filePath) => basename(filePath));
}
throw new SfError(messages.getMessage('error_expected_directory_path', [fsPath]), 'LibraryError');
}
public async readFile(fsPath: string): Promise<Buffer> {
const resolvedPath = this.match(fsPath);
if (resolvedPath) {
const jsZipObj = this.zip.file(resolvedPath);
if (jsZipObj?.dir === false) {
return jsZipObj.async('nodebuffer');
}
throw new SfError(`Expected a file at path ${fsPath} but found a directory.`);
}
throw new SfError(messages.getMessage('error_expected_file_path', [fsPath]), 'LibraryError');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public readFileSync(fsPath: string): Buffer {
throw new Error('Method not implemented');
}
public stream(fsPath: string): Readable {
const resolvedPath = this.match(fsPath);
if (resolvedPath) {
const jsZipObj = this.zip.file(resolvedPath);
if (jsZipObj && !jsZipObj.dir) {
return new Readable().wrap(jsZipObj.nodeStream());
}
throw new SfError(messages.getMessage('error_no_directory_stream', [this.constructor.name]), 'LibraryError');
}
throw new SfError(messages.getMessage('error_expected_file_path', [fsPath]), 'LibraryError');
}
// Finds a matching entry in the zip by first comparing basenames, then dirnames.
// Note that zip files always use forward slash separators, so paths within the
// zip files are normalized for the OS file system before comparing.
private match(fsPath: string): string | undefined {
// "dot" has a special meaning as a directory name and always matches. Just return it.
if (fsPath === '.') {
return fsPath;
}
const fsPathBasename = basename(fsPath);
const fsPathDirname = dirname(fsPath);
return Object.keys(this.zip.files).find((filePath) => {
const normFilePath = normalize(filePath);
if (basename(normFilePath) === fsPathBasename) {
return dirname(normFilePath) === fsPathDirname;
}
});
}
private ensureDirectory(dirPath: string): boolean {
if (dirPath) {
// JSZip can have directory entries or only file entries (with virtual directory entries)
const zipObj = this.zip.file(dirPath);
return zipObj?.dir === true || !zipObj;
}
throw new SfError(messages.getMessage('error_path_not_found', [dirPath]), 'LibraryError');
}
}
/**
* A {@link TreeContainer} useful for mocking a file system.
*/
export class VirtualTreeContainer extends TreeContainer {
private tree = new Map<SourcePath, Set<SourcePath>>();
private fileContents = new Map<SourcePath, Buffer>();
public constructor(virtualFs: VirtualDirectory[]) {
super();
this.populate(virtualFs);
}
/**
* Designed for recreating virtual files from deleted files where the only information we have is the file's former location
* Any use of MetadataResolver was trying to access the non-existent files and throwing
*
* @param paths full paths to files
* @returns VirtualTreeContainer
*/
public static fromFilePaths(paths: string[]): VirtualTreeContainer {
// a map to reduce array iterations
const virtualDirectoryByFullPath = new Map<string, VirtualDirectory>();
paths
// defending against undefined being passed in. The metadata API sometimes responds missing fileName
.filter(isString)
.map((filename) => {
const splits = filename.split(sep);
for (let i = 0; i < splits.length - 1; i++) {
const fullPathSoFar = splits.slice(0, i + 1).join(sep);
const existing = virtualDirectoryByFullPath.get(fullPathSoFar);
virtualDirectoryByFullPath.set(fullPathSoFar, {
dirPath: fullPathSoFar,
// only add to children if we don't already have it
children: Array.from(new Set(existing?.children ?? []).add(splits[i + 1])),
});
}
});
return new VirtualTreeContainer(Array.from(virtualDirectoryByFullPath.values()));
}
public isDirectory(fsPath: string): boolean {
if (this.exists(fsPath)) {
return this.tree.has(fsPath);
}
throw new SfError(messages.getMessage('error_path_not_found', [fsPath]), 'LibraryError');
}
public exists(fsPath: string): boolean {
const files = this.tree.get(dirname(fsPath));
const isFile = files?.has(fsPath);
return this.tree.has(fsPath) || Boolean(isFile);
}
public readDirectory(fsPath: string): string[] {
if (this.isDirectory(fsPath)) {
return Array.from(this.tree.get(fsPath) ?? []).map((p) => basename(p));
}
throw new SfError(messages.getMessage('error_expected_directory_path', [fsPath]), 'LibraryError');
}
public readFile(fsPath: SourcePath): Promise<Buffer> {
return Promise.resolve(this.readFileSync(fsPath));
}
public readFileSync(fsPath: SourcePath): Buffer {
if (this.exists(fsPath)) {
let data = this.fileContents.get(fsPath);
if (!data) {
data = Buffer.from('');
this.fileContents.set(fsPath, data);
}
return data;
}
throw new SfError(messages.getMessage('error_path_not_found', [fsPath]), 'LibraryError');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public stream(fsPath: string): Readable {
throw new Error('Method not implemented');
}
private populate(virtualFs: VirtualDirectory[]): void {
for (const dir of virtualFs) {
const { dirPath, children } = dir;
this.tree.set(dirPath, new Set());
for (const child of children) {
const childPath = isString(child) ? join(dirPath, child) : join(dirPath, child.name);
const dirPathFromTree = this.tree.get(dirPath);
if (!dirPathFromTree) {
throw new SfError(`The directory at path ${dirPath} does not exist in the virtual file system.`);
}
dirPathFromTree.add(childPath);
if (typeof child === 'object' && child.data) {
this.fileContents.set(childPath, child.data);
}
}
}
}
}