2017-11-03 02:09:34 +02:00
const { BaseCommand } = require ( './base-command.js' ) ;
const { Exporter } = require ( 'lib/services/exporter.js' ) ;
const { BaseModel } = require ( 'lib/base-model.js' ) ;
const { Note } = require ( 'lib/models/note.js' ) ;
const { reg } = require ( 'lib/registry.js' ) ;
const { app } = require ( './app.js' ) ;
const { _ } = require ( 'lib/locale.js' ) ;
const fs = require ( 'fs-extra' ) ;
2017-08-20 16:29:18 +02:00
class Command extends BaseCommand {
usage ( ) {
2017-10-30 01:46:04 +02:00
return 'export <directory>' ;
2017-08-20 16:29:18 +02:00
}
description ( ) {
2017-10-30 01:46:04 +02:00
return _ ( 'Exports Joplin data to the given directory. By default, it will export the complete database including notebooks, notes, tags and resources.' ) ;
2017-08-20 16:29:18 +02:00
}
options ( ) {
return [
[ '--note <note>' , _ ( 'Exports only the given note.' ) ] ,
[ '--notebook <notebook>' , _ ( 'Exports only the given notebook.' ) ] ,
] ;
}
async action ( args ) {
let exportOptions = { } ;
2017-10-30 01:46:04 +02:00
exportOptions . destDir = args . directory ;
2017-08-20 16:29:18 +02:00
exportOptions . writeFile = ( filePath , data ) => {
return fs . writeFile ( filePath , data ) ;
} ;
exportOptions . copyFile = ( source , dest ) => {
return fs . copy ( source , dest , { overwrite : true } ) ;
} ;
if ( args . options . note ) {
const notes = await app ( ) . loadItems ( BaseModel . TYPE _NOTE , args . options . note , { parent : app ( ) . currentFolder ( ) } ) ;
if ( ! notes . length ) throw new Error ( _ ( 'Cannot find "%s".' , args . options . note ) ) ;
exportOptions . sourceNoteIds = notes . map ( ( n ) => n . id ) ;
} else if ( args . options . notebook ) {
const folders = await app ( ) . loadItems ( BaseModel . TYPE _FOLDER , args . options . notebook ) ;
if ( ! folders . length ) throw new Error ( _ ( 'Cannot find "%s".' , args . options . notebook ) ) ;
exportOptions . sourceFolderIds = folders . map ( ( n ) => n . id ) ;
}
const exporter = new Exporter ( ) ;
const result = await exporter . export ( exportOptions ) ;
reg . logger ( ) . info ( 'Export result: ' , result ) ;
}
}
module . exports = Command ;