UNPKG

5.7 kBTypeScriptView Raw
1/// <reference types="node" />
2import * as fs from 'fs-extra';
3import * as stream from 'stream';
4export * from 'fs-extra';
5export { stat as statSafe, readdir as readdirSafe } from './safe';
6export interface ReaddirPOptions {
7 /**
8 * Filter out items from the walk process from the final result.
9 *
10 * @return `true` to keep, otherwise the item is filtered out
11 */
12 readonly filter?: (item: WalkerItem) => boolean;
13 /**
14 * Called whenever an error occurs during the walk process.
15 *
16 * If excluded, the function will throw an error when first encountered.
17 */
18 readonly onError?: (err: Error) => void;
19 readonly walkerOptions?: WalkerOptions;
20}
21export declare function readdirp(dir: string, { filter, onError, walkerOptions }?: ReaddirPOptions): Promise<string[]>;
22export declare const enum FileType {
23 FILE = "file",
24 DIRECTORY = "directory"
25}
26export interface RegularFileNode {
27 path: string;
28 type: FileType.FILE;
29 parent: FileNode;
30}
31export interface DirectoryNode {
32 path: string;
33 type: FileType.DIRECTORY;
34 parent?: FileNode;
35 children: FileNode[];
36}
37export declare type FileNode = RegularFileNode | DirectoryNode;
38export interface GetFileTreeOptions<RE = {}, DE = {}> {
39 /**
40 * Called whenever an error occurs during the walk process.
41 *
42 * If excluded, the function will throw an error when first encountered.
43 */
44 readonly onError?: (err: Error) => void;
45 /**
46 * Called whenever a file node is added to the tree.
47 *
48 * File nodes can be supplemented by returning a new object from this
49 * function.
50 */
51 readonly onFileNode?: (node: RegularFileNode) => RegularFileNode & RE;
52 /**
53 * Called whenever a directory node is added to the tree.
54 *
55 * Directory nodes can be supplemented by returning a new object from this
56 * function.
57 */
58 readonly onDirectoryNode?: (node: DirectoryNode) => DirectoryNode & DE;
59 readonly walkerOptions?: WalkerOptions;
60}
61/**
62 * Compile and return a file tree structure.
63 *
64 * This function walks a directory structure recursively, building a nested
65 * object structure in memory that represents it. When finished, the root
66 * directory node is returned.
67 *
68 * @param dir The root directory from which to compile the file tree
69 */
70export declare function getFileTree<RE = {}, DE = {}>(dir: string, { onError, onFileNode, onDirectoryNode, walkerOptions }?: GetFileTreeOptions<RE, DE>): Promise<RegularFileNode & RE | DirectoryNode & DE>;
71export declare function fileToString(filePath: string): Promise<string>;
72export declare function getFileChecksum(filePath: string): Promise<string>;
73/**
74 * Return true and cached checksums for a file by its path.
75 *
76 * Cached checksums are stored as `.md5` files next to the original file. If
77 * the cache file is missing, the cached checksum is undefined.
78 *
79 * @param p The file path
80 * @return Promise<[true checksum, cached checksum or undefined if cache file missing]>
81 */
82export declare function getFileChecksums(p: string): Promise<[string, string | undefined]>;
83/**
84 * Store a cache file containing the source file's md5 checksum hash.
85 *
86 * @param p The file path
87 * @param checksum The checksum. If excluded, the checksum is computed
88 */
89export declare function cacheFileChecksum(p: string, checksum?: string): Promise<void>;
90export declare function writeStreamToFile(stream: NodeJS.ReadableStream, destination: string): Promise<any>;
91export declare function pathAccessible(filePath: string, mode: number): Promise<boolean>;
92export declare function pathExists(filePath: string): Promise<boolean>;
93export declare function pathReadable(filePath: string): Promise<boolean>;
94export declare function pathWritable(filePath: string): Promise<boolean>;
95export declare function pathExecutable(filePath: string): Promise<boolean>;
96export declare function isExecutableFile(filePath: string): Promise<boolean>;
97/**
98 * Find the base directory based on the path given and a marker file to look for.
99 */
100export declare function findBaseDirectory(dir: string, file: string): Promise<string | undefined>;
101/**
102 * Generate a random file path within the computer's temporary directory.
103 *
104 * @param prefix Optionally provide a filename prefix.
105 */
106export declare function tmpfilepath(prefix?: string): string;
107/**
108 * Given an absolute system path, compile an array of paths working backwards
109 * one directory at a time, always ending in the root directory.
110 *
111 * For example, `'/some/dir'` => `['/some/dir', '/some', '/']`
112 *
113 * @param filePath Absolute system base path.
114 */
115export declare function compilePaths(filePath: string): string[];
116export interface WalkerItem {
117 path: string;
118 stats: fs.Stats;
119}
120export interface WalkerOptions {
121 /**
122 * Filter out file paths during walk.
123 *
124 * As the file tree is walked, this function can be used to exclude files and
125 * directories from the final result.
126 *
127 * It can also be used to tune performance. If a subdirectory is excluded, it
128 * is not walked.
129 *
130 * @param p The file path.
131 * @return `true` to include file path, otherwise it is excluded
132 */
133 readonly pathFilter?: (p: string) => boolean;
134}
135export interface Walker extends stream.Readable {
136 on(event: 'data', callback: (item: WalkerItem) => void): this;
137 on(event: string, callback: (...args: any[]) => any): this;
138}
139export declare class Walker extends stream.Readable {
140 readonly p: string;
141 readonly options: WalkerOptions;
142 readonly paths: string[];
143 constructor(p: string, options?: WalkerOptions);
144 _read(): void;
145}
146export declare function walk(p: string, options?: WalkerOptions): Walker;