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.0 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
  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 dereferencedDir = 'lib/rest/static/dereferenced'
  7. const decoratedDir = 'lib/rest/static/decorated'
  8. // [start-readme]
  9. //
  10. // This script creates new static openAPI files for a new version and modifies the info.version.
  11. //
  12. // [end-readme]
  13. program
  14. .description('Create new openAPI files in lib/rest/static/decorated and lib/rest/static/dereferenced based on an existing version.')
  15. .option('-n, --newVersion <version>', 'The new version to copy the REST files to. Must be in <plan@release> format.')
  16. .option('-o, --oldVersion <version>', 'The existing version to copy the REST files from. Must be in <plan@release> format.')
  17. .parse(process.argv)
  18. const newVersion = program.newVersion
  19. const oldVersion = program.oldVersion
  20. if (!(newVersion && oldVersion)) {
  21. console.log('Error! You must provide --newVersion and --oldVersion.')
  22. process.exit(1)
  23. }
  24. if (!(Object.keys(allVersions).includes(newVersion) && Object.keys(allVersions).includes(oldVersion))) {
  25. console.log('Error! You must provide the full name of a currently supported version, e.g., enterprise-server@2.22.')
  26. process.exit(1)
  27. }
  28. const oldDereferencedFilename = `${allVersions[oldVersion].openApiVersionName}.deref.json`
  29. const newDereferencedFilename = `${allVersions[newVersion].openApiVersionName}.deref.json`
  30. const oldDecoratedFilename = `${allVersions[oldVersion].openApiVersionName}.json`
  31. const newDecoratedFilename = `${allVersions[newVersion].openApiVersionName}.json`
  32. const oldDereferencedFile = path.join(dereferencedDir, oldDereferencedFilename)
  33. const newDereferencedFile = path.join(dereferencedDir, newDereferencedFilename)
  34. const oldDecoratedFile = path.join(decoratedDir, oldDecoratedFilename)
  35. const newDecoratedFile = path.join(decoratedDir, newDecoratedFilename)
  36. // check that the old files exist
  37. if (!fs.existsSync(oldDereferencedFile)) {
  38. console.log(`Error! Can't find ${oldDereferencedFile} for ${oldVersion}.`)
  39. process.exit(1)
  40. }
  41. if (!fs.existsSync(oldDecoratedFile)) {
  42. console.log(`Error! Can't find ${oldDecoratedFile} for ${oldVersion}.`)
  43. process.exit(1)
  44. }
  45. // copy the files
  46. fs.copyFileSync(oldDereferencedFile, newDereferencedFile)
  47. fs.copyFileSync(oldDecoratedFile, newDecoratedFile)
  48. // check that it worked
  49. if (!fs.existsSync(newDereferencedFile)) {
  50. console.log(`Error! Can't find ${newDereferencedFile} for ${oldVersion}.`)
  51. process.exit(1)
  52. }
  53. if (!fs.existsSync(newDecoratedFile)) {
  54. console.log(`Error! Can't find ${newDecoratedFile} for ${oldVersion}.`)
  55. process.exit(1)
  56. }
  57. // set the info.version to development mode
  58. const derefFilepath = path.join(process.cwd(), newDereferencedFile)
  59. const derefSchema = require(derefFilepath)
  60. console.log(derefSchema.info.version)
  61. derefSchema.info.version = `Copy of ${oldVersion} !!DEVELOPMENT MODE - DO NOT MERGE!!`
  62. fs.writeFileSync(derefFilepath, JSON.stringify(derefSchema, null, 2))
  63. // print success message
  64. console.log(`Done! Created ${newDereferencedFile} and ${newDecoratedFile}.`)
Tip!

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

Comments

Loading...