@lppx/nlearn 1.1.5 → 1.1.7

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.
@@ -1,8 +1,48 @@
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
  */
@@ -27,10 +67,12 @@ async function scanDemoFunctions() {
27
67
  let match;
28
68
  while ((match = functionRegex.exec(content)) !== null) {
29
69
  const functionName = match[1];
70
+ const displayName = `${folder.name}@${file.name.replace('.ts', '')}@${functionName}`;
30
71
  results.push({
31
72
  folder: folder.name,
32
73
  file: file.name.replace('.ts', ''),
33
- functionName
74
+ functionName,
75
+ displayName
34
76
  });
35
77
  }
36
78
  }
@@ -42,3 +84,61 @@ async function scanDemoFunctions() {
42
84
  return [];
43
85
  }
44
86
  }
87
+ /**
88
+ * 使用 prompts 和 fuse.js 实现模糊搜索选择函数
89
+ */
90
+ async function selectFunctionWithFuzzySearch(functions) {
91
+ // 配置 Fuse.js 进行模糊搜索
92
+ const fuse = new fuse_js_1.default(functions, {
93
+ keys: ['displayName', 'folder', 'file', 'functionName'],
94
+ threshold: 0.4,
95
+ includeScore: true
96
+ });
97
+ // 自定义搜索建议函数
98
+ const suggest = (input, choices) => {
99
+ if (!input) {
100
+ return Promise.resolve(choices);
101
+ }
102
+ const results = fuse.search(input);
103
+ return Promise.resolve(results.map(result => ({
104
+ title: result.item.displayName,
105
+ value: result.item
106
+ })));
107
+ };
108
+ const response = await (0, prompts_1.default)({
109
+ type: 'autocomplete',
110
+ name: 'selectedFunction',
111
+ message: '请选择要运行的示例函数 (支持模糊搜索):',
112
+ choices: functions.map(fn => ({
113
+ title: fn.displayName,
114
+ value: fn
115
+ })),
116
+ suggest
117
+ });
118
+ return response.selectedFunction || null;
119
+ }
120
+ /**
121
+ * 动态导入并执行选中的函数
122
+ */
123
+ async function executeDemoFunction(demoFunc) {
124
+ try {
125
+ console.log(`\n正在运行: ${demoFunc.displayName}\n`);
126
+ console.log('='.repeat(50));
127
+ // 构建模块路径
128
+ const modulePath = (0, node_path_1.join)(process.cwd(), 'src', 'demo', demoFunc.folder, `${demoFunc.file}.js`);
129
+ // 动态导入模块
130
+ const module = await Promise.resolve(`${modulePath}`).then(s => __importStar(require(s)));
131
+ // 检查函数是否存在
132
+ if (typeof module[demoFunc.functionName] !== 'function') {
133
+ console.error(`错误: 函数 ${demoFunc.functionName} 不存在或不是一个函数`);
134
+ return;
135
+ }
136
+ // 执行函数
137
+ await module[demoFunc.functionName]();
138
+ console.log('\n' + '='.repeat(50));
139
+ console.log('✓ 执行完成\n');
140
+ }
141
+ catch (error) {
142
+ console.error('\n执行函数时出错:', error);
143
+ }
144
+ }
@@ -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);
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.1.5",
6
+ "version": "1.1.7",
7
7
  "description": "一个nodejs学习工具",
8
8
  "bin": {
9
9
  "nlearn": "dist/src/cli/index.js",