UNPKG

ws-filewatcher

Version:

Ws-Filewatcher is a cli that will watch for file change and open a websocket server where it will send a message everytime the file is updated

125 lines (105 loc) 3.74 kB
// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE 'use strict'; var Fs = require("fs"); var Ws = require("ws"); var Path = require("path"); var Curry = require("bs-platform/lib/js/curry.js"); var Chalk = require("chalk"); var Yargs = require("yargs"); var Printf = require("bs-platform/lib/js/printf.js"); var Child_process = require("child_process"); var Caml_exceptions = require("bs-platform/lib/js/caml_exceptions.js"); var NotFound = Caml_exceptions.create("Index.NotFound"); var match = Yargs.usage("Usage: $0 <path-to-file> [options]").alias("f", "file").nargs("f", 1).option("port", { description: "Set the port of the WebSocket server", alias: "p", type_: "number" }).option("command", { description: "Run the command on every file update", alias: "c", type_: "string" }).option("verbose", { description: "Allow logging", alias: "v", type_: "boolean" }).demandOption(["f"]).default({ port: 9999 }).argv; var command = match.command; var file = match.file; var port = match.port; var fileToWatch; if (file === "") { console.log(Chalk.red("You must pass a path to the file to watch")); throw { RE_EXN_ID: NotFound, Error: new Error() }; } fileToWatch = Path.resolve(file); var verbose = match.verbose !== undefined; var fileWatcher = Fs.watchFile(fileToWatch, { interval: 300 }, (function (param, param$1) { })); var wss = new Ws.Server({ port: port }); if (verbose) { console.log(Chalk.green(Curry._2(Printf.sprintf(/* Format */{ _0: { TAG: /* String_literal */11, _0: "> Server is watching '", _1: { TAG: /* String */2, _0: /* No_padding */0, _1: { TAG: /* String_literal */11, _0: "' and is starting on port '", _1: { TAG: /* Int */4, _0: /* Int_i */3, _1: /* No_padding */0, _2: /* No_precision */0, _3: { TAG: /* String_literal */11, _0: "' \n", _1: /* End_of_format */0 } } } } }, _1: "> Server is watching '%s' and is starting on port '%i' \n" }), fileToWatch, port))); } wss.on("connection", (function (ws) { var fsCb = function (param) { if (verbose) { console.log(Chalk.green("> The file was updated -> Sending an update to socket")); } if (command !== undefined) { Child_process.exec(command); } ws.send("update"); }; if (verbose) { console.log(Chalk.green("> A socket is connected")); } fileWatcher.on("change", fsCb); ws.on("close", (function (param) { if (verbose) { console.log(Chalk.green("> A socket disconnected")); } fileWatcher.off("change", fsCb); })); })); exports.NotFound = NotFound; exports.port = port; exports.file = file; exports.command = command; exports.fileToWatch = fileToWatch; exports.verbose = verbose; exports.fileWatcher = fileWatcher; exports.wss = wss; /* match Not a pure module */