1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Moved commands to separate files

This commit is contained in:
Laurent Cozic 2017-07-10 20:03:46 +00:00
parent e0184d94c8
commit cb30035060
21 changed files with 1218 additions and 1000 deletions

332
CliClient/app/app.js Normal file
View File

@ -0,0 +1,332 @@
import { FileApi } from 'lib/file-api.js';
import { FileApiDriverOneDrive } from 'lib/file-api-driver-onedrive.js';
import { FileApiDriverMemory } from 'lib/file-api-driver-memory.js';
import { FileApiDriverLocal } from 'lib/file-api-driver-local.js';
import { OneDriveApiNodeUtils } from './onedrive-api-node-utils.js';
import { JoplinDatabase } from 'lib/joplin-database.js';
import { Database } from 'lib/database.js';
import { DatabaseDriverNode } from 'lib/database-driver-node.js';
import { BaseModel } from 'lib/base-model.js';
import { Folder } from 'lib/models/folder.js';
import { BaseItem } from 'lib/models/base-item.js';
import { Note } from 'lib/models/note.js';
import { Setting } from 'lib/models/setting.js';
import { Synchronizer } from 'lib/synchronizer.js';
import { Logger } from 'lib/logger.js';
import { sprintf } from 'sprintf-js';
import { vorpalUtils } from 'vorpal-utils.js';
import { reg } from 'lib/registry.js';
import { fileExtension } from 'lib/path-utils.js';
import { _ } from 'lib/locale.js';
import os from 'os';
import fs from 'fs-extra';
class Application {
constructor() {
this.showPromptString_ = true;
this.logger_ = new Logger();
this.dbLogger_ = new Logger();
this.syncLogger_ = new Logger();
this.synchronizers_ = {};
}
vorpal() {
return this.vorpal_;
}
currentFolder() {
return this.currentFolder_;
}
updatePrompt() {
if (!this.showPromptString_) return '';
let path = '~';
if (this.currentFolder()) {
path += '/' + this.currentFolder().title;
}
const prompt = Setting.value('appName') + ':' + path + '$ ';
this.vorpal().delimiter(prompt);
}
switchCurrentFolder(folder) {
this.currentFolder_ = folder;
Setting.setValue('activeFolderId', folder ? folder.id : '');
this.updatePrompt();
}
async parseNotePattern(pattern) {
if (pattern.indexOf('..') === 0) {
let pieces = pattern.split('/');
if (pieces.length != 3) throw new Error(_('Invalid pattern: %s', pattern));
let parent = await this.loadItem(BaseModel.TYPE_FOLDER, pieces[1]);
if (!parent) throw new Error(_('Notebook not found: %s', pieces[1]));
return {
parent: parent,
title: pieces[2],
};
} else {
return {
parent: null,
title: pattern,
};
}
}
async loadItem(type, pattern) {
let output = await this.loadItems(type, pattern);
return output.length ? output[0] : null;
}
async loadItems(type, pattern) {
let ItemClass = BaseItem.itemClass(type);
let item = await ItemClass.loadByTitle(pattern);
if (item) return [item];
item = await ItemClass.load(pattern);
return [item];
}
// Handles the initial flags passed to main script and
// returns the remaining args.
async handleStartFlags_(argv) {
let matched = {};
argv = argv.slice(0);
argv.splice(0, 2); // First arguments are the node executable, and the node JS file
while (argv.length) {
let arg = argv[0];
let nextArg = argv.length >= 2 ? argv[1] : null;
if (arg == '--profile') {
if (!nextArg) throw new Error(_('Usage: --profile <dir-path>'));
matched.profileDir = nextArg;
argv.splice(0, 2);
continue;
}
if (arg == '--env') {
if (!nextArg) throw new Error(_('Usage: --env <dev|prod>'));
matched.env = nextArg;
argv.splice(0, 2);
continue;
}
if (arg == '--redraw-disabled') {
vorpalUtils.setRedrawEnabled(false);
argv.splice(0, 1);
continue;
}
if (arg == '--stack-trace-enabled') {
vorpalUtils.setStackTraceEnabled(true);
argv.splice(0, 1);
continue;
}
if (arg == '--log-level') {
if (!nextArg) throw new Error(_('Usage: --log-level <none|error|warn|info|debug>'));
matched.logLevel = Logger.levelStringToId(nextArg);
argv.splice(0, 2);
continue;
}
if (arg.length && arg[0] == '-') {
throw new Error(_('Unknown flag: %s', arg));
} else {
break;
}
}
if (!matched.logLevel) matched.logLevel = Logger.LEVEL_INFO;
return {
matched: matched,
argv: argv,
};
}
escapeShellArg(arg) {
if (arg.indexOf('"') >= 0 && arg.indexOf("'") >= 0) throw new Error(_('Command line argument "%s" contains both quotes and double-quotes - aborting.', arg)); // Hopeless case
let quote = '"';
if (arg.indexOf('"') >= 0) quote = "'";
if (arg.indexOf(' ') >= 0 || arg.indexOf("\t") >= 0) return quote + arg + quote;
return arg;
}
shellArgsToString(args) {
let output = [];
for (let i = 0; i < args.length; i++) {
output.push(this.escapeShellArg(args[i]));
}
return output.join(' ');
}
loadCommands_() {
fs.readdirSync(__dirname).forEach((path) => {
if (path.indexOf('command-') !== 0) return;
const ext = fileExtension(path)
if (ext != 'js') return;
let CommandClass = require('./' + path);
let cmd = new CommandClass();
let vorpalCmd = this.vorpal().command(cmd.usage(), cmd.description());
for (let i = 0; i < cmd.aliases().length; i++) {
vorpalCmd.alias(cmd.aliases()[i]);
}
for (let i = 0; i < cmd.options().length; i++) {
let options = cmd.options()[i];
if (options.length == 2) vorpalCmd.option(options[0], options[1]);
if (options.length == 3) vorpalCmd.option(options[0], options[1], options[2]);
if (options.length > 3) throw new Error('Invalid number of option arguments');
}
if (cmd.autocomplete()) vorpalCmd.autocomplete(cmd.autocomplete());
let actionFn = async function(args, end) {
try {
const fn = cmd.action.bind(this);
await fn(args);
} catch (error) {
this.log(error);
}
vorpalUtils.redrawDone();
end();
};
vorpalCmd.action(actionFn);
let cancelFn = async function() {
const fn = cmd.cancel.bind(this);
await fn();
};
vorpalCmd.cancel(cancelFn);
});
}
async synchronizer(syncTarget) {
if (this.synchronizers_[syncTarget]) return this.synchronizers_[syncTarget];
let fileApi = null;
if (syncTarget == 'onedrive') {
const oneDriveApi = reg.oneDriveApi();
let driver = new FileApiDriverOneDrive(oneDriveApi);
let auth = Setting.value('sync.onedrive.auth');
if (!oneDriveApi.auth()) {
const oneDriveApiUtils = new OneDriveApiNodeUtils(oneDriveApi);
auth = await oneDriveApiUtils.oauthDance(this.vorpal());
Setting.setValue('sync.onedrive.auth', auth ? JSON.stringify(auth) : auth);
if (!auth) return;
}
let appDir = await oneDriveApi.appDirectory();
this.logger_.info('App dir: ' + appDir);
fileApi = new FileApi(appDir, driver);
fileApi.setLogger(this.logger_);
} else if (syncTarget == 'memory') {
fileApi = new FileApi('joplin', new FileApiDriverMemory());
fileApi.setLogger(this.logger_);
} else if (syncTarget == 'local') {
let syncDir = Setting.value('sync.local.path');
if (!syncDir) syncDir = Setting.value('profileDir') + '/sync';
this.vorpal().log(_('Synchronizing with directory "%s"', syncDir));
await fs.mkdirp(syncDir, 0o755);
fileApi = new FileApi(syncDir, new FileApiDriverLocal());
fileApi.setLogger(this.logger_);
} else {
throw new Error('Unknown backend: ' + syncTarget);
}
this.synchronizers_[syncTarget] = new Synchronizer(this.database_, fileApi, Setting.value('appType'));
this.synchronizers_[syncTarget].setLogger(this.syncLogger_);
return this.synchronizers_[syncTarget];
}
async start() {
this.vorpal_ = require('vorpal')();
vorpalUtils.initialize(this.vorpal());
this.loadCommands_();
let argv = process.argv;
let startFlags = await this.handleStartFlags_(argv);
argv = startFlags.argv;
let initArgs = startFlags.matched;
if (argv.length) this.showPromptString_ = false;
const profileDir = initArgs.profileDir ? initArgs.profileDir : os.homedir() + '/.config/' + Setting.value('appName');
const resourceDir = profileDir + '/resources';
const tempDir = profileDir + '/tmp';
Setting.setConstant('env', initArgs.env);
Setting.setConstant('profileDir', profileDir);
Setting.setConstant('resourceDir', resourceDir);
Setting.setConstant('tempDir', tempDir);
await fs.mkdirp(profileDir, 0o755);
await fs.mkdirp(resourceDir, 0o755);
await fs.mkdirp(tempDir, 0o755);
this.logger_.addTarget('file', { path: profileDir + '/log.txt' });
this.logger_.setLevel(initArgs.logLevel);
reg.setLogger(this.logger_);
this.dbLogger_.addTarget('file', { path: profileDir + '/log-database.txt' });
this.dbLogger_.setLevel(initArgs.logLevel);
this.syncLogger_.addTarget('file', { path: profileDir + '/log-sync.txt' });
this.syncLogger_.setLevel(initArgs.logLevel);
const packageJson = require('./package.json');
this.logger_.info(sprintf('Starting %s %s (%s)...', packageJson.name, packageJson.version, Setting.value('env')));
this.logger_.info('Profile directory: ' + profileDir);
this.database_ = new JoplinDatabase(new DatabaseDriverNode());
this.database_.setLogger(this.dbLogger_);
await this.database_.open({ name: profileDir + '/database.sqlite' });
BaseModel.db_ = this.database_;
await Setting.load();
let currentFolderId = Setting.value('activeFolderId');
this.currentFolder_ = null;
if (currentFolderId) this.currentFolder_ = await Folder.load(currentFolderId);
if (!this.currentFolder_) this.currentFolder_ = await Folder.defaultFolder();
Setting.setValue('activeFolderId', this.currentFolder_ ? this.currentFolder_.id : '');
if (this.currentFolder_) await this.vorpal().exec('use ' + this.currentFolder_.title);
// If we still have arguments, pass it to Vorpal and exit
if (argv.length) {
let cmd = this.shellArgsToString(argv);
await this.vorpal().exec(cmd);
await this.vorpal().exec('exit');
return;
} else {
this.updatePrompt();
this.vorpal().show();
this.vorpal().history(Setting.value('appId')); // Enables persistent history
if (!this.currentFolder()) {
this.vorpal().log(_('No notebook is defined. Create one with `mkbook <notebook>`.'));
}
}
}
}
let application_ = null;
function app() {
if (application_) return application_;
application_ = new Application();
return application_;
}
export { app };

