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

update-files.js 4.9 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 { execSync } = require('child_process')
  6. const mkdirp = require('mkdirp').sync
  7. const rimraf = require('rimraf').sync
  8. const tempDocsDir = path.join(process.cwd(), 'openapiTmp')
  9. const githubRepoDir = path.join(process.cwd(), '../github')
  10. const dereferencedPath = path.join(process.cwd(), 'lib/rest/static/dereferenced')
  11. const schemas = fs.readdirSync(dereferencedPath)
  12. const getOperations = require('./utils/get-operations')
  13. const decoratedPath = path.join(process.cwd(), 'lib/rest/static/decorated')
  14. // [start-readme]
  15. //
  16. // Run this script to pull openAPI files from github/github, dereference them, and decorate them.
  17. //
  18. // [end-readme]
  19. program
  20. .description('Generate dereferenced OpenAPI and decorated schema files.')
  21. .option('--decorate-only', '⚠️ Only used by a πŸ€– to generate decorated schema files from existing dereferenced schema files.')
  22. .parse(process.argv)
  23. const decorateOnly = program.decorateOnly
  24. main()
  25. async function main () {
  26. // Generate the dereferenced OpenAPI schema files
  27. if (!decorateOnly) {
  28. if (!fs.existsSync(githubRepoDir)) {
  29. console.log(`πŸ›‘ The ${githubRepoDir} does not exist. Make sure you have a local, bootstrapped checkout of github/github at the same level as your github/docs-internal repo before running this script.`)
  30. process.exit(1)
  31. }
  32. await getDereferencedFiles()
  33. }
  34. await decorate()
  35. console.log('\n🏁 The static REST API files are now up-to-date with your local `github/github` checkout. To revert uncommitted changes, run `git checkout lib/rest/static/*.\n\n')
  36. }
  37. async function getDereferencedFiles () {
  38. // Get the github/github repo branch name and pull latest
  39. const githubBranch = execSync('git rev-parse --abbrev-ref HEAD', { cwd: githubRepoDir }).toString().trim()
  40. // Only pull master branch because development mode branches are assumed
  41. // to be up-to-date during active work.
  42. if (githubBranch === 'master') {
  43. execSync('git pull', { cwd: githubRepoDir })
  44. }
  45. // create a tmp directory to store schema files generated from github/github
  46. rimraf(tempDocsDir)
  47. mkdirp(tempDocsDir)
  48. console.log(`\nπŸƒβ€β™€οΈπŸƒπŸƒβ€β™€οΈRunning \`bin/openapi bundle\` in branch '${githubBranch}' of your github/github checkout to generate the dereferenced OpenAPI schema files.\n`)
  49. try {
  50. execSync(`${path.join(githubRepoDir, 'bin/openapi')} bundle -o ${tempDocsDir} --include_unpublished`, { stdio: 'inherit' })
  51. } catch (error) {
  52. console.error(error)
  53. console.log('πŸ›‘ Whoops! It looks like the `bin/openapi bundle` command failed to run in your `github/github` repository checkout. To troubleshoot, ensure that your OpenAPI schema YAML is formatted correctly. A CI test runs on your `github/github` PR that flags malformed YAML. You can check the PR diff view for comments left by the openapi CI test to find and fix any formatting errors.')
  54. process.exit(1)
  55. }
  56. execSync(`find ${tempDocsDir} -type f -name "*deref.json" -exec mv '{}' ${dereferencedPath} ';'`)
  57. rimraf(tempDocsDir)
  58. // When running in development mode (locally), the the info.version
  59. // property in the dereferenced schema is replaced with the branch
  60. // name of the `github/github` checkout. A CI test
  61. // checks the version and fails if it's not a semantic version.
  62. schemas.forEach(filename => {
  63. const schema = require(path.join(dereferencedPath, filename))
  64. schema.info.version = `${githubBranch} !!DEVELOPMENT MODE - DO NOT MERGE!!`
  65. fs.writeFileSync(path.join(dereferencedPath, filename), JSON.stringify(schema, null, 2))
  66. })
  67. }
  68. async function decorate () {
  69. console.log('\nπŸŽ„ Decorating the OpenAPI schema files in lib/rest/static/dereferenced.\n')
  70. const dereferencedSchemas = schemas.reduce((acc, filename) => {
  71. const schema = require(path.join(dereferencedPath, filename))
  72. const key = filename.replace('.deref.json', '')
  73. return { ...acc, [key]: schema }
  74. }, {})
  75. for (const [schemaName, schema] of Object.entries(dereferencedSchemas)) {
  76. try {
  77. // munge OpenAPI definitions object in an array of operations objects
  78. const operations = await getOperations(schema)
  79. // process each operation, asynchronously rendering markdown and stuff
  80. await Promise.all(operations.map(operation => operation.process()))
  81. const filename = path.join(decoratedPath, `${schemaName}.json`)
  82. .replace('.deref', '')
  83. // write processed operations to disk
  84. fs.writeFileSync(filename, JSON.stringify(operations, null, 2))
  85. console.log('Wrote', path.relative(process.cwd(), filename))
  86. } catch (error) {
  87. console.error(error)
  88. console.log('πŸ› Whoops! It looks like the decorator script wasn\'t able to parse the dereferenced schema. A recent change may not yet be supported by the decorator. Please reach out in the #docs-engineering slack channel for help.')
  89. process.exit(1)
  90. }
  91. }
  92. }
Tip!

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

Comments

Loading...