Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate codebase to TypeScript #59

Merged
merged 3 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ updates:
versioning-strategy: widen
schedule:
interval: "weekly"
groups:
development-dependencies:
dependency-type: "development"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "monthly"
groups:
github-actions:
patterns:
- "*"
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Modern FileSystem (fs) utilities to lazy walk directories Asynchronously (but al
- Enforce usage of Symbols for CONSTANTS.
- Synchronous API.

> [!NOTE]
> Performance over some of the features is a non-goal.

## Requirements
Expand All @@ -35,7 +36,7 @@ $ yarn add @nodesecure/fs-walk
## Usage example

```js
import path from "path";
import path from "node:path";
import { walk } from "@nodesecure/fs-walk";

for await (const [dirent, absoluteFileLocation] of walk(".")) {
Expand All @@ -53,14 +54,14 @@ export interface WalkOptions {
extensions?: Set<string>;
}

export type WalkResult = [dirent: fs.Dirent, absoluteFileLocation: string];
export type WalkEntry = [dirent: fs.Dirent, absoluteFileLocation: string];
```

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

Asynchronous walk.

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

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

Expand Down
1 change: 0 additions & 1 deletion index.d.ts

This file was deleted.

77 changes: 0 additions & 77 deletions index.js

This file was deleted.

30 changes: 18 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
"name": "@nodesecure/fs-walk",
"version": "1.0.0",
"description": "Modern FileSystem (fs) utilities to lazy walk directories Asynchronously (but also Synchronously)",
"exports": "./index.js",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
"type": "module",
"engines": {
"node": ">=18.0.0"
},
"scripts": {
"lint": "eslint index.js",
"test-only": "node --test",
"build": "tsc",
"prepublishOnly": "npm run build",
"lint": "eslint src/**/*.ts test/**/*.ts",
"test-only": "glob -c \"tsx --test\" \"./test/**/*.spec.ts\"",
"test": "npm run lint && npm run test-only",
"coverage": "c8 -r html npm test"
},
Expand All @@ -24,21 +31,20 @@
],
"author": "GENTILHOMME Thomas <[email protected]>",
"files": [
"index.d.ts",
"index.js",
"type"
"dist"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/NodeSecure/fs-walk/issues"
},
"homepage": "https://github.com/NodeSecure/fs-walk#readme",
"devDependencies": {
"@nodesecure/eslint-config": "^1.7.0",
"c8": "^8.0.0"
},
"type": "module",
"engines": {
"node": ">=18.0.0"
"@nodesecure/eslint-config": "^1.9.0",
"@types/node": "^20.11.28",
"c8": "^8.0.1",
"eslint": "^8.57.0",
"glob": "^10.3.10",
"tsx": "^4.7.1",
"typescript": "^5.4.2"
}
}
5 changes: 5 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const EXCLUDED_DIRECTORY = new Set([
"node_modules",
".vscode",
".git"
]);
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./walk.js";
export * from "./walkSync.js";
export * from "./types.js";
14 changes: 14 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Import Node.js Dependencies
import { Dirent } from "node:fs";

export interface WalkOptions {
/**
* Whitelist of extensions
*
* @example
* new Set([".js", ".cjs", ".mjs"]);
*/
extensions?: Set<string>;
}

export type WalkEntry = [dirent: Dirent, absoluteFileLocation: string];
45 changes: 45 additions & 0 deletions src/walk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Import Node.js Dependencies
import * as fs from "node:fs/promises";
import * as path from "node:path";

// Import Internal Dependencies
import { EXCLUDED_DIRECTORY } from "./constants.js";
import type { WalkOptions, WalkEntry } from "./types.js";

/**
* @example
* import { walk } from "@nodesecure/fs-walk";
*
* for await (const [dirent, location] of walk(__dirname) {
* if (dirent.isFile()) {
* console.log(location);
* }
* }
*/
export async function* walk(
directory: string,
options: WalkOptions = Object.create(null)
): AsyncIterableIterator<WalkEntry> {
const extensions = options?.extensions ?? null;
const dirents = await fs.opendir(directory);

for await (const dirent of dirents) {
if (EXCLUDED_DIRECTORY.has(dirent.name)) {
continue;
}

if (dirent.isFile()) {
if (extensions !== null && !extensions.has(path.extname(dirent.name))) {
continue;
}

yield [dirent, path.join(directory, dirent.name)];
}
else if (dirent.isDirectory()) {
const subDirectoryLocation = path.join(directory, dirent.name);

yield [dirent, subDirectoryLocation];
yield* walk(subDirectoryLocation, options);
}
}
}
45 changes: 45 additions & 0 deletions src/walkSync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Import Node.js Dependencies
import * as fs from "node:fs";
import * as path from "node:path";

// Import Internal Dependencies
import { EXCLUDED_DIRECTORY } from "./constants.js";
import type { WalkOptions, WalkEntry } from "./types.js";

/**
* @example
* import { walkSync, FILE } from "@nodesecure/fs-walk";
*
* for (const [type, location] of walkSync(__dirname) {
* if (type === FILE) {
* console.log(location);
* }
* }
*/
export function* walkSync(
directory: string,
options: WalkOptions = Object.create(null)
): IterableIterator<WalkEntry> {
const extensions = options?.extensions ?? null;
const dirents = fs.readdirSync(directory, { withFileTypes: true });

for (const dirent of dirents) {
if (EXCLUDED_DIRECTORY.has(dirent.name)) {
continue;
}

if (dirent.isFile()) {
if (extensions !== null && !extensions.has(path.extname(dirent.name))) {
continue;
}

yield [dirent, path.join(directory, dirent.name)];
}
else if (dirent.isDirectory()) {
const subDirectoryLocation = path.join(directory, dirent.name);

yield [dirent, subDirectoryLocation];
yield* walkSync(subDirectoryLocation, options);
}
}
}
54 changes: 0 additions & 54 deletions test/walk.js

This file was deleted.

Loading
Loading