UNPKG

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