add walk and walkSync to fs-extra (#13103)

* add signature for walkSync

* flesh out the response definitions

* added example tests

* signature changes to let PathEntryStream flow
This commit is contained in:
Brendan Forster
2017-01-08 17:20:28 -08:00
committed by Andy
parent d67e721495
commit d33f09009e
2 changed files with 62 additions and 2 deletions
+24
View File
@@ -2,6 +2,7 @@
/// <reference types="node" />
import fs = require('fs-extra');
import * as Path from 'path'
var src: string;
var dest: string;
@@ -110,3 +111,26 @@ fs.walk("my-path")
.on('end', function () {
console.dir(items);
});
const ignoreHiddenFiles = (item: string): boolean => {
const basename = Path.basename(item)
return basename === '.' || basename[0] !== '.'
}
const sortPaths = (left: string, right: string) => left.localeCompare(right);
const options = {
filter: ignoreHiddenFiles,
pathSorter: sortPaths
}
fs.walk(path, options)
.on('readable', function (this: fs.PathEntryStream) {
let item: fs.PathEntry | undefined
while ((item = this.read())) {
items.push(item.path)
}
})
.on('end', function () {
})
+38 -2
View File
@@ -7,8 +7,8 @@
/// <reference types="node" />
import * as stream from 'stream';
import { Stats } from "fs";
export * from "fs";
export declare function copy(src: string, dest: string, callback?: (err: Error) => void): void;
@@ -76,6 +76,8 @@ export declare function emptyDirSync(path: string): boolean;
export interface WalkEventEmitter extends NodeJS.ReadableStream {
on(event: 'data', callback: (file: WalkEventFile) => void): this;
on(event: 'readable', callback: (this: PathEntryStream) => void): this;
on(event: 'error', callback: (error: Error, item: PathEntry) => void): this;
on(event: 'end', callback: () => void): this;
on(event: string | symbol, callback: Function): this;
}
@@ -85,7 +87,41 @@ export interface WalkEventFile {
stats: Stats;
}
export function walk(path: string, options?: {}): WalkEventEmitter;
export interface WalkOptions {
/**
* Control how results are enumerated from `readdir`:
* - 'shift' will return the first element from the array.
* - 'pop' will return the last element from the array.
*
* If not specified, the default behaviour is 'shift'
*/
queueMethod?: 'pop' | 'shift';
/**
* Provide a function to sort the paths before they are enumerated
*/
pathSorter?: (left: string, right: string) => number;
/**
* An optional object to override the default `fs` APIs for testing purposes
*/
fs?: Object;
/**
* Provide a function to exclude certain file paths. The function should
* return true when the element should be kept, and false otherwise.
*/
filter?: (path: string, index: number, array: Array<PathEntry>) => boolean;
}
export type PathEntry = {
path: string;
stats: Stats;
}
export type PathEntryStream = {
read(): PathEntry | null
}
export function walk(path: string, options?: WalkOptions): WalkEventEmitter;
export function walkSync(path: string): ReadonlyArray<string>;
export interface CopyFilterFunction {
(src: string): boolean