Skip to content

Commit

Permalink
feat: support tracking for all commands
Browse files Browse the repository at this point in the history
  • Loading branch information
field123 committed Sep 21, 2023
1 parent fb636c9 commit 6f91643
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ConfigCommandData,
ConfigCommandError,
} from "./config.types"
import { trackCommandHandler } from "../../util/track-command-handler"

export function configClearCommand(store: Conf): void {
store.clear()
Expand Down Expand Up @@ -36,7 +37,7 @@ export function createConfigCommand(
})
.help()
},
handler: handleErrors(createConfigCommandHandler(ctx)),
handler: handleErrors(trackCommandHandler(ctx, createConfigCommandHandler)),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import {
} from "./feedback.types"
import open from "open"
import { Feedback } from "../ui/feedback/feedback"
import { trackCommandHandler } from "../../util/track-command-handler"
export function createFeedbackCommand(
ctx: CommandContext
): yargs.CommandModule<{}, FeedbackCommandArguments> {
return {
command: "feedback",
describe: "Feedback to the Composable CLI",
handler: handleErrors(createFeedbackCommandHandler(ctx)),
handler: handleErrors(
trackCommandHandler(ctx, createFeedbackCommandHandler)
),
}
}

Expand Down
32 changes: 17 additions & 15 deletions packages/composable-cli/src/commands/generate/d2c/d2c-command.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import yargs from "yargs"
import { logging, schema, tags } from "@angular-devkit/core"
import { createConsoleLogger, ProcessOutput } from "@angular-devkit/core/node"
import { createConsoleLogger } from "@angular-devkit/core/node"
import * as ansiColors from "ansi-colors"
import { NodeWorkflow } from "@angular-devkit/schematics/tools"

Expand All @@ -27,11 +27,10 @@ import React from "react"
import { D2CGenerated } from "../../ui/generate/d2c-generated"
import { getStore } from "../../../lib/stores/get-store"
import { selectStoreById } from "../../store/store-command"
import { trackCommandHandler } from "../../../util/track-command-handler"

export function createD2CCommand(
ctx: CommandContext,
stdout: ProcessOutput,
stderr: ProcessOutput
ctx: CommandContext
): yargs.CommandModule<{}, D2CCommandArguments> {
return {
command: "d2c [name]",
Expand All @@ -56,7 +55,7 @@ export function createD2CCommand(
"strip-aliased": true,
})
},
handler: handleErrors(createD2CCommandHandler(ctx, stdout, stderr)),
handler: handleErrors(trackCommandHandler(ctx, createD2CCommandHandler)),
}
}

Expand All @@ -68,9 +67,7 @@ function resolveD2CCollectionName(nodeEnv: string): string {
}

