import axios from 'axios' ; import path from 'path' ; import { InterfaceDeclaration , ModuleDeclaration , Project } from 'ts-morph' ; import { jsdocsLabel , typeFile } from './Utils.js' ; const docList = [ 'berries' , 'contests' , 'encounters' , 'evolution' , 'games' , 'items' , 'locations' , 'machines' , 'moves' , 'pokemon' , 'resource-lists' , 'utility' , ] ; function addJsDoc ( generatedInterface , index , description , model ) { if ( index === 0 && description && description !== ' ' ) { const jsDocs = generatedInterface . getJsDocs ( ) ; if ( jsDocs && jsDocs [ 0 ] && jsDocs [ 0 ] . getDescription ( ) ) { jsDocs [ 0 ] . setDescription ( description ) ; } else { generatedInterface . addJsDoc ( { description , } ) ; } } for ( const field of model . fields ) { if ( ! field . description || field . description === ' ' ) { continue ; } const property = generatedInterface . getPropertyOrThrow ( field . name ) ; const jsDocs = property . getJsDocs ( ) ; if ( jsDocs && jsDocs [ 0 ] && jsDocs [ 0 ] . getDescription ( ) ) { jsDocs [ 0 ] . setDescription ( field . description ) ; } else { property . addJsDoc ( { description : field . description , } ) ; } } } async function loadDocumentation ( namespace , docName ) { const response = await axios . get ( `https://raw.githubusercontent.com/PokeAPI/pokeapi.co/master/src/docs/${ docName }.json` ) ; const apis = await response . data ; for ( const api of apis ) { for ( const [ index , model ] of api . responseModels . entries ( ) ) { try { const generatedInterface = namespace . getInterface ( model . name === 'PokemonEncounter' ? 'LocationAreaPokemonEncounter' : model . name ) ; const purpleGeneratedInterface = namespace . getInterface ( `Purple${ model . name }` ) ; const fluffyGeneratedInterface = namespace . getInterface ( `Fluffy${ model . name }` ) ; if ( generatedInterface ) { addJsDoc ( generatedInterface , index , api . description , model ) ; } if ( purpleGeneratedInterface ) { addJsDoc ( purpleGeneratedInterface , index , api . description , model ) ; } if ( fluffyGeneratedInterface ) { addJsDoc ( fluffyGeneratedInterface , index , api . description , model ) ; } } catch ( error ) { console . log ( model . name ) ; console . log ( error ) ; } } } } console . time ( jsdocsLabel ) ; console . timeLog ( jsdocsLabel , '- Starting to generate JSDocs...' ) ; const project = new Project ( { tsConfigFilePath : path . resolve ( './tsconfig.json' ) , } ) ; const file = project . getSourceFileOrThrow ( typeFile ) ; const rootModule = file . getModuleOrThrow ( '\'pokedex-promise-v2\'' ) ; const namespace = rootModule . getModuleOrThrow ( 'PokeAPI' ) ; ( async ( ) => { for ( const docName of docList ) { await loadDocumentation ( namespace , docName ) ; } await file . save ( ) ; console . timeEnd ( jsdocsLabel ) ; console . log ( 'JSDocs added!' ) ; } ) ( ) ;