import type * as d from '../../declarations' ; import { catchError } from '@utils' ; import { getSitemapUrls } from './sitemap-xml' ; import { join } from 'path' ; export const generateRobotsTxt = async ( manager , sitemapResults ) => { if ( manager . prerenderConfig . robotsTxt === null ) { return null ; } try { if ( typeof manager . prerenderConfig . robotsTxt !== 'function' ) { manager . prerenderConfig . robotsTxt = function robotsTxt ( opts ) { const content = [ `User-agent: *` , `Disallow:` ] ; if ( typeof opts . sitemapUrl === 'string' ) { content . push ( `Sitemap: ${ opts . sitemapUrl }` ) ; } return content . join ( '\n' ) ; } ; } const opts = { urls : getSitemapUrls ( manager ) , baseUrl : manager . outputTarget . baseUrl , sitemapUrl : sitemapResults ? sitemapResults . url : null , dir : manager . outputTarget . dir , } ; const userResults = manager . prerenderConfig . robotsTxt ( opts ) ; if ( userResults == null ) { return null ; } const results = { content : null , filePath : null , url : null , } ; if ( typeof userResults === 'string' ) { results . content = userResults ; } else { results . content = userResults . content ; results . filePath = userResults . filePath ; } if ( typeof results . content !== 'string' ) { return null ; } const lines = results . content . replace ( /\r/g , '\n' ) . split ( '\n' ) ; results . content = lines . map ( ( l ) => l . trim ( ) ) . join ( '\n' ) ; if ( typeof results . filePath !== 'string' ) { results . filePath = join ( manager . outputTarget . dir , `robots.txt` ) ; } if ( typeof results . url !== 'string' ) { const robotsTxtUrl = new URL ( `/robots.txt` , manager . outputTarget . baseUrl ) ; results . url = robotsTxtUrl . href ; } await manager . config . sys . writeFile ( results . filePath , results . content ) ; return results ; } catch ( e ) { catchError ( manager . diagnostics , e ) ; return null ; } } ;