diff --git a/cli.js b/cli.js index f6ef3d18..41bc586a 100644 --- a/cli.js +++ b/cli.js @@ -67,6 +67,10 @@ const cli = meow(help, { move: { type: 'boolean', alias: 'm' + }, + renameBoard: { + type: 'boolean', + alias: 'rb' } } }); 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..2d1334cf 100644 --- a/lib/help.js +++ b/lib/help.js @@ -5,22 +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 - --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 @@ -39,4 +40,5 @@ module.exports = ` $ tb --list pending coding $ tb --archive $ tb --restore 4 + $ tb --rename-board 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..4fd69fd3 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -108,6 +108,35 @@ class Storage { return archive; } + renameBoardsWithName(oldName, newName) { + let finalData = null; + 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 => { + 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('@'));