From f6a538f06de0a753982650bd8c78440dc5884d78 Mon Sep 17 00:00:00 2001 From: Juan Picado Date: Sat, 11 Mar 2023 22:28:58 +0100 Subject: [PATCH] fix: missing version on footer (#3674) * fix: missing version on fotter * add tests * fix version * Delete Footer.test.tsx.snap * Update renderHTML.ts --- e2e/ui/cypress/e2e/home.cy.ts | 5 + packages/core/types/src/configuration.ts | 39 +- .../src/middlewares/web/utils/renderHTML.ts | 18 +- packages/server/express/src/server.ts | 5 + .../src/sections/Footer/Footer.test.tsx | 9 +- .../src/sections/Footer/Footer.tsx | 2 +- .../Footer/__snapshots__/Footer.test.tsx.snap | 486 ------------------ 7 files changed, 45 insertions(+), 519 deletions(-) delete mode 100644 packages/ui-components/src/sections/Footer/__snapshots__/Footer.test.tsx.snap diff --git a/e2e/ui/cypress/e2e/home.cy.ts b/e2e/ui/cypress/e2e/home.cy.ts index 806b36cd6..552719d03 100644 --- a/e2e/ui/cypress/e2e/home.cy.ts +++ b/e2e/ui/cypress/e2e/home.cy.ts @@ -12,6 +12,11 @@ describe('home spec', () => { cy.title().should('eq', 'verdaccio-server-e2e'); }); + it('version should be displayed', () => { + cy.visit(ctx.url); + cy.getByTestId('version-footer').contains('Powered by'); + }); + it('should match title with no packages published', () => { cy.visit(ctx.url); cy.getByTestId('help-card').contains('No Package Published Yet.'); diff --git a/packages/core/types/src/configuration.ts b/packages/core/types/src/configuration.ts index 07aa170fe..fd24c0190 100644 --- a/packages/core/types/src/configuration.ts +++ b/packages/core/types/src/configuration.ts @@ -75,16 +75,6 @@ export type RateLimit = { max?: number; }; -// export interface WebConf { -// enable?: boolean; -// title?: string; -// logo?: string; -// favicon?: string; -// gravatar?: boolean; -// sort_packages?: string; -// rateLimit?: RateLimit; -// } - export type FlagsConfig = { searchRemote?: boolean; changePassword?: boolean; @@ -105,6 +95,14 @@ export type CommonWebConf = { login?: boolean; scope?: string; pkgManagers?: PackageManagers[]; + showInfo?: boolean; + showSettings?: boolean; + showSearch?: boolean; + showFooter?: boolean; + showThemeSwitch?: boolean; + showDownloadTarball?: boolean; + primaryColor: string; + showRaw?: boolean; }; /** @@ -112,21 +110,11 @@ export type CommonWebConf = { */ export type TemplateUIOptions = { uri?: string; - darkMode?: boolean; protocol?: string; host?: string; - // deprecated + // @deprecated use base instead basename?: string; - scope?: string; - showInfo?: boolean; - showSettings?: boolean; - showSearch?: boolean; - showFooter?: boolean; - showThemeSwitch?: boolean; - showDownloadTarball?: boolean; - showRaw?: boolean; base: string; - primaryColor: string; version?: string; flags: FlagsConfig; } & CommonWebConf; @@ -135,14 +123,18 @@ export type TemplateUIOptions = { * Options on config.yaml for web */ export type WebConf = { - // FIXME: rename to primaryColor and move it to CommonWebConf + // @deprecated use primaryColor primary_color?: string; + primaryColor?: string; enable?: boolean; scriptsHead?: string[]; scriptsBodyAfter?: string[]; + scriptsbodyBefore?: string[]; metaScripts?: string[]; bodyBefore?: string[]; bodyAfter?: string[]; + rateLimit?: RateLimit; + html_cache?: boolean; } & CommonWebConf; export interface UpLinksConfList { @@ -267,10 +259,13 @@ export interface ConfigYaml { url_prefix?: string; server?: ServerSettingsConf; flags?: FlagsConfig; + // @deprecated use flags instead + experiments?: FlagsConfig; userRateLimit?: RateLimit; // internal objects, added by internal yaml to JS config parser // save the configuration file path configPath?: string; + i18n?: { web: string }; } /** diff --git a/packages/middleware/src/middlewares/web/utils/renderHTML.ts b/packages/middleware/src/middlewares/web/utils/renderHTML.ts index 15727d273..93fdf8601 100644 --- a/packages/middleware/src/middlewares/web/utils/renderHTML.ts +++ b/packages/middleware/src/middlewares/web/utils/renderHTML.ts @@ -5,7 +5,7 @@ import { URL } from 'url'; import { WEB_TITLE } from '@verdaccio/config'; import { HEADERS } from '@verdaccio/core'; -import { TemplateUIOptions } from '@verdaccio/types'; +import { ConfigYaml, TemplateUIOptions } from '@verdaccio/types'; import { isURLhasValidProtocol } from '@verdaccio/url'; import { getPublicUrl } from '@verdaccio/url'; @@ -22,7 +22,10 @@ const defaultManifestFiles = { ico: 'favicon.ico', }; -export function resolveLogo(config, req) { +export function resolveLogo(config: ConfigYaml, req) { + if (typeof config?.web?.logo !== 'string') { + return ''; + } const isLocalFile = config?.web?.logo && !isURLhasValidProtocol(config?.web?.logo); if (isLocalFile) { @@ -34,27 +37,29 @@ export function resolveLogo(config, req) { } } -export default function renderHTML(config, manifest, manifestFiles, req, res) { +export default function renderHTML(config: ConfigYaml, manifest, manifestFiles, req, res) { const { url_prefix } = config; const base = getPublicUrl(config?.url_prefix, req); const basename = new URL(base).pathname; const language = config?.i18n?.web ?? DEFAULT_LANGUAGE; + // @ts-ignore const needHtmlCache = [undefined, null].includes(config?.web?.html_cache) ? true - : config.web.html_cache; + : config?.web?.html_cache; const darkMode = config?.web?.darkMode ?? false; const title = config?.web?.title ?? WEB_TITLE; const login = hasLogin(config); const scope = config?.web?.scope ?? ''; const logo = resolveLogo(config, req); const pkgManagers = config?.web?.pkgManagers ?? ['yarn', 'pnpm', 'npm']; - const version = config?.web?.version; + const version = res.locals.app_version ?? ''; const flags = { ...config.flags, // legacy from 5.x ...config.experiments, }; - const primaryColor = validatePrimaryColor(config?.web?.primary_color) ?? '#4b5e40'; + const primaryColor = + validatePrimaryColor(config?.web?.primary_color ?? config?.web?.primaryColor) ?? '#4b5e40'; const { scriptsBodyAfter, metaScripts, @@ -111,6 +116,7 @@ export default function renderHTML(config, manifest, manifestFiles, req, res) { }, manifest ); + if (needHtmlCache) { cache.set('template', webPage); debug('set template cache'); diff --git a/packages/server/express/src/server.ts b/packages/server/express/src/server.ts index 3f61619e5..a04723249 100644 --- a/packages/server/express/src/server.ts +++ b/packages/server/express/src/server.ts @@ -22,6 +22,7 @@ import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types/cust import hookDebug from './debug'; const debug = buildDebug('verdaccio:server'); +const { version } = require('../package.json'); const defineAPI = async function (config: IConfig, storage: Storage): Promise { const auth: Auth = new Auth(config); @@ -81,6 +82,10 @@ const defineAPI = async function (config: IConfig, storage: Storage): Promise { + res.locals.app_version = version ?? ''; + next(); + }); app.use(await webMiddleware(config, auth, storage)); } else { app.get('/', function (req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer) { diff --git a/packages/ui-components/src/sections/Footer/Footer.test.tsx b/packages/ui-components/src/sections/Footer/Footer.test.tsx index 469338808..c02543170 100644 --- a/packages/ui-components/src/sections/Footer/Footer.test.tsx +++ b/packages/ui-components/src/sections/Footer/Footer.test.tsx @@ -1,11 +1,11 @@ import React from 'react'; -import { render } from '../../test/test-react-testing-library'; +import { render, screen } from '../../test/test-react-testing-library'; import Footer from './Footer'; describe('