1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-11 18:24:43 +02:00
This commit is contained in:
Laurent Cozic 2017-08-04 19:11:10 +02:00
parent 1f91d0dfdb
commit 97093feebf
4 changed files with 133 additions and 6 deletions

View File

@ -27,6 +27,7 @@ class Application {
this.commands_ = {};
this.commandMetadata_ = null;
this.activeCommand_ = null;
this.allCommandsLoaded_ = false;
}
currentFolder() {
@ -242,7 +243,9 @@ class Application {
}
}
loadAllCommands() {
commands() {
if (this.allCommandsLoaded_) return this.commands_;
fs.readdirSync(__dirname).forEach((path) => {
if (path.indexOf('command-') !== 0) return;
const ext = fileExtension(path)
@ -252,8 +255,26 @@ class Application {
let cmd = new CommandClass();
if (!cmd.enabled()) return;
cmd.log = (...object) => {
return console.log(...object);
}
this.commands_[cmd.name()] = cmd;
});
this.allCommandsLoaded_ = true;
return this.commands_;
}
async commandNames() {
const metadata = await this.commandMetadata();
let output = [];
for (let n in metadata) {
if (!metadata.hasOwnProperty(n)) continue;
output.push(n);
}
return output;
}
async commandMetadata() {
@ -268,12 +289,12 @@ class Application {
return Object.assign({}, this.commandMetadata_);
}
this.loadAllCommands();
const commands = this.commands();
output = {};
for (let n in this.commands_) {
if (!this.commands_.hasOwnProperty(n)) continue;
const cmd = this.commands_[n];
for (let n in commands) {
if (!commands.hasOwnProperty(n)) continue;
const cmd = commands[n];
output[n] = cmd.metadata();
}
@ -284,6 +305,8 @@ class Application {
}
findCommandByName(name) {
if (this.commands_[name]) return this.commands_[name];
let CommandClass = null;
try {
CommandClass = require('./command-' + name + '.js');
@ -298,7 +321,8 @@ class Application {
return console.log(...object);
}
return cmd;
this.commands_[name] = cmd;
return this.commands_[name];
}
async execCommand(argv) {

View File

@ -137,6 +137,11 @@ async function handleAutocompletion(autocompletion) {
return filterList(['toggle', 'clear'], currentWord);
}
if (argName == 'command') {
const commands = await app().commandNames();
return this.filterList(commands, currentWord);
}
return [];
}

View File

@ -0,0 +1,34 @@
import { BaseCommand } from './base-command.js';
import { app } from './app.js';
import { renderCommandHelp } from './help-utils.js';
import { Database } from 'lib/database.js';
import { Setting } from 'lib/models/setting.js';
import { _ } from 'lib/locale.js';
import { ReportService } from 'lib/services/report.js';
class Command extends BaseCommand {
usage() {
return 'help [command]';
}
description() {
return _('Displays usage information.');
}
async action(args) {
const commands = args['command'] ? [app().findCommandByName(args['command'])] : app().commands();
let output = [];
for (let n in commands) {
if (!commands.hasOwnProperty(n)) continue;
const command = commands[n];
output.push(renderCommandHelp(command));
}
this.log(output.join("\n\n"));
}
}
module.exports = Command;

View File

@ -0,0 +1,64 @@
require('source-map-support').install();
require('babel-plugin-transform-runtime');
import fs from 'fs-extra';
import { fileExtension, basename, dirname } from 'lib/path-utils.js';
import wrap_ from 'word-wrap';
import { _, setLocale, languageCode } from 'lib/locale.js';
const rootDir = dirname(dirname(__dirname));
const MAX_WIDTH = 78;
const INDENT = ' ';
function wrap(text, indent) {
return wrap_(text, {
width: MAX_WIDTH - indent.length,
indent: indent,
});
}
function renderOptions(options, baseIndent) {
let output = [];
const optionColWidth = getOptionColWidth(options);
for (let i = 0; i < options.length; i++) {
let option = options[i];
const flag = option[0];
const indent = baseIndent + INDENT + ' '.repeat(optionColWidth + 2);
let r = wrap(option[1], indent);
r = r.substr(flag.length + (baseIndent + INDENT).length);
r = baseIndent + INDENT + flag + r;
output.push(r);
}
return output.join("\n");
}
function renderCommandHelp(cmd) {
const baseIndent = '';
let output = [];
output.push(baseIndent + cmd.usage());
output.push('');
output.push(wrap(cmd.description(), baseIndent + INDENT));
const optionString = renderOptions(cmd.options(), baseIndent);
if (optionString) {
output.push('');
output.push(optionString);
}
return output.join("\n");
}
function getOptionColWidth(options) {
let output = 0;
for (let j = 0; j < options.length; j++) {
const option = options[j];
if (option[0].length > output) output = option[0].length;
}
return output;
}
export { renderCommandHelp };