2017-07-10 22:03:46 +02:00
import { BaseCommand } from './base-command.js' ;
import { app } from './app.js' ;
import { _ } from 'lib/locale.js' ;
import { Folder } from 'lib/models/folder.js' ;
import { importEnex } from 'import-enex' ;
import { filename , basename } from 'lib/path-utils.js' ;
2017-08-04 18:50:12 +02:00
import { cliUtils } from './cli-utils.js' ;
2017-07-10 22:03:46 +02:00
class Command extends BaseCommand {
usage ( ) {
2017-07-28 20:13:07 +02:00
return 'import-enex <file> [notebook]' ;
2017-07-10 22:03:46 +02:00
}
description ( ) {
return _ ( 'Imports an Evernote notebook file (.enex file).' ) ;
}
options ( ) {
return [
2017-07-18 20:21:03 +02:00
[ '-f, --force' , _ ( 'Do not ask for confirmation.' ) ] ,
2017-07-10 22:03:46 +02:00
] ;
}
async action ( args ) {
let filePath = args . file ;
let folder = null ;
let folderTitle = args [ 'notebook' ] ;
2017-07-13 21:16:01 +02:00
let force = args . options . force === true ;
2017-07-10 22:03:46 +02:00
2017-07-31 20:57:31 +02: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 19:50:43 +02:00
const ok = force ? true : await this . prompt ( msg ) ;
2017-07-10 22:03:46 +02:00
if ( ! ok ) return ;
2017-10-24 22:22:57 +02:00
let lastProgress = '' ;
2017-07-10 22:03:46 +02: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 22:22:57 +02:00
lastProgress = line . join ( ' ' ) ;
cliUtils . redraw ( lastProgress ) ;
2017-07-10 22:03:46 +02:00
} ,
onError : ( error ) => {
let s = error . trace ? error . trace : error . toString ( ) ;
2017-10-07 18:30:27 +02:00
this . stdout ( s ) ;
2017-07-10 22:03:46 +02:00
} ,
}
folder = ! folder ? await Folder . save ( { title : folderTitle } ) : folder ;
2017-10-25 19:23:45 +02:00
app ( ) . gui ( ) . showConsole ( ) ;
2017-10-07 18:30:27 +02:00
this . stdout ( _ ( 'Importing notes...' ) ) ;
2017-07-10 22:03:46 +02:00
await importEnex ( folder . id , filePath , options ) ;
2017-08-04 18:50:12 +02:00
cliUtils . redrawDone ( ) ;
2017-10-24 22:22:57 +02:00
this . stdout ( _ ( 'The notes have been imported: %s' , lastProgress ) ) ;
2017-07-10 22:03:46 +02:00
}
}
module . exports = Command ;