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-rest-files.js 3.2 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
  1. #!/usr/bin/env node
  2. const fs = require('fs')
  3. const path = require('path')
  4. const program = require('commander')
  5. const allVersions = require('../../lib/all-versions')
  6. const getOperations = require('../rest/utils/get-operations')
  7. const dereferencedDir = 'lib/rest/static/dereferenced'
  8. const decoratedDir = 'lib/rest/static/decorated'
  9. // [start-readme]
  10. //
  11. // This script first copies the dereferenced schema from the previous GHES version for the new one.
  12. // It then replaces references to the previous version's docs URL (e.g., enterprise-server@3.0) with the new version (e.g., enterprise-server@3.1).
  13. // Finally, it generates a new decorated file from the new dereferenced file to ensure that the dereferenced and decorated files match.
  14. //
  15. // [end-readme]
  16. program
  17. .description('Create new openAPI files in lib/rest/static/decorated and lib/rest/static/dereferenced based on an existing version.')
  18. .option('-n, --newVersion <version>', 'The new version to copy the REST files to. Must be in <plan@release> format.')
  19. .option('-o, --oldVersion <version>', 'The existing version to copy the REST files from. Must be in <plan@release> format.')
  20. .parse(process.argv)
  21. const newVersion = program.newVersion
  22. const oldVersion = program.oldVersion
  23. if (!(newVersion && oldVersion)) {
  24. console.log('Error! You must provide --newVersion and --oldVersion.')
  25. process.exit(1)
  26. }
  27. if (!(Object.keys(allVersions).includes(newVersion) && Object.keys(allVersions).includes(oldVersion))) {
  28. console.log('Error! You must provide the full name of a currently supported version, e.g., enterprise-server@2.22.')
  29. process.exit(1)
  30. }
  31. main()
  32. async function main () {
  33. const oldDereferencedFilename = `${allVersions[oldVersion].openApiVersionName}.deref.json`
  34. const newDereferencedFilename = `${allVersions[newVersion].openApiVersionName}.deref.json`
  35. const newDecoratedFilename = `${allVersions[newVersion].openApiVersionName}.json`
  36. const oldDereferencedFile = path.join(dereferencedDir, oldDereferencedFilename)
  37. const newDereferencedFile = path.join(dereferencedDir, newDereferencedFilename)
  38. const newDecoratedFile = path.join(decoratedDir, newDecoratedFilename)
  39. // check that the old files exist
  40. if (!fs.existsSync(oldDereferencedFile)) {
  41. console.log(`Error! Can't find ${oldDereferencedFile} for ${oldVersion}.`)
  42. process.exit(1)
  43. }
  44. const oldDereferencedContent = fs.readFileSync(oldDereferencedFile, 'utf8')
  45. // Replace old version with new version
  46. // (ex: enterprise-server@3.0 -> enterprise-server@3.1)
  47. const regexOldVersion = new RegExp(oldVersion, 'gi')
  48. const newDereferenceContent = oldDereferencedContent.replace(regexOldVersion, newVersion)
  49. // Write processed dereferenced schema to disk
  50. fs.writeFileSync(newDereferencedFile, newDereferenceContent)
  51. console.log(`Created ${newDereferencedFile}.`)
  52. const dereferencedSchema = require(path.join(process.cwd(), newDereferencedFile))
  53. // Store all operations in an array of operation objects
  54. const operations = await getOperations(dereferencedSchema)
  55. // Process each operation asynchronously
  56. await Promise.all(operations.map(operation => operation.process()))
  57. // Write processed operations to disk
  58. fs.writeFileSync(newDecoratedFile, JSON.stringify(operations, null, 2))
  59. console.log(`Done! Created ${newDecoratedFile}.`)
  60. }
Tip!

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

Comments

Loading...