New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

koishi-core

Package Overview
Dependencies
Maintainers
1
Versions
182
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koishi-core - npm Package Compare versions

Comparing version 2.0.0-alpha.7 to 2.0.0-alpha.8

2

dist/app.d.ts

@@ -15,2 +15,3 @@ /// <reference types="node" />

database?: DatabaseConfig;
prefix?: string | string[];
nickname?: string | string[];

@@ -20,3 +21,2 @@ retryTimes?: number;

maxMiddlewares?: number;
commandPrefix?: string | string[];
defaultAuthority?: number | ((meta: Meta) => number);

@@ -23,0 +23,0 @@ quickOperationTimeout?: number;

@@ -274,5 +274,5 @@ "use strict";

}
const { nickname, commandPrefix } = this.options;
const { nickname, prefix } = this.options;
const nicknames = Array.isArray(nickname) ? nickname : nickname ? [nickname] : [];
const prefixes = Array.isArray(commandPrefix) ? commandPrefix : [commandPrefix || ''];
const prefixes = Array.isArray(prefix) ? prefix : [prefix || ''];
this.atMeRE = new RegExp(`^\\[CQ:at,qq=${this.selfId}\\]${nicknameSuffix}`);

@@ -279,0 +279,0 @@ this.nicknameRE = createLeadingRE(nicknames, '@?', nicknameSuffix);

@@ -16,3 +16,2 @@ /// <reference types="node" />

default?: any;
hidden?: boolean;
authority?: number;

@@ -24,2 +23,3 @@ notUsage?: boolean;

