Skip to content

Commit

Permalink
♻️ Update gatsby-transformer-excel to use createContentDigest helper
Browse files Browse the repository at this point in the history
  • Loading branch information
frinyvonnick committed Apr 1, 2019
1 parent 747ab8f commit 19615ba
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 33 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby-transformer-excel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"license": "MIT",
"main": "index.js",
"peerDependencies": {
"gatsby": "^2.0.0"
"gatsby": "^2.0.15"
},
"repository": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-transformer-excel",
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Array [
"funny": "yup",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "73901821b17d5aa9dd6026181f73b64c",
"contentDigest": "contentDigest",
"type": "TestCsv__Sheet1",
},
"parent": "whatever",
Expand All @@ -22,7 +22,7 @@ Array [
"funny": "nope",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "f624311d932d73dcd416d2a8bea2b67d",
"contentDigest": "contentDigest",
"type": "TestCsv__Sheet1",
},
"parent": "whatever",
Expand All @@ -33,7 +33,7 @@ Array [
"children": Array [],
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "10c867561d207bc7dae0455d3cccad5b",
"contentDigest": "contentDigest",
"type": "TestCsv",
},
"parent": "whatever",
Expand All @@ -52,7 +52,7 @@ Array [
"funny": "yup",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "73901821b17d5aa9dd6026181f73b64c",
"contentDigest": "contentDigest",
"type": "TestCsv__Sheet1",
},
"parent": "whatever",
Expand Down Expand Up @@ -82,7 +82,7 @@ FALSE,nope
"funny": "nope",
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "f624311d932d73dcd416d2a8bea2b67d",
"contentDigest": "contentDigest",
"type": "TestCsv__Sheet1",
},
"parent": "whatever",
Expand Down Expand Up @@ -110,7 +110,7 @@ FALSE,nope
"children": Array [],
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "10c867561d207bc7dae0455d3cccad5b",
"contentDigest": "contentDigest",
"type": "TestCsv",
},
"parent": "whatever",
Expand Down Expand Up @@ -142,7 +142,7 @@ Array [
"children": Array [],
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "c685d551c99950924e4b7a78a267a993",
"contentDigest": "contentDigest",
"type": "TestCsv__Sheet1",
},
"parent": "whatever",
Expand All @@ -155,7 +155,7 @@ Array [
"children": Array [],
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "f197dfd371325c9aa0693a4c406f6cff",
"contentDigest": "contentDigest",
"type": "TestCsv__Sheet1",
},
"parent": "whatever",
Expand All @@ -168,7 +168,7 @@ Array [
"children": Array [],
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "10c867561d207bc7dae0455d3cccad5b",
"contentDigest": "contentDigest",
"type": "TestCsv",
},
"parent": "whatever",
Expand All @@ -185,7 +185,7 @@ Array [
"children": Array [],
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "c685d551c99950924e4b7a78a267a993",
"contentDigest": "contentDigest",
"type": "TestCsv__Sheet1",
},
"parent": "whatever",
Expand Down Expand Up @@ -215,7 +215,7 @@ FALSE,nada
"children": Array [],
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "f197dfd371325c9aa0693a4c406f6cff",
"contentDigest": "contentDigest",
"type": "TestCsv__Sheet1",
},
"parent": "whatever",
Expand Down Expand Up @@ -245,7 +245,7 @@ FALSE,nada
"children": Array [],
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "10c867561d207bc7dae0455d3cccad5b",
"contentDigest": "contentDigest",
"type": "TestCsv",
},
"parent": "whatever",
Expand Down Expand Up @@ -277,7 +277,7 @@ Array [
"children": Array [],
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "c685d551c99950924e4b7a78a267a993",
"contentDigest": "contentDigest",
"type": "TestCsv__Sheet1",
},
"parent": "whatever",
Expand All @@ -290,7 +290,7 @@ Array [
"children": Array [],
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "f197dfd371325c9aa0693a4c406f6cff",
"contentDigest": "contentDigest",
"type": "TestCsv__Sheet1",
},
"parent": "whatever",
Expand All @@ -303,7 +303,7 @@ Array [
"children": Array [],
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "10c867561d207bc7dae0455d3cccad5b",
"contentDigest": "contentDigest",
"type": "TestCsv",
},
"parent": "whatever",
Expand All @@ -320,7 +320,7 @@ Array [
"children": Array [],
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "c685d551c99950924e4b7a78a267a993",
"contentDigest": "contentDigest",
"type": "TestCsv__Sheet1",
},
"parent": "whatever",
Expand Down Expand Up @@ -350,7 +350,7 @@ FALSE,nada
"children": Array [],
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "f197dfd371325c9aa0693a4c406f6cff",
"contentDigest": "contentDigest",
"type": "TestCsv__Sheet1",
},
"parent": "whatever",
Expand Down Expand Up @@ -380,7 +380,7 @@ FALSE,nada
"children": Array [],
"id": "uuid-from-gatsby",
"internal": Object {
"contentDigest": "10c867561d207bc7dae0455d3cccad5b",
"contentDigest": "contentDigest",
"type": "TestCsv",
},
"parent": "whatever",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@ describe(`Process nodes correctly`, () => {
const actions = { createNode, createParentChildLink }
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)
const createDigestContent = jest.fn().mockReturnValue(`contentDigest`)

await onCreateNode({
node,
loadNodeContent,
actions,
createNodeId,
createDigestContent,
}).then(() => {
expect(createNode.mock.calls).toMatchSnapshot()
expect(createParentChildLink.mock.calls).toMatchSnapshot()
expect(createNode).toHaveBeenCalledTimes(2 + 1)
expect(createParentChildLink).toHaveBeenCalledTimes(2 + 1)
expect(createDigestContent).toHaveBeenCalledTimes(2 + 1)
})
})

