From f733fe31ed115bb5554839cd4ddda8bed0552f86 Mon Sep 17 00:00:00 2001 From: Panu Horsmalahti Date: Tue, 22 Dec 2015 15:34:46 +0200 Subject: [PATCH] Add type definitions for node-dir. --- node-dir/node-dir-tests.ts | 90 ++++++++++++++++++++++++++++++++++++++ node-dir/node-dir.d.ts | 65 +++++++++++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 node-dir/node-dir-tests.ts create mode 100644 node-dir/node-dir.d.ts diff --git a/node-dir/node-dir-tests.ts b/node-dir/node-dir-tests.ts new file mode 100644 index 0000000000..6cc03ec538 --- /dev/null +++ b/node-dir/node-dir-tests.ts @@ -0,0 +1,90 @@ +/// + +import * as dir from "node-dir"; + +// display contents of files in this script's directory +dir.readFiles("./", + function(err, content, next) { + console.log('content:', content); + next(); + }, + function(err, files) { + console.log('finished reading files:', files); + }); + +// display contents of huge files in this script's directory +dir.readFilesStream("./", + function(err: any, stream: any, next: any) { + var content = ''; + stream.on('data', function(buffer: any) { + content += buffer.toString(); + }); + stream.on('end',function() { + console.log('content:', content); + next(); + }); + }, + function(err, files) { + console.log('finished reading files:', files); + }); + +// match only filenames with a .txt extension and that don't start with a `.ยด +dir.readFiles("./", { + match: /.txt$/, + exclude: /^\./ + }, function(err, content, next) { + console.log('content:', content); + next(); + }, + function(err, files){ + console.log('finished reading files:',files); + }); + +// exclude an array of subdirectory names +dir.readFiles("./", { + exclude: ['node_modules', 'test'] + }, function(err, content, next) { + console.log('content:', content); + next(); + }, + function(err, files){ + console.log('finished reading files:',files); + }); + + +// the callback for each file can optionally have a filename argument as its 3rd parameter +// and the finishedCallback argument is optional, e.g. +dir.readFiles("./", function(err: any, content: any, filename: string, next: any) { + console.log('processing content of file', filename); + next(); +}); + +dir.files("./", function(err, files) { + console.log(files); +}); + +dir.files("./", function(err, files) { + // sort descending + files.reverse(); + // include only certain filenames + files = files.filter(function(file: any) { + return ['allowed', 'file', 'names'].indexOf(file) > -1; + }); + // exclude some filenames + files = files.filter(function(file: any) { + return ['exclude', 'these', 'files'].indexOf(file) === -1; + }); +}); + +dir.subdirs("./", function(err, subdirs) { + console.log(subdirs); +}); + +dir.paths("./", function(err, paths) { + console.log('files:\n', paths.files); + console.log('subdirs:\n', paths.dirs); +}); + +dir.paths("./", true, function(err, paths) { + console.log('paths:\n', paths); +}); diff --git a/node-dir/node-dir.d.ts b/node-dir/node-dir.d.ts new file mode 100644 index 0000000000..d131f6b9a1 --- /dev/null +++ b/node-dir/node-dir.d.ts @@ -0,0 +1,65 @@ +// Type definitions for node-dir +// Project: https://github.com/fshost/node-dir +// Definitions by: Panu Horsmalahti +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module "node-dir" { + export interface Options { + // file encoding (defaults to 'utf8') + encoding?: string; + + // a regex pattern or array to specify filenames to ignore + exclude?: RegExp | string[]; + + // a regex pattern or array to specify directories to ignore + excludeDir?: RegExp | string[]; + + // a regex pattern or array to specify filenames to operate on + match?: RegExp | string[]; + + // a regex pattern or array to specify directories to recurse + matchDir?: RegExp | string[]; + + // whether to recurse subdirectories when reading files (defaults to true) + recursive?: boolean; + + // sort files in each directory in descending order + reverse?: boolean; + + // whether to aggregate only the base filename rather than the full filepath + shortName?: boolean; + + // sort files in each directory in ascending order (defaults to true) + sort?: boolean; + + // control if done function called on error (defaults to true) + doneOnErr?: boolean; + } + + export interface FileCallback { + (error: any, content: any, next: () => void): void; + } + + export interface FileNamedCallback { + (error: any, content: any, filename: string, next: () => void): void; + } + + export interface StreamCallback { + (error: any, stream: any, next: () => void): void; + } + + export interface FinishedCallback { + (error: any, files: any): void; + } + + export function readFiles(dir: string, fileCallback: FileCallback, finishedCallback?: FinishedCallback): void; + export function readFiles(dir: string, fileCallback: FileNamedCallback, finishedCallback?: FinishedCallback): void; + export function readFiles(dir: string, options: Options, fileCallback: FileCallback, finishedCallback?: FinishedCallback): void; + export function readFiles(dir: string, options: Options, fileCallback: FileNamedCallback, finishedCallback?: FinishedCallback): void; + export function readFilesStream(dir: string, options: Options, streamCallback: StreamCallback, + finishedCallback?: FinishedCallback): void; + export function files(dir: string, callback: (error: any, files: any) => void): void; + export function subdirs(dir: string, callback: (error: any, subdirs: any) => void): void; + export function paths(dir: string, callback: (error: any, paths: any) => void): void; + export function paths(dir: string, combine: boolean, callback: (error: any, paths: any) => void): void; +}