@lppx/nlearn 1.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.
Files changed (19) hide show
  1. package/dist/scripts/sync-repo.js +100 -0
  2. package/dist/src/cli/cli.js +9 -0
  3. package/dist/src/demo/commander/01-/345/237/272/347/241/200/346/246/202/345/277/265.js +174 -0
  4. package/dist/src/demo/commander/02-/345/221/275/344/273/244/347/263/273/347/273/237.js +260 -0
  5. package/dist/src/demo/commander/03-/351/253/230/347/272/247/347/211/271/346/200/247.js +285 -0
  6. package/dist/src/demo/commander/04-/345/256/236/346/210/230/346/241/210/344/276/213.js +408 -0
  7. package/dist/src/demo/esm/01-/345/237/272/347/241/200/346/246/202/345/277/265.js +226 -0
  8. package/dist/src/demo/esm/02-/345/257/274/345/207/272/350/257/255/346/263/225.js +281 -0
  9. package/dist/src/demo/esm/03-/345/257/274/345/205/245/350/257/255/346/263/225.js +366 -0
  10. package/dist/src/demo/esm/04-/345/256/236/346/210/230/346/241/210/344/276/213.js +509 -0
  11. package/dist/src/demo/inquirer/01-/345/237/272/347/241/200/346/246/202/345/277/265.js +135 -0
  12. package/dist/src/demo/inquirer/02-/351/200/211/346/213/251/347/261/273/345/236/213.js +143 -0
  13. package/dist/src/demo/inquirer/03-/351/253/230/347/272/247/347/211/271/346/200/247.js +211 -0
  14. package/dist/src/demo/inquirer/04-/345/256/236/346/210/230/346/241/210/344/276/213.js +343 -0
  15. package/dist/src/demo/yargs/01-/345/237/272/347/241/200/346/246/202/345/277/265.js +142 -0
  16. package/dist/src/demo/yargs/02-/345/221/275/344/273/244/347/263/273/347/273/237.js +211 -0
  17. package/dist/src/demo/yargs/03-/351/253/230/347/272/247/347/211/271/346/200/247.js +205 -0
  18. package/dist/src/demo/yargs/04-/345/256/236/346/210/230/346/241/210/344/276/213.js +276 -0
  19. package/package.json +39 -0
