2018-02-25 23:08:32 +02:00
|
|
|
const { BaseCommand } = require('./base-command.js');
|
|
|
|
const InteropService = require('lib/services/InteropService.js');
|
|
|
|
const BaseModel = require('lib/BaseModel.js');
|
2018-02-26 21:16:01 +02:00
|
|
|
const { cliUtils } = require('./cli-utils.js');
|
2018-02-25 23:08:32 +02:00
|
|
|
const { app } = require('./app.js');
|
|
|
|
const { _ } = require('lib/locale.js');
|
|
|
|
|
|
|
|
class Command extends BaseCommand {
|
|
|
|
usage() {
|
2018-02-26 21:16:01 +02:00
|
|
|
return 'import <path> [notebook]';
|
2018-02-25 23:08:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
description() {
|
|
|
|
return _('Imports data into Joplin.');
|
|
|
|
}
|
|
|
|
|
|
|
|
options() {
|
2018-02-27 22:04:38 +02:00
|
|
|
const service = new InteropService();
|
2019-07-30 09:35:42 +02:00
|
|
|
const formats = service
|
|
|
|
.modules()
|
2020-05-21 10:14:33 +02:00
|
|
|
.filter(m => m.type === 'importer')
|
|
|
|
.map(m => m.format);
|
2018-02-27 22:04:38 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
return [['--format <format>', _('Source format: %s', ['auto'].concat(formats).join(', '))], ['-f, --force', _('Do not ask for confirmation.')]];
|
2018-02-25 23:08:32 +02:00
|
|
|
}
|
2019-07-30 09:35:42 +02:00
|
|
|
|
2018-02-25 23:08:32 +02:00
|
|
|
async action(args) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder = await app().loadItem(BaseModel.TYPE_FOLDER, args.notebook);
|
2018-02-26 21:16:01 +02:00
|
|
|
|
|
|
|
if (args.notebook && !folder) throw new Error(_('Cannot find "%s".', args.notebook));
|
2018-02-26 20:43:50 +02:00
|
|
|
|
2018-02-25 23:08:32 +02:00
|
|
|
const importOptions = {};
|
|
|
|
importOptions.path = args.path;
|
2018-02-26 21:16:01 +02:00
|
|
|
importOptions.format = args.options.format ? args.options.format : 'auto';
|
2018-02-26 20:43:50 +02:00
|
|
|
importOptions.destinationFolderId = folder ? folder.id : null;
|
2018-02-25 23:08:32 +02:00
|
|
|
|
2018-02-26 21:16:01 +02:00
|
|
|
let lastProgress = '';
|
|
|
|
|
|
|
|
// onProgress/onError supported by Enex import only
|
|
|
|
|
2020-05-21 10:14:33 +02:00
|
|
|
importOptions.onProgress = progressState => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const line = [];
|
2018-02-26 21:16:01 +02:00
|
|
|
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));
|
|
|
|
lastProgress = line.join(' ');
|
|
|
|
cliUtils.redraw(lastProgress);
|
|
|
|
};
|
|
|
|
|
2020-05-21 10:14:33 +02:00
|
|
|
importOptions.onError = error => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const s = error.trace ? error.trace : error.toString();
|
2018-02-26 21:16:01 +02:00
|
|
|
this.stdout(s);
|
|
|
|
};
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
app()
|
|
|
|
.gui()
|
|
|
|
.showConsole();
|
2018-02-26 21:16:01 +02:00
|
|
|
this.stdout(_('Importing notes...'));
|
2018-02-25 23:08:32 +02:00
|
|
|
const service = new InteropService();
|
|
|
|
const result = await service.import(importOptions);
|
2020-05-21 10:14:33 +02:00
|
|
|
result.warnings.map(w => this.stdout(w));
|
2018-02-26 21:16:01 +02:00
|
|
|
cliUtils.redrawDone();
|
|
|
|
if (lastProgress) this.stdout(_('The notes have been imported: %s', lastProgress));
|
2018-02-25 23:08:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
module.exports = Command;
|