Tuesday 23 May 2017

Brunch: how to run `watch --server` programmatically

I need to be able to run the watch server command programatically and continue with my program once it compiled successfully. Use case: running app server automatically for a (nightwatch.js) test suite and ensuring it compiled before running the tests.

I've had some partial success by reverse engineering the source code into this and using multithreading to be able to kill the server later:

const spawn = require('threads').spawn;

module.exports = {
  start() {
    console.log("App server: STARTING");

    process.env.API_URL = "http://localhost:1234";

    return new Promise((fulfill, reject) =>
      this.childProcess =
        spawn((_, __, progress) => {
          const brunchWatch = require('brunch/lib/watch');

          brunchWatch(true, '.', {
            server: true,
            port:   3331,
          }, _ => progress(1))
        })
          .on('progress', fulfill)
          .on('done', fulfill)
          .on('error', reject)
          .send()
    )
      .then(_ => console.log("App server: STARTED"))
      .catch(error => {
        console.log(`App server: ERROR: ${error}`);
        return Promise.reject(error);
      });
  },

  stop() {
    console.log("App server: STOPPING");
    this.childProcess && this.childProcess.kill();
    console.log("App server: STOPPED");
    return Promise.resolve();
  }
};

Is there a better solution for my problem?



via nicooga

No comments:

Post a Comment