Skip to content

Commit 3ca1a11

Browse files
committed
tell user about manual required changes and transform some content type selectors in code
1 parent 92ebddf commit 3ca1a11

File tree

1 file changed

+67
-20
lines changed

1 file changed

+67
-20
lines changed

packages/gatsby-codemods/src/transforms/gatsby-source-contentful.js

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,72 @@ export function babelRecast(code, filePath) {
3939
return code
4040
}
4141

42+
const CONTENT_TYPE_SELECTOR_REGEX = /^(allContentful|contentful)([A-Z0-9].+)/
43+
const CONTENT_TYPE_SELECTOR_BLACKLIST = [`Asset`, `Reference`, `Id`]
44+
const SYS_FIELDS_TRANSFORMS = new Map([
45+
[`node_locale`, `locale`],
46+
[`contentful_id`, `id`],
47+
[`spaceId`, `spaceId`],
48+
[`createdAt`, `firstPublishedAt`],
49+
[`updatedAt`, `publishedAt`],
50+
[`revision`, `publishedVersion`],
51+
[`type`, `contentType`],
52+
])
53+
54+
const isContentTypeSelector = selector => {
55+
if (!selector) {
56+
return false
57+
}
58+
const res = selector.match(CONTENT_TYPE_SELECTOR_REGEX)
59+
return res && !CONTENT_TYPE_SELECTOR_BLACKLIST.includes(res[2])
60+
}
61+
const updateContentfulSelector = selector =>
62+
selector.replace(`ontentful`, `ontentfulContentType`)
63+
64+
const renderFilename = (path, state) =>
65+
`${state.opts.filename} (Line ${path.node.loc.start.line})`
66+
4267
export function updateImport(babel) {
4368
const { types: t, template } = babel
4469
return {
4570
visitor: {
71+
Identifier(path, state) {
72+
if (path.node.name === `contentfulId`) {
73+
console.log(
74+
`${renderFilename(
75+
path,
76+
state
77+
)}: You should change "contentfulId" to "sys.id"`
78+
)
79+
}
80+
if (path.node.name === `type`) {
81+
console.log(
82+
`${renderFilename(
83+
path,
84+
state
85+
)}: You may change "type" to "sys.contentType.name"`
86+
)
87+
}
88+
},
89+
MemberExpression(path, state) {
90+
if (isContentTypeSelector(path.node.property?.name)) {
91+
if (
92+
path.node.object?.name === `data` ||
93+
path.node.object.property?.name === `data`
94+
) {
95+
path.node.property.name = updateContentfulSelector(
96+
path.node.property.name
97+
)
98+
state.opts.hasChanged = true
99+
} else {
100+
console.log(
101+
`${renderFilename(path, state)}: You might need to change "${
102+
path.node.property?.name
103+
}" to "${updateContentfulSelector(path.node.property.name)}"`
104+
)
105+
}
106+
}
107+
},
46108
TaggedTemplateExpression({ node }, state) {
47109
if (node.tag.name !== `graphql`) {
48110
return
@@ -82,17 +144,6 @@ export function updateImport(babel) {
82144
}
83145
}
84146

85-
const RENAME_BLACKLIST = [`Asset`, `Reference`]
86-
const SYS_FIELDS_TRANSFORMS = new Map([
87-
[`node_locale`, `locale`],
88-
[`contentful_id`, `id`],
89-
[`spaceId`, `spaceId`],
90-
[`createdAt`, `firstPublishedAt`],
91-
[`updatedAt`, `publishedAt`],
92-
[`revision`, `publishedVersion`],
93-
[`type`, `contentType`],
94-
])
95-
96147
function locateSubfield(node, fieldName) {
97148
return (
98149
node.selectionSet &&
@@ -108,17 +159,13 @@ function processGraphQLQuery(query, state) {
108159
graphql.visit(ast, {
109160
SelectionSet(node) {
110161
// Rename content type node selectors
111-
const contentfulSelector = node.selections.find(({ name }) => {
112-
const res = name?.value.match(
113-
/^(allContentful|contentful)([A-Z0-9].+)/
114-
)
115-
return res && !RENAME_BLACKLIST.includes(res[2])
116-
})
162+
const contentfulSelector = node.selections.find(({ name }) =>
163+
isContentTypeSelector(name?.value)
164+
)
117165

118166
if (contentfulSelector) {
119-
contentfulSelector.name.value = contentfulSelector.name.value.replace(
120-
`ontentful`,
121-
`ontentfulContentType`
167+
contentfulSelector.name.value = updateContentfulSelector(
168+
contentfulSelector.name.value
122169
)
123170
hasChanged = true
124171
return

0 commit comments

Comments
 (0)