-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (52 loc) · 1.12 KB
/
index.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
'use strict'
const rfs = require('fs')
const Promise = require('bluebird')
const deepKeys = require('deep-keys')
const dir = require('node-dir')
const fs = Promise.promisifyAll(rfs)
function search(json, folder) {
let props = []
return fs.readFileAsync(json, 'utf8')
.then(jsonData => {
const json = JSON.parse(jsonData)
props = deepKeys(json)
})
.then(() => traverseFolders(folder))
.then(files => filterFiles(files, props))
.catch(err => {
console.log('error', err)
})
}
function traverseFolders(folder) {
return new Promise((resolve, reject) => {
dir.files(folder, (err, files) => {
if (err) {
reject(err)
}
return Promise.map(files, f => {
return fs.readFileAsync(f, 'utf8')
})
.then(resolve)
.catch(reject)
})
})
}
function filterFiles(files, k) {
let keys = k
files.forEach(f => {
keys = filterKeys(f, keys)
})
return keys
}
function filterKeys(file, keys) {
return keys.filter(k => {
if (file.indexOf(k) > -1) {
return false
}
return true
})
}
module.exports = {
search,
traverseFolders
}