joplin/CliClient/app/command-import-enex.js

64 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-07-10 20:03:46 +00:00
import { BaseCommand } from './base-command.js';
import { app } from './app.js';
import { _ } from 'lib/locale.js';
import { Folder } from 'lib/models/folder.js';
import { importEnex } from 'import-enex';
import { filename, basename } from 'lib/path-utils.js';
2017-08-04 16:50:12 +00:00
import { cliUtils } from './cli-utils.js';
2017-07-10 20:03:46 +00:00
class Command extends BaseCommand {
usage() {
2017-07-28 18:13:07 +00:00
return 'import-enex <file> [notebook]';
2017-07-10 20:03:46 +00:00
}
description() {
return _('Imports an Evernote notebook file (.enex file).');
}
options() {
return [
2017-07-18 18:21:03 +00:00
['-f, --force', _('Do not ask for confirmation.')],
2017-07-18 18:49:47 +00:00
['--fuzzy-matching', 'For debugging purposes. Do not use.'],
2017-07-10 20:03:46 +00:00
];
}
async action(args) {
let filePath = args.file;
let folder = null;
let folderTitle = args['notebook'];
2017-07-13 19:16:01 +00:00
let force = args.options.force === true;
2017-07-10 20:03:46 +00:00
2017-07-31 18:57:31 +00:00
if (!folderTitle) folderTitle = filename(filePath);
folder = await Folder.loadByField('title', folderTitle);
const msg = folder ? _('File "%s" will be imported into existing notebook "%s". Continue?', basename(filePath), folderTitle) : _('New notebook "%s" will be created and file "%s" will be imported into it. Continue?', folderTitle, basename(filePath));
2017-10-08 17:50:43 +00:00
const ok = force ? true : await this.prompt(msg);
2017-07-10 20:03:46 +00:00
if (!ok) return;
let options = {
fuzzyMatching: args.options['fuzzy-matching'] === true,
onProgress: (progressState) => {
let line = [];
line.push(_('Found: %d.', progressState.loaded));
line.push(_('Created: %d.', progressState.created));
if (progressState.updated) line.push(_('Updated: %d.', progressState.updated));
if (progressState.skipped) line.push(_('Skipped: %d.', progressState.skipped));
if (progressState.resourcesCreated) line.push(_('Resources: %d.', progressState.resourcesCreated));
if (progressState.notesTagged) line.push(_('Tagged: %d.', progressState.notesTagged));
2017-08-04 16:50:12 +00:00
cliUtils.redraw(line.join(' '));
2017-07-10 20:03:46 +00:00
},
onError: (error) => {
let s = error.trace ? error.trace : error.toString();
2017-10-07 16:30:27 +00:00
this.stdout(s);
2017-07-10 20:03:46 +00:00
},
}
folder = !folder ? await Folder.save({ title: folderTitle }) : folder;
2017-10-07 16:30:27 +00:00
this.stdout(_('Importing notes...'));
2017-07-10 20:03:46 +00:00
await importEnex(folder.id, filePath, options);
2017-08-04 16:50:12 +00:00
cliUtils.redrawDone();
2017-07-10 20:03:46 +00:00
}
}
module.exports = Command;