2017-07-10 22:03:46 +02:00
|
|
|
import { BaseCommand } from './base-command.js';
|
|
|
|
import { app } from './app.js';
|
|
|
|
import { _ } from 'lib/locale.js';
|
|
|
|
import { BaseItem } from 'lib/models/base-item.js';
|
|
|
|
import { Folder } from 'lib/models/folder.js';
|
|
|
|
import { Note } from 'lib/models/note.js';
|
|
|
|
import { BaseModel } from 'lib/base-model.js';
|
|
|
|
import { autocompleteItems } from './autocomplete.js';
|
|
|
|
|
|
|
|
class Command extends BaseCommand {
|
|
|
|
|
|
|
|
usage() {
|
2017-07-28 20:13:07 +02:00
|
|
|
return 'rm <pattern>';
|
2017-07-10 22:03:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
description() {
|
2017-07-18 20:21:03 +02:00
|
|
|
return _('Deletes the items matching <pattern>.');
|
2017-07-10 22:03:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
autocomplete() {
|
|
|
|
return { data: autocompleteItems };
|
|
|
|
}
|
|
|
|
|
|
|
|
options() {
|
|
|
|
return [
|
2017-07-18 20:21:03 +02:00
|
|
|
['-f, --force', _('Deletes the items without asking for confirmation.')],
|
|
|
|
['-r, --recursive', _('Deletes a notebook.')],
|
2017-07-10 22:03:46 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
async action(args) {
|
2017-07-11 20:17:23 +02:00
|
|
|
const pattern = args['pattern'].toString();
|
|
|
|
const recursive = args.options && args.options.recursive === true;
|
2017-08-03 19:48:14 +02:00
|
|
|
const force = true || args.options && args.options.force === true; // TODO
|
2017-07-11 20:17:23 +02:00
|
|
|
|
|
|
|
if (recursive) {
|
|
|
|
const folder = await app().loadItem(BaseModel.TYPE_FOLDER, pattern);
|
2017-07-18 20:21:03 +02:00
|
|
|
if (!folder) throw new Error(_('Cannot find "%s".', pattern));
|
2017-08-03 19:48:14 +02:00
|
|
|
//const ok = force ? true : await vorpalUtils.cmdPromptConfirm(this, _('Delete notebook "%s"?', folder.title));
|
2017-07-11 20:17:23 +02:00
|
|
|
if (!ok) return;
|
|
|
|
await Folder.delete(folder.id);
|
2017-07-17 20:46:09 +02:00
|
|
|
await app().refreshCurrentFolder();
|
2017-07-11 20:17:23 +02:00
|
|
|
} else {
|
|
|
|
const notes = await app().loadItems(BaseModel.TYPE_NOTE, pattern);
|
2017-07-18 20:21:03 +02:00
|
|
|
if (!notes.length) throw new Error(_('Cannot find "%s".', pattern));
|
2017-07-11 20:17:23 +02:00
|
|
|
const ok = force ? true : await vorpalUtils.cmdPromptConfirm(this, _('%d notes match this pattern. Delete them?', notes.length));
|
|
|
|
if (!ok) return;
|
|
|
|
let ids = notes.map((n) => n.id);
|
|
|
|
await Note.batchDelete(ids);
|
2017-07-10 22:03:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Command;
|