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-import.js

75 lines
2.5 KiB
JavaScript
Raw Normal View History

const BaseCommand = require('./base-command').default;
const InteropService = require('@joplin/lib/services/interop/InteropService').default;
const BaseModel = require('@joplin/lib/BaseModel').default;
2018-02-26 21:16:01 +02:00
const { cliUtils } = require('./cli-utils.js');
const { app } = require('./app.js');
const { _ } = require('@joplin/lib/locale');
class Command extends BaseCommand {
usage() {
2018-02-26 21:16:01 +02:00
return 'import <path> [notebook]';
}
description() {
return _('Imports data into Joplin.');
}
options() {
const service = InteropService.instance();
const formats = service
.modules()
.filter(m => m.type === 'importer')
.map(m => m.format);
2018-02-27 22:04:38 +02:00
return [
['--format <format>', _('Source format: %s', ['auto'].concat(formats).join(', '))],
['-f, --force', _('Do not ask for confirmation.')],
['--output-format <output-format>', _('Output format: %s', 'md, html')],
];
}
async action(args) {
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
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-26 21:16:01 +02:00
let lastProgress = '';
// onProgress/onError supported by Enex import only
importOptions.onProgress = progressState => {
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);
};
importOptions.onError = error => {
const s = error.stack ? error.stack : error.toString();
2018-02-26 21:16:01 +02:00
this.stdout(s);
};
if (args.options.outputFormat) importOptions.outputFormat = args.options.outputFormat;
app().gui().showConsole();
2018-02-26 21:16:01 +02:00
this.stdout(_('Importing notes...'));
const service = InteropService.instance();
const result = await service.import(importOptions);
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));
}
}
module.exports = Command;