2 const path = require('path');
3 const pathType = require('path-type');
5 const getExtensions = extensions => extensions.length > 1 ? `{${extensions.join(',')}}` : extensions[0];
7 const getPath = (filepath, cwd) => {
8 const pth = filepath[0] === '!' ? filepath.slice(1) : filepath;
9 return path.isAbsolute(pth) ? pth : path.join(cwd, pth);
12 const addExtensions = (file, extensions) => {
13 if (path.extname(file)) {
17 return `**/${file}.${getExtensions(extensions)}`;
20 const getGlob = (dir, opts) => {
21 if (opts.files && !Array.isArray(opts.files)) {
22 throw new TypeError(`Expected \`files\` to be of type \`Array\` but received type \`${typeof opts.files}\``);
25 if (opts.extensions && !Array.isArray(opts.extensions)) {
26 throw new TypeError(`Expected \`extensions\` to be of type \`Array\` but received type \`${typeof opts.extensions}\``);
29 if (opts.files && opts.extensions) {
30 return opts.files.map(x => path.join(dir, addExtensions(x, opts.extensions)));
34 return opts.files.map(x => path.join(dir, `**/${x}`));
37 if (opts.extensions) {
38 return [path.join(dir, `**/*.${getExtensions(opts.extensions)}`)];
41 return [path.join(dir, '**')];
44 module.exports = (input, opts) => {
45 opts = Object.assign({cwd: process.cwd()}, opts);
47 if (typeof opts.cwd !== 'string') {
48 return Promise.reject(new TypeError(`Expected \`cwd\` to be of type \`string\` but received type \`${typeof opts.cwd}\``));
51 return Promise.all([].concat(input).map(x => pathType.dir(getPath(x, opts.cwd))
52 .then(isDir => isDir ? getGlob(x, opts) : x)))
53 .then(globs => [].concat.apply([], globs));
56 module.exports.sync = (input, opts) => {
57 opts = Object.assign({cwd: process.cwd()}, opts);
59 if (typeof opts.cwd !== 'string') {
60 throw new TypeError(`Expected \`cwd\` to be of type \`string\` but received type \`${typeof opts.cwd}\``);
63 const globs = [].concat(input).map(x => pathType.dirSync(getPath(x, opts.cwd)) ? getGlob(x, opts) : x);
64 return [].concat.apply([], globs);