@@ -113,6 +113,40 @@ export async function downloadDiscoveryDocs(
113113 return changes ;
114114}
115115
116+ /**
117+ * Checks that a discovery doc file name is in the expected format of
118+ * [NAME(alphanumeric)]-[VERSION(alphanumeric, _, .)].json.
119+ * Throws an error if the file name is not in the expected format.
120+ * @param fileName The file name to validate
121+ */
122+ export function validateDiscoveryDocFileName ( fileName : string ) {
123+ const regex = / ^ [ a - z A - Z 0 - 9 ] + - [ a - z A - Z 0 - 9 _ . ] + \. j s o n $ / ;
124+ if ( ! regex . test ( fileName ) ) {
125+ throw new Error (
126+ `Discovery doc file name '${ fileName } ' is not in the expected format of '[NAME(alphanumeric)]-[VERSION(alphanumeric, _, .)].json'.` ,
127+ ) ;
128+ }
129+ }
130+
131+ export interface ApiData {
132+ name : string ;
133+ version : string ;
134+ }
135+
136+ /**
137+ * Parses a discovery doc file name and returns the API name and version.
138+ * @param fileName The file name to parse.
139+ * @returns The API data (name and version).
140+ */
141+ export function getApiData ( fileName : string ) : ApiData {
142+ validateDiscoveryDocFileName ( fileName ) ;
143+ const firstHyphenIndex = fileName . indexOf ( '-' ) ;
144+ const lastDotIndex = fileName . lastIndexOf ( '.' ) ;
145+ const name = fileName . substring ( 0 , firstHyphenIndex ) ;
146+ const version = fileName . substring ( firstHyphenIndex + 1 , lastDotIndex ) ;
147+ return { name, version} ;
148+ }
149+
116150// These are libraries we should no longer support because
117151// they are not present in the index.json
118152// example: b/148605368
@@ -123,22 +157,21 @@ function cleanupLibrariesNotInIndexJSON(
123157 const srcPath = path . join ( __dirname , '../../../src' , 'apis' ) ;
124158 const discoveryDirectory = fs . readdirSync ( options . downloadPath ) ;
125159 const apisReplaced = apis . map (
126- x => x . id . toString ( ) . replace ( ':' , '-' ) + '.json' ,
160+ api => api . id . toString ( ) . replace ( ':' , '-' ) + '.json' ,
127161 ) ;
128162 // So that we don't delete index.json
129163 apisReplaced . push ( 'index.json' ) ;
130164 const discoveryDocsToDelete = discoveryDirectory . filter (
131- x => ! apisReplaced . includes ( x ) ,
165+ fileName => ! apisReplaced . includes ( fileName ) ,
132166 ) ;
133- const clientFilesToDelete = discoveryDocsToDelete . map ( x => {
134- const apiName = x . split ( '-' ) [ 0 ] ;
135- const versionName = apiName [ 1 ] . split ( '.' ) [ 0 ] ;
136- return path . join ( srcPath , apiName , `${ versionName } .ts` ) ;
167+ const clientFilesToDelete = discoveryDocsToDelete . map ( docFileName => {
168+ const api = getApiData ( docFileName ) ;
169+ return path . join ( srcPath , api . name , `${ api . version } .ts` ) ;
137170 } ) ;
138- discoveryDocsToDelete . forEach ( x =>
139- fs . unlinkSync ( path . join ( options . downloadPath , x ) ) ,
171+ discoveryDocsToDelete . forEach ( docFileName =>
172+ fs . unlinkSync ( path . join ( options . downloadPath , docFileName ) ) ,
140173 ) ;
141- clientFilesToDelete . forEach ( x => fs . unlinkSync ( x ) ) ;
174+ clientFilesToDelete . forEach ( clientFile => fs . unlinkSync ( clientFile ) ) ;
142175}
143176
144177const ignoreLines = / ^ \s + " (?: e t a g | r e v i s i o n ) " : " .+ " / ;
0 commit comments