View File

@ -0,0 +1,42 @@
import { app } from './app.js';
import { Folder } from 'lib/models/folder.js';
import { Note } from 'lib/models/note.js';
// For now, to go around this issue: https://github.com/dthree/vorpal/issues/114
function quotePromptArg(s) {
if (s.indexOf(' ') >= 0) {
return '"' + s + '"';
}
return s;
}
function autocompleteFolders() {
return Folder.all().then((folders) => {
let output = [];
for (let i = 0; i < folders.length; i++) {
output.push(quotePromptArg(folders[i].title));
}
output.push('..');
output.push('.');
return output;
});
}
function autocompleteItems() {
let promise = null;
if (!app().currentFolder()) {
promise = Folder.all();
} else {
promise = Note.previews(app().currentFolder().id);
}
return promise.then((items) => {
let output = [];
for (let i = 0; i < items.length; i++) {
output.push(quotePromptArg(items[i].title));
}
return output;
});
}
export { autocompleteFolders, autocompleteItems };

View File

@ -0,0 +1,31 @@
class BaseCommand {
usage() {
throw new Error('Usage not defined');
}
description() {
throw new Error('Description not defined');
}
async action(args) {
throw new Error('Action not defined');
}
aliases() {
return [];
}
autocomplete() {
return null;
}
options() {
return [];
}
async cancel() {}
}
export { BaseCommand };

