-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
75 lines (69 loc) · 2 KB
/
utils.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
75
const _ = require('lodash')
const fs = require('fs')
const glob = require('glob')
const minimatch = require('minimatch')
const path = require('path')
/**
* Reads a JSON file, returns a default value if file does not exist.
*
* @param {string} path
* @param {object} defaultValue
*/
const read_json = (path, defaultValue = {}) => {
if (fs.existsSync(path) && fs.lstatSync(path).isFile()) {
const json = JSON.parse(fs.readFileSync(path, "utf-8"));
return json;
} else {
return defaultValue;
}
}
/**
* Reads a file by line into an array
*
* @param {string} path
* @param {object} defaultValue
*/
const read_list = (path, defaultValue = []) => {
if (fs.existsSync(path) && fs.lstatSync(path).isFile()) {
const content = fs.readFileSync(path, "utf-8");
return content.split(/\r?\n/);
} else {
return defaultValue;
}
}
/**
* Lists files of a directory.
*
* @param {string} directory The name of the directory to list
* @param {array[string]} ignorelist Items to ignore
* @param {string} filetype The type of the files, one of: "*" - all, "d" - directories, "f" - files
* @param {boolean} recursive If true, the directory is listed recursively
*/
const read_filelist = (directory, ignorelist = [], filetype = '*', recursive = true) => {
const pattern = recursive ? '**' : '*';
const files = glob.sync(pattern, { cwd: directory });
return _
.chain(files)
.filter(file => _.findIndex(ignorelist, entry => minimatch(file, entry)) < 0)
.filter(file => {
if (filetype === '*') {
return true;
} else {
const file_path = path.resolve(directory, file);
const stats = fs.statSync(file_path);
if (filetype === "d" && stats.isDirectory()) {
return true;
} else if (filetype === "f" && stats.isFile()) {
return true;
} else {
return false;
}
}
})
.value();
}
exports.default = {
read_json,
read_list,
read_filelist
}