Added types for klaw (#11492)

* Added types for klaw

* addressed pr comments and `tsfmt -r klaw/*.ts`
This commit is contained in:
Matthew McEachen
2016-09-25 16:22:47 -07:00
committed by Andy
parent a1769ac42a
commit f82ccb560e
2 changed files with 90 additions and 0 deletions
+46
View File
@@ -0,0 +1,46 @@
/// <reference path="klaw.d.ts" />
/// <reference path="../node/node.d.ts" />
import * as klaw from "klaw";
const path = require('path');
// README.md: Streams 1 (push) example:
let items: klaw.Item[] = [] // files, directories, symlinks, etc
klaw('/some/dir')
.on('data', function(item: klaw.Item) {
items.push(item)
})
.on('end', function() {
console.dir(items) // => [ ... array of files]
})
// README.md: Streams 2 & 3 (pull) with error handling
klaw('/some/dir')
.on('readable', function() {
let item: klaw.Item;
while (item = this.read()) {
items.push(item)
}
})
.on('error', function(err: Error, item: klaw.Item) {
console.log(err.message)
console.log(item.path) // the file the error occurred on
})
.on('end', function() {
console.log(items) // => [ ... array of files]
})
// README.md: Example (ignore hidden directories):
var filterFunc = function(item: klaw.Item): boolean {
var basename = path.basename(item.path)
return basename === '.' || basename[0] !== '.'
}
klaw('/some/dir', { filter: filterFunc })
.on('data', function(item: klaw.Item) {
// only items of none hidden folders will reach here
})
+44
View File
@@ -0,0 +1,44 @@
// Type definitions for klaw v1.3.0
// Project: https://github.com/jprichardson/node-klaw
// Definitions by: Matthew McEachen <https://github.com/mceachen>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="../node/node.d.ts" />
declare module "klaw" {
import * as fs from "fs"
import { Readable, ReadableOptions } from 'stream'
function K(root: string, options?: K.Options): K.Walker
namespace K {
interface Item {
path: string
stats: fs.Stats
}
type QueueMethod = "shift" | "pop"
interface Options extends ReadableOptions {
queueMethod?: QueueMethod
pathSorter?: (a: Array<Item>) => Array<Item>
fs?: any // fs or mock-fs
filter?: (a: Item) => boolean
}
type Event = "close" | "data" | "end" | "readable" | "error"
interface Walker {
on(event: Event, listener: Function): this
on(event: "close", listener: () => void): this
on(event: "data", listener: (item: Item) => void): this
on(event: "end", listener: () => void): this
on(event: "readable", listener: () => void): this
on(event: "error", listener: (err: Error) => void): this
read(): Item
}
}
export = K
}