View File

@ -0,0 +1,46 @@
import { BaseCommand } from './base-command.js';
import { app } from './app.js';
import { _ } from 'lib/locale.js';
import { Folder } from 'lib/models/folder.js';
import { Note } from 'lib/models/note.js';
import { autocompleteItems } from './autocomplete.js';
class Command extends BaseCommand {
usage() {
return 'cat <title>';
}
description() {
return 'Displays the given item data.';
}
autocomplete() {
return { data: autocompleteItems };
}
async action(args) {
let title = args['title'];
let item = null;
if (!app().currentFolder()) {
item = await Folder.loadByField('title', title);
} else {
item = await Note.loadFolderNoteByField(app().currentFolder().id, 'title', title);
}
if (!item) throw new Error(_('No item with title "%s" found.', title));
let content = null;
if (!app().currentFolder()) {
content = await Folder.serialize(item);
} else {
content = await Note.serialize(item);
}
this.log(content);
}
}
module.exports = Command;

View File

@ -0,0 +1,35 @@
import { BaseCommand } from './base-command.js';
import { _ } from 'lib/locale.js';
import { Setting } from 'lib/models/setting.js';
class Command extends BaseCommand {
usage() {
return 'config [name] [value]';
}
description() {
return 'Gets or sets a config value. If [value] is not provided, it will show the value of [name]. If neither [name] nor [value] is provided, it will list the current configuration.';
}
async action(args) {
if (!args.name && !args.value) {
let keys = Setting.publicKeys();
for (let i = 0; i < keys.length; i++) {
this.log(keys[i] + ' = ' + Setting.value(keys[i]));
}
return;
}
if (args.name && !args.value) {
this.log(args.name + ' = ' + Setting.value(args.name));
return;
}
Setting.setValue(args.name, args.value);
await Setting.saveAll();
}
}
module.exports = Command;

