2017-11-03 02:09:34 +02:00
const { BaseCommand } = require ( './base-command.js' ) ;
const { app } = require ( './app.js' ) ;
const { _ } = require ( 'lib/locale.js' ) ;
2017-12-14 20:12:14 +02:00
const Tag = require ( 'lib/models/Tag.js' ) ;
const BaseModel = require ( 'lib/BaseModel.js' ) ;
2018-08-13 23:28:19 +02:00
const { time } = require ( 'lib/time-utils.js' ) ;
2017-07-10 22:03:46 +02:00
class Command extends BaseCommand {
usage ( ) {
2017-08-04 18:02:43 +02:00
return 'tag <tag-command> [tag] [note]' ;
2017-07-10 22:03:46 +02:00
}
description ( ) {
2019-10-30 11:43:47 +02:00
return _ ( '<tag-command> can be "add", "remove", "list", or "notetags" to assign or remove [tag] from [note], to list notes associated with [tag], or to list tags associated with [note]. The command `tag list` can be used to list all the tags (use -l for long option).' ) ;
2017-07-10 22:03:46 +02:00
}
2018-08-13 23:28:19 +02:00
2018-09-03 22:18:15 +02:00
options ( ) {
2019-07-30 09:35:42 +02:00
return [ [ '-l, --long' , _ ( 'Use long list format. Format is ID, NOTE_COUNT (for notebook), DATE, TODO_CHECKED (for to-dos), TITLE' ) ] ] ;
2018-09-03 22:18:15 +02:00
}
2018-08-13 23:28:19 +02:00
2017-07-10 22:03:46 +02:00
async action ( args ) {
let tag = null ;
2020-03-14 01:46:14 +02:00
const options = args . options ;
2018-08-13 23:28:19 +02:00
2017-07-10 22:03:46 +02:00
if ( args . tag ) tag = await app ( ) . loadItem ( BaseModel . TYPE _TAG , args . tag ) ;
2017-07-11 20:17:23 +02:00
let notes = [ ] ;
if ( args . note ) {
notes = await app ( ) . loadItems ( BaseModel . TYPE _NOTE , args . note ) ;
}
2017-07-10 22:03:46 +02:00
2017-08-04 18:02:43 +02:00
const command = args [ 'tag-command' ] ;
2017-07-10 22:03:46 +02:00
2017-08-04 18:02:43 +02:00
if ( command == 'remove' && ! tag ) throw new Error ( _ ( 'Cannot find "%s".' , args . tag ) ) ;
if ( command == 'add' ) {
2017-07-18 20:21:03 +02:00
if ( ! notes . length ) throw new Error ( _ ( 'Cannot find "%s".' , args . note ) ) ;
2017-11-12 01:13:14 +02:00
if ( ! tag ) tag = await Tag . save ( { title : args . tag } , { userSideValidation : true } ) ;
2017-07-11 20:17:23 +02:00
for ( let i = 0 ; i < notes . length ; i ++ ) {
await Tag . addNote ( tag . id , notes [ i ] . id ) ;
}
2017-08-04 18:02:43 +02:00
} else if ( command == 'remove' ) {
2017-07-18 20:21:03 +02:00
if ( ! tag ) throw new Error ( _ ( 'Cannot find "%s".' , args . tag ) ) ;
if ( ! notes . length ) throw new Error ( _ ( 'Cannot find "%s".' , args . note ) ) ;
2017-07-11 20:17:23 +02:00
for ( let i = 0 ; i < notes . length ; i ++ ) {
await Tag . removeNote ( tag . id , notes [ i ] . id ) ;
}
2017-08-04 18:02:43 +02:00
} else if ( command == 'list' ) {
2017-07-10 22:03:46 +02:00
if ( tag ) {
2020-03-14 01:46:14 +02:00
const notes = await Tag . notes ( tag . id ) ;
2020-05-20 18:16:43 +02:00
notes . map ( ( note ) => {
2018-09-03 22:18:15 +02:00
let line = '' ;
2018-09-05 12:46:49 +02:00
if ( options . long ) {
line += BaseModel . shortId ( note . id ) ;
line += ' ' ;
line += time . formatMsToLocal ( note . user _updated _time ) ;
line += ' ' ;
}
if ( note . is _todo ) {
line += '[' ;
if ( note . todo _completed ) {
2019-07-30 09:35:42 +02:00
line += 'X' ;
2018-09-05 12:46:49 +02:00
} else {
line += ' ' ;
}
line += '] ' ;
} else {
line += ' ' ;
}
line += note . title ;
this . stdout ( line ) ;
2018-09-03 22:18:15 +02:00
} ) ;
2017-07-10 22:03:46 +02:00
} else {
2020-03-14 01:46:14 +02:00
const tags = await Tag . all ( ) ;
2020-05-20 18:16:43 +02:00
tags . map ( ( tag ) => {
2019-07-30 09:35:42 +02:00
this . stdout ( tag . title ) ;
} ) ;
2017-07-10 22:03:46 +02:00
}
2019-10-30 11:43:47 +02:00
} else if ( command == 'notetags' ) {
if ( args . tag ) {
2019-10-30 11:54:32 +02:00
const note = await app ( ) . loadItem ( BaseModel . TYPE _NOTE , args . tag ) ;
if ( ! note ) throw new Error ( _ ( 'Cannot find "%s".' , args . tag ) ) ;
const tags = await Tag . tagsByNoteId ( note . id ) ;
2020-05-20 18:16:43 +02:00
tags . map ( ( tag ) => {
2019-10-30 11:43:47 +02:00
this . stdout ( tag . title ) ;
} ) ;
} else {
2019-10-30 20:12:06 +02:00
throw new Error ( _ ( 'Cannot find "%s".' , '' ) ) ;
2019-10-30 11:43:47 +02:00
}
2017-07-10 22:03:46 +02:00
} else {
2019-10-30 20:12:06 +02:00
throw new Error ( _ ( 'Invalid command: "%s"' , command ) ) ;
2017-07-10 22:03:46 +02:00
}
}
}
2019-07-30 09:35:42 +02:00
module . exports = Command ;