Skip to content

Commit

Permalink
fix(gatsby-plugin-sharp): Allow brackets in paths (#18289)
Browse files Browse the repository at this point in the history
* fix(gatsby-plugin-sharp): Allow brackets in paths

* Add tests to assert brackets in paths are set correctly

* Fix linting issues

* Fix already queued job not being returned properly

* Reapply my initial fix for brackets in paths.

* Fix setJobToProcess test
  • Loading branch information
emilpalsson authored and GatsbyJS Bot committed Oct 14, 2019
1 parent db3f7b0 commit f9933b1
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`setJobToProcess allows brackets in paths 1`] = `
Object {
"1234/file%2Ejpg": Object {
"myoutputpath/1234/file[new]%2Ejpg": Object {
"deferred": Object {
"promise": Promise {},
"reject": [Function],
"resolve": [Function],
},
"job": Object {
"args": Object {},
"contentDigest": "8675309jenny",
"inputPath": "1234/file.jpg",
"outputPath": "myoutputpath/1234/file[new].jpg",
},
},
},
}
`;
20 changes: 20 additions & 0 deletions packages/gatsby-plugin-sharp/src/__tests__/scheduler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { setJobToProcess } = require(`../scheduler`)

describe(`setJobToProcess`, () => {
it(`allows brackets in paths`, () => {
let deferred = {}
deferred.promise = new Promise((resolve, reject) => {
deferred.resolve = resolve
deferred.reject = reject
})
const toProcess = {}
const job = {
args: {},
inputPath: `1234/file.jpg`,
contentDigest: `8675309jenny`,
outputPath: `myoutputpath/1234/file[new].jpg`,
}
setJobToProcess(toProcess, job, deferred)
expect(toProcess).toMatchSnapshot()
})
})
55 changes: 36 additions & 19 deletions packages/gatsby-plugin-sharp/src/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,66 @@ q.drain = () => {
totalJobs = 0
}

exports.scheduleJob = async (
job,
boundActionCreators,
pluginOptions,
reporter,
reportStatus = true
) => {
const inputFileKey = job.inputPath.replace(/\./g, `%2E`)
const outputFileKey = job.outputPath.replace(/\./g, `%2E`)
const jobPath = `${inputFileKey}.${outputFileKey}`
const getFileKey = filePath => filePath.replace(/\./g, `%2E`)

const setJobToProcess = (toProcess, job, deferred) => {
const inputFileKey = getFileKey(job.inputPath)
const outputFileKey = getFileKey(job.outputPath)
const jobPath = `["${inputFileKey}"].["${outputFileKey}"]`

// Check if the job has already been queued. If it has, there's nothing
// to do, return.
if (_.has(toProcess, jobPath)) {
return _.get(toProcess, `${jobPath}.deferred.promise`)
return { existingPromise: _.get(toProcess, `${jobPath}.deferred.promise`) }
}

// Check if the output file already exists so we don't redo work.
if (existsSync(job.outputPath)) {
return Promise.resolve(job)
return { existingPromise: Promise.resolve(job) }
}

let isQueued = false
if (toProcess[inputFileKey]) {
isQueued = true
}

_.set(toProcess, jobPath, {
job: job,
deferred,
})

return { isQueued }
}

const scheduleJob = async (
job,
boundActionCreators,
pluginOptions,
reporter,
reportStatus = true
) => {
// deferred naming comes from https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Deferred
let deferred = {}
deferred.promise = new Promise((resolve, reject) => {
deferred.resolve = resolve
deferred.reject = reject
})

const { existingPromise, isQueued } = setJobToProcess(
toProcess,
job,
deferred
)
if (existingPromise) {
return existingPromise
}

if (totalJobs === 0) {
bar = createProgress(`Generating image thumbnails`, reporter)
bar.start()
}

totalJobs += 1

_.set(toProcess, jobPath, {
job: job,
deferred,
})

if (!isQueued) {
// Create image job
const jobId = uuidv4()
Expand All @@ -80,7 +95,7 @@ exports.scheduleJob = async (
q.push(cb => {
runJobs(
jobId,
inputFileKey,
getFileKey(job.inputPath),
boundActionCreators,
pluginOptions,
reportStatus,
Expand Down Expand Up @@ -169,3 +184,5 @@ function runJobs(
})
}
}

export { scheduleJob, setJobToProcess }

0 comments on commit f9933b1

Please sign in to comment.