-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.js
59 lines (49 loc) · 1.34 KB
/
read.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
const fs = require('fs');
const path = require('path');
/**
* hmdoc.Read
* written by Joel Dentici
* on 7/19/2017
*
* Reads the source of each JavaScript file in
* a directory (recursively).
*/
/**
* concat :: [[a]] -> [a]
*
* Flattens a nested list by one level.
*/
function concat(arr) {
return [].concat(...arr);
}
/**
* readFiles :: string -> [string]
*
* Reads all the files in a directory into a list,
* recursively reading each subdirectory that is found.
*/
function readFiles(dir, exts) {
//special case to make sure we can handle
//a single file rather than directory
if (fs.statSync(dir).isFile()) {
if (exts.has(path.extname(dir)))
return [fs.readFileSync(dir).toString()];
else {
console.error('Expected JS file');
process.exit(1);
}
}
const fileNames = fs.readdirSync(dir).map(x => path.join(dir, x));
const fileStats = fileNames.map(x => [x, fs.statSync(x)]);
const files = fileStats
.filter(([n,stats]) => stats.isFile())
.map(([n, stats]) => n)
.filter(n => !n.startsWith('.') && exts.has(path.extname(n)));
const dirs = fileStats
.filter(([n,stats]) => !stats.isFile())
.map(([n, stats]) => n);
const subDirFiles = concat(dirs.map(x => readFiles(x, exts)));
const dirFiles = files.map(x => fs.readFileSync(x).toString());
return dirFiles.concat(subDirFiles);
}
module.exports = readFiles;