Skip to content

Commit f6ca05d

Browse files
committed
Updating the command run implementation to perform error check of projectRoot before running run method.
1 parent 748616e commit f6ca05d

5 files changed

Lines changed: 29 additions & 9 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nexical/cli-core",
3-
"version": "0.1.8",
3+
"version": "0.1.9",
44
"type": "module",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/BaseCommand.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,19 @@ export abstract class BaseCommand implements CommandInterface {
3232
this.projectRoot = await findProjectRoot(this.cli.name, process.cwd());
3333
}
3434

35-
const requiresProject = (this.constructor as any).requiresProject;
35+
if (this.projectRoot) {
36+
this.config = await loadConfig(this.cli.name, this.projectRoot);
37+
logger.debug(`Loaded config from ${this.projectRoot}`);
38+
}
39+
}
3640

41+
async runInit(options: any): Promise<void> {
42+
const requiresProject = (this.constructor as any).requiresProject;
3743
if (requiresProject && !this.projectRoot) {
3844
this.error(`This command requires to be run within an app project (${this.cli.name}.yml not found).`, 1);
39-
return; // TS doesn't know error exits
40-
}
41-
42-
if (this.projectRoot) {
43-
this.config = await loadConfig(this.cli.name, this.projectRoot);
44-
// logger.debug(`Loaded config from ${this.projectRoot}`);
45+
return;
4546
}
47+
await this.run(options);
4648
}
4749

4850
abstract run(options: any): Promise<void>;

src/CLI.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ export class CLI {
315315
try {
316316
const instance = new CommandClass(this, options);
317317
await instance.init();
318-
await instance.run(options);
318+
await instance.runInit(options);
319319
} catch (e: any) {
320320
console.error(pc.red(e.message));
321321
if (options.debug) console.error(e.stack);

test/unit/core/BaseCommand.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,23 @@ describe('BaseCommand', () => {
8484
const cli = new CLI({ commandName: 'app' });
8585
const cmd = new ProjectRequiredCommand(cli, {});
8686
await cmd.init();
87+
await cmd.runInit({});
8788

8889
expect(consoleLogSpy).toHaveBeenCalledWith(pc.red('✖ This command requires to be run within an app project (app.yml not found).'));
8990
expect(process.exit).toHaveBeenCalledWith(1);
9091
});
9192

93+
it('should execute run method via runInit', async () => {
94+
const cli = new CLI({ commandName: 'app' });
95+
const cmd = new TestCommand(cli);
96+
const runSpy = vi.spyOn(cmd, 'run');
97+
98+
await cmd.init();
99+
await cmd.runInit({});
100+
101+
expect(runSpy).toHaveBeenCalled();
102+
});
103+
92104
it('should log success', () => {
93105
const cli = new CLI({ commandName: 'app' });
94106
const cmd = new TestCommand(cli);

test/unit/core/ConfigLoading.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@ describe('Config Loading & Error Messaging', () => {
3838
(ConfigUtils.loadConfig as any).mockResolvedValue({});
3939

4040
await command.init();
41+
// project root finding happens in init, verification happens in runInit
42+
// but this test marks requiresProject=true, so if we don't call runInit, we only verify init logic.
43+
// The original test verified init logic calling loadConfig.
4144

4245
expect(ConfigUtils.findProjectRoot).toHaveBeenCalledWith('astrical', expect.any(String));
46+
// loadConfig is called in init if root is found
4347
expect(ConfigUtils.loadConfig).toHaveBeenCalledWith('astrical', '/some/path');
4448
});
4549

@@ -50,6 +54,7 @@ describe('Config Loading & Error Messaging', () => {
5054
(ConfigUtils.findProjectRoot as any).mockResolvedValue(null);
5155

5256
await command.init();
57+
await command.runInit({});
5358

5459
expect(consoleLogSpy).toHaveBeenCalledWith(
5560
expect.stringContaining(pc.red('✖ This command requires to be run within an app project (astrical.yml not found).'))
@@ -64,6 +69,7 @@ describe('Config Loading & Error Messaging', () => {
6469
(ConfigUtils.findProjectRoot as any).mockResolvedValue(null);
6570

6671
await command.init();
72+
await command.runInit({});
6773

6874
expect(consoleLogSpy).toHaveBeenCalledWith(
6975
expect.stringContaining(pc.red('✖ This command requires to be run within an app project (app.yml not found).'))

0 commit comments

Comments
 (0)