export function createD2CCommandHandler(
ctx: CommandContext,
stdout: ProcessOutput,
stderr: ProcessOutput
ctx: CommandContext
): CommandHandlerFunction<
D2CCommandData,
D2CCommandError,
Expand All @@ -85,13 +82,18 @@ export function createD2CCommandHandler(
parseArgs(args)

/** Create the DevKit Logger used through the CLI. */
const logger = createConsoleLogger(!!cliOptions.verbose, stdout, stderr, {
info: (s) => s,
debug: (s) => s,
warn: (s) => colors.bold.yellow(s),
error: (s) => colors.bold.red(s),
fatal: (s) => colors.bold.red(s),
})
const logger = createConsoleLogger(
!!cliOptions.verbose,
ctx.stdout,
ctx.stderr,
{
info: (s) => s,
debug: (s) => s,
warn: (s) => colors.bold.yellow(s),
error: (s) => colors.bold.red(s),
fatal: (s) => colors.bold.red(s),
}
)

logger.debug(`Cli Options: ${JSON.stringify(cliOptions)}`)
logger.debug(`Schematic Options: ${JSON.stringify(schematicOptions)}`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import {
GenerateCommandData,
GenerateCommandError,
} from "./generate.types"
import { ProcessOutput } from "@angular-devkit/core/node"
import { createD2CCommand } from "./d2c/d2c-command"
import { hasActiveStore } from "../../util/active-store"
import { createSetStoreCommandHandler } from "../store/store-command"
import { isAuthenticated } from "../../util/check-authenticated"
import { trackCommandHandler } from "../../util/track-command-handler"

export function createGenerateCommand(
ctx: CommandContext,
stdout: ProcessOutput,
stderr: ProcessOutput
ctx: CommandContext
): yargs.CommandModule<{}, GenerateCommandArguments> {
return {
command: "generate",
Expand All @@ -25,7 +23,7 @@ export function createGenerateCommand(
return yargs
.middleware(createAuthenticationCheckerMiddleware(ctx))
.middleware(createActiveStoreMiddleware(ctx))
.command(createD2CCommand(ctx, stdout, stderr))
.command(createD2CCommand(ctx))
.option("name", { type: "string", default: null })
.option("interactive", { type: "boolean", default: true })
.option("debug", { type: "boolean", default: null })
Expand All @@ -45,7 +43,9 @@ export function createGenerateCommand(
"strip-aliased": true,
})
},
handler: handleErrors(createGenerateCommandHandler(ctx, stdout, stderr)),
handler: handleErrors(
trackCommandHandler(ctx, createGenerateCommandHandler)
),
}
}

Expand Down Expand Up @@ -100,9 +100,7 @@ export function createActiveStoreMiddleware(
}

export function createGenerateCommandHandler(
_ctx: CommandContext,
_stdout: ProcessOutput,
_stderr: ProcessOutput
_ctx: CommandContext
): CommandHandlerFunction<
GenerateCommandData,
GenerateCommandError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "./insights.types"
import { promptOptInProductInsights } from "../../lib/insights/opt-in-product-insights-middleware"
import { optInsights } from "../../util/has-opted-insights"
import { trackCommandHandler } from "../../util/track-command-handler"

export function createInsightsCommand(
ctx: CommandContext
Expand All @@ -24,7 +25,9 @@ export function createInsightsCommand(
})
.help()
},
handler: handleErrors(createInsightsCommandHandler(ctx)),
handler: handleErrors(
trackCommandHandler(ctx, createInsightsCommandHandler)
),
}
}

Expand Down
13 changes: 5 additions & 8 deletions packages/composable-cli/src/commands/login/login-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { isAuthenticated } from "../../util/check-authenticated"
import React from "react"
import { WelcomeNote } from "../ui/login/welcome-note"
import { render } from "ink"
import { trackCommandHandler } from "../../util/track-command-handler"

