yeow-api 0.2.57 → 0.2.59

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yeow-api",
3
- "version": "0.2.57",
3
+ "version": "0.2.59",
4
4
  "description": "Yeow API",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -0,0 +1,3 @@
1
+ declare module '__yeow-assets' {
2
+ export function getPath(path: string): string;
3
+ }
@@ -0,0 +1,7 @@
1
+ /// <reference path="assets-path.d.ts" />
2
+
3
+ import { getPath as _getPath } from '__yeow-assets';
4
+
5
+ export function getAssetsPath(path: string): string {
6
+ return _getPath(path);
7
+ }
package/src/command.ts CHANGED
@@ -33,13 +33,21 @@ export function registerCommand(name: string, options: CommandOptions): boolean
33
33
  const completer = options.completer;
34
34
  const pluginName = __plugin?.name || 'unknown';
35
35
 
36
- const cbId = _registerCallback((payload: CommandPayload) => {
37
- const sender = payload.sender;
36
+ function decorateSender(sender: CommandSender): CommandSender {
38
37
  if (sender?.uuid && sender.isPlayer) {
39
38
  (sender as any).sendMessage = (msg: string) => {
40
39
  call('player.sendMessage', { uuid: sender.uuid, message: msg });
41
40
  };
41
+ } else {
42
+ (sender as any).sendMessage = (msg: string) => {
43
+ $send('log', { level: 'INFO', message: msg });
44
+ };
42
45
  }
46
+ return sender;
47
+ }
48
+
49
+ const cbId = _registerCallback((payload: CommandPayload) => {
50
+ (payload as any).sender = decorateSender(payload.sender);
43
51
  executor(payload);
44
52
  }, { persistent: true });
45
53
 
@@ -56,6 +64,7 @@ export function registerCommand(name: string, options: CommandOptions): boolean
56
64
 
57
65
  compCbId = _registerCallback((data: any) => {
58
66
  try {
67
+ const sender = decorateSender(data.sender);
59
68
  if (manualRelease) {
60
69
  const complete = (result: string[]) => {
61
70
  $send('task', {
@@ -64,9 +73,9 @@ export function registerCommand(name: string, options: CommandOptions): boolean
64
73
  cb: '',
65
74
  });
66
75
  };
67
- completerFn(data.sender, data.args, complete);
76
+ completerFn(sender, data.args, complete);
68
77
  } else {
69
- const result = completerFn(data.sender, data.args);
78
+ const result = completerFn(sender, data.args);
70
79
  if (result && typeof result.then === 'function') {
71
80
  $send('task', {
72
81
  type: 'command.tabComplete',
package/src/index.ts CHANGED
@@ -41,6 +41,7 @@ export {
41
41
  export { assets, read as assetsRead, readSync as assetsReadSync,
42
42
  readBase64 as assetsReadBase64, readBase64Sync as assetsReadBase64Sync,
43
43
  extract as assetsExtract, extractSync as assetsExtractSync } from './assets.js';
44
+ export { getAssetsPath } from './assets-path.js';
44
45
  export { path } from './path.js';
45
46
  export { listen, respond, close, request } from './http.js';
46
47
  export { logError } from './log-error.js';
@@ -80,5 +81,5 @@ export { createBoard as createScoreboard, deleteBoard as deleteScoreboard,
80
81
  export { getMaterials, getBlocks, getItems } from './material.js';
81
82
  export type { MaterialInfo } from './material.js';
82
83
  export { registerService, registerNativeService, request as serviceRequest, subscribe as serviceSubscribe, publish as servicePublish } from './service.js';
83
- export type { ServiceResult } from './service.js';
84
+ export type { ServiceResult, NativeServiceResult } from './service.js';
84
85
  export { log, Logger } from './log.js';
package/src/log.ts CHANGED
@@ -3,15 +3,20 @@ function _sendLog(level: string, prefix: string, ...args: unknown[]): void {
3
3
  $send('log', { level, message: msg });
4
4
  }
5
5
 
6
+ function _pluginPrefix(): string {
7
+ return (typeof __plugin !== 'undefined' && __plugin?.name)
8
+ ? '[' + __plugin.name + '] ' : '';
9
+ }
10
+
6
11
  export const log = {
7
12
  info(...args: unknown[]): void {
8
- _sendLog('INFO', '', ...args);
13
+ _sendLog('INFO', _pluginPrefix(), ...args);
9
14
  },
10
15
  warn(...args: unknown[]): void {
11
- _sendLog('WARN', '', ...args);
16
+ _sendLog('WARN', _pluginPrefix(), ...args);
12
17
  },
13
18
  error(...args: unknown[]): void {
14
- _sendLog('ERROR', '', ...args);
19
+ _sendLog('ERROR', _pluginPrefix(), ...args);
15
20
  },
16
21
  };
17
22
 
package/src/service.ts CHANGED
@@ -5,7 +5,18 @@ export interface ServiceResult {
5
5
  token: string;
6
6
  }
7
7
 
8
- let _reqSeq = 0;
8
+ export interface NativeTerminateInfo {
9
+ serviceId: string;
10
+ reason: string;
11
+ exitCode?: number;
12
+ output?: string;
13
+ }
14
+
15
+ export interface NativeServiceResult {
16
+ serviceId: string;
17
+ ready: () => Promise<void>;
18
+ onTerminate: (handler: (info: NativeTerminateInfo) => void) => void;
19
+ }
9
20
 
10
21
  // ── Register Plugin Service ──
11
22
 
@@ -19,7 +30,13 @@ export async function registerService(refName: string, onRequest: (path: string,
19
30
  }
20
31
  }, { persistent: true });
21
32
 
22
- const r = $send('service', { t: 'register', refName, onRequest: svcCbId, public: isPublic }) as ServiceResult;
33
+ const r = $send('service', { t: 'register', refName, onRequest: svcCbId, public: isPublic }) as ServiceResult & { err?: string; serviceId?: string };
34
+ if (r?.err) {
35
+ _unregisterCallback(svcCbId);
36
+ const e: any = new Error(r.err);
37
+ if (r.serviceId) e.serviceId = r.serviceId;
38
+ throw e;
39
+ }
23
40
  return r;
24
41
  }
25
42
 
@@ -28,9 +45,42 @@ export async function registerService(refName: string, onRequest: (path: string,
28
45
  type NativePlatform = string | { file: string } | { dir: string; entry: string };
29
46
  type NativePlatforms = Record<string, NativePlatform>;
30
47
 
31
- export async function registerNativeService(refName: string, platforms: NativePlatforms, isPublic = true): Promise<{ serviceId: string }> {
32
- const r = $send('service', { t: 'registerNative', refName, platforms, public: isPublic }) as { serviceId: string };
33
- return r;
48
+ function _serviceReady(serviceId: string): Promise<void> {
49
+ return new Promise((resolve, reject) => {
50
+ const cbId = _registerCallback((result: any) => {
51
+ if (result?.err) {
52
+ const info = result.err;
53
+ if (typeof info === 'string') {
54
+ reject(new Error(info));
55
+ } else {
56
+ const e = new Error(info.message || 'Unknown error');
57
+ (e as any).exitCode = info.exitCode;
58
+ (e as any).output = info.output || '';
59
+ reject(e);
60
+ }
61
+ } else resolve();
62
+ });
63
+ $send('service', { t: 'awaitReady', serviceId, cb: cbId });
64
+ });
65
+ }
66
+
67
+ export async function registerNativeService(refName: string, platforms: NativePlatforms, isPublic = true): Promise<NativeServiceResult> {
68
+ const r = $send('service', { t: 'registerNative', refName, platforms, public: isPublic }) as { serviceId: string; err?: string };
69
+ if (r.err) {
70
+ const e: any = new Error(r.err);
71
+ if ((r as any).serviceId) e.serviceId = (r as any).serviceId;
72
+ throw e;
73
+ }
74
+ let terminateCb: string | null = null;
75
+ return {
76
+ serviceId: r.serviceId,
77
+ ready: () => _serviceReady(r.serviceId),
78
+ onTerminate(handler) {
79
+ if (terminateCb) _unregisterCallback(terminateCb);
80
+ terminateCb = _registerCallback((info: unknown) => handler(info as NativeTerminateInfo), { persistent: true });
81
+ $send('service', { t: 'registerNativeTerminate', serviceId: r.serviceId, cb: terminateCb });
82
+ },
83
+ };
34
84
  }
35
85
 
36
86
  // ── Request ──