All files / app-play/src app-title.ts

100% Statements 10/10
100% Branches 2/2
100% Functions 5/5
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53                  1x 1x   1x             12x 12x         6x         6x         1x 1x                       2x        
/**
 * The installed player's title per environment, so a Be-Music Play window says which deployment it runs:
 * `Be-Music Play` in production, `Be-Music Play (Preview)` on the preview Worker, `Be-Music Play (Development)` on a
 * local dev server. One build serves both deployments (`cf deploy --mode preview` only changes the Worker), so the
 * Worker retitles the page and the web app manifest as it serves them, and the dev server does the same in
 * `@be-music/app-play`'s Vite config. Production is served untouched: the built files already carry its title.
 */
export type PlayEnvironment = 'development' | 'preview' | 'production';
 
const PLAY_APP_NAME = 'Be-Music Play';
const PLAY_APP_SHORT_NAME = 'Be-Music';
 
const ENVIRONMENT_LABELS: Readonly<Record<PlayEnvironment, string | undefined>> = {
  development: 'Development',
  preview: 'Preview',
  production: undefined,
};
 
function withEnvironmentLabel(name: string, environment: PlayEnvironment): string {
  const label = ENVIRONMENT_LABELS[environment];
  return label === undefined ? name : `${name} (${label})`;
}
 
/** The page title and the manifest `name`: `Be-Music Play`, with the environment after it outside production. */
export function playAppTitle(environment: PlayEnvironment): string {
  return withEnvironmentLabel(PLAY_APP_NAME, environment);
}
 
/** The manifest `short_name` and the home screen title on iOS (`apple-mobile-web-app-title`). */
export function playAppShortName(environment: PlayEnvironment): string {
  return withEnvironmentLabel(PLAY_APP_SHORT_NAME, environment);
}
 
/** The web app manifest's JSON with `name` and `short_name` set for `environment`; every other member is kept. */
export function retitleManifest(manifestJSON: string, environment: PlayEnvironment): string {
  const manifest = JSON.parse(manifestJSON) as Record<string, unknown>;
  return `${JSON.stringify(
    { ...manifest, name: playAppTitle(environment), short_name: playAppShortName(environment) },
    null,
    2,
  )}\n`;
}
 
/**
 * The player's HTML with its `<title>` and `apple-mobile-web-app-title` set for `environment`. For the dev server, which
 * transforms the page as a string; the Worker streams it through `HTMLRewriter` instead.
 */
export function retitleHTML(html: string, environment: PlayEnvironment): string {
  return html
    .replace(/<title>[^<]*<\/title>/, `<title>${playAppTitle(environment)}</title>`)
    .replace(/(<meta\s+name="apple-mobile-web-app-title"\s+content=")[^"]*(")/, `$1${playAppShortName(environment)}$2`);
}