Skip to content

Commit 9031336

Browse files
author
Elanthenral Elangovan
committed
ACNA-4221: fixing alias rt to display help commands
1 parent b350243 commit 9031336

5 files changed

Lines changed: 107 additions & 3 deletions

File tree

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@
6666
"commands": "./src/commands",
6767
"bin": "aio",
6868
"topicSeparator": " ",
69-
"repositoryPrefix": "<%- repo %>/blob/<%- version %>/<%- commandPath %>"
69+
"repositoryPrefix": "<%- repo %>/blob/<%- version %>/<%- commandPath %>",
70+
"hooks": {
71+
"init": "./src/hooks/init/AliasHelpHook"
72+
}
7073
},
7174
"repository": "adobe/aio-cli-plugin-runtime",
7275
"scripts": {

src/commands/runtime/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const RuntimeBaseCommand = require('../../RuntimeBaseCommand')
1616
class IndexCommand extends RuntimeBaseCommand {
1717
async run () {
1818
const help = new Help(this.config)
19-
await help.showHelp(['runtime', '--help'])
19+
await help.showHelp([this.id])
2020
}
2121
}
2222

src/hooks/init/AliasHelpHook.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2025 Adobe Inc. All rights reserved.
3+
This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License. You may obtain a copy
5+
of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
Unless required by applicable law or agreed to in writing, software distributed under
8+
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
OF ANY KIND, either express or implied. See the License for the specific language
10+
governing permissions and limitations under the License.
11+
*/
12+
13+
const { Help } = require('@oclif/core')
14+
15+
/**
16+
* Init hook to fix help display for the 'rt' alias.
17+
*
18+
* Issue: When users run "aio rt --help", oclif's help system doesn't recognize
19+
* that "rt" is an alias for "runtime" and should display all subcommands.
20+
*
21+
* Solution: This hook intercepts help requests for 'rt' and shows help for
22+
* 'runtime' instead, which properly displays all TOPICS and COMMANDS.
23+
*
24+
* @param {object} options - Hook options containing id and argv
25+
* @returns {Promise<void>}
26+
*/
27+
module.exports = async function (options) {
28+
const { id, argv } = options
29+
30+
// Check if this is a help request for the 'rt' alias
31+
const isHelpRequest = argv.includes('--help') || argv.includes('-h')
32+
const isRtCommand = id === 'rt'
33+
34+
if (isHelpRequest && isRtCommand) {
35+
// Show help for 'runtime' which includes all subcommands
36+
const help = new Help(this.config)
37+
await help.showHelp(['runtime'])
38+
// eslint-disable-next-line no-process-exit
39+
process.exit(0)
40+
}
41+
}

test/commands/runtime/index.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ describe('instance methods', () => {
5252
test('returns help file for runtime command', () => {
5353
const spy = jest.spyOn(Help.prototype, 'showHelp').mockReturnValue(true)
5454
command.config = {}
55+
command.id = 'runtime' // Set the command ID for the test
5556
return command.run().then(() => {
56-
expect(spy).toHaveBeenCalledWith(['runtime', '--help'])
57+
expect(spy).toHaveBeenCalledWith(['runtime'])
5758
})
5859
})
5960
})
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2019 Adobe Inc. All rights reserved.
3+
This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License. You may obtain a copy
5+
of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
Unless required by applicable law or agreed to in writing, software distributed under
8+
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
OF ANY KIND, either express or implied. See the License for the specific language
10+
governing permissions and limitations under the License.
11+
*/
12+
13+
const hook = require('../../../src/hooks/init/AliasHelpHook')
14+
const { Help } = require('@oclif/core')
15+
16+
describe('AliasHelpHook', () => {
17+
let exitSpy
18+
let helpSpy
19+
20+
beforeEach(() => {
21+
helpSpy = jest.spyOn(Help.prototype, 'showHelp').mockResolvedValue()
22+
exitSpy = jest.spyOn(process, 'exit').mockImplementation()
23+
})
24+
25+
afterEach(() => {
26+
helpSpy.mockRestore()
27+
exitSpy.mockRestore()
28+
})
29+
30+
test('intercepts rt with help flag', async () => {
31+
const context = { config: {} }
32+
const options = { id: 'rt', argv: ['--help'] }
33+
34+
await hook.call(context, options)
35+
36+
expect(helpSpy).toHaveBeenCalledWith(['runtime'])
37+
expect(exitSpy).toHaveBeenCalledWith(0)
38+
})
39+
40+
test('does not intercept runtime with help flag', async () => {
41+
const context = { config: {} }
42+
const options = { id: 'runtime', argv: ['--help'] }
43+
44+
await hook.call(context, options)
45+
46+
expect(helpSpy).not.toHaveBeenCalled()
47+
expect(exitSpy).not.toHaveBeenCalled()
48+
})
49+
50+
test('does not intercept rt without help flag', async () => {
51+
const context = { config: {} }
52+
const options = { id: 'rt', argv: [] }
53+
54+
await hook.call(context, options)
55+
56+
expect(helpSpy).not.toHaveBeenCalled()
57+
expect(exitSpy).not.toHaveBeenCalled()
58+
})
59+
})

0 commit comments

Comments
 (0)