fwatcher 2.2.9 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +119 -33
- package/lib/index.js +93 -103
- package/package.json +7 -6
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
The MIT License (MIT)
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2015-
|
|
3
|
+
Copyright (c) 2015-26 Ionică Bizău <bizauionica@gmail.com> (https://ionicabizau.net)
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -1,16 +1,54 @@
|
|
|
1
1
|
<!-- Please do not edit this file. Edit the `blah` field in the `package.json` instead. If in doubt, open an issue. -->
|
|
2
2
|
|
|
3
3
|
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
4
10
|
[](#)
|
|
5
11
|
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
6
22
|
# fwatcher
|
|
7
23
|
|
|
8
|
-
[![Support me on Patreon][badge_patreon]][patreon] [![Buy me a book][badge_amazon]][amazon] [![PayPal][badge_paypal_donate]][paypal-donations] [](https://github.com/IonicaBizau/ama) [](https://www.npmjs.com/package/fwatcher) [](https://www.npmjs.com/package/fwatcher) [](https://www.codementor.io
|
|
24
|
+
[![Support me on Patreon][badge_patreon]][patreon] [![Buy me a book][badge_amazon]][amazon] [![PayPal][badge_paypal_donate]][paypal-donations] [](https://github.com/IonicaBizau/ama) [](https://www.npmjs.com/package/fwatcher) [](https://www.npmjs.com/package/fwatcher) [](https://www.codementor.io/@johnnyb?utm_source=github&utm_medium=button&utm_term=johnnyb&utm_campaign=github)
|
|
9
25
|
|
|
10
26
|
<a href="https://www.buymeacoffee.com/H96WwChMy" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/yellow_img.png" alt="Buy Me A Coffee"></a>
|
|
11
27
|
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
12
34
|
> Watch files for changes.
|
|
13
35
|
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
14
52
|
## :cloud: Installation
|
|
15
53
|
|
|
16
54
|
```sh
|
|
@@ -22,41 +60,45 @@ yarn add fwatcher
|
|
|
22
60
|
```
|
|
23
61
|
|
|
24
62
|
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
25
74
|
## :clipboard: Example
|
|
26
75
|
|
|
27
76
|
|
|
28
77
|
|
|
29
78
|
```js
|
|
30
|
-
|
|
31
|
-
var FileWatcher = require("fwatcher")
|
|
32
|
-
, Logger = require("bug-killer")
|
|
33
|
-
;
|
|
79
|
+
import fileWatcher from "fwatcher";
|
|
34
80
|
|
|
35
81
|
// Listen for changes for 5 seconds
|
|
36
|
-
|
|
37
|
-
if (err) { return
|
|
38
|
-
|
|
82
|
+
const watcher = new fileWatcher(`example/test.txt`, (err, ev, path) => {
|
|
83
|
+
if (err) { return console.error(err, "error"); }
|
|
84
|
+
console.log([ev, path].join(" "));
|
|
39
85
|
});
|
|
40
|
-
setTimeout(watcher.off
|
|
41
|
-
|
|
86
|
+
setTimeout(() => watcher.off(), 5000);
|
|
42
87
|
|
|
43
88
|
// Listen only for one change
|
|
44
|
-
|
|
45
|
-
if (err) { return
|
|
46
|
-
|
|
89
|
+
new fileWatcher(`example/once.txt`, true, (err, ev, path) => {
|
|
90
|
+
if (err) { return console.error(err, "error"); }
|
|
91
|
+
console.log([ev, path].join(" "));
|
|
47
92
|
});
|
|
48
93
|
```
|
|
49
94
|
|
|
50
95
|
|
|
51
96
|
|
|
52
|
-
## :question: Get Help
|
|
53
97
|
|
|
54
|
-
There are few ways to get help:
|
|
55
98
|
|
|
56
|
-
1. Please [post questions on Stack Overflow](https://stackoverflow.com/questions/ask). You can open issues with questions, as long you add a link to your Stack Overflow question.
|
|
57
|
-
2. For bug reports and feature requests, open issues. :bug:
|
|
58
99
|
|
|
59
|
-
|
|
100
|
+
|
|
101
|
+
|
|
60
102
|
|
|
61
103
|
|
|
62
104
|
|
|
@@ -68,7 +110,7 @@ Creates a new instance of the internal `Watcher`.
|
|
|
68
110
|
|
|
69
111
|
#### Params
|
|
70
112
|
|
|
71
|
-
- **
|
|
113
|
+
- **string** `path`: The path to the file.
|
|
72
114
|
- **Function** `handler`: A function called when the file changes.
|
|
73
115
|
|
|
74
116
|
### `off()`
|
|
@@ -79,8 +121,8 @@ Creates a new file watcher.
|
|
|
79
121
|
|
|
80
122
|
#### Params
|
|
81
123
|
|
|
82
|
-
- **
|
|
83
|
-
- **
|
|
124
|
+
- **string** `path`: The path to the file.
|
|
125
|
+
- **boolean|Options** `options`: A boolean value representing the `once` value or an object containing the following fields:
|
|
84
126
|
|
|
85
127
|
- `once` (Boolean): If `true`, the handler is deleted after first event.
|
|
86
128
|
- **Function** `callback`: This function will be called when the file is changed or renamed. The first parameter is the error, the second one is
|
|
@@ -91,12 +133,40 @@ the evenit name and the third one is the file path.
|
|
|
91
133
|
|
|
92
134
|
|
|
93
135
|
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
## :question: Get Help
|
|
143
|
+
|
|
144
|
+
There are few ways to get help:
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
1. Please [post questions on Stack Overflow](https://stackoverflow.com/questions/ask). You can open issues with questions, as long you add a link to your Stack Overflow question.
|
|
149
|
+
2. For bug reports and feature requests, open issues. :bug:
|
|
150
|
+
3. For direct and quick help, you can [use Codementor](https://www.codementor.io/johnnyb). :rocket:
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
94
165
|
## :yum: How to contribute
|
|
95
166
|
Have an idea? Found a bug? See [how to contribute][contributing].
|
|
96
167
|
|
|
97
168
|
|
|
98
169
|
## :sparkling_heart: Support my projects
|
|
99
|
-
|
|
100
170
|
I open-source almost everything I can, and I try to reply to everyone needing help using these projects. Obviously,
|
|
101
171
|
this takes time. You can integrate and use these projects in your applications *for free*! You can even change the source code and redistribute (even resell it).
|
|
102
172
|
|
|
@@ -115,29 +185,45 @@ However, if you get some profit from this or just want to encourage me to contin
|
|
|
115
185
|
Thanks! :heart:
|
|
116
186
|
|
|
117
187
|
|
|
118
|
-
## :dizzy: Where is this library used?
|
|
119
|
-
If you are using this library in one of your projects, add it in this list. :sparkles:
|
|
120
188
|
|
|
121
189
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
125
209
|
|
|
126
210
|
## :scroll: License
|
|
127
211
|
|
|
128
212
|
[MIT][license] © [Ionică Bizău][website]
|
|
129
213
|
|
|
130
214
|
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
[license]: /LICENSE
|
|
220
|
+
[website]: https://ionicabizau.net
|
|
221
|
+
[contributing]: /CONTRIBUTING.md
|
|
222
|
+
[docs]: /DOCUMENTATION.md
|
|
131
223
|
[badge_patreon]: https://ionicabizau.github.io/badges/patreon.svg
|
|
132
224
|
[badge_amazon]: https://ionicabizau.github.io/badges/amazon.svg
|
|
133
225
|
[badge_paypal]: https://ionicabizau.github.io/badges/paypal.svg
|
|
134
226
|
[badge_paypal_donate]: https://ionicabizau.github.io/badges/paypal_donate.svg
|
|
135
|
-
|
|
136
227
|
[patreon]: https://www.patreon.com/ionicabizau
|
|
137
228
|
[amazon]: http://amzn.eu/hRo9sIZ
|
|
138
229
|
[paypal-donations]: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RVXDDLKKLQRJW
|
|
139
|
-
|
|
140
|
-
[license]: http://showalicense.com/?fullname=Ionic%C4%83%20Biz%C4%83u%20%3Cbizauionica%40gmail.com%3E%20(https%3A%2F%2Fionicabizau.net)&year=2015#license-mit
|
|
141
|
-
[website]: https://ionicabizau.net
|
|
142
|
-
[contributing]: /CONTRIBUTING.md
|
|
143
|
-
[docs]: /DOCUMENTATION.md
|
package/lib/index.js
CHANGED
|
@@ -1,113 +1,103 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
this._ = Fs.watch(path, handler);
|
|
20
|
-
this.a_path = Abs(path);
|
|
21
|
-
this.handler = handler;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* off
|
|
26
|
-
* Removes the listener.
|
|
27
|
-
*
|
|
28
|
-
* @name off
|
|
29
|
-
* @function
|
|
30
|
-
*/
|
|
31
|
-
Watcher.prototype.off = function () {
|
|
32
|
-
this._.removeListener("change", this.handler);
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* FileWatcher
|
|
37
|
-
* Creates a new file watcher.
|
|
38
|
-
*
|
|
39
|
-
* @name FileWatcher
|
|
40
|
-
* @function
|
|
41
|
-
* @param {String} path The path to the file.
|
|
42
|
-
* @param {Boolean|Options} options A boolean value representing the `once`
|
|
43
|
-
* value or an object containing the following fields:
|
|
44
|
-
*
|
|
45
|
-
* - `once` (Boolean): If `true`, the handler is deleted after first event.
|
|
46
|
-
*
|
|
47
|
-
* @param {Function} callback This function will be called when the file is
|
|
48
|
-
* changed or renamed. The first parameter is the error, the second one is
|
|
49
|
-
* the evenit name and the third one is the file path.
|
|
50
|
-
* @return {Watcher} The watcher instance.
|
|
51
|
-
*/
|
|
52
|
-
function FileWatcher(path, options, callback) {
|
|
53
|
-
|
|
54
|
-
if (typeof options === "function") {
|
|
55
|
-
callback = options;
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import isThere from "is-there";
|
|
3
|
+
import abs from "abs";
|
|
4
|
+
import ul from "ul";
|
|
5
|
+
|
|
6
|
+
class Watcher {
|
|
7
|
+
/**
|
|
8
|
+
* Watcher
|
|
9
|
+
* Creates a new instance of the internal `Watcher`.
|
|
10
|
+
* @name Watcher
|
|
11
|
+
* @function
|
|
12
|
+
* @param {string} path The path to the file.
|
|
13
|
+
* @param {Function} handler A function called when the file changes.
|
|
14
|
+
*/
|
|
15
|
+
constructor (path, handler) {
|
|
16
|
+
this._ = fs.watch(path, handler);
|
|
17
|
+
this.a_path = abs(path);
|
|
18
|
+
this.handler = handler;
|
|
56
19
|
}
|
|
57
20
|
|
|
58
|
-
|
|
59
|
-
|
|
21
|
+
/**
|
|
22
|
+
* off
|
|
23
|
+
* Removes the listener.
|
|
24
|
+
* @name off
|
|
25
|
+
* @function
|
|
26
|
+
*/
|
|
27
|
+
off () {
|
|
28
|
+
this._.removeListener("change", this.handler);
|
|
60
29
|
}
|
|
30
|
+
}
|
|
61
31
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
32
|
+
class FileWatcher {
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* FileWatcher
|
|
36
|
+
* Creates a new file watcher.
|
|
37
|
+
* @name FileWatcher
|
|
38
|
+
* @function
|
|
39
|
+
* @param {string} path The path to the file.
|
|
40
|
+
* @param {boolean | Options} options A boolean value representing the `once`
|
|
41
|
+
* value or an object containing the following fields:
|
|
42
|
+
*
|
|
43
|
+
* - `once` (Boolean): If `true`, the handler is deleted after first event.
|
|
44
|
+
* @param {Function} callback This function will be called when the file is
|
|
45
|
+
* changed or renamed. The first parameter is the error, the second one is
|
|
46
|
+
* the evenit name and the third one is the file path.
|
|
47
|
+
* @returns {Watcher} The watcher instance.
|
|
48
|
+
*/
|
|
49
|
+
constructor (path, options, callback) {
|
|
50
|
+
|
|
51
|
+
if (typeof options === "function") { callback = options; }
|
|
52
|
+
if (typeof options === "boolean") { options = { once: options }; }
|
|
53
|
+
options = ul.merge(options, { once: false });
|
|
54
|
+
|
|
55
|
+
let watcher = null;
|
|
56
|
+
let newWatcher = false;
|
|
57
|
+
|
|
58
|
+
const check = cb => {
|
|
59
|
+
let tries = 5;
|
|
60
|
+
const inter = setInterval(() => {
|
|
61
|
+
isThere(path, exists => {
|
|
62
|
+
if (!--tries || exists) {
|
|
63
|
+
clearInterval(inter);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (exists) {
|
|
67
|
+
cb();
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}, 500);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const handler = ev => {
|
|
74
|
+
|
|
75
|
+
if (options.once) {
|
|
96
76
|
watcher.off();
|
|
97
|
-
|
|
98
|
-
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (newWatcher) { return; }
|
|
80
|
+
|
|
81
|
+
// Check the rename
|
|
82
|
+
if (ev === "rename" && !options.once) {
|
|
83
|
+
newWatcher = true;
|
|
84
|
+
check(() => {
|
|
85
|
+
watcher.off();
|
|
86
|
+
watcher._ = new FileWatcher(path, options, callback)._;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
callback.call(watcher, null, ev, watcher.a_path);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
watcher = new Watcher(path, handler);
|
|
95
|
+
} catch (e) {
|
|
96
|
+
callback(e);
|
|
99
97
|
}
|
|
100
98
|
|
|
101
|
-
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
try {
|
|
105
|
-
watcher = new Watcher(path, handler);
|
|
106
|
-
} catch (e) {
|
|
107
|
-
callback(e);
|
|
99
|
+
return watcher;
|
|
108
100
|
}
|
|
109
|
-
|
|
110
|
-
return watcher;
|
|
111
101
|
}
|
|
112
102
|
|
|
113
|
-
|
|
103
|
+
export default FileWatcher;
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fwatcher",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Watch files for changes.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"directories": {
|
|
7
8
|
"example": "example"
|
|
8
9
|
},
|
|
9
|
-
"devDependencies": {},
|
|
10
10
|
"scripts": {
|
|
11
11
|
"test": "node test"
|
|
12
12
|
},
|
|
@@ -19,10 +19,9 @@
|
|
|
19
19
|
"author": "Ionică Bizău <bizauionica@gmail.com> (https://ionicabizau.net)",
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"
|
|
22
|
+
"abs": "^2.0.0",
|
|
23
23
|
"is-there": "^4.0.0",
|
|
24
|
-
"ul": "^
|
|
25
|
-
"abs": "^1.0.0"
|
|
24
|
+
"ul": "^7.0.0"
|
|
26
25
|
},
|
|
27
26
|
"blah": {
|
|
28
27
|
"h_img": "http://i.imgur.com/vy4T9a6.png"
|
|
@@ -46,8 +45,10 @@
|
|
|
46
45
|
"menu/",
|
|
47
46
|
"cli.js",
|
|
48
47
|
"index.js",
|
|
48
|
+
"index.d.ts",
|
|
49
|
+
"package-lock.json",
|
|
49
50
|
"bloggify.js",
|
|
50
51
|
"bloggify.json",
|
|
51
52
|
"bloggify/"
|
|
52
53
|
]
|
|
53
|
-
}
|
|
54
|
+
}
|