Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / dir-glob / index.js
1 'use strict';
2 const path = require('path');
3 const pathType = require('path-type');
4
5 const getExtensions = extensions => extensions.length > 1 ? `{${extensions.join(',')}}` : extensions[0];
6
7 const getPath = (filepath, cwd) => {
8         const pth = filepath[0] === '!' ? filepath.slice(1) : filepath;
9         return path.isAbsolute(pth) ? pth : path.join(cwd, pth);
10 };
11
12 const addExtensions = (file, extensions) => {
13         if (path.extname(file)) {
14                 return `**/${file}`;
15         }
16
17         return `**/${file}.${getExtensions(extensions)}`;
18 };
19
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}\``);
23         }
24
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}\``);
27         }
28
29         if (opts.files && opts.extensions) {
30                 return opts.files.map(x => path.join(dir, addExtensions(x, opts.extensions)));
31         }
32
33         if (opts.files) {
34                 return opts.files.map(x => path.join(dir, `**/${x}`));
35         }
36
37         if (opts.extensions) {
38                 return [path.join(dir, `**/*.${getExtensions(opts.extensions)}`)];
39         }
40
41         return [path.join(dir, '**')];
42 };
43
44 module.exports = (input, opts) => {
45         opts = Object.assign({cwd: process.cwd()}, opts);
46
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}\``));
49         }
50
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));
54 };
55
56 module.exports.sync = (input, opts) => {
57         opts = Object.assign({cwd: process.cwd()}, opts);
58
59         if (typeof opts.cwd !== 'string') {
60                 throw new TypeError(`Expected \`cwd\` to be of type \`string\` but received type \`${typeof opts.cwd}\``);
61         }
62
63         const globs = [].concat(input).map(x => pathType.dirSync(getPath(x, opts.cwd)) ? getGlob(x, opts) : x);
64         return [].concat.apply([], globs);
65 };