Skip to content

Commit a548c6f

Browse files
committed
Add support for intercepting the command line interface with the command:before hook
1 parent 477e557 commit a548c6f

File tree

6 files changed

+26
-5
lines changed

6 files changed

+26
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11

22
# Changelog
33

4+
## v1.2.0 (2022-07-02)
5+
6+
### Features
7+
8+
* Added support for intercepting the command line interface with the `command:before` hook.
9+
410
## v1.1.2 (2022-06-30)
511

612
### Bug Fixes

docs/Cheatsheet.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ app.addAppHook('command:before', async (app, args) => {
584584
```
585585

586586
Useful for reconfiguring the application before running a command or to modify the behavior of a command. Passed the
587-
application object and command arguments.
587+
application object and command arguments. Can return `true` to intercept the command line interface.
588588

589589
### server:start
590590

src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class CLI {
6161

6262
const app = this._app.deref();
6363
if (app === undefined) return;
64-
await app.hooks.commandBefore(app, commandArgs);
64+
if ((await app.hooks.commandBefore(app, commandArgs)) === true) return;
6565

6666
const parsed = nopt({help: Boolean}, {h: '--help'}, commandArgs);
6767
const argv = parsed.argv;

src/hooks.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ export class Hooks {
2020
* Run `command:start` hook with dependencies. Note that this method is EXPERIMENTAL and might change without
2121
* warning!
2222
*/
23-
async commandBefore(app: MojoApp, commandArgs: string[]): Promise<void> {
24-
await this.runHook('command:before', app, commandArgs);
23+
async commandBefore(app: MojoApp, commandArgs: string[]): Promise<any> {
24+
const result = await this.runHook('command:before', app, commandArgs);
2525
await this._appStart(app);
26+
return result;
2627
}
2728

2829
/**

test/command-app.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ t.test('Command app', async t => {
5454
'Hello Mojo!' +
5555
'server:stop: developmentcommand:after: developmentapp:stop: development'
5656
);
57+
58+
process.env.MOJO_COMMAND_TEST = '1';
59+
const output2 = await captureOutput(async () => {
60+
await app.cli.start();
61+
});
62+
t.match(output2.toString(), 'command:before: skip cliapp:start: development');
63+
delete process.env.MOJO_COMMAND_TEST;
5764
});
5865

5966
await t.test('Custom command', async t => {
@@ -98,7 +105,7 @@ t.test('Command app', async t => {
98105
const output2 = await captureOutput(async () => {
99106
await app.cli.start('eval', 'await 100 + 924');
100107
});
101-
t.equal(output2.toString(), 'app:start: developmentapp:stop: development');
108+
t.equal(output2.toString(), '');
102109

103110
const output3 = await captureOutput(async () => {
104111
await app.cli.start('eval', '-v', 'await 100 + 924');

test/support/js/command-app/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ app.addAppHook('command:before', (app, args) => {
1212
}
1313
});
1414

15+
app.addAppHook('command:before', async () => {
16+
if (process.env.MOJO_COMMAND_TEST !== undefined) {
17+
process.stdout.write(Buffer.from(`command:before: skip cli`));
18+
return true;
19+
}
20+
});
21+
1522
app.addAppHook('app:start', app => {
1623
const mode = app.mode;
1724
process.stdout.write(Buffer.from(`app:start: ${mode}`));

0 commit comments

Comments
 (0)