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 3.7 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
  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.newVersion
  20. const oldVersion = program.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. previews[newVersionId] = previews[oldVersionId]
  48. changes[newVersionId] = changes[oldVersionId]
  49. objects[newVersionId] = objects[oldVersionId]
  50. // check that it worked
  51. if (!Object.keys(previews).includes(newVersionId)) {
  52. console.log(`Error! Can't find ${newVersionId} in ${previewsFile}.`)
  53. process.exit(1)
  54. }
  55. if (!Object.keys(changes).includes(newVersionId)) {
  56. console.log(`Error! Can't find ${newVersionId} in ${changesFile}.`)
  57. process.exit(1)
  58. }
  59. if (!Object.keys(objects).includes(newVersionId)) {
  60. console.log(`Error! Can't find ${newVersionId} in ${objectsFile}.`)
  61. process.exit(1)
  62. }
  63. // write the new files
  64. fs.writeFileSync(previewsFile, JSON.stringify(previews, null, 2))
  65. fs.writeFileSync(changesFile, JSON.stringify(changes, null, 2))
  66. fs.writeFileSync(objectsFile, JSON.stringify(objects, null, 2))
  67. // now create the new version directory in data/graphql
  68. const srcDir = path.join(graphqlDataDir, oldVersionId)
  69. const destDir = path.join(graphqlDataDir, newVersionId)
  70. mkdirp(destDir)
  71. // copy the files
  72. fs.readdirSync(srcDir).forEach(file => {
  73. const srcFile = path.join(srcDir, file)
  74. const destFile = path.join(destDir, file)
  75. fs.copyFileSync(srcFile, destFile)
  76. })
  77. // check that it worked
  78. if (!fs.existsSync(destDir)) {
  79. console.log(`Error! A new directory was not successfully created at ${destDir}.`)
  80. process.exit(1)
  81. }
  82. if (!fs.readdirSync(destDir).length) {
  83. console.log(`Error! The directory created at ${destDir} is empty.`)
  84. process.exit(1)
  85. }
  86. // print success message
  87. 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...