Skip to content

Commit b80e4c9

Browse files
committed
Try publishing the types
1 parent 7a3280f commit b80e4c9

File tree

6 files changed

+140
-9
lines changed

6 files changed

+140
-9
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ Return an async generator that will iterate over all of files inside of a direct
4444
You can iterate over each file and directory individually using a `for-await...of` loop. Note, you must be inside an [async function statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function).
4545

4646
```js
47-
const { asyncFolderWalker } = require('async-folder-walker');
47+
const { asyncFolderWalker } = require('async-folder-walker')
4848
async function iterateFiles () {
49-
const walker = asyncFolderWalker(['.git', 'node_modules']);
49+
const walker = asyncFolderWalker(['.git', 'node_modules'])
5050
for await (const file of walker) {
51-
console.log(file); // logs the file path!
51+
console.log(file) // logs the file path!
5252
}
5353
}
5454

55-
iterateFiles();
55+
iterateFiles()
5656
```
5757

5858
Opts include:

declaration.tsconfig.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "./tsconfig",
3+
"exclude": [
4+
"test.js"
5+
],
6+
"compilerOptions": {
7+
"declaration": true,
8+
"noEmit": false,
9+
"emitDeclarationOnly": true
10+
}
11+
}

index.d.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* Options object
3+
*/
4+
export type Opts = {
5+
/**
6+
* - A pathFilter cb.
7+
*/
8+
pathFilter?: pathFilter | undefined;
9+
/**
10+
* - A statFilter cb.
11+
*/
12+
statFilter?: statFilter | undefined;
13+
/**
14+
* - The maximum number of folders to walk down into.
15+
*/
16+
maxDepth?: number | undefined;
17+
/**
18+
* - A shaper cb.
19+
*/
20+
shaper?: shaper | undefined;
21+
};
22+
/**
23+
* pathFilter lets you filter files based on a resolved `filepath`.
24+
*/
25+
export type pathFilter = (filepath: string) => boolean;
26+
/**
27+
* statFilter lets you filter files based on a lstat object.
28+
*/
29+
export type statFilter = (st: Object) => boolean;
30+
/**
31+
* FWStats is the object that the okdistribute/folder-walker module returns by default.
32+
*/
33+
export type FWStats = {
34+
/**
35+
* - The filepath of the directory where the walk started.
36+
*/
37+
root: string;
38+
/**
39+
* - The resolved filepath.
40+
*/
41+
filepath: string;
42+
/**
43+
* - A fs.Stats instance.
44+
*/
45+
stat: Object;
46+
/**
47+
* - The relative path to `root`.
48+
*/
49+
relname: string;
50+
/**
51+
* - The resolved filepath of the files containing directory.
52+
*/
53+
basename: string;
54+
};
55+
/**
56+
* shaper lets you change the shape of the returned file data from walk-time stats.
57+
*/
58+
export type shaper = (fwStats: FWStats) => any;
59+
/**
60+
* Options object
61+
*
62+
* @typedef Opts
63+
* @property {pathFilter} [pathFilter] - A pathFilter cb.
64+
* @property {statFilter} [statFilter] - A statFilter cb.
65+
* @property {Number} [maxDepth=Infinity] - The maximum number of folders to walk down into.
66+
* @property {shaper} [shaper] - A shaper cb.
67+
*/
68+
/**
69+
* Create an async generator that iterates over all folders and directories inside of `dirs`.
70+
*
71+
* @async
72+
* @generator
73+
* @function
74+
* @public
75+
* @param {String|String[]} dirs - The path of the directory to walk, or an array of directory paths.
76+
* @param {?(Opts)} opts - Options used for the directory walk.
77+
*
78+
* @yields {Promise<String|any>} - An async iterator that returns anything.
79+
*/
80+
export function asyncFolderWalker(dirs: string | string[], opts: (Opts) | null): AsyncGenerator<any, void, unknown>;
81+
/**
82+
* allFiles gives you all files from the directory walk as an array.
83+
*
84+
* @async
85+
* @function
86+
* @public
87+
* @param {String|String[]} dirs - The path of the directory to walk, or an array of directory paths.
88+
* @param {?(Opts)} opts - Options used for the directory walk.
89+
*
90+
* @returns {Promise<String[]|any>} - An async iterator that returns anything.
91+
*/
92+
export function allFiles(dirs: string | string[], opts: (Opts) | null): Promise<string[] | any>;
93+
/**
94+
* Async iterable collector
95+
*
96+
* @async
97+
* @function
98+
* @private
99+
* @param {AsyncIterator} iterator - The iterator to collect into an array
100+
*/
101+
export function all(iterator: AsyncIterator<any, any, undefined>): Promise<any[]>;

index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// @ts-check
2+
3+
'use strict'
4+
15
const fs = require('fs')
26
const path = require('path')
37
const ignore = require('ignore')
@@ -74,6 +78,7 @@ async function * asyncFolderWalker (dirs, opts) {
7478
shaper
7579
}, opts)
7680

81+
// @ts-ignore
7782
const ig = ignore().add(opts.ignore)
7883

7984
const roots = [dirs].flat().filter(opts.pathFilter)
@@ -155,10 +160,12 @@ function depthLimiter (filePath, relativeTo, maxDepth) {
155160
* @async
156161
* @function
157162
* @private
163+
* @param {AsyncIterator} iterator - The iterator to collect into an array
158164
*/
159165
async function all (iterator) {
160166
const collect = []
161167

168+
// @ts-ignore
162169
for await (const result of iterator) {
163170
collect.push(result)
164171
}
@@ -177,8 +184,8 @@ async function all (iterator) {
177184
*
178185
* @returns {Promise<String[]|any>} - An async iterator that returns anything.
179186
*/
180-
async function allFiles (...args) {
181-
return all(asyncFolderWalker(...args))
187+
async function allFiles (dirs, opts) {
188+
return all(asyncFolderWalker(dirs, opts))
182189
}
183190

184191
module.exports = {

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
"ignore": "^5.1.8"
1212
},
1313
"devDependencies": {
14+
"auto-changelog": "^2.2.0",
1415
"dependency-check": "^4.1.0",
16+
"gh-release": "^6.0.0",
1517
"npm-run-all2": "^5.0.0",
1618
"p-temporary-directory": "^1.1.1",
17-
"gh-release": "^6.0.0",
1819
"standard": "^16.0.3",
1920
"tap": "^16.0.0",
20-
"auto-changelog": "^2.2.0"
21+
"typescript": "^4.6.2",
22+
"@voxpelli/tsconfig": "^2.0.0"
2123
},
2224
"homepage": "https://github.com/bcomnes/async-folder-walker",
2325
"keywords": [],
@@ -33,7 +35,8 @@
3335
"test:standard": "standard",
3436
"test:tap": "tap",
3537
"debug": "node --nolazy --inspect-brk=9229 -r esm test.js",
36-
"version": "auto-changelog -p --template keepachangelog auto-changelog --breaking-pattern 'BREAKING CHANGE:' && git add CHANGELOG.md"
38+
"build": "rm -f index.d.ts && tsc -p declaration.tsconfig.json",
39+
"version": "git diff --exit-code -- index.d.ts && auto-changelog -p --template keepachangelog auto-changelog --breaking-pattern 'BREAKING CHANGE:' && git add CHANGELOG.md"
3740
},
3841
"standard": {
3942
"ignore": [

tsconfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "@voxpelli/tsconfig/node16.json",
3+
"files": [
4+
"index.js"
5+
],
6+
"include": [
7+
"test.js"
8+
]
9+
}

0 commit comments

Comments
 (0)