@lppx/nlearn 1.1.5 → 1.1.8
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/dist/src/cli/cli.js +103 -2
- package/dist/src/cli/index.js +15 -1
- package/package.json +1 -1
package/dist/src/cli/cli.js
CHANGED
|
@@ -1,13 +1,54 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
2
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
39
|
exports.scanDemoFunctions = scanDemoFunctions;
|
|
40
|
+
exports.selectFunctionWithFuzzySearch = selectFunctionWithFuzzySearch;
|
|
41
|
+
exports.executeDemoFunction = executeDemoFunction;
|
|
4
42
|
const promises_1 = require("node:fs/promises");
|
|
5
43
|
const node_path_1 = require("node:path");
|
|
44
|
+
const prompts_1 = __importDefault(require("prompts"));
|
|
45
|
+
const fuse_js_1 = __importDefault(require("fuse.js"));
|
|
6
46
|
/**
|
|
7
47
|
* 扫描 demo 文件夹中的所有示例函数
|
|
8
48
|
*/
|
|
9
49
|
async function scanDemoFunctions() {
|
|
10
|
-
|
|
50
|
+
// 从 cli.ts 所在目录向上找到包根目录,然后定位到 demo 目录
|
|
51
|
+
const demoPath = (0, node_path_1.join)(__dirname, '..', 'demo');
|
|
11
52
|
const results = [];
|
|
12
53
|
try {
|
|
13
54
|
// 读取所有文件夹
|
|
@@ -27,10 +68,12 @@ async function scanDemoFunctions() {
|
|
|
27
68
|
let match;
|
|
28
69
|
while ((match = functionRegex.exec(content)) !== null) {
|
|
29
70
|
const functionName = match[1];
|
|
71
|
+
const displayName = `${folder.name}@${file.name.replace('.ts', '')}@${functionName}`;
|
|
30
72
|
results.push({
|
|
31
73
|
folder: folder.name,
|
|
32
74
|
file: file.name.replace('.ts', ''),
|
|
33
|
-
functionName
|
|
75
|
+
functionName,
|
|
76
|
+
displayName
|
|
34
77
|
});
|
|
35
78
|
}
|
|
36
79
|
}
|
|
@@ -42,3 +85,61 @@ async function scanDemoFunctions() {
|
|
|
42
85
|
return [];
|
|
43
86
|
}
|
|
44
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* 使用 prompts 和 fuse.js 实现模糊搜索选择函数
|
|
90
|
+
*/
|
|
91
|
+
async function selectFunctionWithFuzzySearch(functions) {
|
|
92
|
+
// 配置 Fuse.js 进行模糊搜索
|
|
93
|
+
const fuse = new fuse_js_1.default(functions, {
|
|
94
|
+
keys: ['displayName', 'folder', 'file', 'functionName'],
|
|
95
|
+
threshold: 0.4,
|
|
96
|
+
includeScore: true
|
|
97
|
+
});
|
|
98
|
+
// 自定义搜索建议函数
|
|
99
|
+
const suggest = (input, choices) => {
|
|
100
|
+
if (!input) {
|
|
101
|
+
return Promise.resolve(choices);
|
|
102
|
+
}
|
|
103
|
+
const results = fuse.search(input);
|
|
104
|
+
return Promise.resolve(results.map(result => ({
|
|
105
|
+
title: result.item.displayName,
|
|
106
|
+
value: result.item
|
|
107
|
+
})));
|
|
108
|
+
};
|
|
109
|
+
const response = await (0, prompts_1.default)({
|
|
110
|
+
type: 'autocomplete',
|
|
111
|
+
name: 'selectedFunction',
|
|
112
|
+
message: '请选择要运行的示例函数 (支持模糊搜索):',
|
|
113
|
+
choices: functions.map(fn => ({
|
|
114
|
+
title: fn.displayName,
|
|
115
|
+
value: fn
|
|
116
|
+
})),
|
|
117
|
+
suggest
|
|
118
|
+
});
|
|
119
|
+
return response.selectedFunction || null;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* 动态导入并执行选中的函数
|
|
123
|
+
*/
|
|
124
|
+
async function executeDemoFunction(demoFunc) {
|
|
125
|
+
try {
|
|
126
|
+
console.log(`\n正在运行: ${demoFunc.displayName}\n`);
|
|
127
|
+
console.log('='.repeat(50));
|
|
128
|
+
// 构建模块路径(相对于 cli.ts 所在目录)
|
|
129
|
+
const modulePath = (0, node_path_1.join)(__dirname, '..', 'demo', demoFunc.folder, `${demoFunc.file}.js`);
|
|
130
|
+
// 动态导入模块
|
|
131
|
+
const module = await Promise.resolve(`${modulePath}`).then(s => __importStar(require(s)));
|
|
132
|
+
// 检查函数是否存在
|
|
133
|
+
if (typeof module[demoFunc.functionName] !== 'function') {
|
|
134
|
+
console.error(`错误: 函数 ${demoFunc.functionName} 不存在或不是一个函数`);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
// 执行函数
|
|
138
|
+
await module[demoFunc.functionName]();
|
|
139
|
+
console.log('\n' + '='.repeat(50));
|
|
140
|
+
console.log('✓ 执行完成\n');
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
console.error('\n执行函数时出错:', error);
|
|
144
|
+
}
|
|
145
|
+
}
|
package/dist/src/cli/index.js
CHANGED
|
@@ -38,6 +38,20 @@ program.command("list")
|
|
|
38
38
|
program.command("run")
|
|
39
39
|
.description("运行示例函数")
|
|
40
40
|
.action(async () => {
|
|
41
|
-
(0, node_console_1.log)("
|
|
41
|
+
(0, node_console_1.log)("扫描示例函数...\n");
|
|
42
|
+
const functions = await (0, cli_js_1.scanDemoFunctions)();
|
|
43
|
+
if (functions.length === 0) {
|
|
44
|
+
(0, node_console_1.log)("未找到任何示例函数");
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
(0, node_console_1.log)(`找到 ${functions.length} 个示例函数\n`);
|
|
48
|
+
// 使用 prompts 和 fuse.js 实现模糊搜索选择
|
|
49
|
+
const selectedFunction = await (0, cli_js_1.selectFunctionWithFuzzySearch)(functions);
|
|
50
|
+
if (!selectedFunction) {
|
|
51
|
+
(0, node_console_1.log)("\n未选择任何函数");
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
// 执行选中的函数
|
|
55
|
+
await (0, cli_js_1.executeDemoFunction)(selectedFunction);
|
|
42
56
|
});
|
|
43
57
|
program.parse(process.argv);
|