Skip to content

Commit

Permalink
feat(fs/tags): globs is an exposed function
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Glob previously returned a promise; it now returns a promise returning function
  • Loading branch information
rafamel committed May 20, 2019
1 parent 1c5786b commit cf99127
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/public/tags/glob.ts
@@ -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);
}
};
}

0 comments on commit cf99127

Please sign in to comment.