fwatcher 2.2.9 → 3.0.1
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 +30 -63
- package/lib/index.js +93 -103
- package/package.json +6 -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,11 +1,10 @@
|
|
|
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
|
-
|
|
4
3
|
[](#)
|
|
5
4
|
|
|
6
5
|
# fwatcher
|
|
7
6
|
|
|
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
|
|
7
|
+
[![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
8
|
|
|
10
9
|
<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
10
|
|
|
@@ -21,54 +20,32 @@ npm install --save fwatcher
|
|
|
21
20
|
yarn add fwatcher
|
|
22
21
|
```
|
|
23
22
|
|
|
24
|
-
|
|
25
23
|
## :clipboard: Example
|
|
26
24
|
|
|
27
|
-
|
|
28
|
-
|
|
29
25
|
```js
|
|
30
|
-
|
|
31
|
-
var FileWatcher = require("fwatcher")
|
|
32
|
-
, Logger = require("bug-killer")
|
|
33
|
-
;
|
|
26
|
+
import fileWatcher from "fwatcher";
|
|
34
27
|
|
|
35
28
|
// Listen for changes for 5 seconds
|
|
36
|
-
|
|
37
|
-
if (err) { return
|
|
38
|
-
|
|
29
|
+
const watcher = new fileWatcher(`example/test.txt`, (err, ev, path) => {
|
|
30
|
+
if (err) { return console.error(err, "error"); }
|
|
31
|
+
console.log([ev, path].join(" "));
|
|
39
32
|
});
|
|
40
|
-
setTimeout(watcher.off
|
|
41
|
-
|
|
33
|
+
setTimeout(() => watcher.off(), 5000);
|
|
42
34
|
|
|
43
35
|
// Listen only for one change
|
|
44
|
-
|
|
45
|
-
if (err) { return
|
|
46
|
-
|
|
36
|
+
new fileWatcher(`example/once.txt`, true, (err, ev, path) => {
|
|
37
|
+
if (err) { return console.error(err, "error"); }
|
|
38
|
+
console.log([ev, path].join(" "));
|
|
47
39
|
});
|
|
48
40
|
```
|
|
49
41
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
## :question: Get Help
|
|
53
|
-
|
|
54
|
-
There are few ways to get help:
|
|
55
|
-
|
|
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
|
-
|
|
59
|
-
3. For direct and quick help, you can [use Codementor](https://www.codementor.io/johnnyb). :rocket:
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
42
|
## :memo: Documentation
|
|
64
43
|
|
|
65
|
-
|
|
66
44
|
### `Watcher(path, handler)`
|
|
67
45
|
Creates a new instance of the internal `Watcher`.
|
|
68
46
|
|
|
69
47
|
#### Params
|
|
70
|
-
|
|
71
|
-
- **String** `path`: The path to the file.
|
|
48
|
+
- **string** `path`: The path to the file.
|
|
72
49
|
- **Function** `handler`: A function called when the file changes.
|
|
73
50
|
|
|
74
51
|
### `off()`
|
|
@@ -78,9 +55,8 @@ Removes the listener.
|
|
|
78
55
|
Creates a new file watcher.
|
|
79
56
|
|
|
80
57
|
#### Params
|
|
81
|
-
|
|
82
|
-
- **
|
|
83
|
-
- **Boolean|Options** `options`: A boolean value representing the `once` value or an object containing the following fields:
|
|
58
|
+
- **string** `path`: The path to the file.
|
|
59
|
+
- **boolean|Options** `options`: A boolean value representing the `once` value or an object containing the following fields:
|
|
84
60
|
|
|
85
61
|
- `once` (Boolean): If `true`, the handler is deleted after first event.
|
|
86
62
|
- **Function** `callback`: This function will be called when the file is changed or renamed. The first parameter is the error, the second one is
|
|
@@ -89,55 +65,46 @@ the evenit name and the third one is the file path.
|
|
|
89
65
|
#### Return
|
|
90
66
|
- **Watcher** The watcher instance.
|
|
91
67
|
|
|
68
|
+
## :question: Get Help
|
|
92
69
|
|
|
70
|
+
There are a few ways to get help:
|
|
93
71
|
|
|
94
|
-
|
|
95
|
-
|
|
72
|
+
1. Please [post questions on Stack Overflow](https://stackoverflow.com/questions/ask). You can open issues with questions, as long as you add a link to your Stack Overflow question.
|
|
73
|
+
2. For bug reports and feature requests, open issues. :bug:
|
|
74
|
+
3. For direct and quick help, you can [use Codementor](https://www.codementor.io/johnnyb). :rocket:
|
|
96
75
|
|
|
76
|
+
## :yum: How to Contribute
|
|
97
77
|
|
|
98
|
-
|
|
78
|
+
Have an idea? Found a bug? See [how to contribute][contributing].
|
|
99
79
|
|
|
100
|
-
|
|
101
|
-
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).
|
|
80
|
+
## :sparkling_heart: Support My Projects
|
|
102
81
|
|
|
103
|
-
|
|
82
|
+
I open-source almost everything I can, and I try to reply to everyone needing help using these projects. Obviously, this takes time. You can integrate and use these projects in your applications *for free*! You can even change the source code and redistribute it (even resell it).
|
|
104
83
|
|
|
84
|
+
However, if you get some profit from this or just want to encourage me to continue creating stuff, there are a few ways you can do it:
|
|
105
85
|
|
|
106
86
|
- Starring and sharing the projects you like :rocket:
|
|
107
|
-
- [![Buy me a book][badge_amazon]][amazon]—I love books! I will remember you
|
|
87
|
+
- [![Buy me a book][badge_amazon]][amazon]—I love books! I will remember you for years if you buy me one. :grin: :book:
|
|
108
88
|
- [![PayPal][badge_paypal]][paypal-donations]—You can make one-time donations via PayPal. I'll probably buy a ~~coffee~~ tea. :tea:
|
|
109
89
|
- [![Support me on Patreon][badge_patreon]][patreon]—Set up a recurring monthly donation and you will get interesting news about what I'm doing (things that I don't share with everyone).
|
|
110
|
-
- **Bitcoin**—You can send me bitcoins at this address (or scanning the code below): `1P9BRsmazNQcuyTxEqveUsnf5CERdq35V6`
|
|
111
|
-
|
|
112
|
-

|
|
90
|
+
- **Bitcoin**—You can send me bitcoins at this address (or by scanning the code below): `1P9BRsmazNQcuyTxEqveUsnf5CERdq35V6`
|
|
113
91
|
|
|
92
|
+

|
|
114
93
|
|
|
115
94
|
Thanks! :heart:
|
|
116
95
|
|
|
117
|
-
|
|
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
|
-
|
|
121
|
-
|
|
122
|
-
- [`element-status`](https://github.com/callahanrts/element#readme) (by CallahanRTS)—An electron based status bar
|
|
123
|
-
- [`rucksack`](https://github.com/Bloggify/rucksack#readme) (by Bloggify)—JavaScript and CSS bundler.
|
|
124
|
-
- [`web-term`](https://github.com/IonicaBizau/web-term)—A full screen terminal in your browser.
|
|
125
|
-
|
|
126
96
|
## :scroll: License
|
|
127
97
|
|
|
128
98
|
[MIT][license] © [Ionică Bizău][website]
|
|
129
99
|
|
|
130
|
-
|
|
100
|
+
[license]: /LICENSE
|
|
101
|
+
[website]: https://ionicabizau.net
|
|
102
|
+
[contributing]: /CONTRIBUTING.md
|
|
103
|
+
[docs]: /DOCUMENTATION.md
|
|
131
104
|
[badge_patreon]: https://ionicabizau.github.io/badges/patreon.svg
|
|
132
105
|
[badge_amazon]: https://ionicabizau.github.io/badges/amazon.svg
|
|
133
106
|
[badge_paypal]: https://ionicabizau.github.io/badges/paypal.svg
|
|
134
107
|
[badge_paypal_donate]: https://ionicabizau.github.io/badges/paypal_donate.svg
|
|
135
|
-
|
|
136
108
|
[patreon]: https://www.patreon.com/ionicabizau
|
|
137
|
-
[amazon]:
|
|
109
|
+
[amazon]: https://amzn.eu/hRo9sIZ
|
|
138
110
|
[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.1",
|
|
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,9 @@
|
|
|
46
45
|
"menu/",
|
|
47
46
|
"cli.js",
|
|
48
47
|
"index.js",
|
|
48
|
+
"index.d.ts",
|
|
49
49
|
"bloggify.js",
|
|
50
50
|
"bloggify.json",
|
|
51
51
|
"bloggify/"
|
|
52
52
|
]
|
|
53
|
-
}
|
|
53
|
+
}
|