npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dmail/filesystem-watch

v2.8.0

Published

[![npm package](https://img.shields.io/npm/v/@dmail/filesystem-watch.svg)](https://www.npmjs.com/package/@dmail/filesystem-watch) [![build](https://travis-ci.com/dmail/filesystem-watch.svg?branch=master)](http://travis-ci.com/dmail/filesystem-watch) [![co

Downloads

77

Readme

filesystem watch

npm package build codecov

Watch changes on your filesystem, either in a folder or on a specific file.

Introduction

@dmail/filesystem-watch has the following exports.

  • registerFolderLifecycle
  • registerFileLifecycle

registerFolderLifecycle example

registerFolderLifecycle is meant to be used when you need to something in sync with a given folder contents.

import { registerFolderLifecycle } from "@dmail/filesystem-watch"

const folderContentMap = {}
registerFolderLifecycle("/Users/you/folder", {
  added: ({ relativePath, type ) => {
    folderContentMap[relativePath] = type
  },
  removed: ({ relativePath }) => {
    delete folderContentMap[relativePath]
  },
})

— see registerFolderLifecycle documentation

registerFileLifecycle example

registerFileLifecycle is meant to be used when you need to do something in sync with a given file content.

import { readFileSync } from "fs"
import { registerFileLifecycle } from "@dmail/filesystem-watch"

const path = "/Users/you/folder/config.js"
let currentConfig = null
registerFileLifecycle(path, {
  added: () => {
    currentConfig = JSON.parse(String(readFileSync(path)))
  },
  updated: () => {
    currentConfig = JSON.parse(String(readFileSync(path)))
  },
  removed: () => {
    currentConfig = null
  },
})

— see registerFileLifecycle documentation

Installation

npm install @dmail/[email protected]

Why ?

I needed something capable to watch file changes on the filesystem.

The documentation of fs.watch makes it clear that you cannot really use it directly because it has several limitations specific to the filesystem.

Then I tried chokidar, a wrapper around fs.watch. However I could not fully understand what chokidar was doing under the hood.

What I needed was small wrapper around fs.watch that do not shallow events sent by the operating system. Ultimately chokidar could maybe do what I need but it's a bit too big for my use case.

— see fs.watch documentation — see chokidar on github

fs.watch caveats

Everywhere:

  • Writing a file emits a change as chunk gets written in the file.
  • Trigger change on folder when something inside folder changes
  • might send event with filename being null (never reproduced yet)

On mac:

  • Trigger rename instead of change when updating a file

On linux:

  • recursive option not supported

On windows:

  • filesystem emits nothing when a file is removed
  • removing a watched folder throw with EPERM error