1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/packages/app-cli/app/main.js

101 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-06-25 18:30:44 +02:00
#!/usr/bin/env node
// Use njstrace to find out what Node.js might be spending time on
// var njstrace = require('njstrace').inject();
const compareVersion = require('compare-version');
const nodeVersion = process && process.versions && process.versions.node ? process.versions.node : '0.0.0';
if (compareVersion(nodeVersion, '10.0.0') < 0) {
console.error(`Joplin requires Node 10+. Detected version ${nodeVersion}`);
2018-01-09 21:56:38 +02:00
process.exit(1);
}
const { app } = require('./app.js');
const Folder = require('@joplin/lib/models/Folder').default;
const Resource = require('@joplin/lib/models/Resource').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;
const NoteTag = require('@joplin/lib/models/NoteTag').default;
const MasterKey = require('@joplin/lib/models/MasterKey').default;
const Setting = require('@joplin/lib/models/Setting').default;
const Revision = require('@joplin/lib/models/Revision').default;
const Logger = require('@joplin/lib/Logger').default;
const FsDriverNode = require('@joplin/lib/fs-driver-node').default;
2020-11-05 18:58:23 +02:00
const sharp = require('sharp');
const { shimInit } = require('@joplin/lib/shim-init-node.js');
const shim = require('@joplin/lib/shim').default;
const { _ } = require('@joplin/lib/locale');
const { FileApiDriverLocal } = require('@joplin/lib/file-api-driver-local');
const EncryptionService = require('@joplin/lib/services/e2ee/EncryptionService').default;
const envFromArgs = require('@joplin/lib/envFromArgs');
const nodeSqlite = require('sqlite3');
const env = envFromArgs(process.argv);
2017-06-22 23:52:27 +02:00
2017-07-05 23:52:31 +02:00
const fsDriver = new FsDriverNode();
Logger.fsDriver_ = fsDriver;
Resource.fsDriver_ = fsDriver;
2017-12-13 21:01:04 +02:00
EncryptionService.fsDriver_ = fsDriver;
FileApiDriverLocal.fsDriver_ = fsDriver;
2017-07-05 23:52:31 +02:00
2017-07-10 22:03:46 +02:00
// That's not good, but it's to avoid circular dependency issues
// in the BaseItem class.
BaseItem.loadClass('Note', Note);
BaseItem.loadClass('Folder', Folder);
BaseItem.loadClass('Resource', Resource);
BaseItem.loadClass('Tag', Tag);
BaseItem.loadClass('NoteTag', NoteTag);
BaseItem.loadClass('MasterKey', MasterKey);
BaseItem.loadClass('Revision', Revision);
2017-07-10 22:03:46 +02:00
Setting.setConstant('appId', `net.cozic.joplin${env === 'dev' ? 'dev' : ''}-cli`);
Setting.setConstant('appType', 'cli');
2017-06-29 22:52:52 +02:00
2020-11-05 18:58:23 +02:00
let keytar;
try {
keytar = shim.platformSupportsKeyChain() ? require('keytar') : null;
} catch (error) {
console.error('Cannot load keytar - keychain support will be disabled', error);
keytar = null;
}
function appVersion() {
const p = require('./package.json');
return p.version;
}
shimInit({ sharp, keytar, appVersion, nodeSqlite });
2017-06-22 23:52:27 +02:00
2017-08-04 18:50:12 +02:00
const application = app();
if (process.platform === 'win32') {
const rl = require('readline').createInterface({
2017-08-04 18:50:12 +02:00
input: process.stdin,
output: process.stdout,
2017-08-04 18:50:12 +02:00
});
rl.on('SIGINT', function() {
process.emit('SIGINT');
2017-08-04 18:50:12 +02:00
});
}
process.stdout.on('error', function(err) {
// https://stackoverflow.com/questions/12329816/error-write-epipe-when-piping-node-output-to-head#15884508
if (err.code === 'EPIPE') {
process.exit(0);
}
});
application.start(process.argv).catch(error => {
if (error.code === 'flagError') {
2018-03-07 21:11:55 +02:00
console.error(error.message);
console.error(_('Type `joplin help` for usage information.'));
2018-03-07 21:11:55 +02:00
} else {
console.error(_('Fatal error:'));
2018-03-07 21:11:55 +02:00
console.error(error);
}
process.exit(1);
});