2021-09-17 00:26:18 -04:00
|
|
|
/**
|
|
|
|
|
* @file Retrieve the first available glob library.
|
|
|
|
|
*
|
|
|
|
|
* Note that options vary between libraries.
|
|
|
|
|
*
|
|
|
|
|
* Candidates:
|
2022-02-15 09:16:03 -05:00
|
|
|
*
|
2021-09-17 00:26:18 -04:00
|
|
|
* - {@link https://npmjs.com/package/tiny-glob tiny-glob}
|
2022-02-15 09:16:03 -05:00
|
|
|
* - {@link https://npmjs.com/package/globby globby}
|
2021-09-17 00:26:18 -04:00
|
|
|
* - {@link https://npmjs.com/package/fast-glob fast-glob}
|
|
|
|
|
* - {@link https://npmjs.com/package/glob glob}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { promisify } from 'node:util';
|
|
|
|
|
|
2021-09-21 16:54:54 -04:00
|
|
|
/**
|
|
|
|
|
* @type {string[]} A list of packages to attempt import.
|
|
|
|
|
*/
|
2021-09-21 15:59:48 -04:00
|
|
|
const candidates = [
|
2021-09-17 00:26:18 -04:00
|
|
|
'tiny-glob',
|
|
|
|
|
'globby',
|
|
|
|
|
'fast-glob',
|
|
|
|
|
'glob',
|
|
|
|
|
];
|
|
|
|
|
|
2022-02-08 15:18:18 -05:00
|
|
|
let glob;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
glob = await importGlob();
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// do nothing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default glob;
|
2021-09-17 00:26:18 -04:00
|
|
|
|
2021-09-21 16:54:54 -04:00
|
|
|
/**
|
|
|
|
|
* Imports the first available glob function.
|
|
|
|
|
*
|
|
|
|
|
* @throws {TypeError} If no glob library was found.
|
|
|
|
|
* @return {function}
|
|
|
|
|
*/
|
|
|
|
|
async function importGlob() {
|
|
|
|
|
let glob, module;
|
2021-09-17 00:26:18 -04:00
|
|
|
|
2021-09-21 16:54:54 -04:00
|
|
|
for (let name of candidates) {
|
|
|
|
|
try {
|
|
|
|
|
module = await import(name);
|
2021-09-17 00:26:18 -04:00
|
|
|
|
2021-09-21 16:54:54 -04:00
|
|
|
if (typeof module.default !== 'function') {
|
|
|
|
|
throw new TypeError(`Expected ${name} to be a function`);
|
|
|
|
|
}
|
2021-09-21 15:59:48 -04:00
|
|
|
|
2021-09-21 16:54:54 -04:00
|
|
|
/**
|
|
|
|
|
* Wrap the function to ensure
|
|
|
|
|
* a common pattern.
|
|
|
|
|
*/
|
|
|
|
|
switch (name) {
|
|
|
|
|
case 'tiny-glob':
|
|
|
|
|
return createArrayableGlob(module.default, {
|
|
|
|
|
filesOnly: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
case 'glob':
|
|
|
|
|
return promisify(module.default);
|
2021-09-17 00:26:18 -04:00
|
|
|
|
2021-09-21 16:54:54 -04:00
|
|
|
default:
|
|
|
|
|
return module.default;
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// swallow this error; skip to the next candidate.
|
|
|
|
|
}
|
2021-09-17 00:26:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new TypeError(
|
2021-12-03 15:10:21 -05:00
|
|
|
`No glob library was found, expected one of: ${candidates.join(', ')}`
|
2021-09-17 00:26:18 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a wrapper function for the glob function
|
|
|
|
|
* to provide support for arrays of patterns.
|
|
|
|
|
*
|
|
|
|
|
* @param {function} glob - The glob function.
|
|
|
|
|
* @param {object} options - The glob options.
|
2021-09-21 15:59:48 -04:00
|
|
|
* @return {function}
|
2021-09-17 00:26:18 -04:00
|
|
|
*/
|
|
|
|
|
function createArrayableGlob(glob, options) {
|
|
|
|
|
return (patterns, options) => {
|
|
|
|
|
const globs = patterns.map((pattern) => glob(pattern, options));
|
|
|
|
|
|
|
|
|
|
return Promise.all(globs).then((files) => {
|
|
|
|
|
return [].concat.apply([], files);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|