1
0
mirror of synced 2025-12-20 10:28:40 -05:00
Files
docs/script/enterprise-server-releases/create-rest-files.js
Kevin Heis 42e785b0a8 Migrate CommonJS to ESM (#20301)
* First run of script

* Get the app running --- ish

* Get NextJS working

* Remove `node:`

* Get more tests passing in unit directory

* Update FailBot test to use nock

* Update test.yml

* Update Dockerfile

* tests/content fixes

* Update page.js

* Update build-changelog.js

* updating tests/routing

* Update orphan-tests.js

* updating tests/rendering

* Update .eslintrc.js

* Update .eslintrc.js

* Install jest/globals

* "linting" tests

* staging update to server.mjs

* Change '.github/allowed-actions.js' to a ESM export

* Lint

* Fixes for the main package.json

* Move Jest to be last in the npm test command so we can pass args

* Just use 'npm run lint' in the npm test command

* update algolia label script

* update openapi script

* update require on openapi

* Update enterprise-algolia-label.js

* forgot JSON.parse

* Update lunr-search-index.js

* Always explicitly include process.cwd() for JSON file reads pathed from project root

* update graphql/update-files.js script

* Update other npm scripts using jest to pass ESM NODE_OPTIONS

* Update check-for-enterprise-issues-by-label.js for ESM

* Update create-enterprise-issue.js for ESM

* Import jest global for browser tests

* Convert 'script/deploy' to ESM

Co-authored-by: Grace Park <gracepark@github.com>
Co-authored-by: James M. Greene <jamesmgreene@github.com>
2021-07-14 13:49:18 -07:00

78 lines
3.3 KiB
JavaScript
Executable File

#!/usr/bin/env node
import fs from 'fs'
import path from 'path'
import program from 'commander'
import allVersions from '../../lib/all-versions.js'
import getOperations from '../rest/utils/get-operations.js'
const dereferencedDir = 'lib/rest/static/dereferenced'
const decoratedDir = 'lib/rest/static/decorated'
// [start-readme]
//
// This script first copies the dereferenced schema from the previous GHES version for the new one.
// 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).
// Finally, it generates a new decorated file from the new dereferenced file to ensure that the dereferenced and decorated files match.
//
// [end-readme]
program
.description('Create new openAPI files in lib/rest/static/decorated and lib/rest/static/dereferenced based on an existing version.')
.option('-n, --newVersion <version>', 'The new version to copy the REST files to. Must be in <plan@release> format.')
.option('-o, --oldVersion <version>', 'The existing version to copy the REST files from. Must be in <plan@release> format.')
.parse(process.argv)
const newVersion = program.opts().newVersion
const oldVersion = program.opts().oldVersion
if (!(newVersion && oldVersion)) {
console.log('Error! You must provide --newVersion and --oldVersion.')
process.exit(1)
}
if (!(Object.keys(allVersions).includes(newVersion) && Object.keys(allVersions).includes(oldVersion))) {
console.log('Error! You must provide the full name of a currently supported version, e.g., enterprise-server@2.22.')
process.exit(1)
}
main()
async function main () {
const oldDereferencedFilename = `${allVersions[oldVersion].openApiVersionName}.deref.json`
const newDereferencedFilename = `${allVersions[newVersion].openApiVersionName}.deref.json`
const newDecoratedFilename = `${allVersions[newVersion].openApiVersionName}.json`
const oldDereferencedFile = path.join(dereferencedDir, oldDereferencedFilename)
const newDereferencedFile = path.join(dereferencedDir, newDereferencedFilename)
const newDecoratedFile = path.join(decoratedDir, newDecoratedFilename)
// check that the old files exist
if (!fs.existsSync(oldDereferencedFile)) {
console.log(`Error! Can't find ${oldDereferencedFile} for ${oldVersion}.`)
process.exit(1)
}
const oldDereferencedContent = fs.readFileSync(oldDereferencedFile, 'utf8')
// Replace old version with new version
// (ex: enterprise-server@3.0 -> enterprise-server@3.1)
const regexOldVersion = new RegExp(oldVersion, 'gi')
const newDereferenceContent = oldDereferencedContent.replace(regexOldVersion, newVersion)
// Write processed dereferenced schema to disk
fs.writeFileSync(newDereferencedFile, newDereferenceContent)
console.log(`Created ${newDereferencedFile}.`)
const dereferencedSchema = JSON.parse(fs.readFileSync(path.join(process.cwd(), newDereferencedFile)))
// Store all operations in an array of operation objects
const operations = await getOperations(dereferencedSchema)
// Process each operation asynchronously
await Promise.all(operations.map(operation => operation.process()))
// Write processed operations to disk
fs.writeFileSync(newDecoratedFile, JSON.stringify(operations, null, 2))
console.log(`Done! Created ${newDecoratedFile}.`)
}