-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-dir-into-json.esm.js
64 lines (53 loc) · 2.18 KB
/
js-dir-into-json.esm.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
/*!
* js-dir-into-json v2.3.0
* (c) 2020 Martin Rafael Gonzalez <tin@devtin.io>
* MIT
*/
import { deepListDir, deepListDirSync } from 'deep-list-dir';
import merge from 'deepmerge';
import path from 'path';
import set from 'lodash/set';
import camelCase from 'lodash/camelCase.js';
import trim from 'lodash/trim';
function dirPath2ObjPath (dirPath = '') {
return trim(dirPath, '/').replace(/((^|\/)index)?\.js(on)?$/i, '').split('/').map(camelCase).join('.')
}
const unwrapDefaults = (obj) => {
if (typeof obj !== 'object' || Array.isArray(obj) || obj === null) {
return obj
}
if (Object.keys(obj).length === 1 && obj.default) {
return obj.default
}
Object.keys(obj).forEach(prop => {
obj[prop] = unwrapDefaults(obj[prop]);
});
return obj
};
/**
* @param {String[]} fileList - List of js / json files
* @param {Object} [options]
* @param {Function|NodeRequire} [options.fileLoader=esm] - Function that resolves the files
* @param {Function} [options.path2dot=dirPath2ObjPath] - Function that receives the file path and resolves a dot notation path
* @return {Promise<{}>}
*/
function fileListIntoJson (fileList, { fileLoader = require, base = './', path2dot = dirPath2ObjPath } = {}) {
let finalObject = {};
fileList.forEach(jsFile => {
const dotProp = path2dot(path.relative(base, jsFile));
let fileContent = dotProp ? set({}, dotProp, fileLoader(jsFile)) : fileLoader(jsFile);
fileContent = unwrapDefaults(fileContent);
finalObject = merge(finalObject, fileContent);
});
return finalObject
}
const settings = {
fileLoader: require
};
async function jsDirIntoJson (directory, { extensions = ['*.js', '*.json'], fileLoader = settings.fileLoader, path2dot } = {}) {
return fileListIntoJson(await deepListDir(path.resolve(process.cwd(), directory), { pattern: extensions }), { fileLoader, base: directory, path2dot })
}
function jsDirIntoJsonSync (directory, { extensions = ['*.js', '*.json'], fileLoader = settings.fileLoader, path2dot } = {}) {
return fileListIntoJson(deepListDirSync(path.resolve(process.cwd(), directory), { pattern: extensions }), { fileLoader, base: directory, path2dot })
}
export { jsDirIntoJson, jsDirIntoJsonSync, settings };