@util.js/node-files 0.41.5 → 0.41.6

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/lib/Files.js CHANGED
@@ -92,8 +92,22 @@ module.exports = class Files {
92
92
  return path().dirname(...arguments);
93
93
  }
94
94
 
95
- exists() {
96
- return promises().applyCallback(fs(), fs().exists, arguments);
95
+ exists(path, callback) {
96
+ if (callback) {
97
+ return fs().exists(path, callback);
98
+ }
99
+ return new Promise((resolve, reject) => {
100
+ // From https://nodejs.org/api/fs.html#fsexistspath-callback:
101
+ // The parameters for this callback are not consistent with other Node.js callbacks.
102
+ // Normally, the first parameter to a Node.js callback is an err parameter, optionally
103
+ // followed by other parameters. The fs.exists() callback has only one boolean
104
+ // parameter.
105
+ try {
106
+ fs().exists(path, resolve);
107
+ } catch (err) {
108
+ reject(err);
109
+ }
110
+ });
97
111
  }
98
112
 
99
113
  existsSync() {
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "author": "Chris Topher <c@creemama.com>",
3
3
  "dependencies": {
4
- "@util.js/objects": "0.41.5",
5
- "@util.js/promises": "0.41.5",
6
- "@util.js/strings": "0.41.5",
4
+ "@util.js/objects": "0.41.6",
5
+ "@util.js/promises": "0.41.6",
6
+ "@util.js/strings": "0.41.6",
7
7
  "graceful-fs": "4.2.11",
8
8
  "mkdirp": "3.0.1",
9
9
  "ncp": "2.0.0",
@@ -38,5 +38,5 @@
38
38
  "type": "git",
39
39
  "url": "https://github.com/creemama/utiljs/tree/master/packages/utiljs-node-files"
40
40
  },
41
- "version": "0.41.5"
41
+ "version": "0.41.6"
42
42
  }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+
3
+ const { expect } = require("chai");
4
+ const files = require("..");
5
+
6
+ describe("Files#exists", () => {
7
+ const targetDir = files.join(__dirname, "..", "target");
8
+ files.mkdirpSync(targetDir);
9
+
10
+ it("should return true for an existing file via a callback", (done) => {
11
+ files.exists(targetDir, (exists) => {
12
+ expect(exists).to.be.true;
13
+ done();
14
+ });
15
+ });
16
+
17
+ it("should return true for an existing file via a Promise", async () => {
18
+ expect(await files.exists(targetDir)).to.be.true;
19
+ });
20
+
21
+ it("should return false for a nonexistent file via a callback", (done) => {
22
+ files.exists(targetDir + "-nonexistent", (exists) => {
23
+ expect(exists).to.be.false;
24
+ done();
25
+ });
26
+ });
27
+
28
+ it("should return false for an nonexistent file via a Promise", async () => {
29
+ expect(await files.exists(targetDir + "-nonexistent")).to.be.false;
30
+ });
31
+ });