1 | var util = require('util');
|
2 | var path = require('path');
|
3 | var fs = require('fs');
|
4 |
|
5 | var common = require('../common');
|
6 | var flow = require('../flow');
|
7 |
|
8 | var 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 |
|
20 | fsWatchI.defaultConfig = {
|
21 | verbose: true
|
22 | };
|
23 |
|
24 | fsWatchI.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;
|
43 | df.data.filename = filename;
|
44 |
|
45 |
|
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 | };
|