diff --git a/js/grunt/Gruntfile.js b/js/grunt/Gruntfile.js index ab065a330..15f60bfa2 100644 --- a/js/grunt/Gruntfile.js +++ b/js/grunt/Gruntfile.js @@ -453,7 +453,7 @@ module.exports = function( grunt ) { const filePath = `${buildDirectory}/${new Date().toLocaleString().split( ', ' ).join( '_' ).split( '/' ).join( '-' ).split( ' ' ).join( '_' ).split( ':' ).join( '.' )}.json`; - const api = await generatePhetioAPI( repo, buildLocal.localTestingURL ); + const api = await generatePhetioAPI( repo, false ); writeFile( filePath, api ); } ) ); diff --git a/js/grunt/phet-io/copySupplementalPhetioFiles.js b/js/grunt/phet-io/copySupplementalPhetioFiles.js index 14d6e99c5..98e33b6ee 100644 --- a/js/grunt/phet-io/copySupplementalPhetioFiles.js +++ b/js/grunt/phet-io/copySupplementalPhetioFiles.js @@ -266,7 +266,7 @@ module.exports = async ( repo, version, simulationDisplayName, packageObject, bu await handleJSDOC( buildDir ); if ( generatePhetioAPIFile ) { - const fullAPI = await generatePhetioAPI( repo, buildLocal.localTestingURL ); + const fullAPI = await generatePhetioAPI( repo, true ); grunt.file.write( `${buildDir}${repo}-phet-io-api.json`, fullAPI ); } }; diff --git a/js/grunt/phet-io/generatePhetioAPI.js b/js/grunt/phet-io/generatePhetioAPI.js index ced59c9ae..d50889bac 100644 --- a/js/grunt/phet-io/generatePhetioAPI.js +++ b/js/grunt/phet-io/generatePhetioAPI.js @@ -9,17 +9,53 @@ 'use strict'; +const http = require( 'http' ); +const fs = require( 'fs' ); const puppeteer = require( 'puppeteer' ); /** * @param {string} repo - * @param {string} localTestingURL - the browser url to access the root of phet repos + * @param {boolean} [fromBuiltVersion] - if the built file should be used to generate the API (otherwise uses unbuilt) */ -module.exports = async ( repo, localTestingURL ) => { - localTestingURL = localTestingURL.endsWith( '/' ) ? localTestingURL : `${localTestingURL}/`; +module.exports = async ( repo, fromBuiltVersion = false ) => { - return new Promise( async ( resolve, reject ) => { + // Consider using https://github.com/cloudhead/node-static or reading https://nodejs.org/en/knowledge/HTTP/servers/how-to-serve-static-files/ + const server = http.createServer( ( req, res ) => { + + // Trim query string + const tail = req.url.indexOf( '?' ) >= 0 ? req.url.substring( 0, req.url.indexOf( '?' ) ) : req.url; + const path = `${process.cwd()}/..${tail}`; + + // See https://gist.github.com/aolde/8104861 + const mimeTypes = { + html: 'text/html', + jpeg: 'image/jpeg', + jpg: 'image/jpeg', + png: 'image/png', + js: 'text/javascript', + css: 'text/css', + gif: 'image/gif', + mp3: 'audio/mpeg', + wav: 'audio/wav' + }; + const mimeType = mimeTypes[ path.split( '.' ).pop() ] || 'text/plain'; + + fs.readFile( path, function( err, data ) { + if ( err ) { + res.writeHead( 404 ); + res.end( JSON.stringify( err ) ); + } + else { + res.writeHead( 200, { 'Content-Type': mimeType } ); + res.end( data ); + } + } ); + } ); + server.listen( 0 ); + const port = server.address().port; + + return new Promise( async ( resolve, reject ) => { const browser = await puppeteer.launch(); const page = await browser.newPage(); @@ -44,10 +80,14 @@ module.exports = async ( repo, localTestingURL ) => { page.on( 'pageerror', msg => reject( msg ) ); try { - await page.goto( `${localTestingURL}${repo}/${repo}_en.html?brand=phet-io&phetioStandalone&phetioPrintAPI` ); + const relativePath = fromBuiltVersion ? + `build/phet-io/${repo}_all_phet-io.html` : + `${repo}_en.html`; + const url = `http://localhost:${port}/${repo}/${relativePath}?brand=phet-io&phetioStandalone&phetioPrintAPI`; + await page.goto( url ); } catch( e ) { reject( e ); } } ); -}; +}; \ No newline at end of file