UNPKG

1.45 kBJavaScriptView Raw
1var util = require('util');
2var path = require('path');
3var fs = require('fs');
4
5var common = require('../common');
6var flow = require('../flow');
7
8var fsWatchI = module.exports = function (config) {
9 this.config = util.extend({}, this.defaultConfig);
10
11 if (config) {
12 util.extend(this.config, config);
13 }
14
15 this.flows = config.workflows || config.dataflows || config.flows;
16
17 this.listen();
18};
19
20fsWatchI.defaultConfig = {
21 verbose: true // log messages for each FS path
22};
23
24fsWatchI.prototype.listen = function () {
25 var iConfig = this.config;
26 var rootPath = common.getProject().root.path;
27
28 this.flows.forEach(function (cfg) {
29 var df = new flow (cfg);
30 var watchPath = cfg.path || cfg.dir || cfg.file;
31 var absPath, displayPath;
32
33 if (watchPath) {
34 absPath = path.resolve(rootPath, watchPath);
35 displayPath = './' + watchPath;
36 } else {
37 absPath = rootPath;
38 displayPath = "the project root";
39 }
40
41 fs.watch(absPath, function (event, filename) {
42 df.data.event = event; // "rename" or "change"
43 df.data.filename = filename; // limited number of OSes support it
44
45 // all together now
46 df.data.fsEvent = {
47 event: event,
48 filename: filename
49 };
50
51 df.runDelayed ();
52
53 if (iConfig.verbose || cfg.verbose) {
54 console.log(
55 'Some file has been %sd in %s',
56 event, displayPath
57 );
58 }
59 });
60
61 console.log(
62 'You are now watching for filesytem events in %s 👀',
63 displayPath
64 );
65 });
66};