-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
helpers.ts
138 lines (112 loc) · 3.56 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { NodePluginArgs, SourceNodesArgs } from "gatsby"
import { createRemoteFileNode } from "gatsby-source-filesystem"
import { shopifyTypes } from "./shopify-types"
const pattern = /^gid:\/\/shopify\/(\w+)\/(.+)$/
export function createNodeId(
shopifyId: string,
gatsbyApi: NodePluginArgs,
{ typePrefix }: IShopifyPluginOptions
): string {
return gatsbyApi.createNodeId(`${typePrefix}${shopifyId}`)
}
export function getPluginStatus(
gatsbyApi: NodePluginArgs
): Record<string, never> {
return (
gatsbyApi.store.getState().status.plugins?.[`gatsby-source-shopify`] || {}
)
}
export function getLastBuildTime(
gatsbyApi: NodePluginArgs,
pluginOptions: IShopifyPluginOptions
): Date | undefined {
const { typePrefix } = pluginOptions
const status = getPluginStatus(gatsbyApi)
return (
status[`lastBuildTime${typePrefix}`] &&
new Date(status[`lastBuildTime${typePrefix}`])
)
}
export function setLastBuildTime(
gatsbyApi: NodePluginArgs,
pluginOptions: IShopifyPluginOptions,
currentBuildTime: number
): void {
const { typePrefix } = pluginOptions
const status = getPluginStatus(gatsbyApi)
gatsbyApi.actions.setPluginStatus({
...status,
[`lastBuildTime${typePrefix}`]: currentBuildTime,
})
}
export function isPriorityBuild(pluginOptions: IShopifyPluginOptions): boolean {
const { CI, GATSBY_CLOUD, GATSBY_IS_PR_BUILD, NETLIFY, CONTEXT } = process.env
const isGatsbyCloudPriorityBuild =
CI === `true` && GATSBY_CLOUD === `true` && GATSBY_IS_PR_BUILD !== `true`
const isNetlifyPriorityBuild =
CI === `true` && NETLIFY === `true` && CONTEXT === `production`
return pluginOptions.prioritize !== undefined
? pluginOptions.prioritize
: isGatsbyCloudPriorityBuild || isNetlifyPriorityBuild
}
export function isShopifyId(shopifyId: string): boolean {
return pattern.test(shopifyId)
}
export function parseShopifyId(shopifyId: string): Array<string> {
return shopifyId.match(pattern) || []
}
export function decorateBulkObject(input: unknown): unknown {
if (input && typeof input === `object`) {
if (Array.isArray(input)) {
return input.map(decorateBulkObject)
}
const obj: Record<string, unknown> = { ...input }
for (const key of Object.keys(obj)) {
obj[key] = decorateBulkObject(obj[key])
}
// We must convert ID to ShopifyID so that it doesn't collide with Gatsby's internal ID
if (obj.id) {
obj.shopifyId = obj.id
delete obj.id
}
return obj
}
return input
}
export async function processShopifyImages(
{ actions: { createNode }, createNodeId, cache }: SourceNodesArgs,
node: IShopifyNode
): Promise<void> {
const type = parseShopifyId(node.shopifyId)[1]
const imageFields = shopifyTypes[type].imageFields
if (imageFields) {
for (const fieldPath of imageFields) {
const image = fieldPath
.split(`.`)
.reduce((acc, value) => acc[value] as never, node) as
| IShopifyImage
| undefined
if (image && parseImageExtension(image.originalSrc) !== `gif`) {
const fileNode = await createRemoteFileNode({
url: image.originalSrc,
cache,
createNode,
createNodeId,
parentNodeId: node.id,
})
image.localFile___NODE = fileNode.id
}
}
}
}
export function parseImageExtension(url: string): string {
const basename = url.split(`?`)[0]
const dot = basename.lastIndexOf(`.`)
if (dot !== -1) {
return basename.slice(dot + 1)
} else {
throw new Error(
`Could not parse file extension from Shopify image URL: ${url}`
)
}
}