Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added basic cli telemetry #111

Merged
merged 2 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/__test__/core/env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe("env", () => {
expect(is_toggled(1)).toBe(true);
expect(is_toggled(0)).toBe(false);
expect(is_toggled("anything else")).toBe(false);
expect(is_toggled(undefined, true)).toBe(true);
});

test("env()", () => {
Expand Down
1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"postcss": "^8.5.3",
"postcss-preset-mantine": "^1.17.0",
"postcss-simple-vars": "^7.0.1",
"posthog-js-lite": "^3.4.2",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"react-hook-form": "^7.54.2",
Expand Down
36 changes: 35 additions & 1 deletion app/src/cli/commands/create/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { env } from "core";
import color from "picocolors";
import { overridePackageJson, updateBkndPackages } from "./npm";
import { type Template, templates } from "./templates";
import { createScoped, flush } from "cli/utils/telemetry";

const config = {
types: {
Expand Down Expand Up @@ -48,8 +49,16 @@ function errorOutro() {
process.exit(1);
}

async function onExit() {
await flush();
}

async function action(options: { template?: string; dir?: string; integration?: string }) {
console.log("");
const $t = createScoped("create");
$t.capture("start", {
options,
});

const downloadOpts = {
dir: options.dir || "./",
Expand All @@ -68,45 +77,55 @@ async function action(options: { template?: string; dir?: string; integration?:
})(),
);

$t.properties.at = "dir";
if (!options.dir) {
const dir = await $p.text({
message: "Where to create your project?",
placeholder: downloadOpts.dir,
initialValue: downloadOpts.dir,
});
if ($p.isCancel(dir)) {
await onExit();
process.exit(1);
}

downloadOpts.dir = dir || "./";
}

$t.properties.at = "dir";
if (fs.existsSync(downloadOpts.dir)) {
const clean = await $p.confirm({
message: `Directory ${color.cyan(downloadOpts.dir)} exists. Clean it?`,
initialValue: false,
});
if ($p.isCancel(clean)) {
await onExit();
process.exit(1);
}

downloadOpts.clean = clean;
$t.properties.clean = clean;
}

// don't track name for privacy
let name = downloadOpts.dir.includes("/")
? downloadOpts.dir.split("/").pop()
: downloadOpts.dir.replace(/[./]/g, "");

if (!name || name.length === 0) name = "bknd";

let template: Template | undefined;

if (options.template) {
$t.properties.at = "template";
template = templates.find((t) => t.key === options.template) as Template;
if (!template) {
await onExit();
$p.log.error(`Template ${color.cyan(options.template)} not found`);
process.exit(1);
}
} else {
$t.properties.at = "integration";
let integration: string | undefined = options.integration;
if (!integration) {
await $p.stream.info(
Expand All @@ -128,8 +147,10 @@ async function action(options: { template?: string; dir?: string; integration?:
});

if ($p.isCancel(type)) {
await onExit();
process.exit(1);
}
$t.properties.type = type;

const _integration = await $p.select({
message: `Which ${color.cyan(config.types[type])} do you want to continue with?`,
Expand All @@ -139,11 +160,14 @@ async function action(options: { template?: string; dir?: string; integration?:
})) as any,
});
if ($p.isCancel(_integration)) {
await onExit();
process.exit(1);
}
integration = String(_integration);
$t.properties.integration = integration;
}
if (!integration) {
await onExit();
$p.log.error("No integration selected");
process.exit(1);
}
Expand All @@ -152,15 +176,18 @@ async function action(options: { template?: string; dir?: string; integration?:

const choices = templates.filter((t) => t.integration === integration);
if (choices.length === 0) {
await onExit();
$p.log.error(`No templates found for "${color.cyan(String(integration))}"`);
process.exit(1);
} else if (choices.length > 1) {
$t.properties.at = "template";
const selected_template = await $p.select({
message: "Pick a template",
options: choices.map((t) => ({ value: t.key, label: t.title, hint: t.description })),
});

if ($p.isCancel(selected_template)) {
await onExit();
process.exit(1);
}

Expand All @@ -170,10 +197,12 @@ async function action(options: { template?: string; dir?: string; integration?:
}
}
if (!template) {
await onExit();
$p.log.error("No template selected");
process.exit(1);
}

$t.properties.template = template.key;
const ctx = { template, dir: downloadOpts.dir, name };

{
Expand All @@ -182,6 +211,8 @@ async function action(options: { template?: string; dir?: string; integration?:
$p.log.warn(color.dim("[DEV] Using local ref: ") + color.yellow(given));
},
});
$t.properties.ref = ref;
$t.capture("used");

const prefix =
template.ref === true
Expand All @@ -191,7 +222,6 @@ async function action(options: { template?: string; dir?: string; integration?:
: "";
const url = `${template.path}${prefix}`;

//console.log("url", url);
const s = $p.spinner();
await s.start("Downloading template...");
try {
Expand Down Expand Up @@ -234,8 +264,10 @@ async function action(options: { template?: string; dir?: string; integration?:
});

if ($p.isCancel(install)) {
await onExit();
process.exit(1);
} else if (install) {
$t.properties.install = true;
const install_cmd = template.scripts?.install || "npm install";

const s = $p.spinner();
Expand All @@ -259,6 +291,7 @@ async function action(options: { template?: string; dir?: string; integration?:
await template.postinstall(ctx);
}
} else {
$t.properties.install = false;
await $p.stream.warn(
(async function* () {
yield* typewriter(
Expand Down Expand Up @@ -291,5 +324,6 @@ If you need help, check ${color.cyan("https://docs.bknd.io")} or join our Discor
})(),
);

$t.capture("complete");
$p.outro(color.green("Setup complete."));
}
21 changes: 18 additions & 3 deletions app/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,36 @@ import { Command } from "commander";
import color from "picocolors";
import * as commands from "./commands";
import { getVersion } from "./utils/sys";
import { capture, flush, init } from "cli/utils/telemetry";
const program = new Command();

export async function main() {
await init();
capture("start");

const version = await getVersion();
program
.name("bknd")
.description(color.yellowBright("⚡") + " bknd cli " + color.bold(color.cyan(`v${version}`)))
.version(version);
.version(version)
.hook("preAction", (thisCommand, actionCommand) => {
capture(`cmd_${actionCommand.name()}`);
})
.hook("postAction", async () => {
await flush();
});

// register commands
for (const command of Object.values(commands)) {
command(program);
}

program.parse();
await program.parseAsync();
}

main().then(null).catch(console.error);
main()
.then(null)
.catch(async (e) => {
await flush();
console.error(e);
});
75 changes: 75 additions & 0 deletions app/src/cli/utils/telemetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { PostHog } from "posthog-js-lite";
import { getVersion } from "cli/utils/sys";
import { $console, env } from "core";

type Properties = { [p: string]: any };

let posthog: PostHog | null = null;
let version: string | null = null;

const enabled = env("cli_telemetry");

export async function init() {
try {
if (!enabled) {
$console.debug("Telemetry disabled");
return;
}

$console.debug("Init telemetry");
if (!posthog) {
posthog = new PostHog(process.env.POSTHOG_KEY!, {
host: process.env.POSTHOG_HOST!,
disabled: !enabled,
});
}
version = await getVersion();
} catch (e) {
$console.debug("Failed to initialize telemetry", e);
}
}

export function client(): PostHog {
if (!posthog) {
throw new Error("PostHog client not initialized. Call init() first.");
}

return posthog;
}

export function capture(event: string, properties: Properties = {}): void {
try {
if (!enabled) return;

const name = `cli_${event}`;
const props = {
...properties,
version: version!,
};
$console.debug("Capture", name, props);
client().capture(name, props);
} catch (e) {
$console.debug("Failed to capture telemetry", e);
}
}

export function createScoped(scope: string, p: Properties = {}) {
const properties = p;
const _capture = (event: string, props: Properties = {}) => {
return capture(`${scope}_${event}`, { ...properties, ...props });
};
return { capture: _capture, properties };
}

export async function flush() {
try {
if (!enabled) return;

$console.debug("Flush telemetry");
if (posthog) {
await posthog.flush();
}
} catch (e) {
$console.debug("Failed to flush telemetry", e);
}
}
11 changes: 9 additions & 2 deletions app/src/core/env.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type Env = {};

export const is_toggled = (given: unknown): boolean => {
return typeof given === "string" ? [1, "1", "true"].includes(given) : Boolean(given);
export const is_toggled = (given: unknown, fallback?: boolean): boolean => {
return typeof given === "string" ? [1, "1", "true"].includes(given) : Boolean(given || fallback);
};

export function isDebug(): boolean {
Expand Down Expand Up @@ -34,6 +34,13 @@ const envs = {
return typeof v === "string" ? v : undefined;
},
},
// cli telemetry
cli_telemetry: {
key: "BKND_CLI_TELEMETRY",
validate: (v: unknown) => {
return is_toggled(v, true);
},
},
// module manager debug: {
modules_debug: {
key: "BKND_MODULES_DEBUG",
Expand Down
3 changes: 3 additions & 0 deletions bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"oauth4webapi": "^2.11.1",
"object-path-immutable": "^4.1.2",
"picocolors": "^1.1.1",
"posthog-js-lite": "^3.4.2",
"radix-ui": "^1.1.3",
"swr": "^2.3.3",
},
Expand Down Expand Up @@ -2694,6 +2695,8 @@

"postgres-range": ["postgres-range@1.1.4", "", {}, "sha512-i/hbxIE9803Alj/6ytL7UHQxRvZkI9O4Sy+J3HGc4F4oo/2eQAjTSNJ0bfxyse3bH0nuVesCk+3IRLaMtG3H6w=="],

"posthog-js-lite": ["posthog-js-lite@3.4.2", "", {}, "sha512-YYXZORZHDmgD4OnSCyfRgPnBdHPxFDtgybBGmrQV5fOyCd0Bc12fYs2KlGwrRkIs2jYftT3CdfS0jTNo5RjB7Q=="],

"prelude-ls": ["prelude-ls@1.1.2", "", {}, "sha512-ESF23V4SKG6lVSGZgYNpbsiaAkdab6ZgOxe52p7+Kid3W3u3bxR4Vfd/o21dmN7jSt0IwgZ4v5MUd26FEtXE9w=="],

"prettier": ["prettier@1.19.1", "", { "bin": { "prettier": "./bin-prettier.js" } }, "sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew=="],
Expand Down