Skip to content

Commit

Permalink
feat(docs:prune): --confirm flag to bypass prompt (#648)
Browse files Browse the repository at this point in the history
* Add support for `docs:prune --noPrompt` for CI environments

* refactor: use prompt override, rename flag

Co-authored-by: Kanad Gupta <[email protected]>
  • Loading branch information
shaiarmis and kanadgupta authored Oct 31, 2022
1 parent c8948ee commit 34aa184
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ If you wish to delete documents from ReadMe that are no longer present in your l
rdme docs:prune path-to-directory-of-markdown
```

Run with `--confirm` to bypass the confirmation prompt (useful for CI environments).

This command also has an alias called `guides:prune`:

```sh
Expand Down
27 changes: 27 additions & 0 deletions __tests__/cmds/docs/prune.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,33 @@ describe('rdme docs:prune', () => {
versionMock.done();
});

it('should not ask for user confirmation if `confirm` is set to true', async () => {
const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version });

const apiMocks = getAPIMockWithVersionHeader(version)
.get('/api/v1/categories?perPage=20&page=1')
.basicAuth({ user: key })
.reply(200, [{ slug: 'category1', type: 'guide' }], { 'x-total-count': '1' })
.get('/api/v1/categories/category1/docs')
.basicAuth({ user: key })
.reply(200, [{ slug: 'this-doc-should-be-missing-in-folder' }, { slug: 'some-doc' }])
.delete('/api/v1/docs/this-doc-should-be-missing-in-folder')
.basicAuth({ user: key })
.reply(204, '');

await expect(
docsPrune.run({
folder,
key,
confirm: true,
version,
})
).resolves.toBe('🗑️ successfully deleted `this-doc-should-be-missing-in-folder`.');

apiMocks.done();
versionMock.done();
});

it('should delete doc if file is missing', async () => {
prompts.inject([true]);

Expand Down
17 changes: 13 additions & 4 deletions src/cmds/docs/prune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { CommandOptions } from '../../lib/baseCommand';

import chalk from 'chalk';
import config from 'config';
import prompts from 'prompts';

import Command, { CommandCategories } from '../../lib/baseCommand';
import createGHA from '../../lib/createGHA';
Expand All @@ -13,6 +14,7 @@ import readDoc from '../../lib/readDoc';
import { getProjectVersion } from '../../lib/versionSelect';

export type Options = {
confirm?: boolean;
dryRun?: boolean;
folder?: string;
};
Expand Down Expand Up @@ -42,6 +44,11 @@ export default class DocsPruneCommand extends Command {
defaultOption: true,
},
this.getGitHubArg(),
{
name: 'confirm',
type: Boolean,
description: 'Bypass the confirmation prompt. Useful for CI environments.',
},
{
name: 'dryRun',
type: Boolean,
Expand Down Expand Up @@ -73,13 +80,15 @@ export default class DocsPruneCommand extends Command {

Command.debug(`number of files: ${files.length}`);

const { continueWithDeletion } = await promptTerminal({
prompts.override(opts);

const { confirm } = await promptTerminal({
type: 'confirm',
name: 'continueWithDeletion',
message: `This command will delete all guides page from your ReadMe project (version ${selectedVersion}) that are not also in ${folder}, would you like to continue?`,
name: 'confirm',
message: `This command will delete all guides page from your ReadMe project (version ${selectedVersion}) that are not also in ${folder}, would you like to confirm?`,
});

if (!continueWithDeletion) {
if (!confirm) {
return Promise.reject(new Error('Aborting, no changes were made.'));
}

Expand Down

0 comments on commit 34aa184

Please sign in to comment.