Skip to content

Instantly share code, notes, and snippets.

@jonathantneal
Created November 2, 2021 13:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jonathantneal/ed53cd044e1ec2227db7c2a672a2cc43 to your computer and use it in GitHub Desktop.
Save jonathantneal/ed53cd044e1ec2227db7c2a672a2cc43 to your computer and use it in GitHub Desktop.
Return a list of files, like import.meta.glob in Vite, without calling the files.
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