Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --aliases flag for readme command (#925) #952

Merged
merged 1 commit into from Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/commands/readme.ts
Expand Up @@ -30,6 +30,7 @@ Customize the code URL prefix by setting oclif.repositoryPrefix in package.json.
static flags = {
dir: Flags.string({description: 'output directory for multi docs', default: 'docs', required: true}),
multi: Flags.boolean({description: 'create a different markdown page for each topic'}),
aliases: Flags.boolean({description: 'include aliases in the command list', allowNo: true, default: true}),
}

private HelpClass!: HelpBaseDerived
Expand All @@ -55,6 +56,7 @@ Customize the code URL prefix by setting oclif.repositoryPrefix in package.json.

let commands = config.commands
.filter(c => !c.hidden && c.pluginType === 'core')
.filter(c => flags.aliases ? true : !c.aliases.includes(c.id))
.map(c => c.id === '.' ? {...c, id: ''} : c)

this.debug('commands:', commands.map(c => c.id).length)
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/cli-command-with-alias/README.md
@@ -0,0 +1,17 @@
# cli-command-with-alias

This file is a test for running `oclif-dev readme` with aliases. If the --no-aliases flag
is passed as a flag, no aliases should be in the README.

<!-- toc -->
<!-- tocstop -->

# Usage

<!-- usage -->
<!-- usagestop -->

# Commands

<!-- commands -->
<!-- commandsstop -->
11 changes: 11 additions & 0 deletions test/fixtures/cli-command-with-alias/package.json
@@ -0,0 +1,11 @@
{
"name": "cli-command-with-alias",
"files": [
"/lib"
],
"oclif": {
"commands": "./lib/commands",
"bin": "oclif",
"helpClass": "./lib/help"
}
}
13 changes: 13 additions & 0 deletions test/fixtures/cli-command-with-alias/src/commands/hello.ts
@@ -0,0 +1,13 @@
import {Command} from '@oclif/core'

export default class Hello extends Command {
static description = 'a simple command'

static flags = {}

static aliases = ['hi']

async run(): Promise<void> {
this.log('hello world')
}
}
7 changes: 7 additions & 0 deletions test/fixtures/cli-command-with-alias/src/help.ts
@@ -0,0 +1,7 @@
import {Interfaces, Help} from '@oclif/core'

export default class CustomHelp extends Help {
formatCommand(command: Interfaces.Command): string {
return `Custom help for ${command.id}`
}
}
1 change: 1 addition & 0 deletions test/fixtures/cli-command-with-alias/src/index.ts
@@ -0,0 +1 @@
export {run} from '@oclif/core'
14 changes: 14 additions & 0 deletions test/fixtures/cli-command-with-alias/tsconfig.json
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"declaration": true,
"importHelpers": true,
"module": "commonjs",
"outDir": "lib",
"rootDir": "src",
"strict": true,
"target": "es2017"
},
"include": [
"src/**/*"
]
}
27 changes: 27 additions & 0 deletions test/unit/readme.test.ts
Expand Up @@ -26,6 +26,33 @@ describe('readme', () => {
expect(fs.readFileSync('README.md', 'utf8')).to.contain('multi')
})

describe('with command that has an alias', () => {
const rootPath = path.join(__dirname, '../fixtures/cli-command-with-alias')
const readmePath = path.join(rootPath, 'README.md')
const originalReadme = fs.readFileSync(readmePath, 'utf8')
const aliasOutput = '`oclif hi`'

test
.stdout()
.finally(() => fs.writeFileSync(readmePath, originalReadme))
.stub(process, 'cwd', () => rootPath)
.command(['readme'])
.it('--aliases flag (default)', () => {
const newReadme = fs.readFileSync(readmePath, 'utf8')
expect(newReadme).to.contain(aliasOutput)
})

test
.stdout()
.finally(() => fs.writeFileSync(readmePath, originalReadme))
.stub(process, 'cwd', () => rootPath)
.command(['readme', '--no-aliases'])
.it('--no-aliases flag', () => {
const newReadme = fs.readFileSync(readmePath, 'utf8')
expect(newReadme).not.to.contain(aliasOutput)
})
})

describe('with custom help that implements formatCommand', () => {
const rootPath = path.join(__dirname, '../fixtures/cli-with-custom-help')
const readmePath = path.join(rootPath, 'README.md')
Expand Down