1 | "use strict";
|
2 |
|
3 | const fs = require("./utils/fs");
|
4 | const validate = require("./utils/validate");
|
5 |
|
6 | const validateInput = (methodName, path) => {
|
7 | const methodSignature = `${methodName}(path)`;
|
8 | validate.argument(methodSignature, "path", path, ["string", "undefined"]);
|
9 | };
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | const listSync = path => {
|
16 | try {
|
17 | return fs.readdirSync(path);
|
18 | } catch (err) {
|
19 | if (err.code === "ENOENT") {
|
20 |
|
21 | return undefined;
|
22 | }
|
23 | throw err;
|
24 | }
|
25 | };
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | const listAsync = path => {
|
32 | return new Promise((resolve, reject) => {
|
33 | fs.readdir(path)
|
34 | .then(list => {
|
35 | resolve(list);
|
36 | })
|
37 | .catch(err => {
|
38 | if (err.code === "ENOENT") {
|
39 |
|
40 | resolve(undefined);
|
41 | } else {
|
42 | reject(err);
|
43 | }
|
44 | });
|
45 | });
|
46 | };
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | exports.validateInput = validateInput;
|
53 | exports.sync = listSync;
|
54 | exports.async = listAsync;
|