-
Notifications
You must be signed in to change notification settings - Fork 1
/
find-files.js
74 lines (67 loc) · 1.74 KB
/
find-files.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* eslint-disable no-sync */
const path = require("path")
const fs = require("fs")
const pipe = require("../pipe/pipe")
const map = require("../map/map")
const push = require("../push/push")
const flatten = require("../flatten/flatten")
const reduce = require("../reduce/reduce")
/**
* Determines if file name valid.
*
* @param {Function|RegExp} match The match
*
* @return {boolean} True if file name valid, False otherwise.
*/
const isFileNameValid = match => fileName =>
typeof match === "function"
? match.call(null, fileName)
: match.test(fileName)
/**
* Reads a folder contents
*
* @param {String} dir The dir path
*
* @return {string[]} Array of files paths
*/
const readDir = dir =>
pipe(
fs.readdirSync,
map(file => path.resolve(dir, file))
)(dir)
/**
* Determines if dir.
*
* @param {string} dirPath The dir path
*
* @return {boolean} True if dir, False otherwise.
*/
const isDir = dirPath => fs.statSync(dirPath).isDirectory()
/**
* Recursive dir walk with regular expression matching on file name
*
* @param {Object} arg1 Function props
* @param {Function|RegExp} arg1.test Test function or RegExp to match
* file name agains
* @return {string[]} Array of files paths
*
* @example
* find({test: /*.\.plugin\.js/})("./root-folder")
*/
const matchInFolder = test =>
pipe(
readDir,
reduce(
(acc = [], filePath) =>
isDir(filePath)
? [...acc, ...matchInFolder(test)(filePath)]
: isFileNameValid(test)(path.basename(filePath))
? push(filePath)(acc)
: acc
)
)
module.exports = (test = /.*/) =>
pipe(
map(matchInFolder(test)),
flatten
)