Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fs/tags): globs is an exposed function
BREAKING CHANGE: Glob previously returned a promise; it now returns a promise returning function
- Loading branch information
Showing
1 changed file
with
24 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,35 @@ | ||
import { error } from '~/utils/errors'; | ||
import asTag from '~/utils/as-tag'; | ||
import expose, { TExposedOverload } from '~/utils/expose'; | ||
import _glob from 'glob'; | ||
import { error } from '~/utils/errors'; | ||
|
||
export default glob; | ||
export default expose(glob) as TExposedOverload< | ||
typeof glob, | ||
[string] | [TemplateStringsArray, ...any[]] | ||
>; | ||
|
||
function glob(pattern: string): Promise<string[]>; | ||
function glob(pattern: string): () => Promise<string[]>; | ||
function glob( | ||
literals: TemplateStringsArray, | ||
...placeholders: any[] | ||
): Promise<string[]>; | ||
): () => Promise<string[]>; | ||
|
||
/** | ||
* String tag; returns a promise resolving with all paths matching a glob | ||
* @returns Promise<string[]> | ||
* It is an *exposed* function: use `glob.fn` as tag instead in order to execute on call. | ||
* @returns A function -hence, calling `glob` won't have any effect until the returned function is called. | ||
*/ | ||
async function glob(...args: any[]): Promise<string[]> { | ||
try { | ||
const pattern = asTag(args.shift(), ...args); | ||
return await new Promise((resolve: (arg: string[]) => void, reject) => | ||
_glob(pattern, { cwd: process.cwd() }, (err, matches) => | ||
err ? reject(err) : resolve(matches) | ||
) | ||
); | ||
} catch (err) { | ||
throw error(err); | ||
} | ||
function glob(...args: any[]): () => Promise<string[]> { | ||
return async () => { | ||
try { | ||
const pattern = asTag(args.shift(), ...args); | ||
return await new Promise((resolve: (arg: string[]) => void, reject) => | ||
_glob(pattern, { cwd: process.cwd() }, (err, matches) => | ||
err ? reject(err) : resolve(matches) | ||
) | ||
); | ||
} catch (err) { | ||
throw error(err); | ||
} | ||
}; | ||
} |