@jsenv/filesystem

4.6.5 • Public • Published

Jsenv filesystem npm package

Collection of functions to interact with filesystem in Node.js

List files using pattern matching

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

const jsFiles = await listFilesMatching({
  directoryUrl: new URL("./", import.meta.url),
  patterns: {
    "./**/*.js": true,
    "./**/*.test.js": false,
  },
});
[
  'file:///Users/dmail/docs/demo/a.js',
  'file:///Users/dmail/docs/demo/b.js'
]

Watch a specific file changes

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

const packageJSONFileUrl = new URL("./package.json", import.meta.url);
let packageJSON = null;
const unregister = registerFileLifecycle(packageJSONFileUrl, {
  added: () => {
    packageJSON = JSON.parse(String(readFileSync(packageJSONFileUrl)));
  },
  updated: () => {
    packageJSON = JSON.parse(String(readFileSync(packageJSONFileUrl)));
  },
  removed: () => {
    packageJSON = null;
  },
  notifyExistent: true,
});
unregister(); // stop watching the file changes

Watch many files changes

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

const directoryContentDescription = {};
const unregister = registerDirectoryLifecycle("file:///directory/", {
  watchPatterns: {
    "./**/*": true,
    "./node_modules/": false,
  },
  added: ({ relativeUrl, type }) => {
    directoryContentDescription[relativeUrl] = type;
  },
  removed: ({ relativeUrl }) => {
    delete directoryContentDescription[relativeUrl];
  },
});
unregister(); // stop watching the directory changes

API

docs/API.md

Installation

npm install @jsenv/filesystem

Readme

Keywords

none

Package Sidebar

Install

npm i @jsenv/filesystem

Weekly Downloads

465

Version

4.6.5

License

MIT

Unpacked Size

128 kB

Total Files

70

Last publish

Collaborators

  • jsenv-admin
  • dmail