View File

@ -0,0 +1,40 @@
import { BaseCommand } from './base-command.js';
import { app } from './app.js';
import { _ } from 'lib/locale.js';
import { Folder } from 'lib/models/folder.js';
import { Note } from 'lib/models/note.js';
import { Tag } from 'lib/models/tag.js';
class Command extends BaseCommand {
usage() {
return 'dump';
}
description() {
return 'Dumps the complete database as JSON.';
}
async action(args) {
let items = [];
let folders = await Folder.all();
for (let i = 0; i < folders.length; i++) {
let folder = folders[i];
let notes = await Note.previews(folder.id);
items.push(folder);
items = items.concat(notes);
}
let tags = await Tag.all();
for (let i = 0; i < tags.length; i++) {
tags[i].notes_ = await Tag.tagNoteIds(tags[i].id);
}
items = items.concat(tags);
this.log(JSON.stringify(items));
}
}
module.exports = Command;

View File

@ -0,0 +1,93 @@
import fs from 'fs-extra';
import { BaseCommand } from './base-command.js';
import { app } from './app.js';
import { _ } from 'lib/locale.js';
import { Folder } from 'lib/models/folder.js';
import { Note } from 'lib/models/note.js';
import { Setting } from 'lib/models/setting.js';
import { autocompleteItems } from './autocomplete.js';
class Command extends BaseCommand {
usage() {
return 'edit <title>';
}
description() {
return 'Edit note.';
}
autocomplete() {
return { data: autocompleteItems };
}
async action(args) {
let watcher = null;
const onFinishedEditing = () => {
if (watcher) watcher.close();
app().vorpal().show();
this.log(_('Done editing.'));
}
const textEditorPath = () => {
if (Setting.value('editor')) return Setting.value('editor');
if (process.env.EDITOR) return process.env.EDITOR;
throw new Error(_('No text editor is defined. Please set it using `config editor <editor-path>`'));
}
try {
let title = args['title'];
if (!app().currentFolder()) throw new Error(_('No active notebook.'));
let note = await Note.loadFolderNoteByField(app().currentFolder().id, 'title', title);
if (!note) throw new Error(_('No note with title "%s" found.', title));
let editorPath = textEditorPath();
let editorArgs = editorPath.split(' ');
editorPath = editorArgs[0];
editorArgs = editorArgs.splice(1);
let content = await Note.serializeForEdit(note);
let tempFilePath = Setting.value('tempDir') + '/' + Note.systemPath(note);
editorArgs.push(tempFilePath);
const spawn = require('child_process').spawn;
this.log(_('Starting to edit note. Close the editor to get back to the prompt.'));
app().vorpal().hide();
await fs.writeFile(tempFilePath, content);
let watchTimeout = null;
watcher = fs.watch(tempFilePath, (eventType, filename) => {
// We need a timeout because for each change to the file, multiple events are generated.
if (watchTimeout) return;
watchTimeout = setTimeout(async () => {
let updatedNote = await fs.readFile(tempFilePath, 'utf8');
updatedNote = await Note.unserializeForEdit(updatedNote);
updatedNote.id = note.id;
await Note.save(updatedNote);
watchTimeout = null;
}, 200);
});
const childProcess = spawn(editorPath, editorArgs, { stdio: 'inherit' });
childProcess.on('exit', (error, code) => {
onFinishedEditing();
});
} catch(error) {
onFinishedEditing();
throw error;
}
}
}
module.exports = Command;

