@util.js/node-files 0.41.4 → 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 +22 -8
- package/package.json +9 -10
- package/test/FilesTest.js +33 -33
- package/test/Files_exists_Test.js +31 -0
- package/test/Files_sanitizeFilename_Test.js +1 -1
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
|
-
|
|
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() {
|
|
@@ -135,17 +149,17 @@ module.exports = class Files {
|
|
|
135
149
|
return (async function () {
|
|
136
150
|
if (!objects().isDefined(params))
|
|
137
151
|
throw new TypeError(
|
|
138
|
-
`We expected params to be defined, but it was ${params}
|
|
152
|
+
`We expected params to be defined, but it was ${params}.`,
|
|
139
153
|
);
|
|
140
154
|
const { dir, ext } = params;
|
|
141
155
|
|
|
142
156
|
if (!objects().isDefined(dir))
|
|
143
157
|
throw new TypeError(
|
|
144
|
-
`We expected params.dir to be defined, but it was ${dir}
|
|
158
|
+
`We expected params.dir to be defined, but it was ${dir}.`,
|
|
145
159
|
);
|
|
146
160
|
if (!objects().isDefined(ext))
|
|
147
161
|
throw new TypeError(
|
|
148
|
-
`We expected params.ext to be defined, but it was ${ext}
|
|
162
|
+
`We expected params.ext to be defined, but it was ${ext}.`,
|
|
149
163
|
);
|
|
150
164
|
|
|
151
165
|
const files = await thiz.readdir(dir);
|
|
@@ -167,18 +181,18 @@ module.exports = class Files {
|
|
|
167
181
|
filesWithExtensionSync(params) {
|
|
168
182
|
if (!objects().isDefined(params))
|
|
169
183
|
throw new TypeError(
|
|
170
|
-
`We expected params to be defined, but it was ${params}
|
|
184
|
+
`We expected params to be defined, but it was ${params}.`,
|
|
171
185
|
);
|
|
172
186
|
|
|
173
187
|
const { dir, ext } = params;
|
|
174
188
|
|
|
175
189
|
if (!objects().isDefined(dir))
|
|
176
190
|
throw new TypeError(
|
|
177
|
-
`We expected params.dir to be defined, but it was ${dir}
|
|
191
|
+
`We expected params.dir to be defined, but it was ${dir}.`,
|
|
178
192
|
);
|
|
179
193
|
if (!objects().isDefined(ext))
|
|
180
194
|
throw new TypeError(
|
|
181
|
-
`We expected params.ext to be defined, but it was ${ext}
|
|
195
|
+
`We expected params.ext to be defined, but it was ${ext}.`,
|
|
182
196
|
);
|
|
183
197
|
|
|
184
198
|
const files = this.readdirSync(dir);
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "Chris Topher <c@creemama.com>",
|
|
3
3
|
"dependencies": {
|
|
4
|
-
"@util.js/objects": "0.41.
|
|
5
|
-
"@util.js/promises": "0.41.
|
|
6
|
-
"@util.js/strings": "0.41.
|
|
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
|
-
"mkdirp": "3.0.
|
|
8
|
+
"mkdirp": "3.0.1",
|
|
9
9
|
"ncp": "2.0.0",
|
|
10
|
-
"read-last-lines": "1.
|
|
11
|
-
"rimraf": "
|
|
12
|
-
"sanitize-filename": "1.6.
|
|
13
|
-
"touch": "3.1.
|
|
10
|
+
"read-last-lines": "1.10.0",
|
|
11
|
+
"rimraf": "6.1.3",
|
|
12
|
+
"sanitize-filename": "1.6.4",
|
|
13
|
+
"touch": "3.1.1"
|
|
14
14
|
},
|
|
15
15
|
"description": "JavaScript Node.js utility methods for files",
|
|
16
16
|
"devDependencies": {
|
|
@@ -38,6 +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.
|
|
42
|
-
"gitHead": "2119863a175cf7095ca3a2417e61af32974add15"
|
|
41
|
+
"version": "0.41.6"
|
|
43
42
|
}
|
package/test/FilesTest.js
CHANGED
|
@@ -45,7 +45,7 @@ describe("Files", function () {
|
|
|
45
45
|
lines = await files.readLastLines(
|
|
46
46
|
new URL("file://" + __filename),
|
|
47
47
|
3,
|
|
48
|
-
"utf8"
|
|
48
|
+
"utf8",
|
|
49
49
|
);
|
|
50
50
|
expect(lines).to.eql(" });\n });\n});\n");
|
|
51
51
|
}
|
|
@@ -53,14 +53,14 @@ describe("Files", function () {
|
|
|
53
53
|
lines = await files.readLastLines(
|
|
54
54
|
Buffer.from(__filename, "utf8"),
|
|
55
55
|
3,
|
|
56
|
-
"utf8"
|
|
56
|
+
"utf8",
|
|
57
57
|
);
|
|
58
58
|
expect(lines).to.eql(" });\n });\n});\n");
|
|
59
59
|
|
|
60
60
|
lines = await files.readLastLines(
|
|
61
61
|
Buffer.from(__filename, "utf8"),
|
|
62
62
|
3,
|
|
63
|
-
Buffer.from("utf8", "utf8")
|
|
63
|
+
Buffer.from("utf8", "utf8"),
|
|
64
64
|
);
|
|
65
65
|
expect(lines).to.eql(" });\n });\n});\n");
|
|
66
66
|
});
|
|
@@ -159,7 +159,7 @@ describe("Files", function () {
|
|
|
159
159
|
.readLastLines(new URL("http://www.google.com"), 3)
|
|
160
160
|
.then((lines) => assert.fail())
|
|
161
161
|
.catch((error) =>
|
|
162
|
-
expect(error.message).to.eql("The URL must be of scheme file")
|
|
162
|
+
expect(error.message).to.eql("The URL must be of scheme file"),
|
|
163
163
|
);
|
|
164
164
|
|
|
165
165
|
// The method fails if path is a directory.
|
|
@@ -168,8 +168,8 @@ describe("Files", function () {
|
|
|
168
168
|
.then((lines) => assert.fail())
|
|
169
169
|
.catch((error) =>
|
|
170
170
|
expect(error.message).to.eql(
|
|
171
|
-
"EISDIR: illegal operation on a directory, read"
|
|
172
|
-
)
|
|
171
|
+
"EISDIR: illegal operation on a directory, read",
|
|
172
|
+
),
|
|
173
173
|
);
|
|
174
174
|
|
|
175
175
|
// The method fails if path is one of the primitive types other than string.
|
|
@@ -198,7 +198,7 @@ describe("Files", function () {
|
|
|
198
198
|
files.readLastLines(__dirname, 3, (error, lines) => {
|
|
199
199
|
try {
|
|
200
200
|
expect(error.message).to.eql(
|
|
201
|
-
"EISDIR: illegal operation on a directory, read"
|
|
201
|
+
"EISDIR: illegal operation on a directory, read",
|
|
202
202
|
);
|
|
203
203
|
callback();
|
|
204
204
|
} catch (e) {
|
|
@@ -212,8 +212,8 @@ describe("Files", function () {
|
|
|
212
212
|
.then((lines) => assert.fail())
|
|
213
213
|
.catch((error) =>
|
|
214
214
|
expect(error.message).to.eql(
|
|
215
|
-
"Cannot convert a Symbol value to a number"
|
|
216
|
-
)
|
|
215
|
+
"Cannot convert a Symbol value to a number",
|
|
216
|
+
),
|
|
217
217
|
);
|
|
218
218
|
});
|
|
219
219
|
it("should fail if the encoding is invalid", async function () {
|
|
@@ -221,14 +221,14 @@ describe("Files", function () {
|
|
|
221
221
|
.readLastLines(__filename, 3, false)
|
|
222
222
|
.then((lines) => assert.fail())
|
|
223
223
|
.catch((error) =>
|
|
224
|
-
expect(error.message).to.eql("Unknown encoding: false")
|
|
224
|
+
expect(error.message).to.eql("Unknown encoding: false"),
|
|
225
225
|
);
|
|
226
226
|
|
|
227
227
|
await files
|
|
228
228
|
.readLastLines(__filename, 3, NaN)
|
|
229
229
|
.then((lines) => assert.fail())
|
|
230
230
|
.catch((error) =>
|
|
231
|
-
expect(error.message).to.eql("Unknown encoding: NaN")
|
|
231
|
+
expect(error.message).to.eql("Unknown encoding: NaN"),
|
|
232
232
|
);
|
|
233
233
|
|
|
234
234
|
await files
|
|
@@ -245,7 +245,7 @@ describe("Files", function () {
|
|
|
245
245
|
.readLastLines(__filename, 3, "frog")
|
|
246
246
|
.then((lines) => assert.fail())
|
|
247
247
|
.catch((error) =>
|
|
248
|
-
expect(error.message).to.eql("Unknown encoding: frog")
|
|
248
|
+
expect(error.message).to.eql("Unknown encoding: frog"),
|
|
249
249
|
);
|
|
250
250
|
|
|
251
251
|
await files
|
|
@@ -253,8 +253,8 @@ describe("Files", function () {
|
|
|
253
253
|
.then((lines) => assert.fail())
|
|
254
254
|
.catch((error) =>
|
|
255
255
|
expect(error.message).to.eql(
|
|
256
|
-
"Cannot convert a Symbol value to a string"
|
|
257
|
-
)
|
|
256
|
+
"Cannot convert a Symbol value to a string",
|
|
257
|
+
),
|
|
258
258
|
);
|
|
259
259
|
});
|
|
260
260
|
});
|
|
@@ -469,7 +469,7 @@ describe("Files", function () {
|
|
|
469
469
|
} catch (err) {
|
|
470
470
|
done(err);
|
|
471
471
|
}
|
|
472
|
-
}
|
|
472
|
+
},
|
|
473
473
|
);
|
|
474
474
|
});
|
|
475
475
|
it("should return [] if dir is empty", (done) => {
|
|
@@ -486,7 +486,7 @@ describe("Files", function () {
|
|
|
486
486
|
} catch (err) {
|
|
487
487
|
done(err);
|
|
488
488
|
}
|
|
489
|
-
}
|
|
489
|
+
},
|
|
490
490
|
);
|
|
491
491
|
});
|
|
492
492
|
it("should have a forward slash separating directory and filename in returned filenames if dir does not contain a slash at the end", async function () {
|
|
@@ -520,7 +520,7 @@ describe("Files", function () {
|
|
|
520
520
|
} catch (err) {
|
|
521
521
|
done(err);
|
|
522
522
|
}
|
|
523
|
-
}
|
|
523
|
+
},
|
|
524
524
|
);
|
|
525
525
|
});
|
|
526
526
|
});
|
|
@@ -586,13 +586,13 @@ describe("Files", function () {
|
|
|
586
586
|
});
|
|
587
587
|
it("should return [] if dir is empty", function () {
|
|
588
588
|
expect(
|
|
589
|
-
files.filesWithExtensionSync({ dir: emptyDir, ext: "json" })
|
|
589
|
+
files.filesWithExtensionSync({ dir: emptyDir, ext: "json" }),
|
|
590
590
|
).to.have.members([]);
|
|
591
591
|
});
|
|
592
592
|
it("should have a forward slash separating directory and filename in returned filenames if dir does not contain a slash at the end", function () {
|
|
593
593
|
return prepareDirWithJsonFiles().then(() => {
|
|
594
594
|
expect(
|
|
595
|
-
files.filesWithExtensionSync({ dir: jsonDir, ext: "json" })
|
|
595
|
+
files.filesWithExtensionSync({ dir: jsonDir, ext: "json" }),
|
|
596
596
|
).to.have.members([
|
|
597
597
|
jsonDir + "/package.json",
|
|
598
598
|
jsonDir + "/server.json",
|
|
@@ -603,7 +603,7 @@ describe("Files", function () {
|
|
|
603
603
|
it("should not have two forward slashes in returned filenames if dir contains a slash at the end", function () {
|
|
604
604
|
return prepareDirWithJsonFiles().then(() => {
|
|
605
605
|
expect(
|
|
606
|
-
files.filesWithExtensionSync({ dir: jsonDir, ext: "json" })
|
|
606
|
+
files.filesWithExtensionSync({ dir: jsonDir, ext: "json" }),
|
|
607
607
|
).to.have.members([
|
|
608
608
|
jsonDir + "/package.json",
|
|
609
609
|
jsonDir + "/server.json",
|
|
@@ -614,14 +614,14 @@ describe("Files", function () {
|
|
|
614
614
|
it("should return [] if there are files in dir matching ext but ext starts with a period", function () {
|
|
615
615
|
return prepareDirWithJsonFiles().then(() => {
|
|
616
616
|
expect(
|
|
617
|
-
files.filesWithExtensionSync({ dir: jsonDir, ext: ".json" })
|
|
617
|
+
files.filesWithExtensionSync({ dir: jsonDir, ext: ".json" }),
|
|
618
618
|
).to.have.members([]);
|
|
619
619
|
});
|
|
620
620
|
});
|
|
621
621
|
it("should return [] if ext is empty", function () {
|
|
622
622
|
return prepareDirWithJsonFiles().then(() => {
|
|
623
623
|
expect(
|
|
624
|
-
files.filesWithExtensionSync({ dir: jsonDir, ext: "" })
|
|
624
|
+
files.filesWithExtensionSync({ dir: jsonDir, ext: "" }),
|
|
625
625
|
).to.have.members([]);
|
|
626
626
|
});
|
|
627
627
|
});
|
|
@@ -654,7 +654,7 @@ describe("Files", function () {
|
|
|
654
654
|
} catch (err) {
|
|
655
655
|
done(err);
|
|
656
656
|
}
|
|
657
|
-
}
|
|
657
|
+
},
|
|
658
658
|
);
|
|
659
659
|
});
|
|
660
660
|
it("should error for UnlikelyToExist.js", async function () {
|
|
@@ -808,7 +808,7 @@ describe("Files", function () {
|
|
|
808
808
|
cb(null);
|
|
809
809
|
},
|
|
810
810
|
],
|
|
811
|
-
done
|
|
811
|
+
done,
|
|
812
812
|
);
|
|
813
813
|
});
|
|
814
814
|
});
|
|
@@ -839,7 +839,7 @@ describe("Files", function () {
|
|
|
839
839
|
cb();
|
|
840
840
|
},
|
|
841
841
|
],
|
|
842
|
-
done
|
|
842
|
+
done,
|
|
843
843
|
);
|
|
844
844
|
});
|
|
845
845
|
});
|
|
@@ -902,7 +902,7 @@ describe("Files", function () {
|
|
|
902
902
|
},
|
|
903
903
|
files.writeFile,
|
|
904
904
|
],
|
|
905
|
-
done
|
|
905
|
+
done,
|
|
906
906
|
);
|
|
907
907
|
});
|
|
908
908
|
|
|
@@ -921,7 +921,7 @@ describe("Files", function () {
|
|
|
921
921
|
} catch (err) {
|
|
922
922
|
done(err);
|
|
923
923
|
}
|
|
924
|
-
}
|
|
924
|
+
},
|
|
925
925
|
);
|
|
926
926
|
});
|
|
927
927
|
it("should successfully read three files when called with option 'utf8'", (done) => {
|
|
@@ -936,13 +936,13 @@ describe("Files", function () {
|
|
|
936
936
|
} catch (err) {
|
|
937
937
|
done(err);
|
|
938
938
|
}
|
|
939
|
-
}
|
|
939
|
+
},
|
|
940
940
|
);
|
|
941
941
|
});
|
|
942
942
|
it("should successfully read three files when called with options", async function () {
|
|
943
943
|
const string = await files.readFiles(
|
|
944
944
|
[targetDir + "/c.txt", targetDir + "/a.txt", targetDir + "/c.txt"],
|
|
945
|
-
{ encoding: "utf8", flag: "r" }
|
|
945
|
+
{ encoding: "utf8", flag: "r" },
|
|
946
946
|
);
|
|
947
947
|
expect(string).to.equal("cac");
|
|
948
948
|
});
|
|
@@ -958,7 +958,7 @@ describe("Files", function () {
|
|
|
958
958
|
} catch (err) {
|
|
959
959
|
done(err);
|
|
960
960
|
}
|
|
961
|
-
}
|
|
961
|
+
},
|
|
962
962
|
);
|
|
963
963
|
});
|
|
964
964
|
it("should return a Promise if callback is null", async function () {
|
|
@@ -1019,7 +1019,7 @@ describe("Files", function () {
|
|
|
1019
1019
|
} catch (err) {
|
|
1020
1020
|
done(err);
|
|
1021
1021
|
}
|
|
1022
|
-
}
|
|
1022
|
+
},
|
|
1023
1023
|
);
|
|
1024
1024
|
});
|
|
1025
1025
|
});
|
|
@@ -1051,7 +1051,7 @@ describe("Files", function () {
|
|
|
1051
1051
|
},
|
|
1052
1052
|
files.mkdirp,
|
|
1053
1053
|
],
|
|
1054
|
-
done
|
|
1054
|
+
done,
|
|
1055
1055
|
);
|
|
1056
1056
|
});
|
|
1057
1057
|
|
|
@@ -1090,7 +1090,7 @@ describe("Files", function () {
|
|
|
1090
1090
|
},
|
|
1091
1091
|
files.isFile.bind(files),
|
|
1092
1092
|
],
|
|
1093
|
-
done
|
|
1093
|
+
done,
|
|
1094
1094
|
);
|
|
1095
1095
|
});
|
|
1096
1096
|
it("should throw TypeError when filename is null", async function () {
|
|
@@ -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
|
+
});
|
|
@@ -18,7 +18,7 @@ describe("Files#sanitizeFilename", () => {
|
|
|
18
18
|
});
|
|
19
19
|
it("should throw an error if the filename is not a string", () => {
|
|
20
20
|
expect(() =>
|
|
21
|
-
files.sanitizeFilename(["~/.\u0000ssh/authorized_keys"])
|
|
21
|
+
files.sanitizeFilename(["~/.\u0000ssh/authorized_keys"]),
|
|
22
22
|
).to.throw(Error);
|
|
23
23
|
});
|
|
24
24
|
});
|