Giter VIP home page Giter VIP logo

file-watcher's Introduction

/*
Event-broker implementation based on the Callbags specs.

This will watch changes happening in the ./logs folder, It will log the first 10 change in files inside the folder.
Then it will clear tmp data and stop the watcher.

To use this run:
$ yarn start

And change files inside the ./logs folder.

The expected output is something like:

Watcher started
filename: foo
event: rename
timestamp: 2020-01-06T16:55:13.446Z
########
filename: foo
event: change
timestamp: 2020-01-06T16:55:13.457Z
########
filename: bar
event: rename
timestamp: 2020-01-06T16:55:15.754Z
########
filename: bar
event: change
timestamp: 2020-01-06T16:55:15.762Z
########
filename: foo
event: change
timestamp: 2020-01-06T16:55:26.590Z
diff:
1d0
< hey there

########
filename: foo
event: change
timestamp: 2020-01-06T16:55:26.592Z
diff:
1d0
< hey there

########
filename: bar
event: change
timestamp: 2020-01-06T16:55:39.791Z
diff:
1d0
< hello

########
filename: bar
event: change
timestamp: 2020-01-06T16:55:39.793Z
diff:
1d0
< hello

########
filename: bar
event: change
timestamp: 2020-01-06T16:55:49.314Z
diff:
1d0
< hello

########
filename: foo
event: change
timestamp: 2020-01-06T16:55:52.335Z
diff:
1d0
< hey there

########
Watcher closed
*/

import {
  pipe,
  observe,
  map,
  take
} from "./operators";
import { createFileWatcherSource } from "./createFileWatcherSource";

const path = "./logs";

const folderChangesSource = createFileWatcherSource(path);

pipe(
  folderChangesSource,
  // Add timestamp
  map(event => ({ 
    timestamp: new Date().toISOString(), 
    ...event 
  })),
  // Take the first 10 events
  take(10),
  // Log
  observe(({event, filename, timestamp, diff}) => {
    console.log(`filename: ${filename}`)
    console.log(`event: ${event}`)
    console.log(`timestamp: ${timestamp}`)
    if (diff) {
      console.log(`diff:`);
      console.log(`${diff}`);
    }
    console.log("########")
  }),
);

file-watcher's People

Contributors

mateuszitelli avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.