export interface CommandOption extends OptionConfig {
fullName: string;
rawName: string;

@@ -29,2 +29,3 @@ longest: string;

camels: string[];
aliases: string[];
negated: string[];

@@ -76,8 +77,9 @@ required: boolean;

_aliases: string[];
_arguments: CommandArgument[];
_options: CommandOption[];
_shortcuts: Record<string, ShortcutConfig>;
private _optionMap;
private _optionAliasMap;
private _userFields;
private _groupFields;
_argsDef: CommandArgument[];
_optsDef: Record<string, CommandOption>;
_action?: (this: this, config: ParsedCommandLine, ...args: string[]) => any;

@@ -100,8 +102,9 @@ static defaultConfig: CommandConfig;

* Add a option for this command
* @param rawName raw option name(s)
* @param fullName raw option name(s)
* @param description option description
* @param config option config
*/
option(rawName: string, config?: OptionConfig): this;
option(rawName: string, description: string, config?: OptionConfig): this;
option(fullName: string, config?: OptionConfig): this;
option(fullName: string, description: string, config?: OptionConfig): this;
private _registerOption;
removeOption(name: string): boolean;

@@ -108,0 +111,0 @@ action(callback: (this: this, options: ParsedCommandLine, ...args: string[]) => any): this;

@@ -9,3 +9,3 @@ "use strict";

function removeBrackets(source) {
return source.replace(/[<[].+/, '').trim();
return source.replace(/(<[^<]+>|\[[^[]+\]).*/, '').trim();
}

@@ -44,7 +44,7 @@ exports.removeBrackets = removeBrackets;

const char0 = source[0];
if (char0 === '"' || char0 === "'" || char0 === '“' || char0 === '”') {
const [content] = source.slice(1).split(/["'“”](?=\s|$)/, 1);
if (char0 === '"' || char0 === "'" || char0 === '“' || char0 === '”' || char0 === '‘' || char0 === '’') {
const [content] = source.slice(1).split(/["'“”‘’](?=\s|$)/, 1);
return {
content,
quoted: true,
content,
rest: source.slice(2 + content.length).trimLeft(),

@@ -54,3 +54,7 @@ };

const [content] = source.split(/\s/, 1);
return { content, quoted: false, rest: source.slice(content.length).trimLeft() };
return {
content,
quoted: false,
rest: source.slice(content.length).trimLeft(),
};
}

@@ -86,8 +90,9 @@ function parseValue(source, quoted, config = {}) {

this._shortcuts = {};
this._optionMap = {};
this._optionAliasMap = {};
this._userFields = [];
this._groupFields = [];
this._optsDef = {};
if (!name)
throw new Error(shared_1.errors.EXPECT_COMMAND_NAME);
this._argsDef = parseArguments(declaration);
this._arguments = parseArguments(declaration);
this.config = { ...Command.defaultConfig, ...config };

@@ -127,3 +132,3 @@ this._registerAlias(this.name);

else if (previous !== this) {
throw new Error(shared_1.errors.DUPLICATE_COMMAND);
throw new Error(util_1.format(shared_1.errors.DUPLICATE_COMMAND, name));
}

@@ -163,3 +168,3 @@ }

}
option(rawName, ...args) {
option(fullName, ...args) {
const description = typeof args[0] === 'string' ? args.shift() : undefined;

@@ -170,6 +175,13 @@ const config = args[0] || {};

let required = false, isBoolean = false, longest = '';
const names = removeBrackets(rawName).split(',').map((name) => {
name = name.trim().replace(/^-{1,2}/, '');
const names = [], aliases = [];
const rawName = removeBrackets(fullName);
for (let name of rawName.split(',')) {
name = name.trim();
if (name && !name.startsWith('-')) {
aliases.push(name);
continue;
}
name = name.replace(/^-{1,2}/, '');
let camel;
if (name.startsWith('no-') && !config.noNegated && !this._optsDef[name.slice(3)]) {
if (name.startsWith('no-') && !config.noNegated && !this._optionMap[name.slice(3)]) {
name = name.slice(3);

@@ -185,8 +197,9 @@ camel = koishi_utils_1.camelCase(name);

longest = camel;
return name;
});
if (rawName.includes('<')) {
names.push(name);
}
const brackets = fullName.slice(rawName.length);
if (brackets.includes('<')) {
required = true;
}
else if (!rawName.includes('[')) {
else if (!brackets.includes('[')) {
isBoolean = true;

@@ -197,5 +210,7 @@ }

...config,
fullName,
rawName,
longest,
names,
aliases,
camels,

@@ -208,17 +223,21 @@ negated,

this._options.push(option);
this._registerOption(option, names, this._optionMap);
this._registerOption(option, aliases, this._optionAliasMap);
return this;
}
_registerOption(option, names, optionMap) {
for (const name of names) {
if (name in this._optsDef) {
throw new Error(shared_1.errors.DUPLICATE_OPTION);
if (name in optionMap) {
throw new Error(util_1.format(shared_1.errors.DUPLICATE_OPTION, name));
}
this._optsDef[name] = option;
optionMap[name] = option;
}
return this;
}
removeOption(name) {
name = name.replace(/^-+/, '');
const option = this._optsDef[name];
const option = this._optionMap[name];
if (!option)
return false;
for (const name of option.names) {
delete this._optsDef[name];
delete this._optionMap[name];
}

@@ -238,3 +257,4 @@ const index = this._options.indexOf(option);

parse(source) {
let arg, name, arg0, rest = '';
var _a;
let rest = '';
const args = [];

@@ -244,3 +264,3 @@ const unknown = [];

const handleOption = (name, knownValue, unknownValue) => {
const config = this._optsDef[name];
const config = this._optionMap[name];
if (config) {

@@ -261,3 +281,3 @@ for (const alias of config.camels) {

// long argument
if (source[0] !== '-' && this._argsDef[args.length] && this._argsDef[args.length].noSegment) {
if (source[0] !== '-' && ((_a = this._arguments[args.length]) === null || _a === void 0 ? void 0 : _a.noSegment)) {
args.push(source);

@@ -267,39 +287,48 @@ break;

// parse argv0
arg0 = parseArg0(source);
arg = arg0.content;
let arg0 = parseArg0(source);
let arg = arg0.content;
source = arg0.rest;
if (arg[0] !== '-' || arg0.quoted) {
let option = this._optionAliasMap[arg];
let names;
let param;
if (option && !arg0.quoted) {
names = [option.names[0]];
}
else {
// normal argument
args.push(arg);
continue;
}
else if (arg === '--') {
if (arg[0] !== '-' || arg0.quoted) {
args.push(arg);
continue;
}
// rest part
rest = arg0.rest;
break;
}
// find -
let i = 0;
for (; i < arg.length; ++i) {
if (arg.charCodeAt(i) !== 45)
if (arg === '--') {
rest = arg0.rest;
break;
}
// find -
let i = 0;
let name;
for (; i < arg.length; ++i) {
if (arg.charCodeAt(i) !== 45)
break;
}
if (arg.slice(i, i + 3) === 'no-') {
name = arg.slice(i + 3);
handleOption(name, true, false);
continue;
}
// find =
let j = i + 1;
for (; j < arg.length; j++) {
if (arg.charCodeAt(j) === 61)
break;
}
name = arg.slice(i, j);
names = i > 1 ? [name] : name;
param = arg.slice(++j);
option = this._optionMap[names[names.length - 1]];
}
if (arg.slice(i, i + 3) === 'no-') {
name = arg.slice(i + 3);
handleOption(name, true, false);
continue;
}
// find =
let j = i + 1;
for (; j < arg.length; j++) {
if (arg.charCodeAt(j) === 61)
break;
}
name = arg.slice(i, j);
const names = i === 2 ? [name] : name;
// get parameter
let quoted = false;
let param = arg.slice(++j);
const lastConfig = this._optsDef[names[names.length - 1]];
if (!param && source.charCodeAt(0) !== 45 && (!lastConfig || !lastConfig.isBoolean)) {
if (!param && source.charCodeAt(0) !== 45 && (!option || !option.isBoolean)) {
arg0 = parseArg0(source);

@@ -311,6 +340,6 @@ param = arg0.content;

// handle each name
for (j = 0; j < names.length; j++) {
name = names[j];
const config = this._optsDef[name];
const value = parseValue((j + 1 < names.length) || param, quoted, config);
for (let j = 0; j < names.length; j++) {
const name = names[j];
const config = this._optionMap[name];
const value = parseValue(j + 1 < names.length || param, quoted, config);
handleOption(name, value, value);

@@ -320,5 +349,5 @@ }

// assign default values
for (const name in this._optsDef) {
if (this._optsDef[name].default !== undefined && !(name in options)) {
options[name] = this._optsDef[name].default;
for (const name in this._optionMap) {
if (this._optionMap[name].default !== undefined && !(name in options)) {
options[name] = this._optionMap[name].default;
}

@@ -329,9 +358,10 @@ }

async execute(argv, next = koishi_utils_1.noop) {
argv.command = this;
if (!argv.options)
argv.options = {};
if (!argv.unknown)
argv.unknown = [];
if (!argv.args)
argv.args = [];
argv.command = this;
if (!argv.unknown) {
argv.unknown = Object.keys(argv.options).filter(key => !this._optionMap[key]);
}
if (await this.app.serialize(argv.meta, 'before-command', argv))

@@ -338,0 +368,0 @@ return;

@@ -265,4 +265,3 @@ "use strict";

}
const unknown = Object.keys(options).filter(key => !command._optsDef[key]);
return command.execute({ meta, command, args, options, rest, unknown });
return command.execute({ meta, args, options, rest });
}

@@ -269,0 +268,0 @@ end() {

import { Command, Meta } from '..';
export declare type CommandUsage = string | ((this: Command, meta: Meta) => string | Promise<string>);
declare module '../app' {
interface AppOptions {
globalHelpMessage?: string;
}
}
declare module '../command' {

@@ -10,3 +15,11 @@ interface Command {

}
interface CommandConfig {
/** hide all options by default */
hideOptions?: boolean;
}
interface OptionConfig {
/** hide the option by default */
hidden?: boolean;
}
}
//# sourceMappingURL=help.d.ts.map

@@ -28,5 +28,4 @@ "use strict";

.shortcut('帮助', { fuzzy: true })
.shortcut('全局指令', { options: { shortcut: true } })
.option('-e, --expand', '展开指令列表')
.option('-s, --shortcut', '查看全局指令列表')
.option('-o, --options', '查看全部选项(包括隐藏)')
.action(async ({ meta, options }, name) => {

@@ -81,12 +80,42 @@ if (name) {

function showGlobalHelp(context, meta, config) {
return meta.$send([
const output = [
...getCommandList('当前可用的指令有', context, meta, null, config.expand),
'群聊普通指令可以通过“@我+指令名”的方式进行触发。',
'私聊或全局指令则不需要添加上述前缀,直接输入指令名即可触发。',
'输入“全局指令”查看全部可用的全局指令。',
'输入“帮助+指令名”查看特定指令的语法和使用示例。',
].join('\n'));
];
if (context.app.options.globalHelpMessage) {
output.push(context.app.options.globalHelpMessage);
}
return meta.$send(output.join('\n'));
}
function getOptions(command, maxUsage, config) {
if (command.config.hideOptions && !config.options)
return [];
const options = config.options
? command._options
: command._options.filter(option => !option.hidden);
if (!options.length)
return [];
const output = options.some(o => o.authority)
? ['可用的选项有(括号内为额外要求的权限等级):']
: ['可用的选项有:'];
options.forEach((option) => {
const authority = option.authority ? `(${option.authority}) ` : '';
let line = ` ${authority}${option.fullName} ${option.description}`;
if (option.notUsage && maxUsage !== Infinity) {
line += '(不计入总次数)';
}
output.push(line);
});
return output;
}
async function showCommandHelp(command, meta, config) {
const output = [command.name + command.declaration, command.config.description];
if (config.options) {
const output = getOptions(command, Infinity, config);
if (!output.length)
return meta.$send('该指令没有可用的选项。');
return meta.$send(output.join('\n'));
}
if (command.context.database) {

@@ -121,19 +150,3 @@ meta.$user = await command.context.database.observeUser(meta.userId);

}
const options = command._options.filter(option => !option.hidden);
if (options.length) {
if (options.some(o => o.authority)) {
output.push('可用的选项有(括号内为额外要求的权限等级):');
}
else {
output.push('可用的选项有:');
}
options.forEach((option) => {
const authority = option.authority ? `(${option.authority}) ` : '';
let line = ` ${authority}${option.rawName} ${option.description}`;
if (option.notUsage && maxUsage !== Infinity) {
line += '(不计入总次数)';
}
output.push(line);
});
}
output.push(...getOptions(command, maxUsage, config));
if (command._examples.length) {

@@ -140,0 +153,0 @@ output.push('使用示例:', ...command._examples.map(example => ' ' + example));

@@ -19,2 +19,5 @@ import { UserData, Command } from '..';

}
interface OptionConfig {
validate?(value: any): void | string | boolean;
}
}

@@ -21,0 +24,0 @@ export declare function getUsageName(command: Command): string;

@@ -48,8 +48,8 @@ "use strict";

if (command.config.checkArgCount) {
const nextArg = command._argsDef[args.length];
const nextArg = command._arguments[args.length];
if (nextArg === null || nextArg === void 0 ? void 0 : nextArg.required) {
return sendHint(meta, shared_1.messages.INSUFFICIENT_ARGUMENTS);
}
const finalArg = command._argsDef[command._argsDef.length - 1];
if (args.length > command._argsDef.length && !finalArg.noSegment && !finalArg.variadic) {
const finalArg = command._arguments[command._arguments.length - 1];
if (args.length > command._arguments.length && !finalArg.noSegment && !finalArg.variadic) {
return sendHint(meta, shared_1.messages.REDUNANT_ARGUMENTS);

@@ -71,2 +71,10 @@ }

}
for (const option of command._options) {
if (!option.validate || !(option.longest in options))
continue;
const result = option.validate(options[option.longest]);
if (result) {
return sendHint(meta, shared_1.messages.INVALID_OPTION, option.rawName, result === true ? shared_1.messages.CHECK_SYNTAX : result);
}
}
if (!meta.$user)

@@ -73,0 +81,0 @@ return;

@@ -10,5 +10,7 @@ /// <reference types="node" />

readonly REDUNANT_ARGUMENTS: "存在多余参数,请检查指令语法。";
readonly REQUIRED_OPTIONS: "缺少必需属性 %s,请检查指令语法。";
readonly REQUIRED_OPTIONS: "缺少必需选项 %s,请检查指令语法。";
readonly INVALID_OPTION: "选项 %s 输入无效,%s";
readonly UNKNOWN_OPTIONS: "存在未知选项 %s,请检查指令语法。";
readonly CHECK_SYNTAX: "请检查指令语法。";
readonly SHOW_THIS_MESSAGE: "显示本信息";
readonly UNKNOWN_OPTIONS: "存在未知属性 %s,请检查指令语法。";
readonly USAGE_EXHAUSTED: "调用次数已达上限。";

@@ -20,4 +22,4 @@ readonly COMMAND_SUGGESTION_PREFIX: "没有此命令。";

export declare const errors: {
readonly DUPLICATE_COMMAND: "duplicate command names";
readonly DUPLICATE_OPTION: "duplicate option names";
readonly DUPLICATE_COMMAND: "duplicate command names: \"%s\"";
readonly DUPLICATE_OPTION: "duplicate option names: \"%s\"";
readonly EXPECT_COMMAND_NAME: "expect a command name";

@@ -24,0 +26,0 @@ readonly INVALID_PLUGIN: "invalid plugin, expect function or object with a \"apply\" method";

@@ -11,5 +11,7 @@ "use strict";

REDUNANT_ARGUMENTS: '存在多余参数,请检查指令语法。',
REQUIRED_OPTIONS: '缺少必需属性 %s,请检查指令语法。',
REQUIRED_OPTIONS: '缺少必需选项 %s,请检查指令语法。',
INVALID_OPTION: '选项 %s 输入无效,%s',
UNKNOWN_OPTIONS: '存在未知选项 %s,请检查指令语法。',
CHECK_SYNTAX: '请检查指令语法。',
SHOW_THIS_MESSAGE: '显示本信息',
UNKNOWN_OPTIONS: '存在未知属性 %s,请检查指令语法。',
USAGE_EXHAUSTED: '调用次数已达上限。',

@@ -21,4 +23,4 @@ COMMAND_SUGGESTION_PREFIX: '没有此命令。',

exports.errors = {
DUPLICATE_COMMAND: 'duplicate command names',
DUPLICATE_OPTION: 'duplicate option names',
DUPLICATE_COMMAND: 'duplicate command names: "%s"',
DUPLICATE_OPTION: 'duplicate option names: "%s"',
EXPECT_COMMAND_NAME: 'expect a command name',

@@ -25,0 +27,0 @@ INVALID_PLUGIN: 'invalid plugin, expect function or object with a "apply" method',

{
"name": "koishi-core",
"description": "Core features for Koishi",
"version": "2.0.0-alpha.7",
"version": "2.0.0-alpha.8",
"main": "dist/index.js",

@@ -37,6 +37,6 @@ "typings": "dist/index.d.ts",

"@types/ms": "^2.0.0",
"@types/ws": "^7.2.2",
"@types/ws": "^7.2.3",
"get-port": "^5.1.1",
"koishi-database-memory": "^2.0.0-alpha.7",
"koishi-test-utils": "^4.0.0-alpha.7"
"koishi-database-memory": "^2.0.0-alpha.8",
"koishi-test-utils": "^4.0.0-alpha.8"
},

@@ -47,3 +47,3 @@ "dependencies": {

"escape-string-regexp": "^2.0.0",
"koishi-utils": "^1.0.5",
"koishi-utils": "^1.0.6",
"leven": "^3.1.0",

@@ -50,0 +50,0 @@ "ms": "^2.1.2",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc