gatsby-source-drupal
Advanced tools
Comparing version 1.0.0-alpha13-alpha.0189ea42 to 1.0.0-alpha13-alpha.0f0222b8
{ | ||
"name": "gatsby-source-drupal", | ||
"version": "1.0.0-alpha13-alpha.0189ea42", | ||
"version": "1.0.0-alpha13-alpha.0f0222b8", | ||
"description": "Gatsby source plugin for building websites using the Drupal CMS as a data source", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -1,13 +0,32 @@ | ||
const axios = require("axios") | ||
const Promise = require("bluebird") | ||
const crypto = require("crypto") | ||
const url = require("url") | ||
const _ = require("lodash") | ||
const axios = require(`axios`) | ||
const crypto = require(`crypto`) | ||
const _ = require(`lodash`) | ||
exports.sourceNodes = async ({ | ||
boundActionCreators, | ||
getNode, | ||
hasNodeChanged, | ||
store, | ||
}) => { | ||
const makeTypeName = type => { | ||
return `drupal__${type.replace(/-/g, `_`)}` | ||
} | ||
const processEntities = ents => { | ||
return ents.map(ent => { | ||
const newEnt = { | ||
...ent.attributes, | ||
id: ent.id, | ||
type: ent.type, | ||
created: new Date(ent.attributes.created * 1000).toJSON(), | ||
changed: new Date(ent.attributes.changed * 1000).toJSON(), | ||
} | ||
if (newEnt.revision_timestamp) { | ||
newEnt.revision_timestamp = new Date( | ||
newEnt.revision_timestamp * 1000 | ||
).toJSON() | ||
} | ||
return newEnt | ||
}) | ||
} | ||
exports.sourceNodes = async ( | ||
{ boundActionCreators, getNode, hasNodeChanged, store }, | ||
{ baseUrl } | ||
) => { | ||
const { | ||
@@ -18,7 +37,6 @@ createNode, | ||
} = boundActionCreators | ||
console.log("inside gatsby-source-drupal") | ||
updateSourcePluginStatus({ | ||
plugin: `gatsby-source-drupal`, | ||
status: { | ||
...store.getState().status["gatsby-source-drupal"], | ||
...store.getState().status[`gatsby-source-drupal`], | ||
ready: false, | ||
@@ -29,8 +47,8 @@ }, | ||
// Touch existing Drupal nodes so Gatsby doesn't garbage collect them. | ||
console.log( | ||
"existing drupal nodes", | ||
_.values(store.getState().nodes) | ||
.filter(n => n.type.slice(0, 8) === `drupal__`) | ||
.map(n => n.id) | ||
) | ||
// console.log( | ||
// "existing drupal nodes", | ||
// _.values(store.getState().nodes) | ||
// .filter(n => n.type.slice(0, 8) === `drupal__`) | ||
// .map(n => n.id) | ||
// ) | ||
@@ -41,7 +59,7 @@ _.values(store.getState().nodes) | ||
// Do the initial fetch | ||
console.time("fetch Drupal data") | ||
console.log("Starting to fetch data from Drupal") | ||
// Fetch articles. | ||
console.time(`fetch Drupal data`) | ||
console.log(`Starting to fetch data from Drupal`) | ||
const lastFetched = store.getState().status["gatsby-source-drupal"] | ||
const lastFetched = store.getState().status[`gatsby-source-drupal`] | ||
.lastFetched | ||
@@ -51,10 +69,15 @@ | ||
if (lastFetched) { | ||
url = `http://dev-gatsbyjs-d8.pantheonsite.io/jsonapi/node/article?filter[new-content][path]=changed&filter[new-content][value]=${parseInt(new Date(lastFetched).getTime() / 1000).toFixed(0)}&filter[new-content][operator]=%3E&page[offset]=0&page[limit]=10` | ||
url = `${baseUrl}/jsonapi/node/article?filter[new-content][path]=changed&filter[new-content][value]=${parseInt(new Date(lastFetched).getTime() / 1000).toFixed(0)}&filter[new-content][operator]=%3E&page[offset]=0&page[limit]=10` | ||
} else { | ||
url = `http://dev-gatsbyjs-d8.pantheonsite.io/jsonapi/node/article` | ||
url = `${baseUrl}/jsonapi/node/article` | ||
} | ||
const result = await axios.get(url) | ||
let result | ||
try { | ||
result = await axios.get(url) | ||
} catch (e) { | ||
console.log(`error fetching articles`, e) | ||
} | ||
console.log("articles fetched", result.data.data.length) | ||
console.log(`articles fetched`, result.data.data.length) | ||
@@ -64,3 +87,3 @@ updateSourcePluginStatus({ | ||
status: { | ||
...store.getState().status["gatsby-source-drupal"], | ||
...store.getState().status[`gatsby-source-drupal`], | ||
lastFetched: new Date().toJSON(), | ||
@@ -70,27 +93,15 @@ }, | ||
console.timeEnd("fetch Drupal data") | ||
console.timeEnd(`fetch Drupal data`) | ||
result.data.data.forEach((node, i) => { | ||
// We don't need this information locally. | ||
delete node.relationships | ||
// console.log(node) | ||
const newNode = { | ||
id: node.id, | ||
type: node.type, | ||
...node.attributes, | ||
} | ||
const nodes = processEntities(result.data.data) | ||
nodes.forEach((node, i) => { | ||
const nodeStr = JSON.stringify(node) | ||
newNode.created = new Date(newNode.created * 1000).toJSON() | ||
newNode.changed = new Date(newNode.changed * 1000).toJSON() | ||
newNode.revision_timestamp = new Date( | ||
newNode.revision_timestamp * 1000 | ||
).toJSON() | ||
const nodeStr = JSON.stringify(newNode) | ||
const gatsbyNode = { | ||
...newNode, | ||
...node, | ||
parent: `__SOURCE__`, | ||
type: `drupal__${node.type.replace(/-/g, "_")}`, | ||
type: makeTypeName(node.type), | ||
children: [], | ||
content: nodeStr, | ||
author___NODE: result.data.data[i].relationships.uid.data.id, | ||
mediaType: `application/json`, | ||
@@ -101,5 +112,4 @@ } | ||
const contentDigest = crypto | ||
.createHash("md5") | ||
.update(JSON.stringify(gatsbyNode)) | ||
.digest("hex") | ||
.createHash(`md5`) | ||
.update(JSON.stringify(gatsbyNode)).digest(`hex`) | ||
@@ -111,6 +121,52 @@ gatsbyNode.contentDigest = contentDigest | ||
// Fetch users. | ||
const userUrl = `http://dev-gatsbyjs-d8.pantheonsite.io/jsonapi/user/user` | ||
const userResult = await axios.get(userUrl) | ||
const users = processEntities(userResult.data.data) | ||
const blue = await Promise.all( | ||
users.map((user, i) => { | ||
return new Promise(resolve => { | ||
const userStr = JSON.stringify(user) | ||
const gatsbyUser = { | ||
...user, | ||
parent: `__SOURCE__`, | ||
type: makeTypeName(user.type), | ||
children: [], | ||
content: userStr, | ||
mediaType: `application/json`, | ||
} | ||
if (gatsbyUser.uid === 1) { | ||
return resolve() | ||
} | ||
axios | ||
.get( | ||
userResult.data.data[i].relationships.user_picture.links.related, | ||
{ timeout: 3000 } | ||
) | ||
.catch(() => console.log(`fail fetch`, gatsbyUser)) | ||
.then(pictureResult => { | ||
gatsbyUser.picture = `http://dev-gatsbyjs-d8.pantheonsite.io${pictureResult.data.data.attributes.url}` | ||
// Get content digest of node. | ||
const contentDigest = crypto | ||
.createHash(`md5`) | ||
.update(JSON.stringify(gatsbyUser)).digest(`hex`) | ||
gatsbyUser.contentDigest = contentDigest | ||
createNode(gatsbyUser) | ||
resolve() | ||
}) | ||
}) | ||
}) | ||
) | ||
updateSourcePluginStatus({ | ||
plugin: `gatsby-source-drupal`, | ||
status: { | ||
...store.getState().status["gatsby-source-drupal"], | ||
...store.getState().status[`gatsby-source-drupal`], | ||
ready: true, | ||
@@ -117,0 +173,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
7039
6
142
0
56
4