Skip to content
This repository was archived by the owner on Jun 17, 2025. It is now read-only.

Commit 68083ca

Browse files
refactor: migrate codebase to TypeScript
Update src/constants.ts Co-authored-by: PierreDemailly <[email protected]> Update test/walk.spec.ts Co-authored-by: PierreDemailly <[email protected]>
1 parent 2490a5e commit 68083ca

File tree

13 files changed

+221
-158
lines changed

13 files changed

+221
-158
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ export interface WalkOptions {
5454
extensions?: Set<string>;
5555
}
5656

57-
export type WalkResult = [dirent: fs.Dirent, absoluteFileLocation: string];
57+
export type WalkEntry = [dirent: fs.Dirent, absoluteFileLocation: string];
5858
```
5959

60-
### walk(directory: string, options?: WalkOptions): AsyncIterableIterator< WalkResult >
60+
### walk(directory: string, options?: WalkOptions): AsyncIterableIterator< WalkEntry >
6161

6262
Asynchronous walk.
6363

64-
### walkSync(directory: string, options?: WalkOptions): IterableIterator< WalkResult >
64+
### walkSync(directory: string, options?: WalkOptions): IterableIterator< WalkEntry >
6565

6666
Synchronous walk (using readdirSync under the hood instead of opendir).
6767

index.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

index.js

Lines changed: 0 additions & 77 deletions
This file was deleted.

package.json

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22
"name": "@nodesecure/fs-walk",
33
"version": "1.0.0",
44
"description": "Modern FileSystem (fs) utilities to lazy walk directories Asynchronously (but also Synchronously)",
5-
"exports": "./index.js",
5+
"exports": "./dist/index.js",
6+
"types": "./dist/index.d.ts",
7+
"type": "module",
8+
"engines": {
9+
"node": ">=18.0.0"
10+
},
611
"scripts": {
7-
"lint": "eslint index.js",
8-
"test-only": "node --test",
12+
"build": "tsc",
13+
"prepublishOnly": "npm run build",
14+
"lint": "eslint src/**/*.ts test/**/*.ts",
15+
"test-only": "glob -c \"tsx --test\" \"./test/**/*.spec.ts\"",
916
"test": "npm run lint && npm run test-only",
1017
"coverage": "c8 -r html npm test"
1118
},
@@ -24,21 +31,20 @@
2431
],
2532
"author": "GENTILHOMME Thomas <[email protected]>",
2633
"files": [
27-
"index.d.ts",
28-
"index.js",
29-
"type"
34+
"dist"
3035
],
3136
"license": "MIT",
3237
"bugs": {
3338
"url": "https://github.com/NodeSecure/fs-walk/issues"
3439
},
3540
"homepage": "https://github.com/NodeSecure/fs-walk#readme",
3641
"devDependencies": {
37-
"@nodesecure/eslint-config": "^1.7.0",
38-
"c8": "^8.0.0"
39-
},
40-
"type": "module",
41-
"engines": {
42-
"node": ">=18.0.0"
42+
"@nodesecure/eslint-config": "^1.9.0",
43+
"@types/node": "^20.11.28",
44+
"c8": "^8.0.1",
45+
"eslint": "^8.57.0",
46+
"glob": "^10.3.10",
47+
"tsx": "^4.7.1",
48+
"typescript": "^5.4.2"
4349
}
4450
}

src/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const EXCLUDED_DIRECTORY = new Set([
2+
"node_modules",
3+
".vscode",
4+
".git"
5+
]);

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./walk.js";
2+
export * from "./walkSync.js";
3+
export * from "./types.js";

src/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Import Node.js Dependencies
2+
import { Dirent } from "node:fs";
3+
4+
export interface WalkOptions {
5+
/**
6+
* Whitelist of extensions
7+
*
8+
* @example
9+
* new Set([".js", ".cjs", ".mjs"]);
10+
*/
11+
extensions?: Set<string>;
12+
}
13+
14+
export type WalkEntry = [dirent: Dirent, absoluteFileLocation: string];

src/walk.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Import Node.js Dependencies
2+
import * as fs from "node:fs/promises";
3+
import * as path from "node:path";
4+
5+
// Import Internal Dependencies
6+
import { EXCLUDED_DIRECTORY } from "./constants.js";
7+
import type { WalkOptions, WalkEntry } from "./types.js";
8+
9+
/**
10+
* @example
11+
* import { walk } from "@nodesecure/fs-walk";
12+
*
13+
* for await (const [dirent, location] of walk(__dirname) {
14+
* if (dirent.isFile()) {
15+
* console.log(location);
16+
* }
17+
* }
18+
*/
19+
export async function* walk(
20+
directory: string,
21+
options: WalkOptions = Object.create(null)
22+
): AsyncIterableIterator<WalkEntry> {
23+
const extensions = options?.extensions ?? null;
24+
const dirents = await fs.opendir(directory);
25+
26+
for await (const dirent of dirents) {
27+
if (EXCLUDED_DIRECTORY.has(dirent.name)) {
28+
continue;
29+
}
30+
31+
if (dirent.isFile()) {
32+
if (extensions !== null && !extensions.has(path.extname(dirent.name))) {
33+
continue;
34+
}
35+
36+
yield [dirent, path.join(directory, dirent.name)];
37+
}
38+
else if (dirent.isDirectory()) {
39+
const subDirectoryLocation = path.join(directory, dirent.name);
40+
41+
yield [dirent, subDirectoryLocation];
42+
yield* walk(subDirectoryLocation, options);
43+
}
44+
}
45+
}

src/walkSync.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Import Node.js Dependencies
2+
import * as fs from "node:fs";
3+
import * as path from "node:path";
4+
5+
// Import Internal Dependencies
6+
import { EXCLUDED_DIRECTORY } from "./constants.js";
7+
import type { WalkOptions, WalkEntry } from "./types.js";
8+
9+
/**
10+
* @example
11+
* import { walkSync, FILE } from "@nodesecure/fs-walk";
12+
*
13+
* for (const [type, location] of walkSync(__dirname) {
14+
* if (type === FILE) {
15+
* console.log(location);
16+
* }
17+
* }
18+
*/
19+
export function* walkSync(
20+
directory: string,
21+
options: WalkOptions = Object.create(null)
22+
): IterableIterator<WalkEntry> {
23+
const extensions = options?.extensions ?? null;
24+
const dirents = fs.readdirSync(directory, { withFileTypes: true });
25+
26+
for (const dirent of dirents) {
27+
if (EXCLUDED_DIRECTORY.has(dirent.name)) {
28+
continue;
29+
}
30+
31+
if (dirent.isFile()) {
32+
if (extensions !== null && !extensions.has(path.extname(dirent.name))) {
33+
continue;
34+
}
35+
36+
yield [dirent, path.join(directory, dirent.name)];
37+
}
38+
else if (dirent.isDirectory()) {
39+
const subDirectoryLocation = path.join(directory, dirent.name);
40+
41+
yield [dirent, subDirectoryLocation];
42+
yield* walkSync(subDirectoryLocation, options);
43+
}
44+
}
45+
}

test/walk.js

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)