4.15.4 • Published 7 months ago

@jsenv/filesystem v4.15.4

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Jsenv Filesystem npm package

A modern, Promise-based collection of utilities to interact with the filesystem in Node.js.

šŸ” Pattern-based file operations
šŸ‘€ Efficient file/directory watching
šŸ”„ Lifecycle hooks for file changes
šŸ› ļø URL-based path handling

Installation

npm install @jsenv/filesystem

Table of Contents

Quick Start

import { listFilesMatching, writeFile, moveFile } from "@jsenv/filesystem";

// Find JavaScript files (excluding tests)
const jsFiles = await listFilesMatching({
  directoryUrl: new URL("./src/", import.meta.url),
  patterns: {
    "./**/*.js": true,
    "./**/*.test.js": false,
  },
});

// Create a new file
await writeFile(new URL("./output.txt", import.meta.url), "Hello world");

// Move a file
await moveFile({
  source: new URL("./output.txt", import.meta.url),
  destination: new URL("./moved.txt", import.meta.url),
});

Features

List Files Using Pattern Matching

Find files with powerful pattern matching that supports inclusion and exclusion patterns:

import { listFilesMatching } from "@jsenv/filesystem";

const jsFiles = await listFilesMatching({
  directoryUrl: new URL("./", import.meta.url),
  patterns: {
    "./**/*.js": true, // Include all JS files
    "./**/*.test.js": false, // Exclude test files
    "./node_modules/": false, // Exclude node_modules directory
  },
});

Example output:

[
  'file:///Users/dmail/docs/demo/a.js',
  'file:///Users/dmail/docs/demo/b.js'
]

Watch a Specific File Changes

Monitor a single file for changes with lifecycle hooks:

import { readFileSync } from "node:fs";
import { registerFileLifecycle } from "@jsenv/filesystem";

const packageJSONFileUrl = new URL("./package.json", import.meta.url);
let packageJSON = null;

// Start watching the file
const unregister = registerFileLifecycle(packageJSONFileUrl, {
  added: () => {
    packageJSON = JSON.parse(String(readFileSync(packageJSONFileUrl)));
    console.log("Package.json was added");
  },
  updated: () => {
    packageJSON = JSON.parse(String(readFileSync(packageJSONFileUrl)));
    console.log("Package.json was updated");
  },
  removed: () => {
    packageJSON = null;
    console.log("Package.json was removed");
  },
  notifyExistent: true, // Trigger 'added' callback if file exists when watching starts
});

// Later, when done watching:
unregister(); // Stop watching the file changes

Watch Many Files Changes

Monitor an entire directory with pattern filtering:

import { registerDirectoryLifecycle } from "@jsenv/filesystem";

const directoryContentDescription = {};

// Start watching the directory
const unregister = registerDirectoryLifecycle("file:///directory/", {
  watchPatterns: {
    "./**/*": true, // Watch all files
    "./node_modules/": false, // Except files in node_modules
  },
  added: ({ relativeUrl, type }) => {
    directoryContentDescription[relativeUrl] = type;
    console.log(`Added: ${relativeUrl} (${type})`);
  },
  updated: ({ relativeUrl, type }) => {
    console.log(`Updated: ${relativeUrl} (${type})`);
  },
  removed: ({ relativeUrl }) => {
    delete directoryContentDescription[relativeUrl];
    console.log(`Removed: ${relativeUrl}`);
  },
});

// Later, when done watching:
unregister(); // Stop watching the directory changes

Other Common Operations

import {
  ensureEmptyDirectory,
  writeFile,
  readFile,
  copyFile,
  moveFile,
  removeFile,
} from "@jsenv/filesystem";

// Create or empty a directory
await ensureEmptyDirectory(new URL("./dist/", import.meta.url));

// Write a file (creates directories if needed)
await writeFile(
  new URL("./logs/debug.log", import.meta.url),
  "Debug information",
);

