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

68 lines
2.2 KiB
JavaScript
Raw Normal View History

const { BaseCommand } = require('./base-command.js');
const { app } = require('./app.js');
const { _ } = require('lib/locale.js');
const { Folder } = require('lib/models/folder.js');
const { importEnex } = require('import-enex');
const { filename, basename } = require('lib/path-utils.js');
const { cliUtils } = require('./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-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;
2017-10-24 20:22:57 +00:00
let lastProgress = '';
2017-07-10 20:03:46 +00:00
let options = {
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-10-24 20:22:57 +00:00
lastProgress = line.join(' ');
cliUtils.redraw(lastProgress);
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;
app().gui().showConsole();
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-10-24 20:22:57 +00:00
this.stdout(_('The notes have been imported: %s', lastProgress));
2017-07-10 20:03:46 +00:00
}
}
module.exports = Command;