2023-12-20 21:08:07 +02:00
import BaseCommand from './base-command' ;
import InteropService from '@joplin/lib/services/interop/InteropService' ;
import BaseModel from '@joplin/lib/BaseModel' ;
2024-01-20 16:29:21 +02:00
import app from './app' ;
2023-12-20 21:08:07 +02:00
import { _ } from '@joplin/lib/locale' ;
import { ExportOptions } from '@joplin/lib/services/interop/types' ;
2017-08-20 16:29:18 +02:00
class Command extends BaseCommand {
2023-12-20 21:08:07 +02:00
public override usage() {
2018-02-23 21:32:19 +02:00
return 'export <path>' ;
2017-08-20 16:29:18 +02:00
}
2023-12-20 21:08:07 +02:00
public override description() {
2018-02-23 21:32:19 +02:00
return _ ( 'Exports Joplin data to the given path. By default, it will export the complete database including notebooks, notes, tags and resources.' ) ;
2017-08-20 16:29:18 +02:00
}
2023-12-20 21:08:07 +02:00
public override options() {
2020-10-09 19:35:46 +02:00
const service = InteropService . instance ( ) ;
2019-07-30 09:35:42 +02:00
const formats = service
. module s ( )
2020-05-21 10:14:33 +02:00
. filter ( m = > m . type === 'exporter' && m . format !== 'html' )
. map ( m = > m . format + ( m . description ? ` ( ${ m . description } ) ` : '' ) ) ;
2018-02-27 22:04:38 +02:00
2019-07-30 09:35:42 +02:00
return [ [ '--format <format>' , _ ( 'Destination format: %s' , formats . join ( ', ' ) ) ] , [ '--note <note>' , _ ( 'Exports only the given note.' ) ] , [ '--notebook <notebook>' , _ ( 'Exports only the given notebook.' ) ] ] ;
2017-08-20 16:29:18 +02:00
}
2019-07-30 09:35:42 +02:00
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2023-12-20 21:08:07 +02:00
public override async action ( args : any ) {
const exportOptions : ExportOptions = { } ;
2018-02-23 21:32:19 +02:00
exportOptions . path = args . path ;
2018-02-25 23:08:32 +02:00
exportOptions . format = args . options . format ? args . options . format : 'jex' ;
2017-08-20 16:29:18 +02:00
2020-05-20 18:31:41 +02:00
if ( exportOptions . format === 'html' ) throw new Error ( 'HTML export is not supported. Please use the desktop application.' ) ;
2017-08-20 16:29:18 +02:00
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 ) ) ;
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2023-12-20 21:08:07 +02:00
exportOptions . sourceNoteIds = notes . map ( ( n : any ) = > n . id ) ;
2017-08-20 16:29:18 +02:00
} 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 ) ) ;
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2023-12-20 21:08:07 +02:00
exportOptions . sourceFolderIds = folders . map ( ( n : any ) = > n . id ) ;
2017-08-20 16:29:18 +02:00
}
2020-10-09 19:35:46 +02:00
const service = InteropService . instance ( ) ;
2018-02-23 21:32:19 +02:00
const result = await service . export ( exportOptions ) ;
2017-08-20 16:29:18 +02:00
2020-05-21 10:14:33 +02:00
result . warnings . map ( w = > this . stdout ( w ) ) ;
2017-08-20 16:29:18 +02:00
}
}
2019-07-30 09:35:42 +02:00
module .exports = Command ;