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

Commit 05830f9

Browse files
committed
refactor: migrate codebase to TypeScript
1 parent 2490a5e commit 05830f9

File tree

11 files changed

+195
-155
lines changed

11 files changed

+195
-155
lines changed

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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
export const EXCLUDED_DIRECTORY = new Set([
3+
"node_modules",
4+
".vscode",
5+
".git"
6+
]);

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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Import Node.js Dependencies
2+
import { Dirent } from "node:fs";
3+
4+
export interface WalkOptions {
5+
extensions?: Set<string>;
6+
}
7+
8+
export type WalkResult = [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 fs from "node:fs/promises";
3+
import path from "node:path";
4+
5+
// Import Internal Dependencies
6+
import { EXCLUDED_DIRECTORY } from "./constants.js";
7+
import type { WalkOptions, WalkResult } 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<WalkResult> {
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 fs from "node:fs";
3+
import path from "node:path";
4+
5+
// Import Internal Dependencies
6+
import { EXCLUDED_DIRECTORY } from "./constants.js";
7+
import type { WalkOptions, WalkResult } 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<WalkResult> {
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.

test/walk.spec.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Import Node.js Dependencies
2+
import { describe, it } from "node:test";
3+
import assert from "node:assert";
4+
import path from "node:path";
5+
import { fileURLToPath } from "node:url";
6+
7+
// Import Internal Dependencies
8+
import { walk, walkSync } from "../src/index.js";
9+
10+
// CONSTANTS
11+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
12+
13+
const kRootLocation = path.join(__dirname, "..");
14+
const kFixturesDir = path.join(__dirname, "fixtures");
15+
16+
const kExpectedJSFiles = [
17+
"src/index.ts",
18+
"src/constants.ts",
19+
"src/types.ts",
20+
"src/walk.ts",
21+
"src/walkSync.ts",
22+
"test/walk.spec.ts"
23+
]
24+
.map((fileLocation) => path.normalize(fileLocation))
25+
.sort();
26+
27+
describe("walk", () => {
28+
it("should return all TypeScript files of the package", async() => {
29+
const files: string[] = [];
30+
const options = { extensions: new Set([".ts"]) };
31+
32+
for await (const [dirent, absoluteFileLocation] of walk(
33+
kRootLocation,
34+
options
35+
)) {
36+
if (dirent.isFile()) {
37+
files.push(path.relative(kRootLocation, absoluteFileLocation));
38+
}
39+
}
40+
41+
assert.deepEqual(files.sort(), kExpectedJSFiles);
42+
});
43+
});
44+
45+
describe("walkSync", () => {
46+
it("should return all TypeScript files of the package", () => {
47+
const options = { extensions: new Set([".ts"]) };
48+
49+
const files = [...walkSync(kRootLocation, options)]
50+
.filter(([dirent]) => dirent.isFile())
51+
.map(([, absoluteFileLocation]) => path.relative(kRootLocation, absoluteFileLocation));
52+
53+
assert.deepEqual(files, kExpectedJSFiles);
54+
});
55+
56+
it("should return all files in the fixtures directory", () => {
57+
const files = [...walkSync(kFixturesDir)]
58+
.filter(([dirent]) => dirent.isFile())
59+
.map(([, absoluteFileLocation]) => path.relative(kRootLocation, absoluteFileLocation));
60+
61+
const expectedFiles = [
62+
"test/fixtures/foobar.txt",
63+
"test/fixtures/test.md"
64+
].map((fileLocation) => path.normalize(fileLocation));
65+
66+
assert.deepEqual(files, expectedFiles);
67+
});
68+
});
69+
70+

0 commit comments

Comments
 (0)