2023-12-20 21:08:07 +02:00
|
|
|
import BaseCommand from './base-command';
|
2024-01-20 16:29:21 +02:00
|
|
|
import app from './app';
|
2023-12-20 21:08:07 +02:00
|
|
|
import { _ } from '@joplin/lib/locale';
|
|
|
|
import BaseModel from '@joplin/lib/BaseModel';
|
|
|
|
import Database from '@joplin/lib/database';
|
|
|
|
import Note from '@joplin/lib/models/Note';
|
2017-07-10 22:03:46 +02:00
|
|
|
|
|
|
|
class Command extends BaseCommand {
|
2023-12-20 21:08:07 +02:00
|
|
|
public override usage() {
|
2017-07-28 20:13:07 +02:00
|
|
|
return 'set <note> <name> [value]';
|
2017-07-10 22:03:46 +02:00
|
|
|
}
|
|
|
|
|
2023-12-20 21:08:07 +02:00
|
|
|
public override description() {
|
2017-12-07 20:12:46 +02:00
|
|
|
const fields = Note.fields();
|
|
|
|
const s = [];
|
|
|
|
for (let i = 0; i < fields.length; i++) {
|
|
|
|
const f = fields[i];
|
|
|
|
if (f.name === 'id') continue;
|
2019-09-19 23:51:18 +02:00
|
|
|
s.push(`${f.name} (${Database.enumName('fieldType', f.type)})`);
|
2017-12-07 20:12:46 +02:00
|
|
|
}
|
2017-07-10 22:03:46 +02:00
|
|
|
|
2017-12-07 20:12:46 +02:00
|
|
|
return _('Sets the property <name> of the given <note> to the given [value]. Possible properties are:\n\n%s', s.join(', '));
|
2017-08-04 18:02:43 +02:00
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-12-20 21:08:07 +02:00
|
|
|
public override async action(args: any) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const title = args['note'];
|
|
|
|
const propName = args['name'];
|
2017-07-10 22:03:46 +02:00
|
|
|
let propValue = args['value'];
|
|
|
|
if (!propValue) propValue = '';
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const notes = await app().loadItems(BaseModel.TYPE_NOTE, title);
|
2017-07-18 20:21:03 +02:00
|
|
|
if (!notes.length) throw new Error(_('Cannot find "%s".', title));
|
2017-07-10 22:03:46 +02:00
|
|
|
|
2017-07-11 20:17:23 +02:00
|
|
|
for (let i = 0; i < notes.length; i++) {
|
2017-12-26 12:38:53 +02:00
|
|
|
this.encryptionCheck(notes[i]);
|
|
|
|
|
2021-09-08 11:32:44 +02:00
|
|
|
const timestamp = Date.now();
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-12-20 21:08:07 +02:00
|
|
|
const newNote: any = {
|
2017-07-11 20:17:23 +02:00
|
|
|
id: notes[i].id,
|
|
|
|
type_: notes[i].type_,
|
2021-09-08 11:32:44 +02:00
|
|
|
updated_time: timestamp,
|
2017-07-11 20:17:23 +02:00
|
|
|
};
|
|
|
|
newNote[propName] = propValue;
|
2019-10-11 19:49:47 +02:00
|
|
|
|
2021-09-08 11:32:44 +02:00
|
|
|
if (!newNote.id) newNote.created_time = timestamp;
|
2019-10-11 19:49:47 +02:00
|
|
|
|
|
|
|
await Note.save(newNote, {
|
|
|
|
autoTimestamp: false, // No auto-timestamp because user may have provided them
|
|
|
|
});
|
2017-07-10 22:03:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
module.exports = Command;
|