Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

self-hosting: invite non-supporters to support OpenPanel after init and upgrade

Carl-Gerhard Lindesvärdfc67491bc010
diff --git a/self-hosting/cli/src/commands/init.ts b/self-hosting/cli/src/commands/init.ts
index 3ced023a5..3c2b0f15a 100644
--- a/self-hosting/cli/src/commands/init.ts
+++ b/self-hosting/cli/src/commands/init.ts
@@ -24,6 +24,7 @@ import {
import { isInstallDir, writeFiles } from '../install';
import { discoverInstall, isEmptyOrMissing, rememberDir } from '../install-dir';
import { run } from '../run';
+import { showSupportBanner } from '../support-banner';
import { bold } from '../ui';
import { compose } from './compose';
@@ -311,8 +312,13 @@ export const init = async (flags: InitFlags): Promise<number> => {
await confirm({ message: 'Start OpenPanel now?', initialValue: true })
);
if (start) {
- return compose('up', flags.dir);
+ const code = await compose('up', flags.dir);
+ if (code === 0) {
+ showSupportBanner(null);
+ }
+ return code;
}
+ showSupportBanner(null);
outro('Start it with: openpanel up');
return 0;
};
diff --git a/self-hosting/cli/src/commands/upgrade.ts b/self-hosting/cli/src/commands/upgrade.ts
index 9de242f14..c5e64ac1a 100644
--- a/self-hosting/cli/src/commands/upgrade.ts
+++ b/self-hosting/cli/src/commands/upgrade.ts
@@ -4,6 +4,7 @@ import { checkHost } from '../host';
import { isInstallDir, loadInstall, writeInstall } from '../install';
import { run } from '../run';
import { type Fetcher, isCompiledBinary, selfUpdate } from '../self-update';
+import { showSupportBanner } from '../support-banner';
import {
type Channel,
channelOf,
@@ -220,5 +221,9 @@ export const upgrade = async (flags: UpgradeFlags): Promise<number> => {
if (pulled !== 0) {
return pulled;
}
- return compose('up', flags.dir, ['--remove-orphans']);
+ const started = await compose('up', flags.dir, ['--remove-orphans']);
+ if (started === 0) {
+ showSupportBanner(install);
+ }
+ return started;
};
diff --git a/self-hosting/cli/src/support-banner.ts b/self-hosting/cli/src/support-banner.ts
new file mode 100644
index 000000000..82c913763
--- /dev/null
+++ b/self-hosting/cli/src/support-banner.ts
@@ -0,0 +1,59 @@
+import { note } from '@clack/prompts';
+import type { Install } from './doctor/types';
+import { channelOf, isLoggedIn } from './supporter';
+import { bold, cyan } from './ui';
+
+export const SUPPORTER_URL = 'https://openpanel.dev/supporter';
+
+// Perks as the supporter page states them; keep the two in sync.
+const PERKS = [
+ 'The latest images, built on every commit: new features and fixes before public releases',
+ 'Priority support and a supporter role on Discord',
+ 'A direct hand in what OpenPanel builds next',
+] as const;
+
+interface BannerContext {
+ install: Install | null;
+ isTerminal: boolean;
+ env: Record<string, string | undefined>;
+ loggedIn: () => boolean;
+}
+
+// Only for people who see it and are not supporters already: never in CI,
+// scripts or pipes, and never on an install that runs supporter images.
+export const shouldShowSupportBanner = ({
+ install,
+ isTerminal,
+ env,
+ loggedIn,
+}: BannerContext): boolean => {
+ const optedOut = Boolean(env.OPENPANEL_NO_SUPPORT_BANNER || env.CI);
+ if (optedOut || !isTerminal) {
+ return false;
+ }
+ const isSupporter =
+ (install !== null && channelOf(install) === 'supporter') || loggedIn();
+ return !isSupporter;
+};
+
+export const supportBannerText = (): string =>
+ [
+ 'OpenPanel is open source and self-hosting is free.',
+ 'Supporters keep it that way, and get:',
+ '',
+ ...PERKS.map((perk) => ` • ${perk}`),
+ '',
+ `${bold('Become a supporter:')} ${cyan(SUPPORTER_URL)}`,
+ ].join('\n');
+
+export const showSupportBanner = (install: Install | null): void => {
+ const show = shouldShowSupportBanner({
+ install,
+ isTerminal: Boolean(process.stdout.isTTY),
+ env: process.env,
+ loggedIn: () => isLoggedIn(),
+ });
+ if (show) {
+ note(supportBannerText(), 'Enjoying OpenPanel?');
+ }
+};
diff --git a/self-hosting/cli/test/support-banner.test.ts b/self-hosting/cli/test/support-banner.test.ts
new file mode 100644
index 000000000..17d893baa
--- /dev/null
+++ b/self-hosting/cli/test/support-banner.test.ts
@@ -0,0 +1,63 @@
+import { describe, expect, test } from 'bun:test';
+import { parseDocument } from 'yaml';
+import type { Install } from '../src/doctor/types';
+import { EnvFile } from '../src/env-file';
+import {
+ SUPPORTER_URL,
+ shouldShowSupportBanner,
+ supportBannerText,
+} from '../src/support-banner';
+import { pointAt } from '../src/supporter';
+import { templates } from '../src/templates';
+
+const install = (): Install => ({
+ compose: parseDocument(templates.compose),
+ env: new EnvFile(''),
+ caddyfile: null,
+ files: new Map(),
+ fileKind: () => 'file',
+});
+
+const context = (
+ overrides: Partial<Parameters<typeof shouldShowSupportBanner>[0]> = {}
+) => ({
+ install: install(),
+ isTerminal: true,
+ env: {},
+ loggedIn: () => false,
+ ...overrides,
+});
+
+describe('support banner', () => {
+ test('shows for a public install in a terminal', () => {
+ expect(shouldShowSupportBanner(context())).toBe(true);
+ expect(shouldShowSupportBanner(context({ install: null }))).toBe(true);
+ });
+
+ test('never for supporters: supporter images, or logged in to the supporter registry', () => {
+ const supporterInstall = install();
+ pointAt(supporterInstall, 'supporter', '3.1.9');
+ expect(
+ shouldShowSupportBanner(context({ install: supporterInstall }))
+ ).toBe(false);
+ expect(shouldShowSupportBanner(context({ loggedIn: () => true }))).toBe(
+ false
+ );
+ });
+
+ test('never in CI, pipes or when opted out', () => {
+ expect(shouldShowSupportBanner(context({ env: { CI: 'true' } }))).toBe(
+ false
+ );
+ expect(shouldShowSupportBanner(context({ isTerminal: false }))).toBe(false);
+ expect(
+ shouldShowSupportBanner(
+ context({ env: { OPENPANEL_NO_SUPPORT_BANNER: '1' } })
+ )
+ ).toBe(false);
+ });
+
+ test('links the supporter page', () => {
+ expect(supportBannerText()).toContain(SUPPORTER_URL);
+ });
+});