Passed
Pull Request — develop (#758)
by Kevin Van
08:17 queued 04:36
created

gatsby-node.ts   A

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 137
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 101
mnd 2
bc 2
fnc 0
dl 0
loc 137
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
rs 10
1
import * as dotenv from "dotenv"
2
dotenv.config({ path: __dirname + `/.env` })
3
4
import path from "path"
5
6
import type { GatsbyNode } from "gatsby"
7
import { gatsbyNodePageQueries } from "./src/Gatsby/PageQueries"
8
import createPaginatedPages from "gatsby-paginate"
9
10
export const createPages: GatsbyNode["createPages"] = async ({ graphql, actions }) => {
11
  const { createPage } = actions
12
13
  const result = await wrapper(
14
    graphql(`
15
      query {
16
        ${gatsbyNodePageQueries}
17
      }
18
    `)
19
  )
20
21
  const articlesTemplate = path.resolve(`./src/templates/Article.tsx`)
22
  const createArticlesPromise = result.data.articles.edges.map(({ node }) => {
23
    createPage({
24
      path: node.path.alias || ``,
25
      component: articlesTemplate,
26
      context: {
27
        slug: node.path.alias || ``,
28
      },
29
    })
30
  })
31
32
  const newsOverviewTemplate = path.resolve(`./src/templates/NewsOverview.tsx`)
33
  const createNewsOverviewPromise = createPaginatedPages({
34
    edges: result.data.articles.edges,
35
    createPage,
36
    pageTemplate: newsOverviewTemplate,
37
    pageLength: 18,
38
    pathPrefix: `news`,
39
  })
40
41
  const newsTagPageTemplate = path.resolve(`./src/templates/NewsTagPage.tsx`)
42
  const createNewsTagPagePromise = result.data.categories.edges.map(({ node }) => {
43
    createPage({
44
      path: node.path.alias,
45
      component: newsTagPageTemplate,
46
      context: {
47
        slug: node.path.alias,
48
      },
49
    })
50
  })
51
52
  const pageTemplate = path.resolve(`src/templates/Page.tsx`)
53
  const createPagePromise = result.data.pages.edges.map(({ node }) => {
54
    createPage({
55
      path: node.path.alias,
56
      component: pageTemplate,
57
      context: {
58
        slug: node.path.alias,
59
      },
60
    })
61
  })
62
63
  const teamTemplate = path.resolve(`src/templates/Team.tsx`)
64
  const createTeamPromise = result.data.teams.edges.map(({ node }) => {
65
    createPage({
66
      path: node.path.alias,
67
      component: teamTemplate,
68
      context: {
69
        slug: node.path.alias,
70
      },
71
    })
72
  })
73
74
  const playerTemplate = path.resolve(`src/templates/Player.tsx`)
75
  const playerShareTemplate = path.resolve(`src/templates/PlayerShare.tsx`)
76
  const createPlayerPromise = result.data.players.edges.map(({ node }) => {
77
    createPage({
78
      path: node.path.alias,
79
      component: playerTemplate,
80
      context: {
81
        slug: node.path.alias,
82
      },
83
    })
84
    createPage({
85
      path: `${node.path.alias}/share`,
86
      component: playerShareTemplate,
87
      context: {
88
        slug: node.path.alias,
89
      },
90
    })
91
  })
92
93
  const staffTemplate = path.resolve(`src/templates/Staff.tsx`)
94
  const createStaffPromise = result.data.staff.edges.map(({ node }) => {
95
    createPage({
96
      path: node.path.alias,
97
      component: staffTemplate,
98
      context: {
99
        slug: node.path.alias,
100
      },
101
    })
102
  })
103
104
  await Promise.all([
105
    createArticlesPromise,
106
    createNewsOverviewPromise,
107
    createNewsTagPagePromise,
108
    createPagePromise,
109
    createTeamPromise,
110
    createPlayerPromise,
111
    createStaffPromise,
112
  ])
113
}
114
115
// graphql function doesn't throw an error
116
// so we have to check to check for
117
// the result.errors to throw manually
118
const wrapper = (promise) =>
119
  promise.then((result) => {
120
    if (result.errors) {
121
      throw result.errors
122
    }
123
    return result
124
  })
125
126
export const onCreatePage: GatsbyNode["onCreatePage"] = async ({ page, actions }) => {
127
  const { createPage } = actions
128
129
  if (page.path.match(/^\/game\//)) {
130
    createPage({
131
      path: `/game/`,
132
      matchPath: `/game/:matchId`,
133
      component: path.resolve(`src/pages/game.tsx`),
134
    })
135
  }
136
}
137