@lppx/nlearn 1.1.3 → 1.1.4

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 +1,44 @@
1
1
  "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.scanDemoFunctions = scanDemoFunctions;
4
+ const promises_1 = require("node:fs/promises");
5
+ const node_path_1 = require("node:path");
6
+ /**
7
+ * 扫描 demo 文件夹中的所有示例函数
8
+ */
9
+ async function scanDemoFunctions() {
10
+ const demoPath = (0, node_path_1.join)(process.cwd(), 'src', 'demo');
11
+ const results = [];
12
+ try {
13
+ // 读取所有文件夹
14
+ const folders = await (0, promises_1.readdir)(demoPath, { withFileTypes: true });
15
+ for (const folder of folders) {
16
+ if (!folder.isDirectory())
17
+ continue;
18
+ const folderPath = (0, node_path_1.join)(demoPath, folder.name);
19
+ const files = await (0, promises_1.readdir)(folderPath, { withFileTypes: true });
20
+ for (const file of files) {
21
+ if (!file.isFile() || !file.name.endsWith('.ts'))
22
+ continue;
23
+ const filePath = (0, node_path_1.join)(folderPath, file.name);
24
+ const content = await (0, promises_1.readFile)(filePath, 'utf-8');
25
+ // 提取函数名(匹配 async function 和 function 声明)
26
+ const functionRegex = /(?:async\s+)?function\s+(\w+)\s*\(/g;
27
+ let match;
28
+ while ((match = functionRegex.exec(content)) !== null) {
29
+ const functionName = match[1];
30
+ results.push({
31
+ folder: folder.name,
32
+ file: file.name.replace('.ts', ''),
33
+ functionName
34
+ });
35
+ }
36
+ }
37
+ }
38
+ return results;
39
+ }
40
+ catch (error) {
41
+ console.error('扫描示例函数时出错:', error);
42
+ return [];
43
+ }
44
+ }
@@ -2,9 +2,42 @@
2
2
  "use strict";
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  const commander_1 = require("commander");
5
+ const node_console_1 = require("node:console");
6
+ const cli_js_1 = require("./cli.js");
5
7
  const program = new commander_1.Command();
6
8
  program
7
9
  .name('nlearn')
8
10
  .description('nodejs 生态学习工具')
9
11
  .version('1.0.0');
12
+ program.command("list")
13
+ .alias("ls")
14
+ .description("列出所有模块")
15
+ .action(async () => {
16
+ (0, node_console_1.log)("列出所有示例\n");
17
+ const functions = await (0, cli_js_1.scanDemoFunctions)();
18
+ if (functions.length === 0) {
19
+ (0, node_console_1.log)("未找到任何示例函数");
20
+ return;
21
+ }
22
+ // 按文件夹分组显示
23
+ const grouped = functions.reduce((acc, item) => {
24
+ if (!acc[item.folder]) {
25
+ acc[item.folder] = [];
26
+ }
27
+ acc[item.folder].push(item);
28
+ return acc;
29
+ }, {});
30
+ for (const [folder, items] of Object.entries(grouped)) {
31
+ (0, node_console_1.log)(`\n📦 ${folder}`);
32
+ items.forEach(item => {
33
+ (0, node_console_1.log)(` ${item.folder}@${item.file}@${item.functionName}`);
34
+ });
35
+ }
36
+ (0, node_console_1.log)(`\n共找到 ${functions.length} 个示例函数`);
37
+ });
38
+ program.command("run")
39
+ .description("运行示例函数")
40
+ .action(async () => {
41
+ (0, node_console_1.log)("运行用户选择的示例函数");
42
+ });
10
43
  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.3",
6
+ "version": "1.1.4",
7
7
  "description": "一个nodejs学习工具",
8
8
  "bin": {
9
9
  "nlearn": "dist/src/cli/index.js",