Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added types for klaw #11492

Merged
2 commits merged into from
Sep 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions klaw/klaw-tests.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 44 additions & 0 deletions klaw/klaw.d.ts
Original file line number Diff line number Diff line change
@@ -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
}