@weapp-vite/devtools-runtime 0.1.0
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/LICENSE +21 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +79 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ice breaker
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Element, MiniProgram, Page } from "@weapp-vite/miniprogram-automator";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
interface MiniProgramEventMap {
|
|
5
|
+
console: (payload: unknown) => void;
|
|
6
|
+
exception: (payload: unknown) => void;
|
|
7
|
+
}
|
|
8
|
+
type AutomatorMiniProgram = InstanceType<typeof MiniProgram>;
|
|
9
|
+
type AutomatorPage = InstanceType<typeof Page>;
|
|
10
|
+
type AutomatorElement = InstanceType<typeof Element> & {
|
|
11
|
+
input?: (value: string) => Promise<void>;
|
|
12
|
+
};
|
|
13
|
+
interface DevtoolsRuntimeSessionOptions {
|
|
14
|
+
miniProgram?: AutomatorMiniProgram;
|
|
15
|
+
preferOpenedSession?: boolean;
|
|
16
|
+
projectPath: string;
|
|
17
|
+
sharedSession?: boolean;
|
|
18
|
+
timeout?: number;
|
|
19
|
+
}
|
|
20
|
+
interface DevtoolsRuntimeHooks {
|
|
21
|
+
connectMiniProgram: (options: DevtoolsRuntimeSessionOptions) => Promise<AutomatorMiniProgram>;
|
|
22
|
+
normalizeConnectionError?: (error: unknown) => unknown;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* @description 获取指定项目的共享 automator 会话;若不存在则自动创建。
|
|
26
|
+
*/
|
|
27
|
+
declare function acquireSharedMiniProgram(hooks: DevtoolsRuntimeHooks, options: DevtoolsRuntimeSessionOptions): Promise<AutomatorMiniProgram>;
|
|
28
|
+
/**
|
|
29
|
+
* @description 释放指定项目的共享会话引用;会话对象会继续缓存,直到显式关闭或重置。
|
|
30
|
+
*/
|
|
31
|
+
declare function releaseSharedMiniProgram(projectPath: string): void;
|
|
32
|
+
/**
|
|
33
|
+
* @description 关闭并移除指定项目的共享 automator 会话。
|
|
34
|
+
*/
|
|
35
|
+
declare function closeSharedMiniProgram(projectPath: string): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* @description 获取当前共享会话数量,供测试断言使用。
|
|
38
|
+
*/
|
|
39
|
+
declare function getSharedMiniProgramSessionCount(): number;
|
|
40
|
+
/**
|
|
41
|
+
* @description 统一管理 automator 会话生命周期。
|
|
42
|
+
*/
|
|
43
|
+
declare function withMiniProgram<T>(hooks: DevtoolsRuntimeHooks, options: DevtoolsRuntimeSessionOptions, runner: (miniProgram: AutomatorMiniProgram) => Promise<T>): Promise<T>;
|
|
44
|
+
//#endregion
|
|
45
|
+
export { AutomatorElement, AutomatorMiniProgram, AutomatorPage, DevtoolsRuntimeHooks, DevtoolsRuntimeSessionOptions, MiniProgramEventMap, acquireSharedMiniProgram, closeSharedMiniProgram, getSharedMiniProgramSessionCount, releaseSharedMiniProgram, withMiniProgram };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
//#region src/index.ts
|
|
2
|
+
const sharedMiniProgramSessions = /* @__PURE__ */ new Map();
|
|
3
|
+
function normalizeError(hooks, error) {
|
|
4
|
+
const normalized = hooks.normalizeConnectionError?.(error) ?? error;
|
|
5
|
+
return normalized instanceof Error ? normalized : new Error(String(normalized));
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* @description 获取指定项目的共享 automator 会话;若不存在则自动创建。
|
|
9
|
+
*/
|
|
10
|
+
async function acquireSharedMiniProgram(hooks, options) {
|
|
11
|
+
const existing = sharedMiniProgramSessions.get(options.projectPath);
|
|
12
|
+
if (existing) {
|
|
13
|
+
existing.refs += 1;
|
|
14
|
+
return await existing.session;
|
|
15
|
+
}
|
|
16
|
+
const session = hooks.connectMiniProgram(options);
|
|
17
|
+
const entry = {
|
|
18
|
+
refs: 1,
|
|
19
|
+
session
|
|
20
|
+
};
|
|
21
|
+
sharedMiniProgramSessions.set(options.projectPath, entry);
|
|
22
|
+
try {
|
|
23
|
+
return await session;
|
|
24
|
+
} catch (error) {
|
|
25
|
+
sharedMiniProgramSessions.delete(options.projectPath);
|
|
26
|
+
throw normalizeError(hooks, error);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* @description 释放指定项目的共享会话引用;会话对象会继续缓存,直到显式关闭或重置。
|
|
31
|
+
*/
|
|
32
|
+
function releaseSharedMiniProgram(projectPath) {
|
|
33
|
+
const entry = sharedMiniProgramSessions.get(projectPath);
|
|
34
|
+
if (!entry) return;
|
|
35
|
+
entry.refs = Math.max(0, entry.refs - 1);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* @description 关闭并移除指定项目的共享 automator 会话。
|
|
39
|
+
*/
|
|
40
|
+
async function closeSharedMiniProgram(projectPath) {
|
|
41
|
+
const entry = sharedMiniProgramSessions.get(projectPath);
|
|
42
|
+
if (!entry) return;
|
|
43
|
+
sharedMiniProgramSessions.delete(projectPath);
|
|
44
|
+
(await entry.session.catch(() => null))?.disconnect();
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* @description 获取当前共享会话数量,供测试断言使用。
|
|
48
|
+
*/
|
|
49
|
+
function getSharedMiniProgramSessionCount() {
|
|
50
|
+
return sharedMiniProgramSessions.size;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* @description 统一管理 automator 会话生命周期。
|
|
54
|
+
*/
|
|
55
|
+
async function withMiniProgram(hooks, options, runner) {
|
|
56
|
+
if (options.miniProgram) return await runner(options.miniProgram);
|
|
57
|
+
if (options.sharedSession) {
|
|
58
|
+
const miniProgram = await acquireSharedMiniProgram(hooks, options);
|
|
59
|
+
try {
|
|
60
|
+
return await runner(miniProgram);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
await closeSharedMiniProgram(options.projectPath);
|
|
63
|
+
throw normalizeError(hooks, error);
|
|
64
|
+
} finally {
|
|
65
|
+
releaseSharedMiniProgram(options.projectPath);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
let miniProgram = null;
|
|
69
|
+
try {
|
|
70
|
+
miniProgram = await hooks.connectMiniProgram(options);
|
|
71
|
+
return await runner(miniProgram);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
throw normalizeError(hooks, error);
|
|
74
|
+
} finally {
|
|
75
|
+
if (miniProgram) await miniProgram.close();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
//#endregion
|
|
79
|
+
export { acquireSharedMiniProgram, closeSharedMiniProgram, getSharedMiniProgramSessionCount, releaseSharedMiniProgram, withMiniProgram };
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@weapp-vite/devtools-runtime",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "Shared WeChat DevTools runtime automation helpers for weapp-vite packages",
|
|
6
|
+
"author": "ice breaker <1324318532@qq.com>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/weapp-vite/weapp-vite.git",
|
|
11
|
+
"directory": "packages/devtools-runtime"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/weapp-vite/weapp-vite/issues"
|
|
15
|
+
},
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"module": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public",
|
|
34
|
+
"registry": "https://registry.npmjs.org/"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@weapp-vite/miniprogram-automator": "1.0.4"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"dev": "tsdown -w --sourcemap",
|
|
41
|
+
"build": "tsdown",
|
|
42
|
+
"test": "vitest run",
|
|
43
|
+
"test:dev": "vitest",
|
|
44
|
+
"typecheck": "tsc --noEmit",
|
|
45
|
+
"lint": "eslint .",
|
|
46
|
+
"lint:fix": "eslint . --fix"
|
|
47
|
+
}
|
|
48
|
+
}
|