Skip to content

Instantly share code, notes, and snippets.

@cooljl31
Created October 27, 2022 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cooljl31/2a50421a4ddaaefc1735dbe0bb5ac2dc to your computer and use it in GitHub Desktop.
Save cooljl31/2a50421a4ddaaefc1735dbe0bb5ac2dc to your computer and use it in GitHub Desktop.
import glob from 'fast-glob'
import nodeURL from 'url'
import process from 'process'
/** Returns pathnames matching the given pattern. */
const sync = (source: string) => glob.sync(source, options()) as string[]
/** Returns pathnames matching the given pattern. */
const async = (source: string) => glob(source, options()) as Promise<string[]>
/** Metadata context-specific to the current process. */
const meta = {
/** URL href of the current working directory. */
get cwd() {
return nodeURL.pathToFileURL(process.cwd()).href as string
},
/** URL href of the currently running file. */
get url() {
return new URL(
meta.cwd +
Error()
.stack.match(/ eval \((.+?):/)[1]
.replace(nodeURL.pathToFileURL(process.cwd()).pathname, '')
).href
},
}
/** Internal options used for globbing. */
const options = () => ({
cwd: nodeURL.fileURLToPath(new URL('.', meta.url)),
ignore: ['**/node_modules/**'],
})
export { sync as default, meta, sync, async }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment