Skip to content

Commit

Permalink
fix(docs:prune): prune child docs (#713)
Browse files Browse the repository at this point in the history
  • Loading branch information
kanadgupta authored Dec 16, 2022
1 parent c90abb5 commit 2b81439
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
36 changes: 36 additions & 0 deletions __tests__/cmds/docs/prune.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,42 @@ describe('rdme docs:prune', () => {
versionMock.done();
});

it('should delete doc and its child if they are missing', async () => {
prompts.inject([true]);

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', children: [{ slug: 'this-child-is-also-missing' }] },
{ slug: 'some-doc' },
])
.delete('/api/v1/docs/this-doc-should-be-missing-in-folder')
.basicAuth({ user: key })
.reply(204, '')
.delete('/api/v1/docs/this-child-is-also-missing')
.basicAuth({ user: key })
.reply(204, '');

await expect(
docsPrune.run({
folder,
key,
version,
})
).resolves.toBe(
'🗑️ successfully deleted `this-doc-should-be-missing-in-folder`.\n🗑️ successfully deleted `this-child-is-also-missing`.'
);

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

it('should return doc delete info for dry run', async () => {
prompts.inject([true]);

Expand Down
12 changes: 11 additions & 1 deletion src/lib/getDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@ interface Document {
}

function flatten(data: Document[][]): Document[] {
return [].concat(...data);
const allDocs: Document[] = [];
const docs: Document[] = [].concat(...data);
docs.forEach(doc => {
allDocs.push(doc);
if (doc.children) {
doc.children.forEach(child => {
allDocs.push(child);
});
}
});
return allDocs;
}

async function getCategoryDocs(key: string, selectedVersion: string, category: string): Promise<Document[]> {
Expand Down

0 comments on commit 2b81439

Please sign in to comment.