View File

@ -0,0 +1,82 @@
import { BaseCommand } from './base-command.js';
import { app } from './app.js';
import { _ } from 'lib/locale.js';
import { Folder } from 'lib/models/folder.js';
import { vorpalUtils } from './vorpal-utils.js';
import { importEnex } from 'import-enex';
import { filename, basename } from 'lib/path-utils.js';
class Command extends BaseCommand {
usage() {
return 'import-enex <file> [notebook]';
}
description() {
return _('Imports an Evernote notebook file (.enex file).');
}
options() {
return [
['--fuzzy-matching', 'For debugging purposes. Do not use.'],
];
}
async action(args) {
let filePath = args.file;
let folder = null;
let folderTitle = args['notebook'];
if (folderTitle) {
folder = await Folder.loadByField('title', folderTitle);
if (!folder) {
let ok = await vorpalUtils.cmdPromptConfirm(this, _('Folder does not exists: "%s". Create it?', folderTitle))
if (!ok) return;
folder = await Folder.save({ title: folderTitle });
}
} else {
folderTitle = filename(filePath);
folderTitle = _('Imported - %s', folderTitle);
let inc = 0;
while (true) {
let t = folderTitle + (inc ? ' (' + inc + ')' : '');
let f = await Folder.loadByField('title', t);
if (!f) {
folderTitle = t;
break;
}
inc++;
}
}
let ok = await vorpalUtils.cmdPromptConfirm(this, _('File "%s" will be imported into notebook "%s". Continue?', basename(filePath), folderTitle))
if (!ok) return;
let options = {
fuzzyMatching: args.options['fuzzy-matching'] === true,
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));
vorpalUtils.redraw(line.join(' '));
},
onError: (error) => {
vorpalUtils.redrawDone();
let s = error.trace ? error.trace : error.toString();
this.log(s);
},
}
folder = !folder ? await Folder.save({ title: folderTitle }) : folder;
this.log(_('Importing notes...'));
await importEnex(folder.id, filePath, options);
}
}
module.exports = Command;

View File

@ -0,0 +1,78 @@
import { BaseCommand } from './base-command.js';
import { app } from './app.js';
import { _ } from 'lib/locale.js';
import { Folder } from 'lib/models/folder.js';
import { Note } from 'lib/models/note.js';
import { autocompleteFolders } from './autocomplete.js';
class Command extends BaseCommand {
usage() {
return 'ls [pattern]';
}
description() {
return 'Displays the notes in [notebook]. Use `ls ..` to display the list of notebooks.';
}
options() {
return [
['-n, --lines <num>', 'Displays only the first top <num> lines.'],
['-s, --sort <field>', 'Sorts the item by <field> (eg. title, updated_time, created_time).'],
['-r, --reverse', 'Reverses the sorting order.'],
['-t, --type <type>', 'Displays only the items of the specific type(s). Can be `n` for notes, `t` for todos, or `nt` for notes and todos (eg. `-tt` would display only the todos, while `-ttd` would display notes and todos.'],
['-f, --format <format>', 'Either "text" or "json"'],
];
}
autocomplete() {
return { data: autocompleteFolders };
}
async action(args) {
let pattern = args['pattern'];
let suffix = '';
let items = [];
let options = args.options;
let queryOptions = {};
if (options.lines) queryOptions.limit = options.lines;
if (options.sort) {
queryOptions.orderBy = options.sort;
queryOptions.orderByDir = 'ASC';
}
if (options.reverse === true) queryOptions.orderByDir = queryOptions.orderByDir == 'ASC' ? 'DESC' : 'ASC';
queryOptions.caseInsensitive = true;
if (options.type) {
queryOptions.itemTypes = [];
if (options.type.indexOf('n') >= 0) queryOptions.itemTypes.push('note');
if (options.type.indexOf('t') >= 0) queryOptions.itemTypes.push('todo');
}
if (pattern) queryOptions.titlePattern = pattern;
if (pattern == '..' || !app().currentFolder()) {
items = await Folder.all(queryOptions);
suffix = '/';
} else {
if (!app().currentFolder()) throw new Error(_('Please select a notebook first.'));
items = await Note.previews(app().currentFolder().id, queryOptions);
}
if (options.format && options.format == 'json') {
this.log(JSON.stringify(items));
} else {
for (let i = 0; i < items.length; i++) {
let item = items[i];
let line = '';
if (!!item.is_todo) {
line += sprintf('[%s] ', !!item.todo_completed ? 'X' : ' ');
}
line += item.title + suffix;
this.log(line);
}
}
}
}
module.exports = Command;