Expand All @@ -53,20 +56,23 @@ describe(`Process nodes correctly`, () => {
const actions = { createNode, createParentChildLink }
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)
const createDigestContent = jest.fn().mockReturnValue(`contentDigest`)

await onCreateNode(
{
node,
loadNodeContent,
actions,
createNodeId,
createDigestContent,
},
{ raw: false }
).then(() => {
expect(createNode.mock.calls).toMatchSnapshot()
expect(createParentChildLink.mock.calls).toMatchSnapshot()
expect(createNode).toHaveBeenCalledTimes(2 + 1)
expect(createParentChildLink).toHaveBeenCalledTimes(2 + 1)
expect(createDigestContent).toHaveBeenCalledTimes(2 + 1)
})
})

Expand All @@ -80,20 +86,23 @@ describe(`Process nodes correctly`, () => {
const actions = { createNode, createParentChildLink }
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)
const createDigestContent = jest.fn().mockReturnValue(`contentDigest`)

await onCreateNode(
{
node,
loadNodeContent,
actions,
createNodeId,
createDigestContent,
},
{ rawOutput: false }
).then(() => {
expect(createNode.mock.calls).toMatchSnapshot()
expect(createParentChildLink.mock.calls).toMatchSnapshot()
expect(createNode).toHaveBeenCalledTimes(2 + 1)
expect(createParentChildLink).toHaveBeenCalledTimes(2 + 1)
expect(createDigestContent).toHaveBeenCalledTimes(2 + 1)
})
})
})
17 changes: 3 additions & 14 deletions packages/gatsby-transformer-excel/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const XLSX = require(`xlsx`)
const fs = require(`fs-extra`)
const _ = require(`lodash`)
const crypto = require(`crypto`)

// read files as `binary` from file system
function _loadNodeContent(fileNode, fallback) {
Expand All @@ -11,7 +10,7 @@ function _loadNodeContent(fileNode, fallback) {
}

async function onCreateNode(
{ node, actions, loadNodeContent, createNodeId },
{ node, actions, loadNodeContent, createNodeId, createDigestContent },
options = {}
) {
const { createNode, createParentChildLink } = actions
Expand Down Expand Up @@ -45,12 +44,7 @@ async function onCreateNode(

if (_.isArray(parsedContent)) {
const csvArray = parsedContent.map((obj, i) => {
const objStr = JSON.stringify(obj)
const contentDigest = crypto
.createHash(`md5`)
.update(objStr)
.digest(`hex`)

const contentDigest = createDigestContent(obj)
return {
...obj,
id: obj.id
Expand All @@ -74,12 +68,7 @@ async function onCreateNode(
})

const shObj = { name: n, idx: idx }
const shStr = JSON.stringify(shObj)
const contentDigest = crypto
.createHash(`md5`)
.update(shStr)
.digest(`hex`)

const contentDigest = createDigestContent(shObj)
const z = {
id: createNodeId(`${node.id} [${idx}] >>> ${node.extension}`),
children: [],
Expand Down

0 comments on commit 19615ba

Please sign in to comment.