From c481753cff839abf061e03822c9ef6429528a86d Mon Sep 17 00:00:00 2001 From: "Vincent Yanzee J. Tan" Date: Sat, 7 Jun 2025 21:57:38 +0800 Subject: [PATCH] refactor: extract bricklib.args into sift --- src/bricklib/index.ts | 1 - src/index.ts | 9 ++++++--- src/{bricklib/args => sift}/defs.ts | 8 ++++---- src/{bricklib/args => sift}/index.ts | 27 ++++++++++++++++++++------- src/{bricklib/args => sift}/parser.ts | 0 src/{bricklib/args => sift}/tokens.ts | 0 src/{bricklib/args => sift}/types.ts | 0 7 files changed, 30 insertions(+), 15 deletions(-) rename src/{bricklib/args => sift}/defs.ts (81%) rename src/{bricklib/args => sift}/index.ts (69%) rename src/{bricklib/args => sift}/parser.ts (100%) rename src/{bricklib/args => sift}/tokens.ts (100%) rename src/{bricklib/args => sift}/types.ts (100%) diff --git a/src/bricklib/index.ts b/src/bricklib/index.ts index def6786..72505bb 100644 --- a/src/bricklib/index.ts +++ b/src/bricklib/index.ts @@ -7,7 +7,6 @@ export const VERSION = '0.1.0-beta'; /* modules */ export { default as config } from './config.js'; -export * as args from './args/index.js'; export * as command from './command.js'; export * as database from './database.js'; export * as events from './events.js'; diff --git a/src/index.ts b/src/index.ts index 5f129d5..2a2062f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,12 @@ import { FormCancelationReason } from '@minecraft/server-ui'; import * as bricklib from './bricklib/index.js'; import * as gatepass from './gatepass/index.js'; +import * as sift from './sift/index.js'; /* load plugins */ -bricklib.plugin.loadPlugin('gatepass'); +const load = bricklib.plugin.loadPlugin; +load('gatepass'); +load('sift'); const mgr = new bricklib.command.CommandManager(); @@ -23,12 +26,12 @@ const def = { args: [ { id: 'text', - type: bricklib.args.parsers.variadic(bricklib.args.parsers.string()), + type: sift.parsers.variadic(sift.parsers.string()), } ] }; -mgr.registerCommand(...bricklib.args.makeCommand(def, (args, src) => { +mgr.registerCommand(...sift.makeCommand(def, (args, src) => { gatepass.assertPermission('chat.echo', src); src.sendMessage(args.text.join(' ')); return 0; diff --git a/src/bricklib/args/defs.ts b/src/sift/defs.ts similarity index 81% rename from src/bricklib/args/defs.ts rename to src/sift/defs.ts index 4fc1f2a..aee4688 100644 --- a/src/bricklib/args/defs.ts +++ b/src/sift/defs.ts @@ -1,5 +1,5 @@ +import type * as bricklib from '../bricklib/index.js'; import type { ArgTokenStream } from './tokens.js'; -import type { RawString } from '../rawtext.js'; /** * A command argument parser. @@ -17,7 +17,7 @@ export type ParseResult> = T; export interface CmdArgument { id: PropertyKey, - help?: string | RawString, + help?: string | bricklib.rawtext.RawString, name?: string, type: TypeParser, @@ -33,7 +33,7 @@ export interface CmdArgument export interface CmdOption { id: PropertyKey, - help?: string | RawString, + help?: string | bricklib.rawtext.RawString, long?: string[], short?: string, @@ -46,7 +46,7 @@ export interface CmdOption export interface CmdVerb { id: PropertyKey, - help?: string | RawString, + help?: string | bricklib.rawtext.RawString, name: string, aliases?: string[], diff --git a/src/bricklib/args/index.ts b/src/sift/index.ts similarity index 69% rename from src/bricklib/args/index.ts rename to src/sift/index.ts index c719309..c38a48c 100644 --- a/src/bricklib/args/index.ts +++ b/src/sift/index.ts @@ -1,14 +1,27 @@ +/** + * Sift -- A flexible command parser for bricklib. + * This plugin is part of the bricklib project. + */ + +/* TODO: help-text gen, cmd builder, and more type parsers... */ + +import * as bricklib from '../bricklib/index.js'; import { Player } from '@minecraft/server'; -import { CommandCallback, CommandManager } from '../command.js'; import { parseVerb } from './parser.js'; import { ArgTokenStream } from './tokens.js'; import type { CmdVerb, ParseResult } from './defs.ts'; + export type * from './defs.ts'; export * from './parser.js'; export * from './tokens.js'; export * as parsers from './types.js'; +bricklib.plugin.newPlugin('sift', () => { + /* no-op */ +}); + + /** * Parse a custom command. * @param def The command parsing definition. @@ -25,25 +38,25 @@ export function parseCommand(def: CmdVerb, args: string[]): ParseResult /** * Make a command def that you can pass to - * {@link CommandManager.registerCommand}. + * {@link bricklib.command.CommandManager.registerCommand}. * @param def The command definition. * @param fn The callback. * @returns Array of args. * @example * ```ts - * import * as bricklib from './bricklib/index.js'; + * import * as sift from './sift/index.js'; * const def = { * id: 'echo', * name: 'echo', * args: [ * { * id: 'text', - * type: bricklib.args.parsers.string(), + * type: sift.parsers.string(), * } * ] * }; * - * mgr.registerCommand(...bricklib.args.makeCommand(def, (args, src) => { + * mgr.registerCommand(...sift.makeCommand(def, (args, src) => { * src.sendMessage(args.text); * return 0; * })); @@ -52,9 +65,9 @@ export function parseCommand(def: CmdVerb, args: string[]): ParseResult export function makeCommand( def: CmdVerb, fn: (args: ParseResult, src: Player) => number - ): [string[], CommandCallback] + ): [string[], bricklib.command.CommandCallback] { - const out: [string[], CommandCallback] = [null, null]; + const out: [string[], bricklib.command.CommandCallback] = [null, null]; out[0] = [def.name]; if (def.aliases) out[0] = out[0].concat(def.aliases); diff --git a/src/bricklib/args/parser.ts b/src/sift/parser.ts similarity index 100% rename from src/bricklib/args/parser.ts rename to src/sift/parser.ts diff --git a/src/bricklib/args/tokens.ts b/src/sift/tokens.ts similarity index 100% rename from src/bricklib/args/tokens.ts rename to src/sift/tokens.ts diff --git a/src/bricklib/args/types.ts b/src/sift/types.ts similarity index 100% rename from src/bricklib/args/types.ts rename to src/sift/types.ts