View File

@ -0,0 +1,27 @@
import { BaseCommand } from './base-command.js';
import { app } from './app.js';
import { _ } from 'lib/locale.js';
import { Folder } from 'lib/models/folder.js';
class Command extends BaseCommand {
usage() {
return 'mkbook <notebook>';
}
description() {
return 'Creates a new notebook.';
}
aliases() {
return ['mkdir'];
}
async action(args, end) {
let folder = await Folder.save({ title: args['notebook'] }, { duplicateCheck: true });
app().switchCurrentFolder(folder);
}
}
module.exports = Command;

View File

@ -0,0 +1,36 @@
import { BaseCommand } from './base-command.js';
import { app } from './app.js';
import { _ } from 'lib/locale.js';
import { Note } from 'lib/models/note.js';
class Command extends BaseCommand {
usage() {
return 'mknote <note>';
}
description() {
return 'Creates a new note.';
}
aliases() {
return ['touch'];
}
async action(args) {
if (!app().currentFolder()) throw new Error(_('Notes can only be created within a notebook.'));
let path = await app().parseNotePattern(args['note']);
let note = {
title: path.title,
parent_id: path.parent ? path.parent.id : app().currentFolder().id,
};
note = await Note.save(note);
Note.updateGeolocation(note.id);
}
}
module.exports = Command;

View File

@ -0,0 +1,39 @@
import { BaseCommand } from './base-command.js';
import { app } from './app.js';
import { _ } from 'lib/locale.js';
import { Folder } from 'lib/models/folder.js';
import { Note } from 'lib/models/note.js';
import { autocompleteItems } from './autocomplete.js';
class Command extends BaseCommand {
usage() {
return 'mv <pattern> <notebook>';
}
description() {
return 'Moves the notes matching <pattern> to <notebook>.';
}
autocomplete() {
return { data: autocompleteItems };
}
async action(args) {
if (!app().currentFolder()) throw new Error(_('Please select a notebook first.'));
let pattern = args['pattern'];
let folder = await Folder.loadByField('title', args['notebook']);
if (!folder) throw new Error(_('No folder with title "%s"', args['notebook']));
let notes = await Note.previews(app().currentFolder().id, { titlePattern: pattern });
if (!notes.length) throw new Error(_('No note matches this pattern: "%s"', pattern));
for (let i = 0; i < notes.length; i++) {
await Note.save({ id: notes[i].id, parent_id: folder.id });
}
}
}
module.exports = Command;

View File

@ -0,0 +1,71 @@
import { BaseCommand } from './base-command.js';
import { app } from './app.js';
import { _ } from 'lib/locale.js';
import { BaseItem } from 'lib/models/base-item.js';
import { Folder } from 'lib/models/folder.js';
import { Note } from 'lib/models/note.js';
import { BaseModel } from 'lib/base-model.js';
import { autocompleteItems } from './autocomplete.js';
import { vorpalUtils } from './vorpal-utils.js';
class Command extends BaseCommand {
usage() {
return 'rm <pattern>';
}
description() {
return 'Deletes the given item. For a notebook, all the notes within that notebook will be deleted. Use `rm ../<notebook>` to delete a notebook.';
}
autocomplete() {
return { data: autocompleteItems };
}
options() {
return [
['-f, --force', 'Deletes the items without asking for confirmation.'],
];
}
async action(args) {
let pattern = args['pattern'];
let itemType = null;
let force = args.options && args.options.force === true;
if (pattern.indexOf('*') < 0) { // Handle it as a simple title
if (pattern.substr(0, 3) == '../') {
itemType = BaseModel.TYPE_FOLDER;
pattern = pattern.substr(3);
} else {
itemType = BaseModel.TYPE_NOTE;
}
let item = await BaseItem.loadItemByField(itemType, 'title', pattern);
if (!item) throw new Error(_('No item with title "%s" found.', pattern));
let ok = force ? true : await vorpalUtils.cmdPromptConfirm(this, _('Delete item?'));
if (ok) {
await BaseItem.deleteItem(itemType, item.id);
if (app().currentFolder() && app().currentFolder().id == item.id) {
let f = await Folder.defaultFolder();
switchCurrentFolder(f);
}
}
} else { // Handle it as a glob pattern
if (app().currentFolder()) {
let notes = await Note.previews(app().currentFolder().id, { titlePattern: pattern });
if (!notes.length) throw new Error(_('No note matches this pattern: "%s"', pattern));
let ok = force ? true : await vorpalUtils.cmdPromptConfirm(this, _('%d notes match this pattern. Delete them?', notes.length));
if (ok) {
for (let i = 0; i < notes.length; i++) {
await Note.delete(notes[i].id);
}
}
}
}
}
}
module.exports = Command;

