forked from kmalakoff/esm-require-directory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
38 lines (34 loc) · 1.23 KB
/
index.mjs
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
// eslint-disable-next-line import/extensions
import walk from './lib/walk.js';
import importFile from './lib/importFile.mjs';
var EXTENSIONS = ['.mjs', '.js'];
export default function importDirectory(directory, options, callback) {
/* eslint-disable */
if (arguments.length === 2 && typeof options === 'function') {
callback = options;
options = {};
}
// choose between promise and callback API
if (typeof callback === 'function') {
options = options || {};
options = {
recursive: options.recursive,
paths: options.paths,
filename: options.filename,
default: options.default === undefined ? true : options.default,
extensions: options.extensions || EXTENSIONS,
loader: options.loader || importFile,
};
options.extensions.map(function (extension) {
if (!~EXTENSIONS.indexOf(extension)) throw new Error('Extension not supported: ' + extension);
});
if (options.paths && options.filename === undefined) options.filename = true;
walk(directory, options, callback);
} else {
return new Promise(function (resolve, reject) {
importDirectory(directory, options, function (err, results) {
err ? reject(err) : resolve(results);
});
});
}
}