feat: add support for custom help classes #141
Conversation
0daba05
to
8e97b53
Compare
c2e4af0
to
3ed0540
Compare
Rebased this and reformulated the commits a little bit. |
b05b6bc
to
1eef942
Compare
Got the tests for this to a good place. |
Co-authored-by: Chad Carbert <chadcarbert@me.com>
Co-authored-by: Chad Carbert <chadcarbert@me.com>
Co-authored-by: Chad Carbert <chadcarbert@me.com>
Co-authored-by: Chad Carbert <chadcarbert@me.com>
Co-authored-by: Chad Carbert <chadcarbert@me.com>
Co-authored-by: Chad Carbert <chadcarbert@me.com>
1eef942
to
85745f2
Compare
src/commands/readme.ts
Outdated
const plugin = new Config.Plugin({root: p, type: 'core'}) | ||
await plugin.load() | ||
config.plugins.push(plugin) | ||
} catch {} | ||
await config.runHook('init', {id: 'readme', argv: this.argv}) | ||
let readme = await fs.readFile('README.md', 'utf8') | ||
await (config.runHook as any)('init', {id: 'readme', argv: this.argv}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the need for the as any
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, will investigate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, it’s due to this: oclif/config#158
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
src/commands/readme.ts
Outdated
const header = () => `## \`${config.bin} ${this.commandUsage(config, c)}\`` | ||
|
||
const generateCommandHelp = () => { | ||
const original = process.stdout.write |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't love this. Can we make the help class more flexible to return the command help instead? e.g help.formatCommandHelp
and the showCommandHelp would call that to pass to stdout.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can, but it would mean a breaking change to @oclif/plugin-help
because we currently define the minimum contract to be just showHelp
and showCommandHelp
and in the docs we advise users to write directly to stdout.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The base help class already has format methods. It seems like a natural extension to make it part of the interface. It wouldn't really be a breaking change. It would just mean that if people want to take advantage of the new customer help in the readme they would have to implement the additional methods in the interface.
Also, we should still advise writing directly to stdout in the show methods, but update the docs to utilize the format methods for additional abstraction and customization.
With that said, I don't feel strongly enough to fight for it. I just don't like this pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if people want to take advantage of the new customer help in the readme they would have to implement the additional methods in the interface
I think this might be the way to go. Perhaps something like:
$ npm run oclif-dev readme
› Error: You are using a custom help class without required format methods.
› Please define `formatCommand` etc.
› Please see https://oclif.io/docs/help_classes for more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const help = new Help(config, {stripAnsi: true, maxWidth: columns}) | ||
const HelpClass = getHelpClass(config) | ||
const help = new HelpClass(config, {stripAnsi: true, maxWidth: columns}) | ||
|
||
const header = () => `## \`${config.bin} ${this.commandUsage(config, c)}\`` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would love for this to be configurable too via the help class or some other mechanism. That can obviously be done in another PR though.
src/commands/readme.ts
Outdated
return compact([ | ||
header(), | ||
title, | ||
'```\n' + help.command(c).trim() + '\n```', | ||
'```\n' + stripAnsi(generateCommandHelp().trim()) + '\n```', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We pass stripAnsi to the help class. Why do we have too do it here too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It’s been a while since I last looked at this, but I think it’s because consumers who build upon HelpBase
can do whatever they want in showHelp
and showCommandHelp
. stripAnsi
is only used in the concrete Help
implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the stripAnsi
is in the concert Help, then that implies to me that custom help implementation should also strip if that option is passed in. It's not a big deal to do it here though, it just removes the flexibility if someone didn't want to strip the ansi, for whatever random reason.
src/commands/readme.ts
Outdated
return compact([ | ||
header(), | ||
title, | ||
'```\n' + help.command(c).trim() + '\n```', | ||
'```\n' + stripAnsi(generateCommandHelp().trim()) + '\n```', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the stripAnsi
is in the concert Help, then that implies to me that custom help implementation should also strip if that option is passed in. It's not a big deal to do it here though, it just removes the flexibility if someone didn't want to strip the ansi, for whatever random reason.
Going to take another run at this after discussing with @amphro. |
I will be helping shepherd this PR along on |
# [1.24.0](v1.23.1...v1.24.0) (2020-11-19) ### Features * add support for custom help classes ([#141](#141)) ([810e7c1](810e7c1))
🎉 This PR is included in version 1.24.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
No description provided.