|
1 | 1 | import Utils from '../utils' |
2 | 2 | import Urls from '../urls' |
3 | 3 | import Request from '../request' |
| 4 | +import Async from '../request/async' |
4 | 5 |
|
5 | | -export function getFileCount(path, /** pattern, recursive, countDirectories, async */) { |
6 | | - const responder = Utils.extractResponder(arguments) |
7 | | - const isAsync = !!responder |
8 | | - const query = buildCountQueryObject(arguments, isAsync) |
| 6 | +export function getFileCount(path, pattern, recursive, countDirectories, asyncHandler) { |
9 | 7 |
|
10 | | - if (!query.path || !Utils.isString(query.path)) { |
11 | | - throw new Error('Missing value for the "path" argument. The argument must contain a string value') |
12 | | - } |
| 8 | + if (countDirectories instanceof Async) { |
| 9 | + asyncHandler = countDirectories |
| 10 | + countDirectories = undefined |
13 | 11 |
|
14 | | - if (!query.pattern || !Utils.isString(query.pattern)) { |
15 | | - throw new Error('Missing value for the "pattern" argument. The argument must contain a string value') |
16 | | - } |
| 12 | + } else if (recursive instanceof Async) { |
| 13 | + asyncHandler = recursive |
| 14 | + recursive = undefined |
| 15 | + countDirectories = undefined |
17 | 16 |
|
18 | | - if (!Utils.isBoolean(query.recursive)) { |
19 | | - throw new Error('Missing value for the "recursive" argument. The argument must contain a boolean value') |
| 17 | + } else if (pattern instanceof Async) { |
| 18 | + asyncHandler = pattern |
| 19 | + pattern = undefined |
| 20 | + recursive = undefined |
| 21 | + countDirectories = undefined |
20 | 22 | } |
21 | 23 |
|
22 | | - if (!Utils.isBoolean(query.countDirectories)) { |
23 | | - throw new Error('Missing value for the "countDirectories" argument. The argument must contain a boolean value') |
| 24 | + const query = { |
| 25 | + action : 'count', |
| 26 | + pattern : pattern !== undefined ? pattern : '*', |
| 27 | + recursive : !!recursive, |
| 28 | + countDirectories: !!countDirectories |
24 | 29 | } |
25 | 30 |
|
26 | | - delete query.path |
| 31 | + if (!path || !Utils.isString(path)) { |
| 32 | + throw new Error('Files "path" must not be empty and must be String') |
| 33 | + } |
27 | 34 |
|
28 | | - const url = Urls.filePath(path) + '?' + Utils.toQueryParams(query) |
| 35 | + if (!query.pattern || !Utils.isString(query.pattern)) { |
| 36 | + throw new Error('Files "path" must not be empty and must be String') |
| 37 | + } |
29 | 38 |
|
30 | 39 | return Request.get({ |
31 | | - url : url, |
32 | | - isAsync : isAsync, |
33 | | - asyncHandler: responder |
| 40 | + url : Urls.filePath(path), |
| 41 | + query : query, |
| 42 | + isAsync : !!asyncHandler, |
| 43 | + asyncHandler: asyncHandler |
34 | 44 | }) |
35 | 45 | } |
36 | | - |
37 | | -export function buildCountQueryObject(args, isAsync) { |
38 | | - args = isAsync ? Array.prototype.slice.call(args, 0, -1) : args |
39 | | - |
40 | | - return { |
41 | | - action : 'count', |
42 | | - path : args[0], |
43 | | - pattern : args[1] !== undefined ? args[1] : '*', |
44 | | - recursive : args[2] !== undefined ? args[2] : false, |
45 | | - countDirectories: args[3] !== undefined ? args[3] : false |
46 | | - } |
47 | | -} |
0 commit comments