Skip to content

Commit

Permalink
Use screenNameKeys for a fast way of getting translated screen names,…
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanolson committed May 15, 2023
1 parent 4078ed0 commit f4b90c8
Showing 1 changed file with 14 additions and 39 deletions.
53 changes: 14 additions & 39 deletions js/build-server/parseScreenNames.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,28 @@
// Copyright 2021, University of Colorado Boulder

const axios = require( 'axios' );
const puppeteer = require( 'puppeteer' );
const winston = require( 'winston' );
const assert = require( 'assert' );
const puppeteerLoad = require( '../common/puppeteerLoad' );
const getFullStringMap = require( '../common/getFullStringMap' );
const loadJSON = require( '../common/loadJSON' );
const gitCheckout = require( '../common/gitCheckout' );

/**
* NOTE: release branch NEEDS to be checked out for this to be called, since we'll need the dependencies.json file
*
* @param {string} simName
* @param {string[]} locales - a list of locale codes
* @returns {Promise.<{}>}
*/
const parseScreenNamesFromSimulation = async ( simName, locales ) => {

const browser = await puppeteer.launch( {
args: [
'--disable-gpu'
]
} );
const stringMap = await getFullStringMap( simName );
const packageObject = await loadJSON( `../${simName}/package.json` );
const screenNameKeys = packageObject.phet.screenNameKeys || [];

const returnObject = {};

for ( let localeIndex = 0; localeIndex < locales.length; localeIndex++ ) {
const locale = locales[ localeIndex ];
try {
const url = `https://phet.colorado.edu/sims/html/${simName}/latest/${simName}_all.html?locale=${locale}`;
const result = await puppeteerLoad( url, {
waitForFunction: 'phet.joist.sim.screens',
browser: browser,
evaluate: () => {
return phet.joist.sim.screens
.map( screen => screen.name || ( screen.nameProperty && screen.nameProperty.value ) )
.filter( ( screenName, screenIndex ) => !( screenIndex === 0 && screenName === '\u202aHome\u202c' ) );
}
} );
assert( result !== null, 'must be a list of screen names, not null' );

returnObject[ locale ] = result;
}
catch( e ) {
winston.log( 'error', `Could not parse screen names from sim: ${e}` );
e.stack && winston.log( 'error', e.stack );
browser && await browser.close();

throw e;
}
const result = {};
for ( const locale of locales ) {
result[ locale ] = screenNameKeys.map( key => stringMap[ key ][ locale ] || stringMap[ key ][ locale.slice( 0, 2 ) ] || stringMap[ key ].en );
}

await browser.close();

return returnObject;
return result;
};

const parseScreenNamesAllSimulations = async () => {
Expand All @@ -63,7 +36,9 @@ const parseScreenNamesAllSimulations = async () => {
const simulation = project.simulations[ 0 ];
const simName = simulation.name;
const locales = Object.keys( simulation.localizedSimulations );
await gitCheckout( simName, `${project.version.major}.${project.version.minor}` );
screenNameObject[ simName ] = await parseScreenNamesFromSimulation( simName, locales );
await gitCheckout( simName, 'master' );
}

return screenNameObject;
Expand Down

0 comments on commit f4b90c8

Please sign in to comment.