Welcome to the first chapter of the logout feature tutorial! Before we write any complex logic for signing a user out, we first need to tell our application that this command simply exists.
Imagine you are building a restaurant menu. Before a chef cooks a meal, the item must be listed on the paper menu so customers know it's available.
The Command Definition is exactly like that menu entry. It serves two main purposes:
Our goal for this chapter is simple: We want the user to be able to type logout in their terminal and see a description, but we want to ensure this command is hidden if a specific system setting disables it.
Let's look at how we define this command in index.ts. We will break the code down into small, digestible pieces.
First, we define the basics: the name the user types and a helpful description.
// File: index.ts
export default {
type: 'local-jsx',
name: 'logout',
description: 'Sign out from your Anthropic account',
// ... more code coming up
Explanation:
name: This is the keyword the user types (e.g., $ my-app logout).description: This text appears in the help menu (e.g., when the user types --help).Sometimes, you might want to hide a command. For example, maybe an administrator wants to prevent users from logging out on a shared machine.
// ... inside the export object
isEnabled: () => !isEnvTruthy(process.env.DISABLE_LOGOUT_COMMAND),
Explanation:
isEnabled: This is a function that returns true or false.process.env.DISABLE_LOGOUT_COMMAND is set to "true", this function returns false, and the "logout" command effectively disappears from the menu.This is the most important part. We link the definition to the actual code that performs the logout.
// ... inside the export object
load: () => import('./logout.js'),
} satisfies Command
Explanation:
load: This function uses a dynamic import()../logout.js is NOT read when the application starts. It is only read and executed when the user actually runs the logout command. This makes the application start up much faster.satisfies Command: This ensures our object follows the strict rules of a Command structure (TypeScript magic to prevent typos).To understand how the Command Definition fits into the bigger picture, let's visualize the flow.
The following diagram shows what happens when the CLI application starts up and when a user interacts with it.
The index.ts file acts as a lightweight proxy. It doesn't know how to log the user out; it only knows where to find the instruction manual (the logout.js file).
index.ts is so small, the Main CLI App can scan hundreds of commands instantly without slowing down.isEnabled before load, we prevent unauthorized code from even being fetched into memory.
When the load() function is finally called, it triggers the Logout Workflow Orchestrator, which is the subject of our next chapter.
Here is the complete file context again. You can now see how the identity, the visibility check, and the lazy loading come together to form the interface.
// File: index.ts
import type { Command } from '../../commands.js'
import { isEnvTruthy } from '../../utils/envUtils.js'
export default {
type: 'local-jsx',
name: 'logout',
description: 'Sign out from your Anthropic account',
isEnabled: () => !isEnvTruthy(process.env.DISABLE_LOGOUT_COMMAND),
load: () => import('./logout.js'),
} satisfies Command
In this chapter, we learned how to create a Command Definition. We covered:
isEnabled to conditionally hide the command based on environment variables.load to optimize performance by fetching the heavy logic only when needed.Now that we have defined what the command is, it's time to define what it actually does.
Next Chapter: Logout Workflow Orchestrator
Generated by Code IQ