@@ -0,0 +1,276 @@
1
+ "use strict";
2
+ /**
3
+ * yargs 实战案例
4
+ * ================
5
+ * 综合运用 yargs 构建完整的 CLI 工具
6
+ * 适用版本: yargs ^17.0.0 或 ^18.0.0
7
+ */
8
+ var __importDefault = (this && this.__importDefault) || function (mod) {
9
+ return (mod && mod.__esModule) ? mod : { "default": mod };
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const yargs_1 = __importDefault(require("yargs"));
13
+ const helpers_1 = require("yargs/helpers");
14
+ // #region 示例1: 文件管理工具
15
+ function demoFileManagerTool() {
16
+ console.log('\n=== 示例1: 文件管理工具 ===');
17
+ // 模拟一个简单的文件管理 CLI
18
+ // 运行: ts-node 04-实战案例.ts 1 create --name=test.txt --content="Hello World"
19
+ (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv.slice(1)))
20
+ .scriptName('filemgr')
21
+ .usage('$0 <命令> [选项]')
22
+ .command('create', '创建文件', (yargs) => {
23
+ return yargs
24
+ .option('name', {
25
+ alias: 'n',
26
+ type: 'string',
27
+ description: '文件名',
28
+ demandOption: true
29
+ })
30
+ .option('content', {
31
+ alias: 'c',
32
+ type: 'string',
33
+ description: '文件内容',
34
+ default: ''
35
+ });
36
+ }, (argv) => {
37
+ console.log(`✓ 创建文件: ${argv.name}`);
38
+ console.log(` 内容: ${argv.content}`);
39
+ })
40
+ .command('read <filename>', '读取文件', (yargs) => {
41
+ return yargs.positional('filename', {
42
+ describe: '文件名',
43
+ type: 'string'
44
+ });
45
+ }, (argv) => {
46
+ console.log(`✓ 读取文件: ${argv.filename}`);
47
+ })
48
+ .demandCommand(1)
49
+ .help()
50
+ .argv;
51
+ }
52
+ // #endregion
53
+ // #region 示例2: 数据库迁移工具
54
+ function demoDatabaseMigrationTool() {
55
+ console.log('\n=== 示例2: 数据库迁移工具 ===');
56
+ // 模拟数据库迁移 CLI(类似 Prisma、TypeORM)
57
+ // 运行: ts-node 04-实战案例.ts 2 migrate up --env=production
58
+ (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv.slice(1)))
59
+ .scriptName('dbmigrate')
60
+ .command('migrate <action>', '执行数据库迁移', (yargs) => {
61
+ return yargs
62
+ .positional('action', {
63
+ describe: '迁移操作',
64
+ type: 'string',
65
+ choices: ['up', 'down', 'status']
66
+ })
67
+ .option('env', {
68
+ alias: 'e',
69
+ type: 'string',
70
+ description: '环境',
71
+ choices: ['development', 'staging', 'production'],
72
+ default: 'development'
73
+ })
74
+ .option('steps', {
75
+ alias: 's',
76
+ type: 'number',
77
+ description: '迁移步数',
78
+ default: 1
79
+ });
80
+ }, (argv) => {
81
+ console.log(`✓ 执行迁移: ${argv.action}`);
82
+ console.log(` 环境: ${argv.env}`);
83
+ console.log(` 步数: ${argv.steps}`);
84
+ })
85
+ .demandCommand(1)
86
+ .help()
87
+ .argv;
88
+ }
89
+ // #endregion
90
+ // #region 示例3: 项目脚手架工具
91
+ function demoProjectScaffolder() {
92
+ console.log('\n=== 示例3: 项目脚手架工具 ===');
93
+ // 模拟项目生成器(类似 create-react-app)
94
+ // 运行: ts-node 04-实战案例.ts 3 init my-project --template=react --typescript
95
+ (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv.slice(1)))
96
+ .scriptName('create-app')
97
+ .command('init <project-name>', '初始化新项目', (yargs) => {
98
+ return yargs
99
+ .positional('project-name', {
100
+ describe: '项目名称',
101
+ type: 'string'
102
+ })
103
+ .option('template', {
104
+ alias: 't',
105
+ type: 'string',
106
+ description: '项目模板',
107
+ choices: ['react', 'vue', 'angular', 'express'],
108
+ default: 'react'
109
+ })
110
+ .option('typescript', {
111
+ type: 'boolean',
112
+ description: '使用 TypeScript',
113
+ default: false
114
+ })
115
+ .option('git', {
116
+ type: 'boolean',
117
+ description: '初始化 Git 仓库',
118
+ default: true
119
+ });
120
+ }, (argv) => {
121
+ console.log(`✓ 创建项目: ${argv['project-name']}`);
122
+ console.log(` 模板: ${argv.template}`);
123
+ console.log(` TypeScript: ${argv.typescript ? '是' : '否'}`);
124
+ console.log(` Git: ${argv.git ? '是' : '否'}`);
125
+ })
126
+ .demandCommand(1)
127
+ .help()
128
+ .argv;
129
+ }
130
+ // #endregion
131
+ // #region 示例4: 部署工具
132
+ function demoDeploymentTool() {
133
+ console.log('\n=== 示例4: 部署工具 ===');
134
+ // 模拟应用部署 CLI
135
+ // 运行: ts-node 04-实战案例.ts 4 deploy --target=production --branch=main --force
136
+ (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv.slice(1)))
137
+ .scriptName('deployer')
138
+ .command('deploy', '部署应用', (yargs) => {
139
+ return yargs
140
+ .option('target', {
141
+ alias: 't',
142
+ type: 'string',
143
+ description: '部署目标',
144
+ choices: ['development', 'staging', 'production'],
145
+ demandOption: true
146
+ })
147
+ .option('branch', {
148
+ alias: 'b',
149
+ type: 'string',
150
+ description: 'Git 分支',
151
+ default: 'main'
152
+ })
153
+ .option('force', {
154
+ alias: 'f',
155
+ type: 'boolean',
156
+ description: '强制部署',
157
+ default: false
158
+ })
159
+ .check((argv) => {
160
+ if (argv.target === 'production' && !argv.force) {
161
+ console.log('⚠️ 警告: 部署到生产环境,请确认!');
162
+ }
163
+ return true;
164
+ });
165
+ }, (argv) => {
166
+ console.log(`✓ 开始部署...`);
167
+ console.log(` 目标: ${argv.target}`);
168
+ console.log(` 分支: ${argv.branch}`);
169
+ console.log(` 强制: ${argv.force ? '是' : '否'}`);
170
+ })
171
+ .demandCommand(1)
172
+ .help()
173
+ .argv;
174
+ }
175
+ // #endregion
176
+ // #region 示例5: 完整的 CLI 应用
177
+ function demoCompleteTaskManager() {
178
+ console.log('\n=== 示例5: 完整的 CLI 应用 ===');
179
+ // 综合示例:一个完整的任务管理 CLI
180
+ // 运行: ts-node 04-实战案例.ts 5 task add "完成项目文档" --priority=high
181
+ // 运行: ts-node 04-实战案例.ts 5 task list --status=pending
182
+ (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv.slice(1)))
183
+ .scriptName('taskmgr')
184
+ .version('1.0.0')
185
+ .usage('$0 <命令> [选项]')
186
+ .command('task', '任务管理', (yargs) => {
187
+ return yargs
188
+ .command('add <title>', '添加任务', (yargs) => {
189
+ return yargs
190
+ .positional('title', {
191
+ describe: '任务标题',
192
+ type: 'string'
193
+ })
194
+ .option('priority', {
195
+ alias: 'p',
196
+ type: 'string',
197
+ description: '优先级',
198
+ choices: ['low', 'medium', 'high'],
199
+ default: 'medium'
200
+ })
201
+ .option('tags', {
202
+ alias: 't',
203
+ type: 'array',
204
+ description: '标签',
205
+ default: []
206
+ });
207
+ }, (argv) => {
208
+ console.log(`✓ 添加任务: ${argv.title}`);
209
+ console.log(` 优先级: ${argv.priority}`);
210
+ console.log(` 标签: ${argv.tags?.join(', ') || '无'}`);
211
+ })
212
+ .command('list', '列出任务', (yargs) => {
213
+ return yargs
214
+ .option('status', {
215
+ alias: 's',
216
+ type: 'string',
217
+ description: '任务状态',
218
+ choices: ['pending', 'completed', 'all'],
219
+ default: 'all'
220
+ })
221
+ .option('limit', {
222
+ alias: 'l',
223
+ type: 'number',
224
+ description: '显示数量',
225
+ default: 10
226
+ });
227
+ }, (argv) => {
228
+ console.log(`✓ 列出任务 (状态: ${argv.status}, 限制: ${argv.limit})`);
229
+ })
230
+ .command('complete <id>', '完成任务', (yargs) => {
231
+ return yargs.positional('id', {
232
+ describe: '任务ID',
233
+ type: 'number'
234
+ });
235
+ }, (argv) => {
236
+ console.log(`✓ 完成任务 ID: ${argv.id}`);
237
+ })
238
+ .demandCommand(1, '请指定子命令');
239
+ })
240
+ .demandCommand(1, '请指定命令')
241
+ .help()
242
+ .alias('help', 'h')
243
+ .alias('version', 'v')
244
+ .epilogue('更多信息请访问: https://github.com/yargs/yargs')
245
+ .argv;
246
+ }
247
+ // #endregion
248
+ if (require.main === module) {
249
+ const exampleNum = process.argv[2];
250
+ switch (exampleNum) {
251
+ case '1':
252
+ demoFileManagerTool();
253
+ break;
254
+ case '2':
255
+ demoDatabaseMigrationTool();
256
+ break;
257
+ case '3':
258
+ demoProjectScaffolder();
259
+ break;
260
+ case '4':
261
+ demoDeploymentTool();
262
+ break;
263
+ case '5':
264
+ demoCompleteTaskManager();
265
+ break;
266
+ default:
267
+ console.log('请指定示例编号 (1-5)');
268
+ console.log('用法: ts-node 04-实战案例.ts <示例编号> [命令] [参数...]');
269
+ console.log('\n可用示例:');
270
+ console.log(' 1 - 文件管理工具');
271
+ console.log(' 2 - 数据库迁移工具');
272
+ console.log(' 3 - 项目脚手架工具');
273
+ console.log(' 4 - 部署工具');
274
+ console.log(' 5 - 完整的 CLI 应用');
275
+ }
276
+ }
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@lppx/nlearn",
3
+ "publishConfig": {
4
+ "access": "public"
5
+ },
6
+ "version": "1.1.0",
7
+ "description": "一个nodejs学习工具",
8
+ "bin": {
9
+ "nlearn": "dist/src/cli/cli.js"
10
+ },
11
+ "scripts": {
12
+ "sync": "ts-node scripts/sync-repo.ts",
13
+ "build": "tsc",
14
+ "start": "node dist/src/cli/cli.js"
15
+ },
16
+ "author": "lipanpanx",
17
+ "license": "MIT",
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "dependencies": {
22
+ "@types/inquirer": "^9.0.9",
23
+ "commander": "^14.0.2",
24
+ "inquirer": "^9.3.8",
25
+ "isomorphic-git": "^1.36.3",
26
+ "yargs": "^18.0.0"
27
+ },
28
+ "engines": {
29
+ "node": ">=14.0.0"
30
+ },
31
+ "devDependencies": {
32
+ "@types/commander": "^2.12.0",
33
+ "@types/node": "^25.0.10",
34
+ "@types/yargs": "^17.0.35",
35
+ "nodemon": "^3.1.11",
36
+ "ts-node": "^10.9.2",
37
+ "typescript": "^5.9.3"
38
+ }
39
+ }