fwatcher 2.2.8 → 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 +135 -38
- package/lib/index.js +93 -103
- package/package.json +6 -5
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,55 +1,105 @@
|
|
|
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)
|
|
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)
|
|
25
|
+
|
|
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>
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
9
33
|
|
|
10
34
|
> Watch files for changes.
|
|
11
35
|
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
12
52
|
## :cloud: Installation
|
|
13
53
|
|
|
14
54
|
```sh
|
|
15
|
-
|
|
55
|
+
# Using npm
|
|
56
|
+
npm install --save fwatcher
|
|
57
|
+
|
|
58
|
+
# Using yarn
|
|
59
|
+
yarn add fwatcher
|
|
16
60
|
```
|
|
17
61
|
|
|
18
62
|
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
19
74
|
## :clipboard: Example
|
|
20
75
|
|
|
21
76
|
|
|
22
77
|
|
|
23
78
|
```js
|
|
24
|
-
|
|
25
|
-
var FileWatcher = require("fwatcher")
|
|
26
|
-
, Logger = require("bug-killer")
|
|
27
|
-
;
|
|
79
|
+
import fileWatcher from "fwatcher";
|
|
28
80
|
|
|
29
81
|
// Listen for changes for 5 seconds
|
|
30
|
-
|
|
31
|
-
if (err) { return
|
|
32
|
-
|
|
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(" "));
|
|
33
85
|
});
|
|
34
|
-
setTimeout(watcher.off
|
|
35
|
-
|
|
86
|
+
setTimeout(() => watcher.off(), 5000);
|
|
36
87
|
|
|
37
88
|
// Listen only for one change
|
|
38
|
-
|
|
39
|
-
if (err) { return
|
|
40
|
-
|
|
89
|
+
new fileWatcher(`example/once.txt`, true, (err, ev, path) => {
|
|
90
|
+
if (err) { return console.error(err, "error"); }
|
|
91
|
+
console.log([ev, path].join(" "));
|
|
41
92
|
});
|
|
42
93
|
```
|
|
43
94
|
|
|
44
95
|
|
|
45
96
|
|
|
46
|
-
## :question: Get Help
|
|
47
97
|
|
|
48
|
-
There are few ways to get help:
|
|
49
98
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
53
103
|
|
|
54
104
|
|
|
55
105
|
## :memo: Documentation
|
|
@@ -60,7 +110,7 @@ Creates a new instance of the internal `Watcher`.
|
|
|
60
110
|
|
|
61
111
|
#### Params
|
|
62
112
|
|
|
63
|
-
- **
|
|
113
|
+
- **string** `path`: The path to the file.
|
|
64
114
|
- **Function** `handler`: A function called when the file changes.
|
|
65
115
|
|
|
66
116
|
### `off()`
|
|
@@ -71,8 +121,8 @@ Creates a new file watcher.
|
|
|
71
121
|
|
|
72
122
|
#### Params
|
|
73
123
|
|
|
74
|
-
- **
|
|
75
|
-
- **
|
|
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:
|
|
76
126
|
|
|
77
127
|
- `once` (Boolean): If `true`, the handler is deleted after first event.
|
|
78
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
|
|
@@ -83,17 +133,46 @@ the evenit name and the third one is the file path.
|
|
|
83
133
|
|
|
84
134
|
|
|
85
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
|
+
|
|
86
165
|
## :yum: How to contribute
|
|
87
166
|
Have an idea? Found a bug? See [how to contribute][contributing].
|
|
88
167
|
|
|
89
168
|
|
|
90
169
|
## :sparkling_heart: Support my projects
|
|
91
|
-
|
|
92
|
-
I open-source almost everything I can, and I try to reply everyone needing help using these projects. Obviously,
|
|
170
|
+
I open-source almost everything I can, and I try to reply to everyone needing help using these projects. Obviously,
|
|
93
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).
|
|
94
172
|
|
|
95
173
|
However, if you get some profit from this or just want to encourage me to continue creating stuff, there are few ways you can do it:
|
|
96
174
|
|
|
175
|
+
|
|
97
176
|
- Starring and sharing the projects you like :rocket:
|
|
98
177
|
- [![Buy me a book][badge_amazon]][amazon]—I love books! I will remember you after years if you buy me one. :grin: :book:
|
|
99
178
|
- [![PayPal][badge_paypal]][paypal-donations]—You can make one-time donations via PayPal. I'll probably buy a ~~coffee~~ tea. :tea:
|
|
@@ -102,31 +181,49 @@ However, if you get some profit from this or just want to encourage me to contin
|
|
|
102
181
|
|
|
103
182
|

|
|
104
183
|
|
|
184
|
+
|
|
105
185
|
Thanks! :heart:
|
|
106
186
|
|
|
107
187
|
|
|
108
|
-
## :dizzy: Where is this library used?
|
|
109
|
-
If you are using this library in one of your projects, add it in this list. :sparkles:
|
|
110
188
|
|
|
111
189
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
115
209
|
|
|
116
210
|
## :scroll: License
|
|
117
211
|
|
|
118
212
|
[MIT][license] © [Ionică Bizău][website]
|
|
119
213
|
|
|
120
|
-
[badge_patreon]: http://ionicabizau.github.io/badges/patreon.svg
|
|
121
|
-
[badge_amazon]: http://ionicabizau.github.io/badges/amazon.svg
|
|
122
|
-
[badge_paypal]: http://ionicabizau.github.io/badges/paypal.svg
|
|
123
|
-
[badge_paypal_donate]: http://ionicabizau.github.io/badges/paypal_donate.svg
|
|
124
|
-
[patreon]: https://www.patreon.com/ionicabizau
|
|
125
|
-
[amazon]: http://amzn.eu/hRo9sIZ
|
|
126
|
-
[paypal-donations]: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RVXDDLKKLQRJW
|
|
127
|
-
[donate-now]: http://i.imgur.com/6cMbHOC.png
|
|
128
214
|
|
|
129
|
-
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
[license]: /LICENSE
|
|
130
220
|
[website]: https://ionicabizau.net
|
|
131
221
|
[contributing]: /CONTRIBUTING.md
|
|
132
222
|
[docs]: /DOCUMENTATION.md
|
|
223
|
+
[badge_patreon]: https://ionicabizau.github.io/badges/patreon.svg
|
|
224
|
+
[badge_amazon]: https://ionicabizau.github.io/badges/amazon.svg
|
|
225
|
+
[badge_paypal]: https://ionicabizau.github.io/badges/paypal.svg
|
|
226
|
+
[badge_paypal_donate]: https://ionicabizau.github.io/badges/paypal_donate.svg
|
|
227
|
+
[patreon]: https://www.patreon.com/ionicabizau
|
|
228
|
+
[amazon]: http://amzn.eu/hRo9sIZ
|
|
229
|
+
[paypal-donations]: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RVXDDLKKLQRJW
|
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,6 +45,8 @@
|
|
|
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/"
|