Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 | 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 11x 11x 8x 8x 9x 1x 1x 8x 9x 8x 8x 8x 8x 8x 8x 8x 10x 10x 8x 8x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 9x 9x 8x 8x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 10x 10x 10x 10x 10x 10x 10x 13x 13x 1x 13x 13x 13x 13x 13x 2x 13x 11x 1x 11x 10x 10x 1x 1x 10x 10x 11x 13x 13x 13x 13x 10x 10x 10x 10x 10x 10x 10x 8x 8x 2x 1x 1x 1x 1x 8x 6x 6x 8x 10x 10x 10x 10x 10x 10x 10x 3x 3x 3x 3x 3x 2x 2x 1x 1x 1x 3x 10x 10x 10x 10x 10x 10x 10x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 2x 10x 10x 21x 21x 21x 1x 1x 1x 20x 20x 20x 20x 20x 21x 20x 21x 1x 1x 1x 19x 19x 21x 1x 1x 1x 1x 1x 1x 1x 1x 19x 19x 19x 21x 400x 8x 8x 400x 19x 19x 21x 21x 2x 1x 1x 1x 2x 21x 21x 21x 21x 18x 18x 21x 21x 21x 21x 21x 2x 1x 1x 1x 2x 21x 21x 17x 17x 21x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16x 16x 21x 8x 8x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 8x 8x 8x 8x 8x 8x 5x 5x 5x 5x 8x 8x 3x 8x 8x 8x 8x 8x 8x 10x | const kanbn = require('../main');
const utility = require('../utility');
const inquirer = require('inquirer');
const chrono = require('chrono-node');
const yaml = require('yamljs');
inquirer.registerPrompt('recursive', require('inquirer-recursive'));
const searchFields = [
{
name: 'Id',
field: 'id',
type: 'string'
},
{
name: 'Name',
field: 'name',
type: 'string'
},
{
name: 'Description',
field: 'description',
type: 'string'
},
{
name: 'Column',
field: 'column',
type: 'string'
},
{
name: 'Created',
field: 'created',
type: 'date'
},
{
name: 'Updated',
field: 'updated',
type: 'date'
},
{
name: 'Started',
field: 'started',
type: 'date'
},
{
name: 'Completed',
field: 'completed',
type: 'date'
},
{
name: 'Due',
field: 'due',
type: 'date'
},
{
name: 'Planned start',
field: 'plannedStart',
type: 'date'
},
{
name: 'Planned finish',
field: 'plannedFinish',
type: 'date'
},
{
name: 'Progress',
field: 'progress',
type: 'number'
},
{
name: 'Sub-tasks',
field: 'sub-task',
type: 'string'
},
{
name: 'Count sub-tasks',
field: 'count-sub-tasks',
type: 'number'
},
{
name: 'Tags',
field: 'tag',
type: 'string'
},
{
name: 'Count tags',
field: 'count-tags',
type: 'number'
},
{
name: 'Relations',
field: 'relation',
type: 'string'
},
{
name: 'Count relations',
field: 'count-relations',
type: 'number'
},
{
name: 'Comments',
field: 'comment',
type: 'string'
},
{
name: 'Count comments',
field: 'count-comments',
type: 'number'
},
{
name: 'Assigned',
field: 'assigned',
type: 'string'
}
];
/**
* Build search filters interactively
* @return {Promise<any>}
*/
async function interactive() {
return await inquirer.prompt([
{
type: 'recursive',
name: 'filters',
initialMessage: 'Add a filter?',
message: 'Add another filter?',
default: true,
prompts: [
{
type: 'rawlist',
name: 'type',
message: 'Filter type:',
default: searchFields[0].name,
choices: [
...searchFields.map(s => s.name),
new inquirer.Separator(),
'None'
]
},
{
type: 'input',
name: 'value',
message: 'Filter value:',
default: '',
when: answers => searchFields
.filter(s => s.type === 'string')
.map(s => s.name)
.indexOf(answers.type) !== -1,
validate: async value => {
if (!value) {
return 'Filter value cannot be empty';
}
return true;
}
},
{
type: 'input',
name: 'value',
message: 'Filter value:',
default: '',
when: answers => searchFields
.filter(s => s.type === 'date')
.map(s => s.name)
.indexOf(answers.type) !== -1,
validate: async value => {
if (!value) {
return 'Filter value cannot be empty';
}
if (chrono.parseDate(value) === null) {
return 'Unable to parse date';
}
return true;
}
},
{
type: 'input',
name: 'value',
message: 'Filter value:',
default: '',
when: answers => searchFields
.filter(s => s.type === 'number')
.map(s => s.name)
.indexOf(answers.type) !== -1,
validate: async value => {
if (!value) {
return 'Filter value cannot be empty';
}
if (isNaN(value)) {
return 'Filter value must be numeric';
}
return true;
}
},
{
type: 'confirm',
name: 'value',
message: 'Filter value:',
default: true,
when: answers => searchFields
.filter(s => s.type === 'boolean')
.map(s => s.name)
.indexOf(answers.type) !== -1
}
]
}
]);
}
/**
* Search tasks
* @param {object} filters
* @param {boolean} quiet
* @param {boolean} json
*/
function findTasks(filters, quiet, json) {
const removeEmptyProperties = o => Object.fromEntries(Object.entries(o).filter(
([k, v]) => !(Array.isArray(v) && v.length == 0) && !!v
));
kanbn
.search(filters, quiet)
.then(results => {
if (quiet) {
console.log(json ? JSON.stringify(results, null, 2) : results.join('\n'));
} else {
if (json) {
console.log(JSON.stringify(results, null, 2));
} else {
console.log(`Found ${results.length} task${results.length === 1 ? '' : 's'}`);
if (results.length > 0) {
console.log('---');
}
console.log(results.map(result => yaml.stringify(removeEmptyProperties(result), 4, 2).trim()).join('\n---\n'));
}
}
})
.catch(error => {
utility.error(error);
});
}
/**
* Add a filter to the filters object without over-writing any existing filters
* @param {object} filters The current filter object
* @param {string} filterName The property name for the filter
* @param {string} filterValue The filter value
*/
function addFilterValue(filters, filterName, filterValue) {
if (filters[filterName]) {
if (Array.isArray(filters[filterName])) {
filters[filterName].push(filterValue);
} else {
filters[filterName] = [filters[filterName], filterValue];
}
} else {
filters[filterName] = filterValue;
}
}
/**
* Convert a filter or array of filters to numeric values
* @param {object} filters The current filter object
* @param {string} filterName The property name for the filter
* @return {boolean} True if all values could be converted
*/
function convertNumericFilters(filters, filterName) {
if (Array.isArray(filters[filterName])) {
for (let i = 0; i < filters[filterName].length; i++) {
const numericValue = parseInt(filters[filterName][i]);
if (isNaN(numericValue)) {
return false;
}
filters[filterName][i] = numericValue;
}
} else {
const numericValue = parseInt(filters[filterName]);
if (isNaN(numericValue)) {
return false;
}
filters[filterName] = numericValue;
}
return true;
}
/**
* Convert a filter or array of filters to date values
* @param {object} filters The current filter object
* @param {string} filterName The property name for the filter
* @return {boolean} True if all values could be converted
*/
function convertDateFilters(filters, filterName) {
if (Array.isArray(filters[filterName])) {
for (let i = 0; i < filters[filterName].length; i++) {
const dateValue = chrono.parseDate(filters[filterName][i]);
if (dateValue === null) {
return false;
}
filters[filterName][i] = dateValue;
}
} else {
const dateValue = chrono.parseDate(filters[filterName]);
if (dateValue === null) {
return false;
}
filters[filterName] = dateValue;
}
return true;
}
module.exports = async args => {
// Make sure kanbn has been initialised
if (!await kanbn.initialised()) {
utility.error('Kanbn has not been initialised in this folder\nTry running: {b}kanbn init{b}');
return;
}
// Get the index and make sure it has some columns
let index;
try {
index = await kanbn.getIndex();
} catch (error) {
utility.error(error);
return;
}
const columnNames = Object.keys(index.columns);
if (!columnNames.length) {
utility.error('No columns defined in the index\nTry running {b}kanbn init -c "column name"{b}');
return;
}
// Add custom fields to search fields
if ('customFields' in index.options) {
for (let customField of index.options.customFields) {
searchFields.push({
name: customField.name,
field: customField.name,
type: customField.type
});
}
}
// Get filters from args
const filters = {};
for (let filterProperty of searchFields.map(s => s.field)) {
if (args[filterProperty]) {
filters[filterProperty] = args[filterProperty];
}
}
// Check and convert numeric filters
if ('count-sub-tasks' in filters) {
if (!convertNumericFilters(filters, 'count-sub-tasks')) {
utility.error('Count sub-tasks filter value must be numeric');
return;
}
}
if ('count-tags' in filters) {
if (!convertNumericFilters(filters, 'count-tags')) {
utility.error('Count tags filter value must be numeric');
return;
}
}
if ('count-relations' in filters) {
if (!convertNumericFilters(filters, 'count-relations')) {
utility.error('Count relations filter value must be numeric');
return;
}
}
if ('count-comments' in filters) {
if (!convertNumericFilters(filters, 'count-comments')) {
utility.error('Count comments filter value must be numeric');
return;
}
}
if ('workload' in filters){
if (!convertNumericFilters(filters, 'workload')) {
utility.error('Workload filter value must be numeric');
return;
}
}
if ('progress' in filters){
if (!convertNumericFilters(filters, 'progress')) {
utility.error('Progress filter value must be numeric');
return;
}
}
// Check date filters
if ('created' in filters) {
if (!convertDateFilters(filters, 'created')) {
utility.error('Unable to parse created date');
return;
}
}
if ('updated' in filters) {
if (!convertDateFilters(filters, 'updated')) {
utility.error('Unable to parse updated date');
return;
}
}
if ('started' in filters) {
if (!convertDateFilters(filters, 'started')) {
utility.error('Unable to parse started date');
return;
}
}
if ('completed' in filters) {
if (!convertDateFilters(filters, 'completed')) {
utility.error('Unable to parse completed date');
return;
}
}
if ('due' in filters) {
if (!convertDateFilters(filters, 'due')) {
utility.error('Unable to parse due date');
return;
}
}
if ('plannedStart' in filters) {
if (!convertDateFilters(filters, 'plannedStart')) {
utility.error('Unable to parse plannedStart date');
return;
}
}
if ('plannedFinish' in filters) {
if (!convertDateFilters(filters, 'plannedFinish')) {
utility.error('Unable to parse plannedFinish date');
return;
}
}
// Check and add custom field filters
if ('customFields' in index.options) {
for (let customField of index.options.customFields) {
if (customField.name in args) {
filters[customField.name] = args[customField.name];
switch (customField.type) {
case 'boolean':
if (typeof filters[customField.name] !== 'boolean') {
utility.error(`Custom field "${customField.name}" value is not a boolean`);
return;
}
break;
case 'number':
if (!convertNumericFilters(filters, customField.name)) {
utility.error(`Custom field "${customField.name}" value is not a number`);
return;
}
break;
case 'date':
if (!convertDateFilters(filters, customField.name)) {
utility.error(`Unable to parse date for custom field "${customField.name}"`);
return;
}
break;
default: break;
}
}
}
}
// Build search filters interactively
if (args.interactive) {
interactive()
.then(answers => {
inquirer
.prompt([
{
type: 'confirm',
name: 'nonquiet',
message: 'Show full task details in results?',
default: !args.quiet
},
{
type: 'confirm',
name: 'json',
message: 'Show results in JSON format?',
default: !!args.json
}
])
.then(otherAnswers => {
for (let filter of answers.filters) {
addFilterValue(
filters,
searchFields.find(s => s.name === filter.type).field,
filter.value
);
}
findTasks(filters, !otherAnswers.nonquiet, otherAnswers.json);
})
.catch(error => {
utility.error(error);
});
})
.catch(error => {
utility.error(error);
});
// Otherwise create task non-interactively
} else {
findTasks(filters, args.quiet, args.json);
}
};
|