Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

create-graphql-files.js 4.1 KB

You have to be logged in to leave a comment. Sign In
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
  1. #!/usr/bin/env node
  2. const fs = require('fs')
  3. const path = require('path')
  4. const program = require('commander')
  5. const mkdirp = require('mkdirp').sync
  6. const allVersions = require('../../lib/all-versions')
  7. const graphqlStaticDir = path.join(process.cwd(), 'lib/graphql/static')
  8. const graphqlDataDir = path.join(process.cwd(), 'data/graphql')
  9. // [start-readme]
  10. //
  11. // This script creates the static GraphQL files for a new version.
  12. //
  13. // [end-readme]
  14. program
  15. .description('Create GraphQL files in lib/graphql/static based on an existing version.')
  16. .option('-n, --newVersion <version>', 'The version to copy the files to. Must be in <plan@release> format.')
  17. .option('-o, --oldVersion <version>', 'The version to copy the files from. Must be in <plan@release> format.')
  18. .parse(process.argv)
  19. const newVersion = program.opts().newVersion
  20. const oldVersion = program.opts().oldVersion
  21. if (!(newVersion && oldVersion)) {
  22. console.log('Error! You must provide --newVersion and --oldVersion.')
  23. process.exit(1)
  24. }
  25. if (!(Object.keys(allVersions).includes(newVersion) && Object.keys(allVersions).includes(oldVersion))) {
  26. console.log('Error! You must provide the full name of a currently supported version, e.g., enterprise-server@2.22.')
  27. process.exit(1)
  28. }
  29. const newVersionId = allVersions[newVersion].miscVersionName
  30. const oldVersionId = allVersions[oldVersion].miscVersionName
  31. // copy the schema file wholesale (there are separate schema files per version)
  32. const newSchemaFile = path.join(graphqlStaticDir, `schema-${newVersionId}.json`)
  33. const oldSchemaFile = path.join(graphqlStaticDir, `schema-${oldVersionId}.json`)
  34. fs.copyFileSync(oldSchemaFile, newSchemaFile)
  35. // check that it worked
  36. if (!fs.existsSync(newSchemaFile)) {
  37. console.log(`Error! Can't find ${newSchemaFile}.`)
  38. process.exit(1)
  39. }
  40. // the other files are objects with versions as keys, so we need to require them
  41. const previewsFile = path.join(graphqlStaticDir, 'previews.json')
  42. const changesFile = path.join(graphqlStaticDir, 'upcoming-changes.json')
  43. const objectsFile = path.join(graphqlStaticDir, 'prerendered-objects.json')
  44. const previews = require(previewsFile)
  45. const changes = require(changesFile)
  46. const objects = require(objectsFile)
  47. // The prerendered objects file for the "old version" contains hardcoded links with the old version number.
  48. // We need to update those links to include the new version to prevent a test from failing.
  49. const regexOldVersion = new RegExp(oldVersion, 'gi')
  50. const stringifiedObject = JSON.stringify(objects[oldVersionId])
  51. .replace(regexOldVersion, newVersion)
  52. previews[newVersionId] = previews[oldVersionId]
  53. changes[newVersionId] = changes[oldVersionId]
  54. objects[newVersionId] = JSON.parse(stringifiedObject)
  55. // check that it worked
  56. if (!Object.keys(previews).includes(newVersionId)) {
  57. console.log(`Error! Can't find ${newVersionId} in ${previewsFile}.`)
  58. process.exit(1)
  59. }
  60. if (!Object.keys(changes).includes(newVersionId)) {
  61. console.log(`Error! Can't find ${newVersionId} in ${changesFile}.`)
  62. process.exit(1)
  63. }
  64. if (!Object.keys(objects).includes(newVersionId)) {
  65. console.log(`Error! Can't find ${newVersionId} in ${objectsFile}.`)
  66. process.exit(1)
  67. }
  68. // write the new files
  69. fs.writeFileSync(previewsFile, JSON.stringify(previews, null, 2))
  70. fs.writeFileSync(changesFile, JSON.stringify(changes, null, 2))
  71. fs.writeFileSync(objectsFile, JSON.stringify(objects, null, 2))
  72. // now create the new version directory in data/graphql
  73. const srcDir = path.join(graphqlDataDir, oldVersionId)
  74. const destDir = path.join(graphqlDataDir, newVersionId)
  75. mkdirp(destDir)
  76. // copy the files
  77. fs.readdirSync(srcDir).forEach(file => {
  78. const srcFile = path.join(srcDir, file)
  79. const destFile = path.join(destDir, file)
  80. fs.copyFileSync(srcFile, destFile)
  81. })
  82. // check that it worked
  83. if (!fs.existsSync(destDir)) {
  84. console.log(`Error! A new directory was not successfully created at ${destDir}.`)
  85. process.exit(1)
  86. }
  87. if (!fs.readdirSync(destDir).length) {
  88. console.log(`Error! The directory created at ${destDir} is empty.`)
  89. process.exit(1)
  90. }
  91. // print success message
  92. console.log(`Done! Copied ${oldVersion} GraphQL files to ${newVersion} files.`)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...