View File

@ -0,0 +1,53 @@
import { BaseCommand } from './base-command.js';
import { app } from './app.js';
import { _ } from 'lib/locale.js';
import { Folder } from 'lib/models/folder.js';
import { Note } from 'lib/models/note.js';
import { BaseItem } from 'lib/models/base-item.js';
import { autocompleteItems } from './autocomplete.js';
class Command extends BaseCommand {
usage() {
return 'set <item> <name> [value]';
}
description() {
return 'Sets the property <name> of the given <item> to the given [value].';
}
autocomplete() {
return { data: autocompleteItems };
}
async action(args) {
let title = args['item'];
let propName = args['name'];
let propValue = args['value'];
if (!propValue) propValue = '';
let item = null;
if (!app().currentFolder()) {
item = await Folder.loadByField('title', title);
} else {
item = await Note.loadFolderNoteByField(app().currentFolder().id, 'title', title);
}
if (!item) {
item = await BaseItem.loadItemById(title);
}
if (!item) throw new Error(_('No item with title "%s" found.', title));
let newItem = {
id: item.id,
type_: item.type_,
};
newItem[propName] = propValue;
let ItemClass = BaseItem.itemClass(newItem);
await ItemClass.save(newItem);
}
}
module.exports = Command;

View File

@ -0,0 +1,72 @@
import { BaseCommand } from './base-command.js';
import { app } from './app.js';
import { _ } from 'lib/locale.js';
import { Setting } from 'lib/models/setting.js';
import { BaseItem } from 'lib/models/base-item.js';
import { vorpalUtils } from './vorpal-utils.js';
class Command extends BaseCommand {
usage() {
return 'sync';
}
description() {
return 'Synchronizes with remote storage.';
}
options() {
return [
['--random-failures', 'For debugging purposes. Do not use.'],
['--stats', 'Displays stats about synchronization.'],
];
}
async action(args) {
if (args.options.stats) {
const report = await BaseItem.stats();
for (let n in report.items) {
if (!report.items.hasOwnProperty(n)) continue;
const r = report.items[n];
this.log(_('%s: %d/%d', n, r.synced, r.total))
}
this.log(_('Total: %d/%d', report.total.synced, report.total.total));
} else {
let options = {
onProgress: (report) => {
let line = [];
if (report.remotesToUpdate) line.push(_('Items to upload: %d/%d.', report.createRemote + report.updateRemote, report.remotesToUpdate));
if (report.remotesToDelete) line.push(_('Remote items to delete: %d/%d.', report.deleteRemote, report.remotesToDelete));
if (report.localsToUdpate) line.push(_('Items to download: %d/%d.', report.createLocal + report.updateLocal, report.localsToUdpate));
if (report.localsToDelete) line.push(_('Local items to delete: %d/%d.', report.deleteLocal, report.localsToDelete));
if (line.length) vorpalUtils.redraw(line.join(' '));
},
onMessage: (msg) => {
vorpalUtils.redrawDone();
this.log(msg);
},
randomFailures: args.options['random-failures'] === true,
};
this.log(_('Synchronization target: %s', Setting.value('sync.target')));
let sync = await app().synchronizer(Setting.value('sync.target'));
if (!sync) throw new Error(_('Cannot initialize synchronizer.'));
this.log(_('Starting synchronization...'));
await sync.start(options);
this.log(_('Done.'));
}
}
async cancel() {
vorpalUtils.redrawDone();
this.log(_('Cancelling...'));
let sync = await app().synchronizer(Setting.value('sync.target'));
sync.cancel();
}
}
module.exports = Command;