/**
* Region prompts
Expand Down Expand Up @@ -75,7 +76,7 @@ export function createLoginCommand(
"strip-aliased": true,
})
},
handler: handleErrors(createLoginCommandHandler(ctx)),
handler: handleErrors(trackCommandHandler(ctx, createLoginCommandHandler)),
}
}

Expand All @@ -89,7 +90,9 @@ export function createAuthenticationMiddleware(
return
}

return handleErrors(createLoginCommandHandler(ctx))(argv)
return handleErrors(trackCommandHandler(ctx, createLoginCommandHandler))(
argv
)
}
}

Expand Down Expand Up @@ -119,12 +122,6 @@ export function createLoginCommandHandler(

const spinner = ora("Authenticating").start()

if (ctx.posthog) {
ctx.posthog.postHogCapture({
event: "composable cli login",
})
}

const result = await authenticateUserPassword(
store,
apiHost,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import { renderInk } from "../../lib/ink/render-ink"
import React from "react"
import { LogoutNote } from "../ui/logout/logout-note"
import { handleClearCredentials } from "../../util/conf-store/store-credentials"
import { trackCommandHandler } from "../../util/track-command-handler"

export function createLogoutCommand(
ctx: CommandContext
): yargs.CommandModule<{}, LogoutCommandArguments> {
return {
command: "logout",
describe: "Logout of the Composable CLI",
handler: handleErrors(createLogoutCommandHandler(ctx)),
handler: handleErrors(trackCommandHandler(ctx, createLogoutCommandHandler)),
}
}

Expand Down
7 changes: 5 additions & 2 deletions packages/composable-cli/src/commands/store/store-command.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
checkIsErrorResponse,
resolveEPCCErrorMessage,
} from "../../util/epcc-error"
import { trackCommandHandler } from "../../util/track-command-handler"

export function createStoreCommand(
ctx: CommandContext
Expand All @@ -38,7 +39,7 @@ export function createStoreCommand(
.command(createSetStoreCommand(ctx))
.help("h")
},
handler: handleErrors(createStoreCommandHandler(ctx)),
handler: handleErrors(trackCommandHandler(ctx, createStoreCommandHandler)),
}
}

Expand All @@ -56,7 +57,9 @@ export function createSetStoreCommand(
})
.help()
},
handler: handleErrors(createSetStoreCommandHandler(ctx)),
handler: handleErrors(
trackCommandHandler(ctx, createSetStoreCommandHandler)
),
}
}

Expand Down
5 changes: 4 additions & 1 deletion packages/composable-cli/src/composable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export async function main({
stderr = process.stderr,
}: MainOptions): Promise<1 | 0> {
try {
commandContext.stdout = stdout
commandContext.stderr = stderr

await yargs(hideBin(argv))
.middleware(createUUIDMiddleware(commandContext))
.middleware(createOptInProductInsightsMiddleware(commandContext))
Expand All @@ -41,7 +44,7 @@ export async function main({
.command(createFeedbackCommand(commandContext))
.command(createConfigCommand(commandContext))
.command(createStoreCommand(commandContext))
.command(createGenerateCommand(commandContext, stdout, stderr))
.command(createGenerateCommand(commandContext))
.command(createInsightsCommand(commandContext))
.option("verbose", {
alias: "v",
Expand Down
3 changes: 3 additions & 0 deletions packages/composable-cli/src/types/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fetch from "node-fetch"
import yargs from "yargs"
import type { PostHog } from "posthog-node"
import { createPostHogCapture } from "../lib/insights/capture-posthog"
import { ProcessOutput } from "@angular-devkit/core/node"

export type CommandResult<TData, TError> =
| {
Expand All @@ -21,6 +22,8 @@ export type CommandContext = {
client: PostHog
postHogCapture: Awaited<ReturnType<typeof createPostHogCapture>>
}
stdout: ProcessOutput
stderr: ProcessOutput
}

export type CommandHandlerFunction<
Expand Down
2 changes: 2 additions & 0 deletions packages/composable-cli/src/util/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ export function createCommandContext(): CommandContext {
return {
store,
requester: fetch,
stdout: process.stdout,
stderr: process.stderr,
}
}
31 changes: 31 additions & 0 deletions packages/composable-cli/src/util/track-command-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { CommandContext, CommandHandlerFunction } from "../types/command"
import { ProcessOutput } from "@angular-devkit/core/node"

export function trackCommandHandler<
TData,
TError,
TCommandArguments extends Record<string, any>
>(
ctx: CommandContext,
handler: (
ctx: CommandContext,
stdout?: ProcessOutput,
stderr?: ProcessOutput
) => CommandHandlerFunction<TData, TError, TCommandArguments>
): CommandHandlerFunction<TData, TError, TCommandArguments> {
return (args) => {
console.log(args)
if (ctx.posthog) {
// Making sure to filter out any properties that might contain sensitive information
const { _, $0, password, username, ...rest } = args
ctx.posthog.postHogCapture({
event: `composable cli command ${args._.join(" ")}`,
properties: {
...rest,
},
})
}

return handler(ctx)(args)
}
}

0 comments on commit 6f91643

Please sign in to comment.