-
Notifications
You must be signed in to change notification settings - Fork 682
Expand file tree
/
Copy pathindex.ts
More file actions
417 lines (346 loc) · 11.4 KB
/
index.ts
File metadata and controls
417 lines (346 loc) · 11.4 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import * as fs from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
import * as stream from 'stream';
import * as safe from './safe';
export * from 'fs-extra';
export { stat as statSafe, readdir as readdirSafe } from './safe';
export interface ReaddirPOptions {
/**
* Filter out items from the walk process from the final result.
*
* @return `true` to keep, otherwise the item is filtered out
*/
readonly filter?: (item: WalkerItem) => boolean;
/**
* Called whenever an error occurs during the walk process.
*
* If excluded, the function will throw an error when first encountered.
*/
readonly onError?: (err: Error) => void;
readonly walkerOptions?: WalkerOptions;
}
export async function readdirp(dir: string, { filter, onError, walkerOptions }: ReaddirPOptions = {}): Promise<string[]> {
return new Promise<string[]>((resolve, reject) => {
const items: string[] = [];
let rs: NodeJS.ReadableStream = walk(dir, walkerOptions);
if (filter) {
rs = rs.pipe(new stream.Transform({
objectMode: true,
transform(obj: WalkerItem, enc, cb) {
if (!filter || filter(obj)) {
this.push(obj);
}
cb();
},
}));
}
rs
.on('error', (err: Error) => onError ? onError(err) : reject(err))
.on('data', (item: WalkerItem) => items.push(item.path))
.on('end', () => resolve(items));
});
}
export const enum FileType {
FILE = 'file',
DIRECTORY = 'directory',
}
export interface RegularFileNode {
path: string;
type: FileType.FILE;
parent: FileNode;
}
export interface DirectoryNode {
path: string;
type: FileType.DIRECTORY;
parent?: FileNode;
children: FileNode[];
}
export type FileNode = RegularFileNode | DirectoryNode;
export interface GetFileTreeOptions<RE = {}, DE = {}> {
/**
* Called whenever an error occurs during the walk process.
*
* If excluded, the function will throw an error when first encountered.
*/
readonly onError?: (err: Error) => void;
/**
* Called whenever a file node is added to the tree.
*
* File nodes can be supplemented by returning a new object from this
* function.
*/
readonly onFileNode?: (node: RegularFileNode) => RegularFileNode & RE;
/**
* Called whenever a directory node is added to the tree.
*
* Directory nodes can be supplemented by returning a new object from this
* function.
*/
readonly onDirectoryNode?: (node: DirectoryNode) => DirectoryNode & DE;
readonly walkerOptions?: WalkerOptions;
}
/**
* Compile and return a file tree structure.
*
* This function walks a directory structure recursively, building a nested
* object structure in memory that represents it. When finished, the root
* directory node is returned.
*
* @param dir The root directory from which to compile the file tree
*/
export async function getFileTree<RE = {}, DE = {}>(dir: string, { onError, onFileNode = n => n as RegularFileNode & RE, onDirectoryNode = n => n as DirectoryNode & DE, walkerOptions }: GetFileTreeOptions<RE, DE> = {}): Promise<RegularFileNode & RE | DirectoryNode & DE> {
const fileMap = new Map<string, RegularFileNode & RE | DirectoryNode & DE>([]);
const getOrCreateParent = (item: WalkerItem): DirectoryNode & DE => {
const parentPath = path.dirname(item.path);
const parent = fileMap.get(parentPath);
if (parent && parent.type === FileType.DIRECTORY) {
return parent;
}
return onDirectoryNode({ path: parentPath, type: FileType.DIRECTORY, children: [] });
};
const createFileNode = (item: WalkerItem, parent: DirectoryNode & DE): RegularFileNode & RE | DirectoryNode & DE => {
const node = { path: item.path, parent };
return item.stats.isDirectory() ?
onDirectoryNode({ ...node, type: FileType.DIRECTORY, children: [] }) :
onFileNode({ ...node, type: FileType.FILE });
};
return new Promise<RegularFileNode & RE | DirectoryNode & DE>((resolve, reject) => {
dir = path.resolve(dir);
const rs = walk(dir, walkerOptions);
rs
.on('error', err => onError ? onError(err) : reject(err))
.on('data', item => {
const parent = getOrCreateParent(item);
const node = createFileNode(item, parent);
parent.children.push(node);
fileMap.set(item.path, node);
fileMap.set(parent.path, parent);
})
.on('end', () => {
const root = fileMap.get(dir);
if (!root) {
return reject(new Error('No root node found after walking directory structure.'));
}
delete root.parent;
resolve(root);
});
});
}
export async function fileToString(filePath: string): Promise<string> {
try {
return await fs.readFile(filePath, { encoding: 'utf8' });
} catch (e: any) {
if (e.code === 'ENOENT' || e.code === 'ENOTDIR') {
return '';
}
throw e;
}
}
export async function getFileChecksum(filePath: string): Promise<string> {
const crypto = await import('crypto');
return new Promise<string>((resolve, reject) => {
const hash = crypto.createHash('md5');
const input = fs.createReadStream(filePath);
input.on('error', (err: Error) => {
reject(err);
});
hash.once('readable', () => {
const fullChecksum = (hash.read() as Buffer).toString('hex');
resolve(fullChecksum);
});
input.pipe(hash);
});
}
/**
* Return true and cached checksums for a file by its path.
*
* Cached checksums are stored as `.md5` files next to the original file. If
* the cache file is missing, the cached checksum is undefined.
*
* @param p The file path
* @return Promise<[true checksum, cached checksum or undefined if cache file missing]>
*/
export async function getFileChecksums(p: string): Promise<[string, string | undefined]> {
return Promise.all([
getFileChecksum(p),
(async () => {
try {
const md5 = await fs.readFile(`${p}.md5`, { encoding: 'utf8' });
return md5.trim();
} catch (e: any) {
if (e.code !== 'ENOENT') {
throw e;
}
}
})(),
]);
}
/**
* Store a cache file containing the source file's md5 checksum hash.
*
* @param p The file path
* @param checksum The checksum. If excluded, the checksum is computed
*/
export async function cacheFileChecksum(p: string, checksum?: string): Promise<void> {
const md5 = await getFileChecksum(p);
await fs.writeFile(`${p}.md5`, md5, { encoding: 'utf8' });
}
export function writeStreamToFile(stream: NodeJS.ReadableStream, destination: string): Promise<any> {
return new Promise((resolve, reject) => {
const dest = fs.createWriteStream(destination);
stream.pipe(dest);
dest.on('error', reject);
dest.on('finish', resolve);
});
}
export async function pathAccessible(filePath: string, mode: number): Promise<boolean> {
try {
await fs.access(filePath, mode);
} catch (e: any) {
return false;
}
return true;
}
export async function pathExists(filePath: string): Promise<boolean> {
return pathAccessible(filePath, fs.constants.F_OK);
}
export async function pathReadable(filePath: string): Promise<boolean> {
return pathAccessible(filePath, fs.constants.R_OK);
}
export async function pathWritable(filePath: string): Promise<boolean> {
return pathAccessible(filePath, fs.constants.W_OK);
}
export async function pathExecutable(filePath: string): Promise<boolean> {
return pathAccessible(filePath, fs.constants.X_OK);
}
export async function isExecutableFile(filePath: string): Promise<boolean> {
const [ stats, executable ] = await (Promise.all([
safe.stat(filePath),
pathExecutable(filePath),
]));
return !!stats && (stats.isFile() || stats.isSymbolicLink()) && executable;
}
/**
* Find the base directory based on the path given and a marker file to look for.
*/
export async function findBaseDirectory(dir: string, file: string): Promise<string | undefined> {
if (!dir || !file) {
return;
}
for (const d of compilePaths(dir)) {
const results = await safe.readdir(d);
if (results.includes(file)) {
return d;
}
}
}
/**
* Generate a random file path within the computer's temporary directory.
*
* @param prefix Optionally provide a filename prefix.
*/
export function tmpfilepath(prefix?: string): string {
const rn = Math.random().toString(16).substring(2, 8);
const p = path.resolve(os.tmpdir(), prefix ? `${prefix}-${rn}` : rn);
return p;
}
/**
* Given an absolute system path, compile an array of paths working backwards
* one directory at a time, always ending in the root directory.
*
* For example, `'/some/dir'` => `['/some/dir', '/some', '/']`
*
* @param filePath Absolute system base path.
*/
export function compilePaths(filePath: string): string[] {
filePath = path.normalize(filePath);
if (!path.isAbsolute(filePath)) {
throw new Error(`${filePath} is not an absolute path`);
}
const parsed = path.parse(filePath);
if (filePath === parsed.root) {
return [filePath];
}
return filePath
.slice(parsed.root.length)
.split(path.sep)
.map((segment, i, array) => parsed.root + path.join(...array.slice(0, array.length - i)))
.concat(parsed.root);
}
export interface WalkerItem {
path: string;
stats: fs.Stats;
}
export interface WalkerOptions {
/**
* Filter out file paths during walk.
*
* As the file tree is walked, this function can be used to exclude files and
* directories from the final result.
*
* It can also be used to tune performance. If a subdirectory is excluded, it
* is not walked.
*
* @param p The file path.
* @return `true` to include file path, otherwise it is excluded
*/
readonly pathFilter?: (p: string) => boolean;
/**
* Follow symlinks during walk.
*
* Additionally, the `IONIC_UTILS_FS_FOLLOW_SYMLINKS` environment variable can
* be used to set this option as `true` or `false`.
*
* @default false
*/
readonly followSymlinks?: boolean;
}
export interface Walker extends stream.Readable {
on(event: 'data', callback: (item: WalkerItem) => void): this;
on(event: string, callback: (...args: any[]) => any): this;
}
export class Walker extends stream.Readable {
readonly paths: string[] = [this.p];
constructor(readonly p: string, readonly options: WalkerOptions = {}) {
super({ objectMode: true });
}
_read() {
const p = this.paths.shift();
const { pathFilter, followSymlinks } = this.options;
if (!p) {
this.push(null);
return;
}
const envFollowSymlinks = process.env.IONIC_UTILS_FS_FOLLOW_SYMLINKS &&
process.env.IONIC_UTILS_FS_FOLLOW_SYMLINKS === 'true' ||
process.env.IONIC_UTILS_FS_FOLLOW_SYMLINKS === '1';
const finalPath = (followSymlinks || envFollowSymlinks) ? fs.realpathSync(p) : p;
fs.lstat(finalPath, (err, stats) => {
if (err) {
this.emit('error', err);
return;
}
const item: WalkerItem = { path: p, stats };
if (stats.isDirectory()) {
fs.readdir(p, (err, contents) => {
if (err) {
this.emit('error', err);
return;
}
let paths = contents.map(file => path.join(p, file));
if (pathFilter) {
paths = paths.filter(p => pathFilter(p.substring(this.p.length + 1)));
}
this.paths.push(...paths);
this.push(item);
});
} else {
this.push(item);
}
});
}
}
export function walk(p: string, options: WalkerOptions = {}): Walker {
return new Walker(p, options);
}