Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added parameters for custom glob patterns and glob options... #375

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,18 @@ Assume this structure with the compressed asset as a sibling of the un-compresse
└── index.html
```

#### `globPattern`

Default: `'**/**'`

Glob pattern used to match files. Please read full description here: [Glob Primer](https://github.com/isaacs/node-glob#glob-primer)

#### `globOptions`

Default: `{ nodir: true, dot: opts.serveDotFiles }`

Options passed to glob module. All available options are described here: [Glob Options](https://github.com/isaacs/node-glob#options)

#### Disable serving

If you would just like to use the reply decorator and not serve whole directories automatically, you can simply pass the option `{ serve: false }`. This will prevent the plugin from serving everything under `root`.
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,15 @@ async function fastifyStatic (fastify, opts) {
})
}
} else {
const globPattern = '**/**'
const globPattern = opts.globPattern || '**/**'
const globOptions = opts.globOptions || { nodir: true, dot: opts.serveDotFiles }
const indexDirs = new Map()
const routes = new Set()

const winSeparatorRegex = new RegExp(`\\${path.win32.sep}`, 'g')

for (const rootPath of Array.isArray(sendOptions.root) ? sendOptions.root : [sendOptions.root]) {
const files = await globPromise(path.join(rootPath, globPattern).replace(winSeparatorRegex, path.posix.sep), { nodir: true, dot: opts.serveDotFiles })
const files = await globPromise(path.join(rootPath, globPattern).replace(winSeparatorRegex, path.posix.sep), globOptions)
const indexes = typeof opts.index === 'undefined' ? ['index.html'] : [].concat(opts.index)

for (let file of files) {
Expand Down