Skip to content

Commit

Permalink
Create findFiles wrapper around chokidar and readdirp
Browse files Browse the repository at this point in the history
  • Loading branch information
m-allanson committed Dec 14, 2018
1 parent 9b710e7 commit 0e41cba
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
const fileFinder = require(`../file-finder`)
const findFiles = require(`../find-files`)
const path = require(`path`)

describe(`file-finder`, () => {
describe(`findOnce`, () => {
it(`resolves an array of file paths`, async done => {
expect.assertions(3)
const queue = await fileFinder(`${__dirname}/fixtures/test-fs`)
const queue = await findFiles(`${__dirname}/fixtures/test-fs`, {
watch: false,
ignored: [
`.toBeIgnored.md`,
`**/.DS_Store`,
`**/.placeholder.md`,
`**/notInTheFixture.bar`,
],
})
const expectedQueue = [
path.join(__dirname, `fixtures`, `test-fs`, `index.md`),
path.join(__dirname, `fixtures`, `test-fs`, `dirA`, `index.md`),
Expand Down
34 changes: 0 additions & 34 deletions packages/gatsby-source-filesystem/src/file-finder.js

This file was deleted.

5 changes: 3 additions & 2 deletions packages/gatsby-source-filesystem/src/file-watcher.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require(`path`)
const chokidar = require(`chokidar`)
const { Machine } = require(`xstate`)
const findFiles = require(`./find-files`)
const { createFileNode } = require(`./create-file-node`)

/**
Expand Down Expand Up @@ -60,7 +60,8 @@ function fileWatcher(
)
})

const watcher = chokidar.watch(pluginOptions.path, {
const watcher = findFiles(pluginOptions.path, {
watch: process.env.GATSBY_EXECUTING_COMMAND === `develop`,
ignored: [
`**/*.un~`,
`**/.DS_Store`,
Expand Down
31 changes: 31 additions & 0 deletions packages/gatsby-source-filesystem/src/find-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const chokidar = require(`chokidar`)
const readdirp = require(`readdirp`)
const anymatch = require(`anymatch`)

const findOnce = (path, options) => {
const { ignored } = options
return new Promise(resolve => {
let fileList = []
const stream = readdirp({ root: path })

stream.on(`data`, data => {
if (anymatch(ignored, data.path)) return
fileList.push(data.fullPath)
return
})

stream.on(`end`, () => {
resolve(fileList)
})
})
}

module.exports = (path, options) => {
const { watch, ignored } = options

if (watch === true) {
return chokidar.watch(path, { ignored })
} else {
return findOnce(path, { ignored })
}
}

0 comments on commit 0e41cba

Please sign in to comment.