Welcome back! In the previous chapter, Built-in Plugin Initialization, we discovered the "Switchboard"โa specific function called initBuiltinPlugins where the application wakes up its optional features.
Now, we need to learn exactly how to plug a feature into that switchboard.
This chapter covers the Plugin Registration Pattern. This is the standard way we tell the system: "I have written a new feature, and I want you to acknowledge it."
Imagine you are hosting an exclusive party (the CLI application). You hire a security guard (the System) to manage the door.
You have a friend named "Daily Greeting" (your new plugin code). Even if "Daily Greeting" shows up at the door dressed nicely, the security guard will not let them in unless their name is explicitly written on the Guest List.
In our project:
initBuiltinPlugins function in index.ts.If you write the code but forget to register it, the system simply ignores it. It won't appear in the user's settings, and it won't run.
Let's assume you have already written the logic for a "Daily Greeting" plugin in a file named dailyGreeting.ts. Now, we need to add it to the guest list so it appears in the /plugin menu.
To follow this pattern, we perform two simple steps.
First, we need to tell the index.ts file where the plugin code lives. This is like introducing your friend to the host.
Second, we must call the registration function. This is the act of actually writing the name on the list.
We work inside index.ts. Here is how we transform the empty scaffolding into a working registry.
At the very top of index.ts, we import the specific function from our plugin file.
// --- File: index.ts ---
// 1. We import the specific "register" function from the plugin file
import { registerDailyGreeting } from './plugins/dailyGreeting';
Explanation: We are telling the system, "Go look in the plugins/dailyGreeting file and find the function named registerDailyGreeting."
Now, we go to our "Switchboard" function and call the function we just imported.
// --- File: index.ts ---
export function initBuiltinPlugins(): void {
// 2. We explicitly call the function to "sign the guest list"
registerDailyGreeting();
}
Explanation: By adding registerDailyGreeting();, we execute the code that adds the plugin to the system's internal inventory.
Once these two small lines of code are added:
/plugin page.What actually happens when that line of code runs?
initBuiltinPlugins().registerDailyGreeting() is hit.You might ask: "Why can't the system just automatically find all files in the folder?"
We use this Explicit Registration Pattern for safety and control:
index.ts.index.ts and see exactly what features are active in the build, just by reading the list.
Let's look at a simplified version of what that register function usually looks like inside the plugin file. You don't need to write this yet, but it helps to understand what you are calling.
// --- Inside the plugin file (e.g., dailyGreeting.ts) ---
export function registerDailyGreeting() {
// This is the function we imported and called!
registerBuiltinPlugin({
id: 'daily-greeting',
name: 'Daily Greeting',
enabled: false // Default state
});
}
Explanation:
registerDailyGreeting).registerBuiltinPlugin) to hand over its ID and Name.index.ts is just the coordinator, while the plugin file defines the details.In this chapter, we learned:
index.ts, the system ignores it.initBuiltinPlugins.We have now covered the Strategy (Chapter 1), the Configuration (Chapter 2), the Location (Chapter 3), and the Pattern (Chapter 4).
You are now ready to start moving code! In the final chapter, we will discuss how to safely move existing features into this new structure.
Next Chapter: Migration Scaffolding
Generated by Code IQ