Skip to content

Commit

Permalink
feat(gatsby): Upgrade to gatsby v2, cont.
Browse files Browse the repository at this point in the history
  • Loading branch information
jessestuart committed Apr 17, 2019
1 parent 6e3c828 commit 14f6362
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 55 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ module.exports = {
},
},
rules: {
semi: ['error', 'never'],
'@typescript-eslint/no-var-requires': ['off'],
'@typescript-eslint/indent': ['error', 2],
'@typescript-eslint/member-delimiter-style': [
'error',
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
yarn.lock
**/*.d.ts
**/*.map
report*
1 change: 0 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
"singleQuote": true,
"useTabs": false
}

33 changes: 21 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,46 @@
"type": "git",
"url": "git+https://github.com/jessestuart/gatsby-source-s3-image"
},
"peerDependencies": {
"gatsby": "^2.1.0",
"gatsby-image": "^2.0.30",
"gatsby-source-filesystem": "^2.0.20",
"react": "latest"
},
"dependencies": {
"@babel/runtime": "7.3.4",
"aws-sdk": "2.411.0",
"axios": "0.18.0",
"bluebird": "3.5.3",
"exif-parser": "0.1.12",
"gatsby": "2.1.4",
"gatsby-source-filesystem": "2.0.20",
"gatsby-image": "2.0.30",
"lodash": "4.17.11",
"luxon": "1.11.2"
"luxon": "1.11.3",
"react": "16.8.3"
},
"devDependencies": {
"@babel/cli": "7.2.3",
"@babel/core": "7.3.3",
"@babel/core": "7.3.4",
"@babel/preset-typescript": "7.3.3",
"@semantic-release/git": "7.0.8",
"@types/graphql": "14.0.7",
"@types/bluebird": "3.5.26",
"@types/lodash": "4.14.121",
"@types/node": "11.9.4",
"@typescript-eslint/eslint-plugin": "1.3.0",
"@typescript-eslint/parser": "1.3.0",
"@types/node": "11.9.5",
"@typescript-eslint/eslint-plugin": "1.4.2",
"@typescript-eslint/parser": "1.4.2",
"babel-jest": "24.1.0",
"babel-plugin-add-module-exports": "1.0.0",
"babel-plugin-lodash": "3.3.4",
"babel-preset-gatsby-package": "0.1.3",
"eslint": "5.14.0",
"eslint-config-prettier": "4.0.0",
"eslint": "5.14.1",
"eslint-config-prettier": "4.1.0",
"json-stringify-pretty-compact": "2.0.0",
"prettier": "1.16.4",
"semantic-release": "15.13.3",
"tslint": "5.12.1",
"tslint": "5.13.0",
"tslint-config-prettier": "1.18.0",
"tslint-config-standard": "8.0.1",
"typescript": "3.3.3"
"typescript": "3.3.3333"
},
"keywords": [
"exif",
Expand Down
7 changes: 3 additions & 4 deletions src/gatsby-node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export { sourceNodes } from './source-nodes'
import setFieldsOnGraphQLNodeType from './set-fields-on-graphql-node-type'

export {
extendNodeType as setFieldsOnGraphQLNodeType,
} from './extend-node-type'
export { sourceNodes } from './source-nodes'
export { setFieldsOnGraphQLNodeType }
33 changes: 13 additions & 20 deletions src/extend-node-type.ts → src/set-fields-on-graphql-node-type.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import Promise from 'bluebird'
import exif from 'exif-parser'
import fs from 'fs'
import {
import _ from 'lodash'
import { DateTime } from 'luxon'
import ExifDataType from './types/exif-data'
import S3ImageAssetNode from './types/s3-image-asset-node'

const {
GraphQLFloat,
GraphQLInt,
GraphQLObjectType,
GraphQLString,
} from 'gatsby/graphql'
import _ from 'lodash'
import { DateTime } from 'luxon'

import ExifData from './types/exif-data'
import S3ImageAssetNode from './types/s3-image-asset-node'
} = require('gatsby/graphql')

export const resolveExifData = (image: S3ImageAssetNode): ExifData => {
export const resolveExifData = (image: S3ImageAssetNode): ExifDataType => {
const file = fs.readFileSync(image.absolutePath)
const tags = exif.create(file).parse().tags
const timestamp = tags.DateTimeOriginal * 1000
Expand All @@ -39,17 +38,18 @@ export interface ExtendNodeTypeOptions {
}
}

export const extendNodeType = ({
type,
}: ExtendNodeTypeOptions): Promise<object> => {
export default ({ type }: ExtendNodeTypeOptions) => {
if (type.name !== 'S3ImageAsset') {
return {}
}

console.log('extend node type', { type })
return Promise.resolve({
ETag: { type: GraphQLString },
EXIF: {
resolve: (image: S3ImageAssetNode) => ({
...type,
...resolveExifData(image),
}),
type: new GraphQLObjectType({
fields: {
DateCreatedISO: { type: GraphQLString },
Expand All @@ -64,13 +64,6 @@ export const extendNodeType = ({
},
name: 'ExifData',
}),

resolve(image: S3ImageAssetNode) {
return {
...type,
...resolveExifData(image),
}
},
},
Key: { type: GraphQLString },
})
Expand Down
34 changes: 16 additions & 18 deletions src/source-nodes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import AWS from 'aws-sdk'
import { createRemoteFileNode } from 'gatsby-source-filesystem'
import _ from 'lodash'

// tslint:disable eslint-disable-next-line
const { createRemoteFileNode } = require('gatsby-source-filesystem')

// =================
// AWS config setup.
// =================
Expand Down Expand Up @@ -56,10 +58,8 @@ function isImage(entity): boolean {

export const sourceNodes = async (
{ actions, store, cache, createNodeId },
{ bucketName, domain, protocol = 'https' }: SourceS3Options,
done
): Promise<void> => {
console.log('source nodes')
{ bucketName, domain, protocol = 'https' }: SourceS3Options
): Promise<any> => {
const { createNode } = actions

const listObjectsResponse = await S3.makeUnauthenticatedRequest(
Expand All @@ -69,20 +69,20 @@ export const sourceNodes = async (
}
).promise()
const s3Entities = _.get(listObjectsResponse, 'Contents')
// tslint:disable
console.log({ s3Entities })

await Promise.all(
return await Promise.all(
s3Entities.map(async entity => {
console.log('proceessing s3 entity', { entity })
if (!isImage(entity)) {
return null
}

// @ts-ignore
const s3Url: string | null | undefined = constructS3UrlForAsset({
bucketName,
domain,
key: entity.Key,
// @ts-ignore
protocol,
})
if (!s3Url) {
Expand All @@ -106,15 +106,15 @@ export const sourceNodes = async (
if (!fileNode) {
return null
}

entityData.localFile___NODE = fileNode.id
return createS3ImageAssetNode({
...entityData,
done,

return await createS3ImageAssetNode({
..._.pick(entityData, ['createNode', 'entity', 's3Url']),
fileNode,
})
})
)
done()
}

const createS3RemoteFileNode = async ({
Expand All @@ -141,13 +141,11 @@ const createS3RemoteFileNode = async ({

const createS3ImageAssetNode = async ({
createNode,
done,
entity,
fileNode,
s3Url,
// @ts-ignore
// ...rest
}): Promise<void> => {
}): Promise<any> => {
console.log({ entity })
const { Key, ETag } = entity
// TODO: Could probably pull this from fileNode.
const ContentType = 'image/jpeg'
Expand All @@ -156,7 +154,8 @@ const createS3ImageAssetNode = async ({
// > to the contents of an object, not its metadata.
// @see https://docs.aws.amazon.com/AmazonS3/latest/API/RESTCommonResponseHeaders.html
const objectHash = ETag.replace(/"/g, '')
await createNode({
console.log({ objectHash })
return await createNode({
...entity,
id: `${Key} >> ${S3SourceGatsbyNodeType}`,
absolutePath: fileNode.absolutePath,
Expand All @@ -170,5 +169,4 @@ const createS3ImageAssetNode = async ({
type: S3SourceGatsbyNodeType,
},
})
done()
}
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"exclude": ["node_modules"]
},
"rules": {
"semicolon": [true, "never", "ignore-interfaces"],
"interface-name": false,
"no-use-before-declare": false,
"trailing-comma": [true, "es5"]
Expand Down

0 comments on commit 14f6362

Please sign in to comment.