2020-11-07 17:59:37 +02:00
const BaseApplication = require ( '@joplin/lib/BaseApplication' ) . default ;
const { FoldersScreenUtils } = require ( '@joplin/lib/folders-screen-utils.js' ) ;
2020-11-08 18:46:48 +02:00
const ResourceService = require ( '@joplin/lib/services/ResourceService' ) . default ;
2020-11-07 17:59:37 +02:00
const BaseModel = require ( '@joplin/lib/BaseModel' ) . default ;
2021-01-22 19:41:11 +02:00
const Folder = require ( '@joplin/lib/models/Folder' ) . default ;
const BaseItem = require ( '@joplin/lib/models/BaseItem' ) . default ;
const Note = require ( '@joplin/lib/models/Note' ) . default ;
const Tag = require ( '@joplin/lib/models/Tag' ) . default ;
2020-11-07 17:59:37 +02:00
const Setting = require ( '@joplin/lib/models/Setting' ) . default ;
const { reg } = require ( '@joplin/lib/registry.js' ) ;
const { fileExtension } = require ( '@joplin/lib/path-utils' ) ;
2021-05-01 16:09:59 +02:00
const { splitCommandString } = require ( '@joplin/lib/string-utils' ) ;
2020-11-07 17:59:37 +02:00
const { _ } = require ( '@joplin/lib/locale' ) ;
2018-03-09 22:59:12 +02:00
const fs = require ( 'fs-extra' ) ;
const { cliUtils } = require ( './cli-utils.js' ) ;
2020-11-07 17:59:37 +02:00
const Cache = require ( '@joplin/lib/Cache' ) ;
2021-01-22 19:41:11 +02:00
const RevisionService = require ( '@joplin/lib/services/RevisionService' ) . default ;
2020-11-07 17:59:37 +02:00
const shim = require ( '@joplin/lib/shim' ) . default ;
2017-07-10 22:03:46 +02:00
2017-11-04 14:23:46 +02:00
class Application extends BaseApplication {
2017-07-10 22:03:46 +02:00
constructor ( ) {
2017-11-04 14:23:46 +02:00
super ( ) ;
2017-07-10 22:03:46 +02:00
this . showPromptString _ = true ;
2017-08-04 19:51:01 +02:00
this . commands _ = { } ;
this . commandMetadata _ = null ;
2017-08-04 18:50:12 +02:00
this . activeCommand _ = null ;
2017-08-04 19:11:10 +02:00
this . allCommandsLoaded _ = false ;
2017-08-21 20:32:43 +02:00
this . showStackTraces _ = false ;
2017-10-05 19:17:56 +02:00
this . gui _ = null ;
2018-01-17 19:59:33 +02:00
this . cache _ = new Cache ( ) ;
2017-07-10 22:03:46 +02:00
}
2017-10-09 00:34:01 +02:00
gui ( ) {
return this . gui _ ;
}
commandStdoutMaxWidth ( ) {
2017-10-24 22:22:57 +02:00
return this . gui ( ) . stdoutMaxWidth ( ) ;
2017-10-09 00:34:01 +02:00
}
2017-07-17 21:19:01 +02:00
async guessTypeAndLoadItem ( pattern , options = null ) {
let type = BaseModel . TYPE _NOTE ;
2018-03-09 22:59:12 +02:00
if ( pattern . indexOf ( '/' ) === 0 ) {
2017-07-17 21:19:01 +02:00
type = BaseModel . TYPE _FOLDER ;
pattern = pattern . substr ( 1 ) ;
}
return this . loadItem ( type , pattern , options ) ;
}
2017-07-15 17:35:40 +02:00
async loadItem ( type , pattern , options = null ) {
2020-03-14 01:46:14 +02:00
const output = await this . loadItems ( type , pattern , options ) ;
2017-09-10 19:32:04 +02:00
if ( output . length > 1 ) {
2017-10-27 00:22:36 +02:00
// output.sort((a, b) => { return a.user_updated_time < b.user_updated_time ? +1 : -1; });
2017-09-24 16:48:23 +02:00
2017-10-27 00:22:36 +02:00
// let answers = { 0: _('[Cancel]') };
// for (let i = 0; i < output.length; i++) {
// answers[i + 1] = output[i].title;
// }
2017-09-24 16:48:23 +02:00
2017-10-08 19:50:43 +02:00
// Not really useful with new UI?
throw new Error ( _ ( 'More than one item match "%s". Please narrow down your query.' , pattern ) ) ;
// let msg = _('More than one item match "%s". Please select one:', pattern);
// const response = await cliUtils.promptMcq(msg, answers);
// if (!response) return null;
2017-09-10 19:32:04 +02:00
2019-07-30 09:35:42 +02:00
// return output[response - 1];
2017-09-10 19:32:04 +02:00
} else {
return output . length ? output [ 0 ] : null ;
}
2017-07-10 22:03:46 +02:00
}
2017-07-11 20:17:23 +02:00
async loadItems ( type , pattern , options = null ) {
2018-03-09 22:59:12 +02:00
if ( type === 'folderOrNote' ) {
2017-10-27 00:22:36 +02:00
const folders = await this . loadItems ( BaseModel . TYPE _FOLDER , pattern , options ) ;
if ( folders . length ) return folders ;
return await this . loadItems ( BaseModel . TYPE _NOTE , pattern , options ) ;
}
2018-03-09 22:59:12 +02:00
pattern = pattern ? pattern . toString ( ) : '' ;
2017-07-22 20:16:16 +02:00
2017-07-15 17:35:40 +02:00
if ( type == BaseModel . TYPE _FOLDER && ( pattern == Folder . conflictFolderTitle ( ) || pattern == Folder . conflictFolderId ( ) ) ) return [ Folder . conflictFolder ( ) ] ;
2017-07-11 20:17:23 +02:00
if ( ! options ) options = { } ;
2017-07-13 23:26:45 +02:00
2017-07-11 20:17:23 +02:00
const parent = options . parent ? options . parent : app ( ) . currentFolder ( ) ;
const ItemClass = BaseItem . itemClass ( type ) ;
2019-07-30 09:35:42 +02:00
if ( type == BaseModel . TYPE _NOTE && pattern . indexOf ( '*' ) >= 0 ) {
// Handle it as pattern
2018-03-09 22:59:12 +02:00
if ( ! parent ) throw new Error ( _ ( 'No notebook selected.' ) ) ;
2017-07-11 20:17:23 +02:00
return await Note . previews ( parent . id , { titlePattern : pattern } ) ;
2019-07-30 09:35:42 +02:00
} else {
// Single item
2017-07-11 20:17:23 +02:00
let item = null ;
if ( type == BaseModel . TYPE _NOTE ) {
2018-03-09 22:59:12 +02:00
if ( ! parent ) throw new Error ( _ ( 'No notebook has been specified.' ) ) ;
item = await ItemClass . loadFolderNoteByField ( parent . id , 'title' , pattern ) ;
2017-07-11 20:17:23 +02:00
} else {
item = await ItemClass . loadByTitle ( pattern ) ;
}
if ( item ) return [ item ] ;
item = await ItemClass . load ( pattern ) ; // Load by id
if ( item ) return [ item ] ;
2017-09-10 19:32:04 +02:00
if ( pattern . length >= 2 ) {
return await ItemClass . loadByPartialId ( pattern ) ;
2017-07-11 20:17:23 +02:00
}
2017-07-10 22:47:01 +02:00
}
return [ ] ;
2017-07-10 22:03:46 +02:00
}
2017-10-20 00:02:13 +02:00
stdout ( text ) {
return this . gui ( ) . stdout ( text ) ;
}
2017-10-07 19:07:38 +02:00
setupCommand ( cmd ) {
2020-05-21 10:14:33 +02:00
cmd . setStdout ( text => {
2017-10-20 00:02:13 +02:00
return this . stdout ( text ) ;
2017-10-07 19:07:38 +02:00
} ) ;
2020-05-21 10:14:33 +02:00
cmd . setDispatcher ( action => {
2017-10-20 00:02:13 +02:00
if ( this . store ( ) ) {
return this . store ( ) . dispatch ( action ) ;
} else {
2019-09-13 00:16:42 +02:00
return ( ) => { } ;
2017-10-20 00:02:13 +02:00
}
2017-10-14 20:03:23 +02:00
} ) ;
2017-10-07 19:07:38 +02:00
cmd . setPrompt ( async ( message , options ) => {
2017-10-28 19:55:45 +02:00
if ( ! options ) options = { } ;
2018-03-09 22:59:12 +02:00
if ( ! options . type ) options . type = 'boolean' ;
if ( ! options . booleanAnswerDefault ) options . booleanAnswerDefault = 'y' ;
if ( ! options . answers ) options . answers = options . booleanAnswerDefault === 'y' ? [ _ ( 'Y' ) , _ ( 'n' ) ] : [ _ ( 'N' ) , _ ( 'y' ) ] ;
2017-10-28 19:55:45 +02:00
2018-03-09 22:59:12 +02:00
if ( options . type == 'boolean' ) {
2019-09-19 23:51:18 +02:00
message += ` ( ${ options . answers . join ( '/' ) } ) ` ;
2017-10-07 19:07:38 +02:00
}
2019-09-19 23:51:18 +02:00
let answer = await this . gui ( ) . prompt ( '' , ` ${ message } ` , options ) ;
2017-10-15 18:57:09 +02:00
2018-03-09 22:59:12 +02:00
if ( options . type === 'boolean' ) {
2017-10-28 19:55:45 +02:00
if ( answer === null ) return false ; // Pressed ESCAPE
if ( ! answer ) answer = options . answers [ 0 ] ;
2020-03-14 01:46:14 +02:00
const positiveIndex = options . booleanAnswerDefault == 'y' ? 0 : 1 ;
2017-10-28 19:55:45 +02:00
return answer . toLowerCase ( ) === options . answers [ positiveIndex ] . toLowerCase ( ) ;
2017-12-12 20:17:30 +02:00
} else {
return answer ;
2017-10-07 19:07:38 +02:00
}
} ) ;
return cmd ;
}
2017-10-09 22:29:49 +02:00
async exit ( code = 0 ) {
2017-10-24 21:40:15 +02:00
const doExit = async ( ) => {
this . gui ( ) . exit ( ) ;
2017-11-04 14:23:46 +02:00
await super . exit ( code ) ;
2017-10-24 21:40:15 +02:00
} ;
// Give it a few seconds to cancel otherwise exit anyway
2020-10-09 19:35:46 +02:00
shim . setTimeout ( async ( ) => {
2017-10-24 21:40:15 +02:00
await doExit ( ) ;
} , 5000 ) ;
2017-11-24 20:06:30 +02:00
if ( await reg . syncTarget ( ) . syncStarted ( ) ) {
2018-03-09 22:59:12 +02:00
this . stdout ( _ ( 'Cancelling background synchronisation... Please wait.' ) ) ;
2017-11-24 20:06:30 +02:00
const sync = await reg . syncTarget ( ) . synchronizer ( ) ;
2017-10-24 21:40:15 +02:00
await sync . cancel ( ) ;
}
await doExit ( ) ;
2017-10-09 22:29:49 +02:00
}
2017-12-07 20:12:46 +02:00
commands ( uiType = null ) {
if ( ! this . allCommandsLoaded _ ) {
2020-05-21 10:14:33 +02:00
fs . readdirSync ( _ _dirname ) . forEach ( path => {
2018-03-09 22:59:12 +02:00
if ( path . indexOf ( 'command-' ) !== 0 ) return ;
2019-07-30 09:35:42 +02:00
const ext = fileExtension ( path ) ;
2018-03-09 22:59:12 +02:00
if ( ext != 'js' ) return ;
2017-12-07 20:12:46 +02:00
2020-03-14 01:46:14 +02:00
const CommandClass = require ( ` ./ ${ path } ` ) ;
2017-12-07 20:12:46 +02:00
let cmd = new CommandClass ( ) ;
if ( ! cmd . enabled ( ) ) return ;
cmd = this . setupCommand ( cmd ) ;
this . commands _ [ cmd . name ( ) ] = cmd ;
} ) ;
this . allCommandsLoaded _ = true ;
}
2017-08-04 19:11:10 +02:00
2017-12-07 20:12:46 +02:00
if ( uiType !== null ) {
2020-03-14 01:46:14 +02:00
const temp = [ ] ;
for ( const n in this . commands _ ) {
2017-12-07 20:12:46 +02:00
if ( ! this . commands _ . hasOwnProperty ( n ) ) continue ;
const c = this . commands _ [ n ] ;
if ( ! c . supportsUi ( uiType ) ) continue ;
temp [ n ] = c ;
}
return temp ;
}
2017-08-04 19:11:10 +02:00
return this . commands _ ;
}
async commandNames ( ) {
const metadata = await this . commandMetadata ( ) ;
2020-03-14 01:46:14 +02:00
const output = [ ] ;
for ( const n in metadata ) {
2017-08-04 19:11:10 +02:00
if ( ! metadata . hasOwnProperty ( n ) ) continue ;
output . push ( n ) ;
}
return output ;
2017-07-10 22:03:46 +02:00
}
2017-08-04 19:51:01 +02:00
async commandMetadata ( ) {
if ( this . commandMetadata _ ) return this . commandMetadata _ ;
2017-07-24 22:36:49 +02:00
2018-03-09 22:59:12 +02:00
let output = await this . cache _ . getItem ( 'metadata' ) ;
2018-01-17 19:59:33 +02:00
if ( output ) {
2017-08-04 19:51:01 +02:00
this . commandMetadata _ = output ;
return Object . assign ( { } , this . commandMetadata _ ) ;
}
2017-08-04 19:11:10 +02:00
const commands = this . commands ( ) ;
2017-07-24 22:36:49 +02:00
2017-08-04 19:51:01 +02:00
output = { } ;
2020-03-14 01:46:14 +02:00
for ( const n in commands ) {
2017-08-04 19:11:10 +02:00
if ( ! commands . hasOwnProperty ( n ) ) continue ;
const cmd = commands [ n ] ;
2017-08-04 19:51:01 +02:00
output [ n ] = cmd . metadata ( ) ;
2017-07-24 22:36:49 +02:00
}
2017-08-04 19:51:01 +02:00
2018-03-09 22:59:12 +02:00
await this . cache _ . setItem ( 'metadata' , output , 1000 * 60 * 60 * 24 ) ;
2017-08-04 19:51:01 +02:00
this . commandMetadata _ = output ;
return Object . assign ( { } , this . commandMetadata _ ) ;
2017-07-24 22:36:49 +02:00
}
2017-11-05 02:17:48 +02:00
hasGui ( ) {
return this . gui ( ) && ! this . gui ( ) . isDummy ( ) ;
}
2017-08-03 19:48:14 +02:00
findCommandByName ( name ) {
2017-08-04 19:11:10 +02:00
if ( this . commands _ [ name ] ) return this . commands _ [ name ] ;
2017-08-03 19:48:14 +02:00
let CommandClass = null ;
try {
2019-09-19 23:51:18 +02:00
CommandClass = require ( ` ${ _ _dirname } /command- ${ name } .js ` ) ;
2017-08-03 19:48:14 +02:00
} catch ( error ) {
2018-03-09 22:59:12 +02:00
if ( error . message && error . message . indexOf ( 'Cannot find module' ) >= 0 ) {
2020-03-14 01:46:14 +02:00
const e = new Error ( _ ( 'No such command: %s' , name ) ) ;
2018-03-09 22:59:12 +02:00
e . type = 'notFound' ;
2017-12-06 21:29:58 +02:00
throw e ;
} else {
throw error ;
}
2017-08-03 19:48:14 +02:00
}
2017-10-06 19:38:17 +02:00
2017-08-03 19:48:14 +02:00
let cmd = new CommandClass ( ) ;
2017-10-07 19:07:38 +02:00
cmd = this . setupCommand ( cmd ) ;
2017-08-04 19:11:10 +02:00
this . commands _ [ name ] = cmd ;
return this . commands _ [ name ] ;
2017-08-03 19:48:14 +02:00
}
2017-10-20 00:02:13 +02:00
dummyGui ( ) {
return {
2019-07-30 09:35:42 +02:00
isDummy : ( ) => {
return true ;
} ,
prompt : ( initialText = '' , promptString = '' , options = null ) => {
return cliUtils . prompt ( initialText , promptString , options ) ;
} ,
2017-10-20 00:02:13 +02:00
showConsole : ( ) => { } ,
maximizeConsole : ( ) => { } ,
2020-05-21 10:14:33 +02:00
stdout : text => {
2019-07-30 09:35:42 +02:00
console . info ( text ) ;
} ,
2019-09-13 00:16:42 +02:00
fullScreen : ( ) => { } ,
2017-10-24 21:40:15 +02:00
exit : ( ) => { } ,
2019-09-13 00:16:42 +02:00
showModalOverlay : ( ) => { } ,
2017-10-24 21:52:26 +02:00
hideModalOverlay : ( ) => { } ,
2019-07-30 09:35:42 +02:00
stdoutMaxWidth : ( ) => {
return 100 ;
} ,
2017-12-28 21:14:03 +02:00
forceRender : ( ) => { } ,
termSaveState : ( ) => { } ,
2019-09-13 00:16:42 +02:00
termRestoreState : ( ) => { } ,
2017-10-20 00:02:13 +02:00
} ;
}
2017-08-03 19:48:14 +02:00
async execCommand ( argv ) {
2018-03-09 22:59:12 +02:00
if ( ! argv . length ) return this . execCommand ( [ 'help' ] ) ;
2018-03-14 19:28:41 +02:00
// reg.logger().debug('execCommand()', argv);
2017-08-03 19:48:14 +02:00
const commandName = argv [ 0 ] ;
2017-08-04 18:50:12 +02:00
this . activeCommand _ = this . findCommandByName ( commandName ) ;
2017-10-24 23:37:36 +02:00
2017-10-17 19:18:31 +02:00
let outException = null ;
try {
2019-07-30 09:35:42 +02:00
if ( this . gui ( ) . isDummy ( ) && ! this . activeCommand _ . supportsUi ( 'cli' ) ) throw new Error ( _ ( 'The command "%s" is only available in GUI mode' , this . activeCommand _ . name ( ) ) ) ;
2017-10-23 23:48:29 +02:00
const cmdArgs = cliUtils . makeCommandArgs ( this . activeCommand _ , argv ) ;
2017-10-17 19:18:31 +02:00
await this . activeCommand _ . action ( cmdArgs ) ;
} catch ( error ) {
outException = error ;
}
2017-10-14 20:03:23 +02:00
this . activeCommand _ = null ;
2017-10-17 19:18:31 +02:00
if ( outException ) throw outException ;
2017-08-04 18:50:12 +02:00
}
2017-08-20 16:29:18 +02:00
currentCommand ( ) {
return this . activeCommand _ ;
2017-08-03 19:48:14 +02:00
}
2018-02-17 00:53:53 +02:00
async loadKeymaps ( ) {
const defaultKeyMap = [
2019-07-30 09:35:42 +02:00
{ keys : [ ':' ] , type : 'function' , command : 'enter_command_line_mode' } ,
{ keys : [ 'TAB' ] , type : 'function' , command : 'focus_next' } ,
{ keys : [ 'SHIFT_TAB' ] , type : 'function' , command : 'focus_previous' } ,
{ keys : [ 'UP' ] , type : 'function' , command : 'move_up' } ,
{ keys : [ 'DOWN' ] , type : 'function' , command : 'move_down' } ,
{ keys : [ 'PAGE_UP' ] , type : 'function' , command : 'page_up' } ,
{ keys : [ 'PAGE_DOWN' ] , type : 'function' , command : 'page_down' } ,
{ keys : [ 'ENTER' ] , type : 'function' , command : 'activate' } ,
{ keys : [ 'DELETE' , 'BACKSPACE' ] , type : 'function' , command : 'delete' } ,
2020-05-22 13:50:56 +02:00
{ keys : [ 'n' ] , type : 'function' , command : 'next_link' } ,
{ keys : [ 'b' ] , type : 'function' , command : 'previous_link' } ,
{ keys : [ 'o' ] , type : 'function' , command : 'open_link' } ,
2019-07-30 09:35:42 +02:00
{ keys : [ ' ' ] , command : 'todo toggle $n' } ,
{ keys : [ 'tc' ] , type : 'function' , command : 'toggle_console' } ,
{ keys : [ 'tm' ] , type : 'function' , command : 'toggle_metadata' } ,
{ keys : [ '/' ] , type : 'prompt' , command : 'search ""' , cursorPosition : - 2 } ,
{ keys : [ 'mn' ] , type : 'prompt' , command : 'mknote ""' , cursorPosition : - 2 } ,
{ keys : [ 'mt' ] , type : 'prompt' , command : 'mktodo ""' , cursorPosition : - 2 } ,
{ keys : [ 'mb' ] , type : 'prompt' , command : 'mkbook ""' , cursorPosition : - 2 } ,
{ keys : [ 'yn' ] , type : 'prompt' , command : 'cp $n ""' , cursorPosition : - 2 } ,
{ keys : [ 'dn' ] , type : 'prompt' , command : 'mv $n ""' , cursorPosition : - 2 } ,
2018-02-17 00:53:53 +02:00
] ;
// Filter the keymap item by command so that items in keymap.json can override
// the default ones.
const itemsByCommand = { } ;
for ( let i = 0 ; i < defaultKeyMap . length ; i ++ ) {
2019-07-30 09:35:42 +02:00
itemsByCommand [ defaultKeyMap [ i ] . command ] = defaultKeyMap [ i ] ;
2018-02-17 00:53:53 +02:00
}
2019-09-19 23:51:18 +02:00
const filePath = ` ${ Setting . value ( 'profileDir' ) } /keymap.json ` ;
2018-02-17 00:53:53 +02:00
if ( await fs . pathExists ( filePath ) ) {
try {
2018-03-09 22:59:12 +02:00
let configString = await fs . readFile ( filePath , 'utf-8' ) ;
configString = configString . replace ( /^\s*\/\/.*/ , '' ) ; // Strip off comments
2018-02-17 00:53:53 +02:00
const keymap = JSON . parse ( configString ) ;
for ( let keymapIndex = 0 ; keymapIndex < keymap . length ; keymapIndex ++ ) {
const item = keymap [ keymapIndex ] ;
itemsByCommand [ item . command ] = item ;
}
} catch ( error ) {
2018-03-09 22:59:12 +02:00
let msg = error . message ? error . message : '' ;
2019-09-19 23:51:18 +02:00
msg = ` Could not load keymap ${ filePath } \n ${ msg } ` ;
2018-02-17 00:53:53 +02:00
error . message = msg ;
throw error ;
}
}
const output = [ ] ;
2020-03-14 01:46:14 +02:00
for ( const n in itemsByCommand ) {
2018-02-17 00:53:53 +02:00
if ( ! itemsByCommand . hasOwnProperty ( n ) ) continue ;
output . push ( itemsByCommand [ n ] ) ;
}
2019-10-28 22:10:03 +02:00
// Map reserved shortcuts to their equivalent key
// https://github.com/cronvel/terminal-kit/issues/101
for ( let i = 0 ; i < output . length ; i ++ ) {
2020-05-21 10:14:33 +02:00
const newKeys = output [ i ] . keys . map ( k => {
2019-10-28 22:10:03 +02:00
k = k . replace ( /CTRL_H/g , 'BACKSPACE' ) ;
k = k . replace ( /CTRL_I/g , 'TAB' ) ;
k = k . replace ( /CTRL_M/g , 'ENTER' ) ;
return k ;
} ) ;
output [ i ] . keys = newKeys ;
}
2018-02-17 00:53:53 +02:00
return output ;
}
2021-05-01 16:09:59 +02:00
async commandList ( argv ) {
if ( argv . length && argv [ 0 ] === 'batch' ) {
const commands = [ ] ;
const commandLines = ( await fs . readFile ( argv [ 1 ] , 'utf-8' ) ) . split ( '\n' ) ;
for ( const commandLine of commandLines ) {
if ( ! commandLine . trim ( ) ) continue ;
const splitted = splitCommandString ( commandLine . trim ( ) ) ;
commands . push ( splitted ) ;
}
return commands ;
} else {
return [ argv ] ;
}
}
2021-08-11 17:23:37 +02:00
// We need this special case here because by the time the `version` command
// runs, the keychain has already been setup.
checkIfKeychainEnabled ( argv ) {
return argv . indexOf ( 'version' ) < 0 ;
}
2017-11-04 14:23:46 +02:00
async start ( argv ) {
2021-08-11 17:23:37 +02:00
const keychainEnabled = this . checkIfKeychainEnabled ( argv ) ;
argv = await super . start ( argv , { keychainEnabled } ) ;
2017-07-24 20:01:40 +02:00
2019-07-30 09:35:42 +02:00
cliUtils . setStdout ( object => {
2017-10-25 19:41:36 +02:00
return this . stdout ( object ) ;
} ) ;
2017-10-20 00:02:13 +02:00
// If we have some arguments left at this point, it's a command
// so execute it.
if ( argv . length ) {
this . gui _ = this . dummyGui ( ) ;
2018-03-09 22:59:12 +02:00
this . currentFolder _ = await Folder . load ( Setting . value ( 'activeFolderId' ) ) ;
2017-12-07 20:12:46 +02:00
2019-09-06 19:29:40 +02:00
await this . applySettingsSideEffects ( ) ;
2017-10-20 00:02:13 +02:00
try {
2021-05-01 16:09:59 +02:00
const commands = await this . commandList ( argv ) ;
for ( const command of commands ) {
await this . execCommand ( command ) ;
}
2017-10-20 00:02:13 +02:00
} catch ( error ) {
if ( this . showStackTraces _ ) {
2018-02-25 19:01:16 +02:00
console . error ( error ) ;
2017-10-20 00:02:13 +02:00
} else {
console . info ( error . message ) ;
}
2018-02-25 19:01:16 +02:00
process . exit ( 1 ) ;
2017-10-20 00:02:13 +02:00
}
2019-02-09 21:04:00 +02:00
await Setting . saveAll ( ) ;
2019-10-29 11:02:42 +02:00
// Need to call exit() explicitly, otherwise Node wait for any timeout to complete
2019-02-09 21:04:00 +02:00
// https://stackoverflow.com/questions/18050095
process . exit ( 0 ) ;
2019-07-30 09:35:42 +02:00
} else {
// Otherwise open the GUI
2017-11-04 15:23:15 +02:00
this . initRedux ( ) ;
2017-10-20 00:02:13 +02:00
2018-02-17 00:53:53 +02:00
const keymap = await this . loadKeymaps ( ) ;
2018-03-09 22:59:12 +02:00
const AppGui = require ( './app-gui.js' ) ;
2018-02-17 00:53:53 +02:00
this . gui _ = new AppGui ( this , this . store ( ) , keymap ) ;
2020-11-19 17:25:02 +02:00
this . gui _ . setLogger ( this . logger ( ) ) ;
2017-10-20 00:02:13 +02:00
await this . gui _ . start ( ) ;
2017-10-21 18:53:43 +02:00
// Since the settings need to be loaded before the store is created, it will never
2017-11-08 23:22:24 +02:00
// receive the SETTING_UPDATE_ALL even, which mean state.settings will not be
2017-10-21 18:53:43 +02:00
// initialised. So we manually call dispatchUpdateAll() to force an update.
Setting . dispatchUpdateAll ( ) ;
2017-10-20 00:02:13 +02:00
await FoldersScreenUtils . refreshFolders ( ) ;
2017-10-22 19:12:16 +02:00
const tags = await Tag . allWithNotes ( ) ;
2018-03-16 16:32:47 +02:00
ResourceService . runInBackground ( ) ;
2019-07-30 09:35:42 +02:00
2019-05-06 22:35:29 +02:00
RevisionService . instance ( ) . runInBackground ( ) ;
2018-03-15 20:08:46 +02:00
2017-10-22 19:12:16 +02:00
this . dispatch ( {
2018-03-09 22:59:12 +02:00
type : 'TAG_UPDATE_ALL' ,
2017-12-14 19:58:10 +02:00
items : tags ,
2017-10-22 19:12:16 +02:00
} ) ;
2017-10-20 00:02:13 +02:00
this . store ( ) . dispatch ( {
2018-03-09 22:59:12 +02:00
type : 'FOLDER_SELECT' ,
id : Setting . value ( 'activeFolderId' ) ,
2017-10-20 00:02:13 +02:00
} ) ;
}
2017-07-10 22:03:46 +02:00
}
}
let application _ = null ;
function app ( ) {
if ( application _ ) return application _ ;
application _ = new Application ( ) ;
return application _ ;
}
2019-07-30 09:35:42 +02:00
module . exports = { app } ;