From 3363dfb20e55319cb0d9e996bb7394e8d790141b Mon Sep 17 00:00:00 2001 From: Colin Luo Date: Wed, 25 Apr 2018 07:05:35 +0800 Subject: [PATCH] Update `klaw-sync` module interface from v1.0 to v2.0.0 + (#25134) * commit 'Update `klaw-sync` module interface to v2.0.0 + [v2.0.0 apis](https://github.com/manidlou/node-klaw-sync/tree/v2.0.0) [v3.0.2 apis](https://github.com/manidlou/node-klaw-sync/tree/v3.0.2) * Update `klaw-sync` module interface from v1.0 to v2.0.0 + * Update `klaw-sync` module interface from v1.0 to v2.0.0 + * Change `klaw-sync` types version from `3.0` to `2.0` and remove ignore parameter. * Update test case. * Improve the test case of `options.filter`. * Add definitions of `klaw-sync` * Remove `Colin Luo` from `klaw-sync` Difinitions. --- types/klaw-sync/index.d.ts | 69 ++++++++++++++++++------------ types/klaw-sync/klaw-sync-tests.ts | 11 +++-- 2 files changed, 49 insertions(+), 31 deletions(-) diff --git a/types/klaw-sync/index.d.ts b/types/klaw-sync/index.d.ts index 1bdd409dfc..f222804222 100644 --- a/types/klaw-sync/index.d.ts +++ b/types/klaw-sync/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for klaw-sync 1.1 +// Type definitions for klaw-sync 2.0 // Project: https://github.com/manidlou/node-klaw-sync // Definitions by: Brendan Forster // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -6,35 +6,50 @@ /// import * as fs from 'fs' - declare namespace klawSync { - interface Item { - path: string - stats: fs.Stats - } + interface Item { + path: string + stats: fs.Stats + } - interface Options { - /** - * any paths or `micromatch` patterns to ignore. - * - * For more information on micromatch patterns: https://github.com/jonschlinkert/micromatch#features - */ - ignore?: string | string[] - /** - * True to only return files (ignore directories). - * - * Defaults to false if not specified. - */ - nodir?: boolean - /** - * True to only return directories (ignore files). - * - * Defaults to false if not specified. - */ - nofile?: boolean - } + type Filter = (item: Item) => boolean + + interface Options { + /** + * @description True to only return files (ignore directories). + * Defaults to false if not specified. + * @default false + */ + nodir?: boolean + + /** + * @description True to only return directories (ignore files). + * Defaults to false if not specified. + * @default false + */ + nofile?: boolean + + /** + * @description when filter function is used, the default behavior is to read all directories even + * if they don't pass the filter function (won't be included but still will be traversed). + * If you set true, there will be neither inclusion nor traversal for directories that + * don't pass the filter function + * @since v2.0.0 + */ + noRecurseOnFailedFilter?: boolean + + /** + * @description function that gets one argument fn({path: '', stats: {}}) and returns true to include + * or false to exclude the item + * @since v2.0.0 + */ + filter?: Filter + } } -declare function klawSync(root: string, options?: klawSync.Options): ReadonlyArray +declare function klawSync( + root: string, + options?: klawSync.Options, +): ReadonlyArray export = klawSync diff --git a/types/klaw-sync/klaw-sync-tests.ts b/types/klaw-sync/klaw-sync-tests.ts index 4808146882..5875c528fd 100644 --- a/types/klaw-sync/klaw-sync-tests.ts +++ b/types/klaw-sync/klaw-sync-tests.ts @@ -2,7 +2,7 @@ import * as path from 'path' import klawSync = require('klaw-sync') const outputMessage = (result: klawSync.Item) => { - console.log(`file: ${result.path} has size '${result.stats.size}'`) + console.log(`file: ${result.path} has size '${result.stats.size}'`) } klawSync('/some/dir').forEach(outputMessage) @@ -12,9 +12,12 @@ const defaultOptions = {} klawSync('/some/dir', defaultOptions).forEach(outputMessage) const options = { - ignore: ['.exe'], - nodir: true, - nofile: false, + nodir: true, + nofile: false, + noRecurseOnFailedFilter: false, + filter(item: klawSync.Item) { + return item.path.indexOf('node_modules') < 0 + }, } klawSync('/some/dir', options).forEach(outputMessage)