From 149887da4fbe1af173d295dd81426463eb141884 Mon Sep 17 00:00:00 2001 From: Pavlo Cherniovyi Date: Wed, 8 Aug 2018 23:58:42 +0300 Subject: [PATCH 1/3] Add: - [x] Rename boards by specifying --- cli.js | 4 ++++ index.js | 4 ++++ lib/config.js | 5 +---- lib/help.js | 2 ++ lib/render.js | 12 ++++++++++++ lib/storage.js | 29 +++++++++++++++++++++++++++++ lib/taskbook.js | 5 +++++ 7 files changed, 57 insertions(+), 4 deletions(-) diff --git a/cli.js b/cli.js index f6ef3d18..c31f8e00 100644 --- a/cli.js +++ b/cli.js @@ -67,6 +67,10 @@ const cli = meow(help, { move: { type: 'boolean', alias: 'm' + }, + renameBoard: { + type: 'boolean', + alias: 'r' } } }); diff --git a/index.js b/index.js index fac1837d..01cb3f89 100644 --- a/index.js +++ b/index.js @@ -57,6 +57,10 @@ const taskbookCLI = (input, flags) => { return taskbook.moveBoards(input); } + if (flags.renameBoard) { + return taskbook.renameBoard(input); + } + taskbook.displayByBoard(); return taskbook.displayStats(); }; diff --git a/lib/config.js b/lib/config.js index afc95d6f..a0d8f02c 100644 --- a/lib/config.js +++ b/lib/config.js @@ -24,11 +24,8 @@ class Config { } get() { - let config = {}; - const content = fs.readFileSync(this._configFile, 'utf8'); - config = JSON.parse(content); - + const config = JSON.parse(content); return Object.assign({}, defaultConfig, config); } } diff --git a/lib/help.js b/lib/help.js index 0a25e2b2..9e32b546 100644 --- a/lib/help.js +++ b/lib/help.js @@ -19,6 +19,7 @@ module.exports = ` --priority, -p Update priority of task --archive, -a Display archived items --restore, -r Restore items from archive + --renameBoard -r Rename board; boards with spaces - specify via "_" symbol like My_Board --help, -h Display help message --version, -v Display installed version @@ -39,4 +40,5 @@ module.exports = ` $ tb --list pending coding $ tb --archive $ tb --restore 4 + $ tb --renameBoard My_Board Todo `; diff --git a/lib/render.js b/lib/render.js index 628576df..3c3caa6c 100644 --- a/lib/render.js +++ b/lib/render.js @@ -242,6 +242,18 @@ class Render { error({prefix, message}); } + missingStorage() { + const prefix = '\n'; + const message = 'There are no any boards, please create some task at least to start'; + error({prefix, message}); + } + + missingBoardName(name) { + const prefix = '\n'; + const message = `There are no any board with name: ${name}`; + error({prefix, message}); + } + successCreate({_id, _isTask}) { const [prefix, suffix] = ['\n', grey(_id)]; const message = `Created ${_isTask ? 'task:' : 'note:'}`; diff --git a/lib/storage.js b/lib/storage.js index c1338bf3..7479b995 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -108,6 +108,35 @@ class Storage { return archive; } + renameBoardsWithName(oldName, newName) { + let finalData = null; + fs.exists(this._mainStorageFile, (exists) => { + if (!exists) { + render.missingStorage(); + return finalData; + } + const rs = fs.createReadStream(this._mainStorageFile, 'utf8'); + rs.on('data', (rsData) => { + const _rsData = JSON.parse(rsData); + Object.keys(_rsData).forEach((k) => { + _rsData[k].boards.forEach((boardName, i) => { + if (boardName === oldName) { + _rsData[k].boards[i] = newName; + finalData = { ..._rsData }; + finalData = JSON.stringify(finalData, null, 2); + } + }); + }); + }); + rs.on('end', () => { + if (!finalData) { + return render.missingBoardName(oldName); + } + fs.createWriteStream(this._mainStorageFile).write(finalData); + }); + }); + } + set(data) { data = JSON.stringify(data, null, 4); const tempStorageFile = this._getTempFile(this._mainStorageFile); diff --git a/lib/taskbook.js b/lib/taskbook.js index 51562c5e..7beea4ad 100644 --- a/lib/taskbook.js +++ b/lib/taskbook.js @@ -415,6 +415,11 @@ class Taskbook { render.displayByBoard(this._groupByBoard(data, boards)); } + renameBoard(input) { + const oldName = input[1].replace(/_/g, ' '); + this._storage.renameBoardsWithName(oldName, input[2]); + } + moveBoards(input) { let boards = []; const targets = input.filter(x => x.startsWith('@')); From 48933405fe03debfb4494574f0c12f6b9d0a446c Mon Sep 17 00:00:00 2001 From: Pavlo Cherniovyi Date: Sat, 11 Aug 2018 00:04:06 +0300 Subject: [PATCH 2/3] Add: - [x] Update cli command. --- cli.js | 2 +- lib/help.js | 36 ++++++++++++++++++------------------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/cli.js b/cli.js index c31f8e00..41bc586a 100644 --- a/cli.js +++ b/cli.js @@ -70,7 +70,7 @@ const cli = meow(help, { }, renameBoard: { type: 'boolean', - alias: 'r' + alias: 'rb' } } }); diff --git a/lib/help.js b/lib/help.js index 9e32b546..2d1334cf 100644 --- a/lib/help.js +++ b/lib/help.js @@ -5,23 +5,23 @@ module.exports = ` $ tb [ ...] Options - none Display board view - --task, -t Create task - --note, -n Create note - --timeline, -i Display timeline view - --delete, -d Delete item - --check, -c Check/uncheck task - --star, -s Star/unstar item - --list, -l List items by attributes - --find, -f Search for items - --edit, -e Edit item description - --move, -m Move item between boards - --priority, -p Update priority of task - --archive, -a Display archived items - --restore, -r Restore items from archive - --renameBoard -r Rename board; boards with spaces - specify via "_" symbol like My_Board - --help, -h Display help message - --version, -v Display installed version + none Display board view + --task, -t Create task + --note, -n Create note + --timeline, -i Display timeline view + --delete, -d Delete item + --check, -c Check/uncheck task + --star, -s Star/unstar item + --list, -l List items by attributes + --find, -f Search for items + --edit, -e Edit item description + --move, -m Move item between boards + --priority, -p Update priority of task + --archive, -a Display archived items + --restore, -r Restore items from archive + --rename-board, -rb Rename board; boards with spaces - specify via "_" symbol like My_Board + --help, -h Display help message + --version, -v Display installed version Examples $ tb @@ -40,5 +40,5 @@ module.exports = ` $ tb --list pending coding $ tb --archive $ tb --restore 4 - $ tb --renameBoard My_Board Todo + $ tb --rename-board My_Board Todo `; From 61ca4c806d2b76f545ccc2d86d018f967c06700f Mon Sep 17 00:00:00 2001 From: Pavlo Cherniovyi Date: Sat, 11 Aug 2018 00:25:09 +0300 Subject: [PATCH 3/3] Fix code-style according to test env requirements --- lib/storage.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/storage.js b/lib/storage.js index 7479b995..4fd69fd3 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -110,19 +110,19 @@ class Storage { renameBoardsWithName(oldName, newName) { let finalData = null; - fs.exists(this._mainStorageFile, (exists) => { - if (!exists) { + fs.stat(this._mainStorageFile, (err, res) => { + if (err || !res.isFile()) { render.missingStorage(); return finalData; } const rs = fs.createReadStream(this._mainStorageFile, 'utf8'); - rs.on('data', (rsData) => { + rs.on('data', rsData => { const _rsData = JSON.parse(rsData); - Object.keys(_rsData).forEach((k) => { + Object.keys(_rsData).forEach(k => { _rsData[k].boards.forEach((boardName, i) => { if (boardName === oldName) { _rsData[k].boards[i] = newName; - finalData = { ..._rsData }; + finalData = {..._rsData}; finalData = JSON.stringify(finalData, null, 2); } });