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.
Files changed (4) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +119 -33
  3. package/lib/index.js +93 -103
  4. package/package.json +7 -6
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2015-19 Ionică Bizău <bizauionica@gmail.com> (https://ionicabizau.net)
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
  [![fwatcher](http://i.imgur.com/vy4T9a6.png)](#)
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] [![Ask me anything](https://img.shields.io/badge/ask%20me-anything-1abc9c.svg)](https://github.com/IonicaBizau/ama) [![Version](https://img.shields.io/npm/v/fwatcher.svg)](https://www.npmjs.com/package/fwatcher) [![Downloads](https://img.shields.io/npm/dt/fwatcher.svg)](https://www.npmjs.com/package/fwatcher) [![Get help on Codementor](https://cdn.codementor.io/badges/get_help_github.svg)](https://www.codementor.io/johnnyb?utm_source=github&utm_medium=button&utm_term=johnnyb&utm_campaign=github)
24
+ [![Support me on Patreon][badge_patreon]][patreon] [![Buy me a book][badge_amazon]][amazon] [![PayPal][badge_paypal_donate]][paypal-donations] [![Ask me anything](https://img.shields.io/badge/ask%20me-anything-1abc9c.svg)](https://github.com/IonicaBizau/ama) [![Version](https://img.shields.io/npm/v/fwatcher.svg)](https://www.npmjs.com/package/fwatcher) [![Downloads](https://img.shields.io/npm/dt/fwatcher.svg)](https://www.npmjs.com/package/fwatcher) [![Get help on Codementor](https://cdn.codementor.io/badges/get_help_github.svg)](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
- // Dependencies
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
- var watcher = FileWatcher(__dirname + "/test.txt", function (err, ev, path) {
37
- if (err) { return Logger.log(err, "error"); }
38
- Logger.log([ev, path].join(" "));
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.bind(watcher), 5000);
41
-
86
+ setTimeout(() => watcher.off(), 5000);
42
87
 
43
88
  // Listen only for one change
44
- FileWatcher(__dirname + "/once.txt", true, function (err, ev, path) {
45
- if (err) { return Logger.log(err, "error"); }
46
- Logger.log([ev, path].join(" "));
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
- 3. For direct and quick help, you can [use Codementor](https://www.codementor.io/johnnyb). :rocket:
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
- - **String** `path`: The path to the file.
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
- - **String** `path`: The path to the file.
83
- - **Boolean|Options** `options`: A boolean value representing the `once` value or an object containing the following fields:
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
- - [`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.
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
- "use strict";
2
-
3
- // Dependencies
4
- var Fs = require("fs"),
5
- IsThere = require("is-there"),
6
- Abs = require("abs"),
7
- Ul = require("ul");
8
-
9
- /**
10
- * Watcher
11
- * Creates a new instance of the internal `Watcher`.
12
- *
13
- * @name Watcher
14
- * @function
15
- * @param {String} path The path to the file.
16
- * @param {Function} handler A function called when the file changes.
17
- */
18
- function Watcher(path, handler) {
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
- if (typeof options === "boolean") {
59
- options = { once: options };
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
- options = Ul.merge(options, {
63
- once: false
64
- });
65
-
66
- var watcher = null,
67
- newWatcher = false,
68
- check = function check(cb) {
69
- var inter = setInterval(function () {
70
- IsThere(path, function (exists) {
71
- if (! --tries || exists) {
72
- clearInterval(inter);
73
- }
74
-
75
- if (exists) {
76
- cb();
77
- }
78
- });
79
- }, 500),
80
- tries = 5;
81
- },
82
- handler = function handler(ev) {
83
-
84
- if (options.once) {
85
- watcher.off();
86
- }
87
-
88
- if (newWatcher) {
89
- return;
90
- }
91
-
92
- // Check the rename
93
- if (ev === "rename" && !options.once) {
94
- newWatcher = true;
95
- check(function () {
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
- watcher._ = FileWatcher(path, options, callback)._;
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
- callback.call(watcher, null, ev, watcher.a_path);
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
- module.exports = FileWatcher;
103
+ export default FileWatcher;
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "fwatcher",
3
- "version": "2.2.9",
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
- "bug-killer": "^4.0.0",
22
+ "abs": "^2.0.0",
23
23
  "is-there": "^4.0.0",
24
- "ul": "^5.0.0",
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
+ }