保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 9bdb95c9e34cef640534e5e5a1e2225a80442000
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
65
66
67
68
69
70
71
72
73
74
75
'use strict';
const path = require('path');
const pathType = require('path-type');
 
const getExtensions = extensions => extensions.length > 1 ? `{${extensions.join(',')}}` : extensions[0];
 
const getPath = (filepath, cwd) => {
    const pth = filepath[0] === '!' ? filepath.slice(1) : filepath;
    return path.isAbsolute(pth) ? pth : path.join(cwd, pth);
};
 
const addExtensions = (file, extensions) => {
    if (path.extname(file)) {
        return `**/${file}`;
    }
 
    return `**/${file}.${getExtensions(extensions)}`;
};
 
const getGlob = (directory, options) => {
    if (options.files && !Array.isArray(options.files)) {
        throw new TypeError(`Expected \`files\` to be of type \`Array\` but received type \`${typeof options.files}\``);
    }
 
    if (options.extensions && !Array.isArray(options.extensions)) {
        throw new TypeError(`Expected \`extensions\` to be of type \`Array\` but received type \`${typeof options.extensions}\``);
    }
 
    if (options.files && options.extensions) {
        return options.files.map(x => path.posix.join(directory, addExtensions(x, options.extensions)));
    }
 
    if (options.files) {
        return options.files.map(x => path.posix.join(directory, `**/${x}`));
    }
 
    if (options.extensions) {
        return [path.posix.join(directory, `**/*.${getExtensions(options.extensions)}`)];
    }
 
    return [path.posix.join(directory, '**')];
};
 
module.exports = async (input, options) => {
    options = {
        cwd: process.cwd(),
        ...options
    };
 
    if (typeof options.cwd !== 'string') {
        throw new TypeError(`Expected \`cwd\` to be of type \`string\` but received type \`${typeof options.cwd}\``);
    }
 
    const globs = await Promise.all([].concat(input).map(async x => {
        const isDirectory = await pathType.isDirectory(getPath(x, options.cwd));
        return isDirectory ? getGlob(x, options) : x;
    }));
 
    return [].concat.apply([], globs); // eslint-disable-line prefer-spread
};
 
module.exports.sync = (input, options) => {
    options = {
        cwd: process.cwd(),
        ...options
    };
 
    if (typeof options.cwd !== 'string') {
        throw new TypeError(`Expected \`cwd\` to be of type \`string\` but received type \`${typeof options.cwd}\``);
    }
 
    const globs = [].concat(input).map(x => pathType.isDirectorySync(getPath(x, options.cwd)) ? getGlob(x, options) : x);
 
    return [].concat.apply([], globs); // eslint-disable-line prefer-spread
};