1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-18 09:35:20 +02:00
joplin/packages/app-cli/app/command-mv.ts

54 lines
1.8 KiB
TypeScript
Raw Normal View History

import BaseCommand from './base-command';
2024-01-20 16:29:21 +02:00
import app from './app';
import { _ } from '@joplin/lib/locale';
import BaseModel from '@joplin/lib/BaseModel';
import Folder from '@joplin/lib/models/Folder';
import Note from '@joplin/lib/models/Note';
2017-07-10 22:03:46 +02:00
class Command extends BaseCommand {
public override usage() {
return 'mv <item> [notebook]';
2017-07-10 22:03:46 +02:00
}
public override description() {
return _('Moves the given <item> to [notebook]');
2017-07-10 22:03:46 +02:00
}
public override async action(args: any) {
const pattern = args['item'];
2017-08-04 18:02:43 +02:00
const destination = args['notebook'];
let folder = null;
if (destination !== 'root') {
folder = await app().loadItem(BaseModel.TYPE_FOLDER, destination);
if (!folder) throw new Error(_('Cannot find "%s".', destination));
}
2017-07-10 22:03:46 +02:00
const destinationDuplicates = await Folder.search({ titlePattern: destination, limit: 2 });
if (destinationDuplicates.length > 1) {
throw new Error(_('Ambiguous notebook "%s". Please use short notebook id instead - press "ti" to see the short notebook id', destination));
}
2017-07-10 22:03:46 +02:00
const itemFolder = await app().loadItem(BaseModel.TYPE_FOLDER, pattern);
if (itemFolder) {
const sourceDuplicates = await Folder.search({ titlePattern: pattern, limit: 2 });
if (sourceDuplicates.length > 1) {
throw new Error(_('Ambiguous notebook "%s". Please use notebook id instead - press "ti" to see the short notebook id or use $b for current selected notebook', pattern));
}
if (destination === 'root') {
await Folder.moveToFolder(itemFolder.id, '');
} else {
await Folder.moveToFolder(itemFolder.id, folder.id);
}
} else {
const notes = await app().loadItems(BaseModel.TYPE_NOTE, pattern);
if (notes.length === 0) throw new Error(_('Cannot find "%s".', pattern));
for (let i = 0; i < notes.length; i++) {
await Note.moveToFolder(notes[i].id, folder.id);
}
2017-07-10 22:03:46 +02:00
}
}
}
module.exports = Command;