@@ -13,6 +13,7 @@ Use `src/projects/list.ts` as the reference pattern for all refactored commands.
1313Create a new file in ` src/projects/<command-name>.ts ` that consolidates all the existing command files.
1414
1515** Key elements:**
16+
1617- Import ` yargs ` , ` ensure ` , ` build ` , ` Logger ` , and options from ` ../options `
1718- Define a ` <CommandName>Options ` type that picks required fields from ` Opts `
1819- Create an ` options ` array with the required options (e.g., ` [o.workflow, o.workspace, o.workflowMappings] ` )
@@ -24,6 +25,7 @@ Create a new file in `src/projects/<command-name>.ts` that consolidates all the
2425- Export a named ` handler ` function that takes ` (options: <CommandName>Options, logger: Logger) `
2526
2627** Example structure:**
28+
2729``` typescript
2830import yargs from ' yargs' ;
2931import { Workspace } from ' @openfn/project' ;
@@ -88,23 +90,26 @@ export const projectsCommand = {
8890Make three changes:
8991
9092** a) Remove the old import** (if it exists):
93+
9194``` typescript
9295// Remove: import commandName from './<command>/handler';
9396```
9497
9598** b) Add to CommandList type:**
99+
96100``` typescript
97101export type CommandList =
98102 | ' apollo'
99103 // ...
100104 | ' project-list'
101105 | ' project-version'
102- | ' project-<command-name>' // Add this line
106+ | ' project-<command-name>' // Add this line
103107 | ' test'
104108 | ' version' ;
105109```
106110
107111** c) Add to handlers object:**
112+
108113``` typescript
109114const handlers = {
110115 // ...
@@ -121,6 +126,7 @@ If the old command was referenced elsewhere in the handlers object (like `projec
121126### 5. Delete the Old Command Folder
122127
123128Remove the old command directory:
129+
124130``` bash
125131rm -rf packages/cli/src/< command-name>
126132```
@@ -142,6 +148,7 @@ import checkoutCommand from './checkout/command';
142148```
143149
144150The command will be registered with yargs:
151+
145152``` typescript
146153.command (projectsCommand )
147154.command (mergeCommand ) // Top-level alias
@@ -159,18 +166,21 @@ This is different from the `install`/`repo install` pattern, where both entries
159166## Common Patterns
160167
161168### Options to Use
169+
162170- Always include ` o.workspace ` for project-related commands
163171- Use ` o.workflow ` for workflow-specific operations
164172- Include ` o.json ` if the command supports JSON output
165173- Include ` o.log ` for commands that need detailed logging
166174
167175### Handler Pattern
176+
168177- Use ` new Workspace(options.workspace) ` to access the workspace
169178- Check ` workspace.valid ` before proceeding
170- - Use ` workspace.getActiveProject () ` to get the current project
179+ - Use ` workspace.getTrackedProject () ` to get the current project
171180- Use appropriate logger methods: ` logger.info() ` , ` logger.error() ` , ` logger.success() `
172181
173182### Testing Pattern
183+
174184If the old command has tests, they need to be refactored:
175185
1761861 . ** Create new test file** : Move from ` test/<command>/handler.test.ts ` to ` test/projects/<command>.test.ts `
@@ -181,6 +191,7 @@ If the old command has tests, they need to be refactored:
1811914 . ** Delete old test folder** : Remove ` test/<command> ` directory
182192
183193** Example changes:**
194+
184195``` typescript
185196// Before:
186197import mergeHandler from ' ../../src/merge/handler' ;
@@ -194,6 +205,7 @@ await mergeHandler({ command: 'project-merge', ... }, logger);
194205## Checklist
195206
196207### Basic Refactoring
208+
197209- [ ] Create new file in ` src/projects/<command-name>.ts `
198210- [ ] Define ` <CommandName>Options ` type
199211- [ ] Export default command object with ` ensure('project-<command-name>', options) `
@@ -206,13 +218,15 @@ await mergeHandler({ command: 'project-merge', ... }, logger);
206218- [ ] Delete old command folder
207219
208220### Testing (if applicable)
221+
209222- [ ] Create new test file in ` test/projects/<command-name>.test.ts `
210223- [ ] Update import to use ` { handler } from '../../src/projects/<command-name>' `
211224- [ ] Update all ` command: '<command>' ` to ` command: 'project-<command>' ` in test cases
212225- [ ] Delete old test folder ` test/<command> `
213226- [ ] Run tests to verify they pass
214227
215228### Additional Steps for Top-Level Aliasing
229+
216230- [ ] Import command directly in ` src/cli.ts ` (e.g., ` import mergeCommand from './projects/merge' ` )
217231- [ ] Register the imported command with ` .command(mergeCommand) `
218232- [ ] Verify NO duplicate entries in ` src/commands.ts ` - only ` project-<command-name> ` should exist, not ` <command-name> `
0 commit comments