From 72bfda5152ee340ebee6156f941a375f63f969bd Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sat, 1 Jul 2017 16:07:17 +0100 Subject: [PATCH] Allow databases with no existing folders --- CliClient/app/fuzzing.js | 2 +- CliClient/app/main.js | 33 +++++++++++++++++++-------------- CliClient/run.sh | 2 +- lib/models/folder.js | 5 +---- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/CliClient/app/fuzzing.js b/CliClient/app/fuzzing.js index 0cab0bf9f..4685c9201 100644 --- a/CliClient/app/fuzzing.js +++ b/CliClient/app/fuzzing.js @@ -197,7 +197,7 @@ async function compareClientItems(clientItems) { let missingItems = findMissingItems(clientItems[0], clientItems[1]); if (missingItems[0].length || missingItems[1].length) { - logger.error('Item count is different'); + logger.error('Items are different'); logger.error(missingItems); process.exit(1); } diff --git a/CliClient/app/main.js b/CliClient/app/main.js index dbec62a6a..995b74d01 100644 --- a/CliClient/app/main.js +++ b/CliClient/app/main.js @@ -211,12 +211,14 @@ commands.push({ } } } else { // Handle it as a glob pattern - let notes = await Note.previews(currentFolder.id, { titlePattern: pattern }); - if (!notes.length) throw new Error(_('No note matches this pattern: "%s"', pattern)); - let ok = force ? true : await cmdPromptConfirm(this, _('%d notes match this pattern. Delete them?', notes.length)); - if (ok) { - for (let i = 0; i < notes.length; i++) { - await Note.delete(notes[i].id); + if (currentFolder) { + let notes = await Note.previews(currentFolder.id, { titlePattern: pattern }); + if (!notes.length) throw new Error(_('No note matches this pattern: "%s"', pattern)); + let ok = force ? true : await cmdPromptConfirm(this, _('%d notes match this pattern. Delete them?', notes.length)); + if (ok) { + for (let i = 0; i < notes.length; i++) { + await Note.delete(notes[i].id); + } } } } @@ -234,6 +236,8 @@ commands.push({ description: 'Moves the notes matching to .', action: async function(args, end) { try { + if (!currentFolder) throw new Error(_('Please select a notebook first.')); + let pattern = args['pattern']; let folder = await Folder.loadByField('title', args['notebook']); @@ -309,10 +313,11 @@ commands.push({ } if (pattern) queryOptions.titlePattern = pattern; - if (pattern == '..') { + if (pattern == '..' || !currentFolder) { items = await Folder.all(queryOptions); suffix = '/'; } else { + if (!currentFolder) throw new Error(_('Please select a notebook first.')); items = await Note.previews(currentFolder.id, queryOptions); } @@ -602,10 +607,8 @@ async function synchronizer(syncTarget) { } function switchCurrentFolder(folder) { - if (!folder) throw new Error(_('No active folder is defined.')); - currentFolder = folder; - Setting.setValue('activeFolderId', folder.id); + Setting.setValue('activeFolderId', folder ? folder.id : ''); updatePrompt(); } @@ -799,11 +802,10 @@ async function main() { let activeFolder = null; if (activeFolderId) activeFolder = await Folder.load(activeFolderId); if (!activeFolder) activeFolder = await Folder.defaultFolder(); - if (!activeFolder) activeFolder = await Folder.createDefaultFolder(); - if (!activeFolder) throw new Error(_('No default notebook is defined and could not create a new one. The database might be corrupted, please delete it and try again.')); - Setting.setValue('activeFolderId', activeFolder.id); + //if (!activeFolder) throw new Error(_('No default notebook is defined and could not create a new one. The database might be corrupted, please delete it and try again.')); + Setting.setValue('activeFolderId', activeFolder ? activeFolder.id : ''); - await execCommand('cd', { 'notebook': activeFolder.title }); // Use execCommand() so that no history entry is created + if (activeFolder) await execCommand('cd', { 'notebook': activeFolder.title }); // Use execCommand() so that no history entry is created // If we still have arguments, pass it to Vorpal and exit if (argv.length) { @@ -813,6 +815,9 @@ async function main() { return; } else { vorpal.delimiter(promptString()).show(); + if (!activeFolder) { + vorpal.log(_('No notebook is defined. Create one with `mkbook `.')); + } } } diff --git a/CliClient/run.sh b/CliClient/run.sh index 662c4805f..e91a1b6bb 100755 --- a/CliClient/run.sh +++ b/CliClient/run.sh @@ -1,6 +1,6 @@ #!/bin/bash set -e CLIENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -bash $CLIENT_DIR/build.sh && NODE_PATH="$CLIENT_DIR/build/" node build/main.js --profile ~/Temp/TestNotes2 +bash $CLIENT_DIR/build.sh && NODE_PATH="$CLIENT_DIR/build/" node build/main.js --profile ~/Temp/TestNotes2 "$@" #bash $CLIENT_DIR/build.sh && NODE_PATH="$CLIENT_DIR/build/" node build/main.js --profile ~/Temp/TestNotes import-enex --fuzzy-matching /home/laurent/Desktop/afaire.enex afaire #bash $CLIENT_DIR/build.sh && NODE_PATH="$CLIENT_DIR/build/" node build/main.js --profile ~/Temp/TestNotes import-enex --fuzzy-matching /home/laurent/Desktop/Laurent.enex laurent \ No newline at end of file diff --git a/lib/models/folder.js b/lib/models/folder.js index 18b18d1f5..7b025c019 100644 --- a/lib/models/folder.js +++ b/lib/models/folder.js @@ -54,10 +54,7 @@ class Folder extends BaseItem { static async delete(folderId, options = null) { let folder = await Folder.load(folderId); if (!folder) throw new Error('Trying to delete non-existing notebook: ' + folderId); - - let count = await Folder.count(); - if (count <= 1) throw new Error(_('Cannot delete the last notebook')); - + let noteIds = await Folder.noteIds(folderId); for (let i = 0; i < noteIds.length; i++) { await Note.delete(noteIds[i]);