Skip to content
This repository has been archived by the owner on Apr 21, 2022. It is now read-only.

feat: add support for custom help classes #141

Merged
merged 10 commits into from Nov 19, 2020

Conversation

chadian
Copy link
Contributor

@chadian chadian commented Apr 9, 2020

No description provided.

@chadian chadian force-pushed the cc/add-support-for-help-plugins branch 5 times, most recently from 0daba05 to 8e97b53 Compare April 13, 2020 17:21
@chadian chadian changed the title feat: add support for help plugins feat: add support for custom help classes Apr 14, 2020
@RasPhilCo RasPhilCo added this to To Do in oclif Jul 2, 2020
@RasPhilCo RasPhilCo moved this from To Do to In Progress in oclif Aug 7, 2020
@jgwhite jgwhite force-pushed the cc/add-support-for-help-plugins branch 2 times, most recently from c2e4af0 to 3ed0540 Compare August 13, 2020 09:40
@jgwhite jgwhite changed the title feat: add support for custom help classes feat(readme): add support for custom help classes Aug 13, 2020
@jgwhite
Copy link

jgwhite commented Aug 13, 2020

Rebased this and reformulated the commits a little bit.

@jgwhite jgwhite force-pushed the cc/add-support-for-help-plugins branch 5 times, most recently from b05b6bc to 1eef942 Compare September 7, 2020 17:08
@jgwhite
Copy link

jgwhite commented Sep 8, 2020

Got the tests for this to a good place.

@jgwhite jgwhite changed the title feat(readme): add support for custom help classes feat(readme)!: add support for custom help classes Sep 8, 2020
Jamie White and others added 6 commits September 8, 2020 15:21
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>
@jgwhite jgwhite force-pushed the cc/add-support-for-help-plugins branch from 1eef942 to 85745f2 Compare September 8, 2020 13:22
@jgwhite jgwhite self-assigned this Sep 9, 2020
@jgwhite jgwhite marked this pull request as ready for review September 10, 2020 14:59
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})
Copy link
Contributor

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?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, will investigate.

Copy link

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

const header = () => `## \`${config.bin} ${this.commandUsage(config, c)}\``

const generateCommandHelp = () => {
const original = process.stdout.write
Copy link
Contributor

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.

Copy link

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.

Copy link
Contributor

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.

Copy link

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.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve addressed this in 57b01c9.

With the new approach (which is close in spirit to @chadian’s original approach) the readme generator will try to use formatCommand, then command, then throw a helpful error if there’s nothing usable on the custom help class.

The error looks like this:

Screen Shot 2020-11-05 at 14 08 25

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)}\``
Copy link
Contributor

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.

return compact([
header(),
title,
'```\n' + help.command(c).trim() + '\n```',
'```\n' + stripAnsi(generateCommandHelp().trim()) + '\n```',
Copy link
Contributor

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?

Copy link

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.

Copy link
Contributor

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.

return compact([
header(),
title,
'```\n' + help.command(c).trim() + '\n```',
'```\n' + stripAnsi(generateCommandHelp().trim()) + '\n```',
Copy link
Contributor

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.

@jgwhite
Copy link

jgwhite commented Nov 3, 2020

Going to take another run at this after discussing with @amphro.

@jgwhite jgwhite assigned chadian and unassigned jgwhite Nov 10, 2020
@chadian
Copy link
Contributor Author

chadian commented Nov 10, 2020

I will be helping shepherd this PR along on

@RasPhilCo RasPhilCo changed the title feat(readme)!: add support for custom help classes feat: add support for custom help classes Nov 19, 2020
@RasPhilCo RasPhilCo merged commit 810e7c1 into master Nov 19, 2020
@RasPhilCo RasPhilCo deleted the cc/add-support-for-help-plugins branch November 19, 2020 21:56
oclif-bot added a commit that referenced this pull request Nov 19, 2020
# [1.24.0](v1.23.1...v1.24.0) (2020-11-19)

### Features

* add support for custom help classes ([#141](#141)) ([810e7c1](810e7c1))
@oclif-bot
Copy link
Contributor

🎉 This PR is included in version 1.24.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
oclif
  
In Progress
Development

Successfully merging this pull request may close these issues.

None yet

5 participants