Umama-at-Bluchip/Quick-UI
0
1'use strict';2const path = require('path');3const pathType = require('path-type');4 5const getExtensions = extensions => extensions.length > 1 ? `{${extensions.join(',')}}` : extensions[0];6 7const getPath = (filepath, cwd) => {8 const pth = filepath[0] === '!' ? filepath.slice(1) : filepath;9 return path.isAbsolute(pth) ? pth : path.join(cwd, pth);10};11 12const addExtensions = (file, extensions) => {13 if (path.extname(file)) {14 return `**/${file}`;15 }16 17 return `**/${file}.${getExtensions(extensions)}`;18};19 20const getGlob = (directory, options) => {21 if (options.files && !Array.isArray(options.files)) {22 throw new TypeError(`Expected \`files\` to be of type \`Array\` but received type \`${typeof options.files}\``);23 }24 25 if (options.extensions && !Array.isArray(options.extensions)) {26 throw new TypeError(`Expected \`extensions\` to be of type \`Array\` but received type \`${typeof options.extensions}\``);27 }28 29 if (options.files && options.extensions) {30 return options.files.map(x => path.posix.join(directory, addExtensions(x, options.extensions)));31 }32 33 if (options.files) {34 return options.files.map(x => path.posix.join(directory, `**/${x}`));35 }36 37 if (options.extensions) {38 return [path.posix.join(directory, `**/*.${getExtensions(options.extensions)}`)];39 }40 41 return [path.posix.join(directory, '**')];42};43 44module.exports = async (input, options) => {45 options = {46 cwd: process.cwd(),47 ...options48 };49 50 if (typeof options.cwd !== 'string') {51 throw new TypeError(`Expected \`cwd\` to be of type \`string\` but received type \`${typeof options.cwd}\``);52 }53 54 const globs = await Promise.all([].concat(input).map(async x => {55 const isDirectory = await pathType.isDirectory(getPath(x, options.cwd));56 return isDirectory ? getGlob(x, options) : x;57 }));58 59 return [].concat.apply([], globs); // eslint-disable-line prefer-spread60};61 62module.exports.sync = (input, options) => {63 options = {64 cwd: process.cwd(),65 ...options66 };67 68 if (typeof options.cwd !== 'string') {69 throw new TypeError(`Expected \`cwd\` to be of type \`string\` but received type \`${typeof options.cwd}\``);70 }71 72 const globs = [].concat(input).map(x => pathType.isDirectorySync(getPath(x, options.cwd)) ? getGlob(x, options) : x);73 74 return [].concat.apply([], globs); // eslint-disable-line prefer-spread75};76 