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

feat(cli): fix event name and provide additional properties #1519

Merged
merged 1 commit into from
Oct 29, 2024
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
5 changes: 5 additions & 0 deletions .changeset/tricky-trainers-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@bigcommerce/create-catalyst": minor
---

Ensures a command is always passed as an event name and provides a allowlist of args to pass as additional properties to the event.
56 changes: 51 additions & 5 deletions packages/create-catalyst/src/hooks/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,59 @@ import { Telemetry } from '../utils/telemetry/telemetry';

const telemetry = new Telemetry();

const allowlistArguments = ['--gh-ref', '--repository', '--project-name'];

function parseArguments(args: string[]) {
return args.reduce<Record<string, string>>((result, arg, index, array) => {
if (arg.includes('=')) {
const [key, value] = arg.split('=');

if (allowlistArguments.includes(key)) {
return {
...result,
[key]: value,
};
}
}

if (allowlistArguments.includes(arg)) {
const nextValue =
array[index + 1] && !array[index + 1].startsWith('--') ? array[index + 1] : null;

if (nextValue && !nextValue.includes('--')) {
return {
...result,
[arg]: nextValue,
};
}
}

return result;
}, {});
}

export const telemetryPreHook = async (command: Command) => {
// If the binary is ran without a command, the command.args will be an empty array.
// When running `npm create @bigcommerce/catalyst`, npm runs the binary with no args passed,
// but the program defaults to the `create` command.
const [commandName = 'create'] = command.args;
// @ts-expect-error _name is a private property
const availableCommands = command.commands.map((cmd) => cmd._name); // eslint-disable-line @typescript-eslint/no-unsafe-return, no-underscore-dangle

const [commandName = 'create', ...args] = command.args;

// When running `npm create @bigcommerce/catalyst`, the command defaults to
// the `create` command but commander doesn't pass it as part of the arguments.
// We need to handle this case separately.
if (!availableCommands.includes(commandName)) {
// Return the await to get a proper stack trace.
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
return await telemetry.track('create', {
...parseArguments(args),
});
}

await telemetry.track(commandName, {});
// Return the await to get a proper stack trace.
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
return await telemetry.track(commandName, {
...parseArguments(args),
});
};

export const telemetryPostHook = async () => {
Expand Down
Loading