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(docs:prune): --confirm flag to bypass prompt #648

Merged
merged 2 commits into from
Oct 31, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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