import { ListHeader , Manifest , ManifestContext , ProgressList , PWABuilderSession , RawTestResult , Status , } from '../utils/interfaces' ; import { runManifestChecks } from '../utils/manifest-validation' ; import { getChosenServiceWorker } from './service_worker' ; let site_url ; let results ; let manifestContext ; let progress = { progress : [ { header : ListHeader . TEST , location : '/' , done : Status . ACTIVE , items : [ { name : 'Submit URL' , done : Status . PENDING , } , { name : 'Run Tests' , done : Status . PENDING , } , ] , } , { header : ListHeader . REVIEW , location : '/reportcard' , done : Status . PENDING , items : [ { name : 'Manifest' , done : Status . PENDING , } , { name : 'Service Worker' , done : Status . PENDING , } , { name : 'Security' , done : Status . PENDING , } , ] , } , { header : ListHeader . PUBLISH , location : '/publish' , done : Status . PENDING , items : [ { name : 'Package' , done : Status . PENDING , } , { name : 'Publish' , done : Status . PENDING , } , ] , } , { header : ListHeader . COMPLETE , location : '/complete' , done : Status . PENDING , items : [ { name : 'Resources' , done : Status . PENDING , } , ] , } , ] , } ; export function getProgress ( ) { const current_progress = sessionStorage . getItem ( 'current_progress' ) ; if ( current_progress ) { return < ProgressList > JSON . parse ( current_progress ) ; } else { return progress ; } } export function setProgress ( newProgress ) { progress = newProgress ; sessionStorage . setItem ( 'current_progress' , JSON . stringify ( progress ) ) ; } export function setURL ( url ) { if ( url ) { site_url = url ; sessionStorage . setItem ( PWABuilderSession . currentUrl , site_url ) ; } } export function getURL ( ) { const url = sessionStorage . getItem ( PWABuilderSession . currentUrl ) ; if ( site_url ) { return site_url ; } if ( url ) { return url ; } throw new Error ( 'No Good URL found for the current site' ) ; } export function setResults ( testResults ) { results = testResults ; sessionStorage . removeItem ( 'current_results' ) ; sessionStorage . setItem ( 'current_results' , JSON . stringify ( testResults ) ) ; } export function getResults ( ) { if ( results ) { return results ; } else { const testResults = sessionStorage . getItem ( 'current_results' ) ; if ( testResults ) { const parsedResults = < RawTestResult > JSON . parse ( testResults ) ; return parsedResults ; } else { return undefined ; } } } export async function baseOrPublish ( ) { const choseSW = getChosenServiceWorker ( ) ; const maniContext = getManifestContext ( ) ; const generatedFlag = maniContext . isGenerated ; const editedFlag = maniContext . isEdited ; if ( generatedFlag === true || choseSW !== undefined || editedFlag === true ) { return 'base' ; } const doubleCheckResults = await doubleCheckManifest ( maniContext ) ; if ( generatedFlag === false && editedFlag === false && doubleCheckResults . icon && ( doubleCheckResults . name || doubleCheckResults . shortName ) && doubleCheckResults . startURL ) { return 'publish' ; } return 'base' ; } returns export function getManifestContext ( ) { if ( ! manifestContext ) { manifestContext = getManifestFromSessionOrEmpty ( ) ; } return manifestContext ; } param val export function setManifestContext ( val ) { manifestContext = val ; sessionStorage . setItem ( PWABuilderSession . manifest , JSON . stringify ( val ) ) ; setURL ( val . siteUrl ) ; } export function getManifestUrl ( ) { return getManifestContext ( ) . manifestUrl ; } function getManifestFromSessionOrEmpty ( ) { try { const sessionManifest = sessionStorage . getItem ( PWABuilderSession . manifest ) ; if ( sessionManifest ) { return JSON . parse ( sessionManifest ) as ManifestContext ; } } catch ( err ) { console . error ( 'Unable to load manifest from session' , err ) ; } const emptyManifest = { dir : 'auto' , display : 'fullscreen' , name : 'placeholder' , short_name : 'placeholder' , start_url : undefined , scope : '/' , lang : 'en' , description : 'placeholder description' , theme_color : 'none' , background_color : 'none' , icons : [ ] , screenshots : [ ] , } ; return { manifest : emptyManifest , initialManifest : emptyManifest , siteUrl : sessionStorage . getItem ( PWABuilderSession . currentUrl ) || '' , manifestUrl : '' , isGenerated : true , isEdited : false , } ; } export function isManifestEdited ( originalMani , newMani ) { if ( originalMani === newMani ) { getManifestContext ( ) . isEdited = false ; } Object . keys ( originalMani ) . forEach ( ( key ) => { if ( Array . isArray ( originalMani [ key ] ) && Array . isArray ( newMani [ key ] ) ) { let flattened_original = originalMani [ key ] . flat ( 2 ) ; let flattened_new = newMani [ key ] . flat ( 2 ) ; flattened_original . forEach ( ( item , index ) => { if ( flattened_new . includes ( item ) === true && flattened_new [ index ] !== item ) { getManifestContext ( ) . isEdited = true ; } } ) ; } else if ( JSON . stringify ( originalMani [ key ] ) !== JSON . stringify ( newMani [ key ] ) ) { getManifestContext ( ) . isEdited = true ; } } ) ; } export async function doubleCheckManifest ( maniContext ) { const test_results = await runManifestChecks ( maniContext ) ; let startURL = false ; let name = false ; let shortName = false ; let icon = false ; test_results . forEach ( test => { if ( test . category === 'required' ) { if ( test . infoString . includes ( 'start_url' ) ) { startURL = test . result ; } if ( test . infoString . includes ( 'short_name' ) ) { shortName = test . result ; } if ( test . infoString . includes ( 'name' ) && test . infoString . toLowerCase ( ) . includes ( 'short_name' ) === false ) { name = test . result ; } if ( test . infoString . includes ( '512' ) ) { icon = test . result ; } } } ) ; return { startURL , name , shortName , icon , } ; }