// Read a file (returns a string by default)
const content = await readFile(new URL("./config.json", import.meta.url));

// Copy a file
await copyFile({
  source: new URL("./template.html", import.meta.url),
  destination: new URL("./output/index.html", import.meta.url),
});

// Move a file
await moveFile({
  source: new URL("./temp.txt", import.meta.url),
  destination: new URL("./final/document.txt", import.meta.url),
});

// Remove a file
await removeFile(new URL("./obsolete.txt", import.meta.url));

API

For a complete list of all available functions and their parameters, see the API documentation.

4.14.5

9 months ago

4.14.6

9 months ago

4.14.1

9 months ago

4.14.2

9 months ago

4.14.3

9 months ago

4.14.4

9 months ago

4.14.0

10 months ago

4.13.2

11 months ago

4.13.3

11 months ago

4.13.4

11 months ago

4.13.5

10 months ago

4.13.0

12 months ago

4.13.1

11 months ago

4.12.0

12 months ago

4.15.4

7 months ago

4.15.0

8 months ago

4.15.1

8 months ago

4.15.2

8 months ago

4.15.3

8 months ago

4.10.12

1 year ago

4.10.13

1 year ago

4.11.0

1 year ago

4.10.9

1 year ago

4.10.5

1 year ago

4.9.8

1 year ago

4.10.6

1 year ago

4.9.7

1 year ago

4.10.7

1 year ago

4.10.8

1 year ago

4.9.9

1 year ago

4.9.4

1 year ago

4.9.3

1 year ago

4.9.6

1 year ago

4.9.5

1 year ago

4.9.0

1 year ago

4.9.2

1 year ago

4.9.1

1 year ago

4.10.1

1 year ago

4.10.2

1 year ago

4.10.3

1 year ago

4.10.4

1 year ago

4.10.0

1 year ago

4.8.1

1 year ago

4.8.0

1 year ago

4.8.2

1 year ago

4.7.0

1 year ago

4.7.5

1 year ago

4.7.2

1 year ago

4.7.1

1 year ago

4.7.4

1 year ago

4.7.3

1 year ago

4.6.7

1 year ago

4.6.6

1 year ago

4.6.9

1 year ago

4.6.8

1 year ago

4.9.10

1 year ago

4.10.10

1 year ago

4.10.11

1 year ago

4.6.5

2 years ago

4.6.4

2 years ago

4.6.3

2 years ago

4.4.0

2 years ago

4.6.1

2 years ago

4.2.5

2 years ago

4.6.0

2 years ago

4.2.4

2 years ago

4.2.6

2 years ago

4.5.0

2 years ago

4.3.2

2 years ago

4.3.1

2 years ago

4.5.1

2 years ago

4.3.0

2 years ago

4.2.3

3 years ago

4.2.2

3 years ago

4.2.1

3 years ago

4.2.0

3 years ago

4.1.8

3 years ago

4.1.7

3 years ago

4.1.9

3 years ago

4.1.6

3 years ago

4.1.4

3 years ago

4.1.5

3 years ago

4.0.9

4 years ago

4.0.8

4 years ago

4.0.5

4 years ago

4.1.3

3 years ago

4.0.4

4 years ago

4.0.10

4 years ago

4.0.7

4 years ago

4.0.6

4 years ago

4.1.0

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

4.1.2

3 years ago

4.0.3

4 years ago

4.1.1

3 years ago

4.0.2

4 years ago

3.2.2

4 years ago

3.2.1

4 years ago

3.2.0

4 years ago

3.1.0

4 years ago

3.0.0

4 years ago

2.7.2

4 years ago

2.7.0

4 years ago

2.6.1

4 years ago

2.7.1

4 years ago

2.6.0

4 years ago

2.5.0

4 years ago

2.4.0

4 years ago

2.5.1

4 years ago

2.3.0

4 years ago

2.3.1

4 years ago

2.2.0

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.0.0

4 years ago