Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Fixed #10
Browse files Browse the repository at this point in the history
--all flag is added to todo rm & todo ls rm
  • Loading branch information
0phoff committed Sep 15, 2017
1 parent 37e445d commit dcf98e7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ This is an overview of everything that needs to be done for V2.0.1
- [ ] __[cli_functions.js]__ Refactor cli functions
- [X] Add .npmignore/files section in package.json
- [X] Change inquirer prompts to XO coding style
- [ ] __issue #10__ Add todo rm --all
- [X] __issue #10__ Add todo rm --all
- [ ] __issue #11__ option to only list (un)completed tasks
- [ ] *(optional)* Cleanup tests

Expand Down
2 changes: 2 additions & 0 deletions lib/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ list
.command('rm')
.description('Remove a todo list from a project')
.flag('l', 'limit', false, 'Limit the RegExp to only match with titles')
.flag('a', 'all', false, 'Delete all matched lists')
.argument('regexp', false)
.action(cli.listRm);

Expand All @@ -66,6 +67,7 @@ program
.description('Remove a todo item from a list')
.flag('l', 'list', true, 'RegExp to identify the list by title')
.flag('L', 'List', true, 'RegExp to identify the list by title and description')
.flag('a', 'all', false, 'Delete all matched items')
.argument('regexp', false)
.action(cli.itemRm);

Expand Down
12 changes: 8 additions & 4 deletions lib/cli/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ function listRm(options) {
search = options.argv.join('[^]*');
}

return getLists(project, 'specialCheckbox', 'Pick the list you want to remove:', {search, desc: !options.limit, autoresolve: true})
return getLists(project, 'specialCheckbox', 'Pick the list you want to remove:', {search, desc: !options.limit, autoresolve: true, all: options.all})
.then(listindices => {
if (listindices.length === 0) {
return Promise.reject(new Error('No list selected'));
Expand Down Expand Up @@ -296,7 +296,7 @@ function itemRm(options) {
desc = false;
}

return getItems(project, 'specialCheckbox', 'Pick the items you want to remove:', {listSearch: list, listDesc: desc, search})
return getItems(project, 'specialCheckbox', 'Pick the items you want to remove:', {listSearch: list, listDesc: desc, search, all: options.all})
.then(itemindices => {
if (itemindices.length === 0) {
return Promise.reject(new Error('No items selected'));
Expand Down Expand Up @@ -433,7 +433,7 @@ function getFile(options) {
};
}

function getLists(project, type, message, {search, desc, autoresolve}) {
function getLists(project, type, message, {search, desc, autoresolve, all}) {
if (project.length === 0) {
return Promise.reject(new Error('This project does not contain any lists'));
}
Expand All @@ -452,7 +452,7 @@ function getLists(project, type, message, {search, desc, autoresolve}) {
return Promise.reject(new Error(`Could not find a list that matched with ${search}`));
}

if (listindices.length === 1 && autoresolve) {
if (all || (listindices.length === 1 && autoresolve)) {
return Promise.resolve(listindices);
}

Expand All @@ -462,6 +462,10 @@ function getLists(project, type, message, {search, desc, autoresolve}) {
});
}
else {
if (all) {
return Promise.resolve([...Array(project.length).keys()]);
}

if (project.length === 1 && autoresolve) {
return Promise.resolve([0]);
}
Expand Down
19 changes: 18 additions & 1 deletion test/cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const chalk = require('chalk');
const md = require('../lib/md/md');
const todo = require('../lib/todo');
const cli = require('../lib/cli/functions');
const config = require('../lib/config.js');
const config = require('../lib/config');

context('cli-functions.js', () => {
beforeEach(() => {
Expand Down Expand Up @@ -178,6 +178,15 @@ context('cli-functions.js', () => {
});
});

it('should remove all lists, when the all flag is used', () => {
sinon.stub(inquirer, 'prompt').resolves({delete: true});
return cli.listRm({all: true, argv: []})
.then(project => {
sinon.assert.calledOnce(md.writeFile);
assert.equal(project.length, 0);
});
});

it('should ask if a list is not empty', () => {
sinon.stub(inquirer, 'prompt').resolves({delete: true});
return cli.listRm({argv: ['description']})
Expand Down Expand Up @@ -255,6 +264,14 @@ context('cli-functions.js', () => {
});
});

it('should remove all items, when the all flag is used', () => {
return cli.itemRm({list: 'random', all: true, argv: []})
.then(project => {
sinon.assert.calledOnce(md.writeFile);
assert.equal(project.getList(2).length, 0);
});
});

it('should let the user choose if there are multiple matches', () => {
sinon.stub(inquirer, 'prompt').resolves({items: {list: this.project.getList(2), index: 0}});
return cli.itemRm({argv: []})
Expand Down

0 comments on commit dcf98e7

Please sign in to comment.