@@ -4,12 +4,15 @@ const fs = require('fs')
44const path = require ( 'path' )
55const program = require ( 'commander' )
66const allVersions = require ( '../../lib/all-versions' )
7+ const getOperations = require ( '../rest/utils/get-operations' )
78const dereferencedDir = 'lib/rest/static/dereferenced'
89const decoratedDir = 'lib/rest/static/decorated'
910
1011// [start-readme]
1112//
12- // This script creates new static openAPI files for a new version and modifies the info.version.
13+ // This script first copies the dereferenced schema from the previous GHES version for the new one.
14+ // 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).
15+ // Finally, it generates a new decorated file from the new dereferenced file to ensure that the dereferenced and decorated files match.
1316//
1417// [end-readme]
1518
@@ -32,48 +35,43 @@ if (!(Object.keys(allVersions).includes(newVersion) && Object.keys(allVersions).
3235 process . exit ( 1 )
3336}
3437
35- const oldDereferencedFilename = `${ allVersions [ oldVersion ] . openApiVersionName } .deref.json`
36- const newDereferencedFilename = `${ allVersions [ newVersion ] . openApiVersionName } .deref.json`
37- const oldDecoratedFilename = `${ allVersions [ oldVersion ] . openApiVersionName } .json`
38- const newDecoratedFilename = `${ allVersions [ newVersion ] . openApiVersionName } .json`
38+ main ( )
3939
40- const oldDereferencedFile = path . join ( dereferencedDir , oldDereferencedFilename )
41- const newDereferencedFile = path . join ( dereferencedDir , newDereferencedFilename )
42- const oldDecoratedFile = path . join ( decoratedDir , oldDecoratedFilename )
43- const newDecoratedFile = path . join ( decoratedDir , newDecoratedFilename )
40+ async function main ( ) {
41+ const oldDereferencedFilename = ` ${ allVersions [ oldVersion ] . openApiVersionName } .deref.json`
42+ const newDereferencedFilename = ` ${ allVersions [ newVersion ] . openApiVersionName } .deref.json`
43+ const newDecoratedFilename = ` ${ allVersions [ newVersion ] . openApiVersionName } .json`
4444
45- // check that the old files exist
46- if ( ! fs . existsSync ( oldDereferencedFile ) ) {
47- console . log ( `Error! Can't find ${ oldDereferencedFile } for ${ oldVersion } .` )
48- process . exit ( 1 )
49- }
45+ const oldDereferencedFile = path . join ( dereferencedDir , oldDereferencedFilename )
46+ const newDereferencedFile = path . join ( dereferencedDir , newDereferencedFilename )
47+ const newDecoratedFile = path . join ( decoratedDir , newDecoratedFilename )
5048
51- if ( ! fs . existsSync ( oldDecoratedFile ) ) {
52- console . log ( `Error! Can't find ${ oldDecoratedFile } for ${ oldVersion } .` )
53- process . exit ( 1 )
54- }
49+ // check that the old files exist
50+ if ( ! fs . existsSync ( oldDereferencedFile ) ) {
51+ console . log ( `Error! Can't find ${ oldDereferencedFile } for ${ oldVersion } .` )
52+ process . exit ( 1 )
53+ }
5554
56- // copy the files
57- fs . copyFileSync ( oldDereferencedFile , newDereferencedFile )
58- fs . copyFileSync ( oldDecoratedFile , newDecoratedFile )
55+ const oldDereferencedContent = fs . readFileSync ( oldDereferencedFile , 'utf8' )
5956
60- // check that it worked
61- if ( ! fs . existsSync ( newDereferencedFile ) ) {
62- console . log ( `Error! Can't find ${ newDereferencedFile } for ${ oldVersion } .` )
63- process . exit ( 1 )
64- }
57+ // Replace old version with new version
58+ // (ex: enterprise-server@3.0 -> enterprise-server@3.1)
59+ const regexOldVersion = new RegExp ( oldVersion , 'gi' )
60+ const newDereferenceContent = oldDereferencedContent . replace ( regexOldVersion , newVersion )
6561
66- if ( ! fs . existsSync ( newDecoratedFile ) ) {
67- console . log ( `Error! Can't find ${ newDecoratedFile } for ${ oldVersion } .` )
68- process . exit ( 1 )
69- }
62+ // Write processed dereferenced schema to disk
63+ fs . writeFileSync ( newDereferencedFile , newDereferenceContent )
64+ console . log ( `Created ${ newDereferencedFile } .` )
65+
66+ const dereferencedSchema = require ( path . join ( process . cwd ( ) , newDereferencedFile ) )
7067
71- // set the info.version to development mode
72- const derefFilepath = path . join ( process . cwd ( ) , newDereferencedFile )
73- const derefSchema = require ( derefFilepath )
74- console . log ( derefSchema . info . version )
75- derefSchema . info . version = `Copy of ${ oldVersion } !!DEVELOPMENT MODE - DO NOT MERGE!!`
76- fs . writeFileSync ( derefFilepath , JSON . stringify ( derefSchema , null , 2 ) )
68+ // Store all operations in an array of operation objects
69+ const operations = await getOperations ( dereferencedSchema )
7770
78- // print success message
79- console . log ( `Done! Created ${ newDereferencedFile } and ${ newDecoratedFile } .` )
71+ // Process each operation asynchronously
72+ await Promise . all ( operations . map ( operation => operation . process ( ) ) )
73+
74+ // Write processed operations to disk
75+ fs . writeFileSync ( newDecoratedFile , JSON . stringify ( operations , null , 2 ) )
76+ console . log ( `Done! Created ${ newDecoratedFile } .` )
77+ }
0 commit comments