Git Product home page Git Product logo

wch's Introduction

wch v0.10.3

Client library for the wch daemon.

Compatible with wchd v0.10

wch(dir: string): Promise<boolean>

Track a directory indefinitely.

Resolves to true if successful.

wch.unwatch(dir: string): Promise<boolean>

Stop tracking a directory.

Resolves to true if successful.

wch.connect(): Promise<Object>

This method must be called before any other actions are processed. Whenever not connected, actions are stored in the offline queue.

If the returned promise is rejected, you need to manually call connect again if you wish to retry. Though, not all errors will reject the promise. When the wchd server is unreachable, the promise will stay pending while the client waits for the server to be restarted.

The connected and connecting properties reflect the current state of the connection.

wch.close(): void

Force disconnect from the wchd server.

wch.stream(dir: string, query: Object): Readable

Stream changes to matching paths within a directory.

The data event is emitted for every change. Listeners are passed an object representing the affected file.

The close event is emitted when the stream has its destroy method called.

The stream manages its subscription as expected whenever the client loses its connection to the server.

The stream is created by the readable-stream library to ensure reliable behavior between NodeJS versions.

Note: You should always handle error events on a stream, or your process will easily crash.

wch.query(dir: string, query: Object): Promise<Object>

Fetch matching paths within a directory.

The query argument is not yet documented.

The resolved object has the following properties:

  • root: string
  • clock: string
  • files: Object[]

Note: The dir argument must be within a tracked directory.

wch.expr(query: Object): Array

Pass a wch-style query to get the expression property of the equivalent Watchman-style query.

Note: This method doesn't communicate with the server.

wch.list(): Promise<string[]>

Fetch the list of tracked directories.

Events

In addition to events provided by plugins, there are several events provided by the client library.

connecting

The socket is attempting to connect (to the wchd server).

connect

The socket has connected. Emits on reconnect, too.

offline

The wchd server is unreachable.

close

The socket lost its connection.

error

The socket met an error.

Plugins may also use this event to broadcast errors.

Listeners are passed an Error object.

Note: You should always listen for this event.

wch's People

Contributors

aleclarson avatar renovate-bot avatar

Watchers

 avatar  avatar

wch's Issues

add mechanism for resilient plugin streams

In other words, plugins shouldn't need to worry about recreating their watch streams when the subscription root is deleted. When a plugin only cares about file events within the root (and not the root itself), the watch stream should be abstracted away.

Possible solutions:

  • add a new option to Package:stream
  • add a new method to Package.prototype
  • make this behavior the default

add `copy` function for project configuration

Providing a simple copy function that projects can use in their project.js files to copy files from one directory to another (potentially renaming files as well) is a must, and the project shouldn't need a plugin dependency to get such functionality.

temporary watch

Sometimes, all you need is a foreground process that watches until you decide to stop it.

wch . -f

rework the plugin API

Make the plugin API more declarative and less verbose.

  • wrap plugin modules in a function where this is the plugin instance
  • all plugin configuration is hot-swapped (no more restarting every plugin)
  • setup each plugin instance in its own VM context

API design

// Validated option types.
this.options = {
  sourceMaps: '?boolean|string',
};

// Global `warn` function
warn('something bad could happen');

// Global `print` function
print('something just happened');

// Watch a package.
this.watch = (pack, opts) => {
  // File matchers react to file changes (in watch mode).
  const files = pack.match(glob, opts);

  // Do something with every file that exists.
  files.each(file => {
    console.info(file); // Files are transient objects.
  });

  // Do something when a file is removed.
  files.on('unlink', (file) => {});

  // Do something when a file is added or changed.
  files.on('change', (file) => {});

  // Return a cleanup function. (optional)
  return () => {};
};

// Build a package.
this.build = (pack, opts) => {};

// The returned object serves as the prototype for
// the object exposed to "wch.config.js" as the plugin's name.
return {
  constructor() {
    // The `constructor` method is called automatically for each package.
    this.sources = new Map();
  },
  compile(src, dest) {
    // `this` is the `opts` object passed to `watch` and `build`.
    this.sources.set(src, dest);
  }
};

Inside a wch.config.js module, all exposed plugins have a set method that is used to pass an options object, which is validated by the plugin's option types.

When wch.config.js is updated, plugins are only restarted if the arguments to method calls are not deeply equal with the previous configuration. For set calls, plugins are never restarted, because those options are meant to be hot-swappable.

"File matchers" are cleaned up automatically. The pack.match method creates a file stream during the "watch" phase and a list of files during the "build" phase. This implementation detail is meant to be hidden from the plugin developer.

Beyond the warn and print functions, there are other implicitly exposed helpers:

  • fs is the saxon library
  • path is the built-in path module
  • assert is the built-in assert module
  • lazyRequire is a function that installs packages that aren't available

move to wch.config.js

It will be clearer what the configuration is used by/for if we change the name to wch.config.js.

The project.js convention will be deprecated when the plugin API is reworked (#7).

rewatch project roots when they're renamed

Renaming a project root will probably always crash the wch server. Upon server restart, the old path is unwatched, so you'll need to use wch . in the project root after renaming it. Apart from the crashing, this isn't terrible, but also not ideal.

wch ui

A simple Electron application for managing the wch daemon and its components.

  • ability to start/stop the daemon

  • toggle for starting the daemon on system startup

  • view logs

    • filter by warn, error, or all
    • filter by plugin
      ย 
  • list of watched roots

  • click a watched root to inspect it

    • generate a wch.config.js module
    • view/upgrade its plugins
    • unwatch it
      ย 
  • install/upgrade global plugins

  • customize plugin defaults

  • menu bar icon

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.