Skip to content

Commit cdae91e

Browse files
authored
feat: Add JSDoc & generate TypeScript types (#178)
1 parent 1b173a1 commit cdae91e

7 files changed

Lines changed: 234 additions & 46 deletions

File tree

‎.github/workflows/dev.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ jobs:
4747
- name: Check format
4848
run: npm run format-check
4949

50-
- name: Check format
51-
run: npm run format-check
50+
- name: Check types
51+
run: npm run types-check
5252

5353
- name: Coveralls
5454
uses: coverallsapp/github-action@8d6379e14d29928660c4ba802d8e85393440b329 # v2.3.8

‎index.d.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Creates a ReadableStream that emits file path objects that match a series of globs.
3+
*
4+
* @param {string | string[]} globs The globs to match against.
5+
* @param {Options} [opt] Configuration for the glob-stream instance.
6+
* @returns {ReadableStream<PathObject>} A stream that emits file path objects that have matched.
7+
*/
8+
export default function globStream(
9+
globs: string | string[],
10+
opt?: Options,
11+
): ReadableStream<PathObject>;
12+
/**
13+
* Object of path information for a file matched by glob.
14+
*/
15+
export type PathObject = {
16+
/**
17+
* The current working directory of the matcher.
18+
*/
19+
cwd: string;
20+
/**
21+
* The non-magic portion of the glob that matched.
22+
*/
23+
base: string;
24+
/**
25+
* The absolute file path to the file.
26+
*/
27+
path: string;
28+
};
29+
/**
30+
* A function used to provide a value of uniqueness.
31+
*/
32+
export type UniqueByFunc = (p: PathObject) => unknown;
33+
/**
34+
* Configuration for the glob-stream instance.
35+
*/
36+
export type Options = {
37+
/**
38+
* A property or function to determine uniqueness of a {@link PathObject} - defaults to `"path"`.
39+
*/
40+
uniqueBy?: "base" | "path" | UniqueByFunc | "cwd" | undefined;
41+
/**
42+
* The current working directory - defaults to `process.cwd()`.
43+
*/
44+
cwd?: string | undefined;
45+
/**
46+
* If `true`, the `cwd` value will be used as the `base` on {@link PathObject}.
47+
*/
48+
cwdbase?: boolean | undefined;
49+
/**
50+
* A hardcoded `base` to use on each {@link PathObject}.
51+
*/
52+
base?: string | undefined;
53+
/**
54+
* Paths or globs for the glob instance to exclude.
55+
*/
56+
ignore?: string | string[] | undefined;
57+
/**
58+
* Paths or globs for the glob instance to exclude.
59+
*/
60+
exclude?: string | string[] | undefined;
61+
};
62+
//# sourceMappingURL=index.d.ts.map

‎index.d.ts.map‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎index.js‎

Lines changed: 111 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,39 @@
11
import fs from "node:fs/promises";
22
import path from "node:path";
3+
import process from "node:process";
34
import { deprecate } from "node:util";
45
import { ReadableStream } from "node:stream/web";
56

67
import globParent from "glob-parent";
78

9+
/**
10+
* @typedef {object} PathObject Object of path information for a file matched by glob.
11+
* @property {string} cwd The current working directory of the matcher.
12+
* @property {string} base The non-magic portion of the glob that matched.
13+
* @property {string} path The absolute file path to the file.
14+
*/
15+
16+
/**
17+
* @typedef {(p: PathObject) => unknown} UniqueByFunc A function used to provide a value of uniqueness.
18+
*/
19+
20+
/**
21+
* @typedef {object} Options Configuration for the glob-stream instance.
22+
* @property {"cwd" | "base" | "path" | UniqueByFunc} [uniqueBy] A property or function to determine uniqueness of a {@link PathObject} - defaults to `"path"`.
23+
* @property {string} [cwd] The current working directory - defaults to `process.cwd()`.
24+
* @property {boolean} [cwdbase] If `true`, the `cwd` value will be used as the `base` on {@link PathObject}.
25+
* @property {string} [base] A hardcoded `base` to use on each {@link PathObject}.
26+
* @property {string | string[]} [ignore] Paths or globs for the glob instance to exclude.
27+
* @property {string | string[]} [exclude] Paths or globs for the glob instance to exclude.
28+
*/
29+
30+
/**
31+
* Validates that all items in an array are strings and at least
32+
* one isn't a negative glob (doesn't start with a `!`).
33+
*
34+
* @param {unknown[]} globs The glob array to validate.
35+
* @returns {asserts globs is string[]}
36+
*/
837
function validateGlobs(globs) {
938
let hasPositiveGlob = false;
1039

@@ -23,14 +52,31 @@ function validateGlobs(globs) {
2352
}
2453
}
2554

55+
/**
56+
* Determines if a glob starts with `!`.
57+
*
58+
* @param {string} glob The glob string to check.
59+
* @returns `true` if the glob string starts with ! or `false` otherwise.
60+
*/
2661
function isNegatedGlob(glob) {
2762
return glob.startsWith("!");
2863
}
2964

65+
/**
66+
* Determines if a glob does not start with a `!`.
67+
*
68+
* @param {string} glob The glob string to check.
69+
* @returns `true` if the glob does not start with `!` or `false` otherwise.
70+
*/
3071
function isPositiveGlob(glob) {
3172
return !isNegatedGlob(glob);
3273
}
3374

75+
/**
76+
* Validates the `uniqueBy` option is of the allowed types.
77+
*
78+
* @param {Options} opts The options to validate.
79+
*/
3480
function validateUniqueBy({ uniqueBy } = {}) {
3581
if (typeof uniqueBy === "function") {
3682
return;
@@ -47,14 +93,33 @@ function validateUniqueBy({ uniqueBy } = {}) {
4793
throw new Error("The `uniqueBy` option must be a string or function");
4894
}
4995

50-
function isDefined(item) {
51-
return item != null;
96+
/**
97+
* Determines if a value is not `null` or `undefined`.
98+
*
99+
* @param {unknown} value The value to check.
100+
* @returns {boolean} `false` if the value is `null`/`undefined` or `true` otherwise.
101+
*/
102+
function isDefined(value) {
103+
return value != null;
52104
}
53105
const showIgnoreDeprecation = deprecate(
54106
() => {},
55107
"The `ignore` option is deprecated - use `exclude` instead",
56108
);
57109

110+
/**
111+
* Determines if all options are of valid types.
112+
*
113+
* @param {Options} opts The options to validate.
114+
* @returns {asserts opts is {
115+
* uniqueBy: "cwd" | "base" | "path" | UniqueByFunc;
116+
* cwd: string;
117+
* cwdbase: boolean;
118+
* base?: string;
119+
* ignore: string[];
120+
* exclude: string[];
121+
* }}
122+
*/
58123
function validateOptions(opts) {
59124
if (typeof opts.cwd !== "string") {
60125
throw new Error("The `cwd` option must be a string");
@@ -83,23 +148,32 @@ function validateOptions(opts) {
83148
}
84149
}
85150

151+
/**
152+
* Creates a uniqueness matching function based on a comparator.
153+
*
154+
* @param {"cwd" | "base" | "path" | UniqueByFunc} comparator
155+
* @returns {(p: PathObject) => boolean}
156+
*/
86157
function uniqueBy(comparator) {
87158
const seen = new Set();
88159

89160
if (typeof comparator === "string") {
90161
if (comparator === "cwd") {
91-
return isUniqueByCwd;
162+
return (obj) => isUnique(obj.cwd);
92163
}
93164
if (comparator === "base") {
94-
return isUniqueByBase;
95-
}
96-
if (comparator === "path") {
97-
return isUniqueByPath;
165+
return (obj) => isUnique(obj.base);
98166
}
167+
// Has to be "path" given previous assertions
168+
return (obj) => isUnique(obj.path);
99169
} else {
100-
return isUniqueByFunc;
170+
return (obj) => isUnique(comparator(obj));
101171
}
102172

173+
/**
174+
* @param {string | unknown} value The value to check uniqueness of.
175+
* @returns {boolean} `true` if the value has been seen before or `false` otherwise.
176+
*/
103177
function isUnique(value) {
104178
if (seen.has(value)) {
105179
return false;
@@ -108,45 +182,32 @@ function uniqueBy(comparator) {
108182
return true;
109183
}
110184
}
111-
112-
function isUniqueByCwd(obj) {
113-
return isUnique(obj.cwd);
114-
}
115-
function isUniqueByBase(obj) {
116-
return isUnique(obj.base);
117-
}
118-
function isUniqueByPath(obj) {
119-
return isUnique(obj.path);
120-
}
121-
122-
function isUniqueByFunc(obj) {
123-
return isUnique(comparator(obj));
124-
}
125185
}
126186

187+
/**
188+
* Creates a ReadableStream that emits file path objects that match a series of globs.
189+
*
190+
* @param {string | string[]} globs The globs to match against.
191+
* @param {Options} [opt] Configuration for the glob-stream instance.
192+
* @returns {ReadableStream<PathObject>} A stream that emits file path objects that have matched.
193+
*/
127194
export default function globStream(globs, opt) {
128195
if (!Array.isArray(globs)) {
129196
globs = [globs];
130197
}
131198

132199
validateGlobs(globs);
133200

134-
const ourOpt = Object.assign(
135-
{},
136-
{
137-
cwd: process.cwd(),
138-
cwdbase: false,
139-
uniqueBy: "path",
140-
exclude: [],
141-
// TODO: deprecate `ignore` option
142-
ignore: [],
143-
},
144-
opt,
145-
{
146-
withFileTypes: true,
147-
followSymlinks: true,
148-
},
149-
);
201+
const ourOpt = {
202+
cwd: process.cwd(),
203+
cwdbase: false,
204+
/** @type {"path"} */
205+
uniqueBy: "path",
206+
exclude: [],
207+
ignore: [],
208+
// Spread user-provided options
209+
...opt,
210+
};
150211
// Normalize `ignore` to array
151212
ourOpt.ignore =
152213
typeof ourOpt.ignore === "string" ? [ourOpt.ignore] : ourOpt.ignore;
@@ -176,18 +237,27 @@ export default function globStream(globs, opt) {
176237

177238
const isUnique = uniqueBy(ourOpt.uniqueBy);
178239

240+
/**
241+
* @param {string[]} globs
242+
* @param {{ cwd: string, exclude: string[] }} opts
243+
*/
179244
async function* globber(globs, opts) {
245+
const globOpts = {
246+
cwd: opts.cwd,
247+
exclude: opts.exclude,
248+
withFileTypes: true,
249+
followSymlinks: true,
250+
};
180251
for (const glob of globs) {
181-
const globber = fs.glob(glob, opts);
182-
const globIter = globber[Symbol.asyncIterator]();
252+
const globber = fs.glob(glob, globOpts);
183253

184254
// Build the glob parent exactly once for each glob
185255
let parent = globParent(glob);
186256
if (!path.isAbsolute(parent)) {
187257
parent = path.join(opts.cwd, parent);
188258
}
189259

190-
for await (const dirent of globIter) {
260+
for await (const dirent of globber) {
191261
const filepath = path.join(dirent.parentPath, dirent.name);
192262

193263
// Decide on the glob's base path

‎node.d.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
declare module "node:fs/promises" {
2+
interface GlobOpts {
3+
cwd: string;
4+
exclude: string[];
5+
withFileTypes: boolean;
6+
followSymlinks: boolean;
7+
}
8+
interface Dirent {
9+
parentPath: string;
10+
name: string;
11+
}
12+
function glob(g: string, opts: GlobOpts): AsyncIterable<Dirent>;
13+
}
14+
15+
declare module "node:path" {
16+
function resolve(p1: string): string;
17+
function isAbsolute(p1: string): boolean;
18+
function join(p1: string, p2: string): string;
19+
function normalize(p1: string): string;
20+
}
21+
22+
declare module "node:process" {
23+
function cwd(): string;
24+
}
25+
26+
declare module "node:util" {
27+
function deprecate(fn: () => void, msg: string): () => void;
28+
}
29+
30+
declare module "node:stream/web" {
31+
const ReadableStream: globalThis.ReadableStream & {
32+
from<T>(iter: AsyncIterable<T>): globalThis.ReadableStream<T>;
33+
};
34+
}

‎package-lock.json‎

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)