import BaseCommand from './base-command'; import app from './app'; import { _ } from '@joplin/lib/locale'; import { ModelType } from '@joplin/lib/BaseModel'; import Database from '@joplin/lib/database'; import Note from '@joplin/lib/models/Note'; import isNoteLockEnabled from '@joplin/lib/services/noteLock/isNoteLockEnabled'; class Command extends BaseCommand { public override usage() { return 'set [value]'; } public override description() { const fields = Note.fields(); const s = []; for (let i = 0; i < fields.length; i++) { const f = fields[i]; if (f.name === 'id') continue; s.push(`${f.name} (${Database.enumName('fieldType', f.type)})`); } return _('Sets the property of the given to the given [value]. Possible properties are:\n\n%s', s.join(', ')); } public override async action(args: { note: string; name: string; value?: string }) { const title = args['note']; const propName = args['name']; let propValue = args['value']; if (!propValue) propValue = ''; const notes = await app().loadItems(ModelType.Note, title); if (!notes.length) throw new Error(_('Cannot find "%s".', title)); for (let i = 0; i < notes.length; i++) { this.encryptionCheck(notes[i]); // Enabling the lock here would only fail deep inside the gated save, so refuse it outright. if (isNoteLockEnabled() && propName === 'is_locked') { throw new Error(_('The note lock state cannot be changed from the command line')); } const timestamp = Date.now(); const newNote: Record = { id: notes[i].id, type_: notes[i].type_, updated_time: timestamp, }; newNote[propName] = propValue; if (!newNote.id) newNote.created_time = timestamp; await Note.save(newNote, { autoTimestamp: false, // No auto-timestamp because user may have provided them }); } } } module.exports = Command;