View File

@ -0,0 +1,48 @@
import { BaseCommand } from './base-command.js';
import { app } from './app.js';
import { _ } from 'lib/locale.js';
import { Tag } from 'lib/models/tag.js';
import { BaseModel } from 'lib/base-model.js';
class Command extends BaseCommand {
usage() {
return 'tag <command> [tag] [note]';
}
description() {
return '<command> can be "add", "remove" or "list" to assign or remove [tag] from [note], or to list the notes associated with [tag]. The command `tag list` can be used to list all the tags.';
}
async action(args) {
let tag = null;
if (args.tag) tag = await app().loadItem(BaseModel.TYPE_TAG, args.tag);
let note = null;
if (args.note) note = await app().loadItem(BaseModel.TYPE_NOTE, args.note);
if (args.command == 'remove' && !tag) throw new Error(_('Tag does not exist: "%s"', args.tag));
if (args.command == 'add') {
if (!note) throw new Error(_('Note does not exist: "%s"', args.note));
if (!tag) tag = await Tag.save({ title: args.tag });
await Tag.addNote(tag.id, note.id);
} else if (args.command == 'remove') {
if (!tag) throw new Error(_('Tag does not exist: "%s"', args.tag));
if (!note) throw new Error(_('Note does not exist: "%s"', args.note));
await Tag.removeNote(tag.id, note.id);
} else if (args.command == 'list') {
if (tag) {
let notes = await Tag.notes(tag.id);
notes.map((note) => { this.log(note.title); });
} else {
let tags = await Tag.all();
tags.map((tag) => { this.log(tag.title); });
}
} else {
throw new Error(_('Invalid command: "%s"', args.command));
}
}
}
module.exports = Command;

View File

@ -0,0 +1,35 @@
import { BaseCommand } from './base-command.js';
import { app } from './app.js';
import { _ } from 'lib/locale.js';
import { Folder } from 'lib/models/folder.js';
import { autocompleteFolders } from './autocomplete.js';
class Command extends BaseCommand {
usage() {
return 'use <notebook>';
}
description() {
return 'Switches to [notebook] - all further operations will happen within this notebook.';
}
aliases() {
return ['cd'];
}
autocomplete() {
return { data: autocompleteFolders };
}
async action(args) {
let title = args['notebook'];
let folder = await Folder.loadByField('title', title);
if (!folder) throw new Error(_('Invalid folder title: %s', title));
app().switchCurrentFolder(folder);
}
}
module.exports = Command;

View File

@ -1,9 +1,20 @@
module.exports = {
usage: 'version',
description: 'Displays version information',
action: function(args, end) {
import { BaseCommand } from './base-command.js';
class Command extends BaseCommand {
usage() {
return 'version';
}
description() {
return 'Displays version information';
}
async ction(args) {
const packageJson = require('./package.json');
this.log(packageJson.name + ' ' + packageJson.version);
end();
},
};
}
}
module.exports = Command;

File diff suppressed because it is too large Load Diff

View File

@ -66,6 +66,7 @@ function redrawDone() {
vorpal_.ui.redraw.done();
}
redrawLastLog_ = null;
redrawStarted_ = false;
}
@ -81,11 +82,31 @@ function log(commandInstance, o) {
}
}
function cmdPromptConfirm(commandInstance, message) {
return new Promise((resolve, reject) => {
let options = {
type: 'confirm',
name: 'ok',
default: false, // This needs to be false so that, when pressing Ctrl+C, the prompt returns false
message: message,
};
commandInstance.prompt(options, (result) => {
if (result.ok) {
resolve(true);
} else {
resolve(false);
}
});
});
}
vorpalUtils.initialize = initialize;
vorpalUtils.redraw = redraw;
vorpalUtils.redrawDone = redrawDone;
vorpalUtils.setRedrawEnabled = setRedrawEnabled;
vorpalUtils.setStackTraceEnabled = setStackTraceEnabled;
vorpalUtils.log = log;
vorpalUtils.cmdPromptConfirm = cmdPromptConfirm;
export { vorpalUtils };

View File

@ -160,6 +160,7 @@ Setting.constants_ = {
'appType': 'SET_ME', // 'cli' or 'mobile'
'resourceDir': '',
'profileDir': '',
'tempDir': '',
}
export { Setting };