Welcome to the first chapter of our tutorial! In this project, we are building a set of tools to interact with GitHub. Before we start running complex commands, we need a way to understand the environment our code is running in.
Imagine you are a car mechanic. Before you start taking apart an engine, you look at the dashboard. You check the sensors: Is there gas in the tank? Is the "Check Engine" light on?
We need a similar "sensor" for our application. We want to collect Telemetry Data. This isn't about spying on the user; it's about understanding the health of their setup.
The Use Case: We want to know two simple things about our user's computer:
gh) installed?If we know this, we can track how many of our users are successfully setting up their environment.
We have created a helper function called getGhAuthStatus. You don't need to pass it any complicated data; you just ask it for the status, and it returns a simple string.
Here is how you would use it in your code:
import { getGhAuthStatus } from './ghAuthStatus'
async function checkHealth() {
// Ask the sensor for the status
const status = await getGhAuthStatus()
console.log(`User status: ${status}`)
}
The function returns one of three specific text strings (we call these types):
'not_installed': The user doesn't even have the GitHub tool.'not_authenticated': The tool is there, but they aren't logged in.'authenticated': Everything is ready to go!So, what is happening under the hood? It acts like a security guard performing a two-step check.
Here is a diagram showing the conversation between our code and the system:
Let's look at the actual code in ghAuthStatus.ts. We will break it down into small pieces.
First, we need to see if the tool exists. To do this, we use a helper utility called which.
(Note: We will learn how to build the which utility in Tool Availability Check)
import { which } from '../which.js'
export async function getGhAuthStatus() {
// Check if 'gh' is in the system path
const ghPath = await which('gh')
// If path is empty, the tool is missing
if (!ghPath) {
return 'not_installed'
}
// ... continued below
Explanation: If ghPath comes back empty, we stop immediately. There is no point checking for a login if the software isn't there!
If the tool is installed, we run a command to check the login token. We use a library called execa to run this command safely.
(Note: We will explore safe command running in Secure Subprocess Execution)
import { execa } from 'execa'
// ... inside getGhAuthStatus ...
// Run 'gh auth token' to check login validity
const { exitCode } = await execa('gh', ['auth', 'token'], {
stdout: 'ignore', // Don't show the actual token!
stderr: 'ignore', // Don't show errors
reject: false, // Don't crash if it fails
})
Explanation:
gh auth token. This command specifically looks at local configuration files. It does not make a slow network request to GitHub servers. This makes our telemetry sensor very fast.stdout: 'ignore'. This is a security feature. We only care if the command worked (the exitCode), we don't want to accidentally read or log the user's secret password (token).Finally, we translate the computer code (exit code) into a human-readable status.
// Exit code 0 means success (logged in)
// Anything else means not logged in
return exitCode === 0 ? 'authenticated' : 'not_authenticated'
}
In this chapter, we built a Telemetry Data Source. It acts like a car's dashboard sensor, quickly telling us if the GitHub CLI is installed and configured correctly without exposing sensitive user data or slowing down the application.
We touched on a few concepts that are so important they have their own chapters!
But first, let's look deeper into what defines the "Authenticated" state and how we manage GitHub-specific logic.
Next Chapter: GitHub Authentication State
Generated by Code IQ