@cloudbase/cli 2.5.1-alpha.4 → 2.5.1-alpha.5

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.
@@ -42,7 +42,9 @@ const events_1 = require("events");
42
42
  const commander_1 = require("commander");
43
43
  const yargs_parser_1 = __importDefault(require("yargs-parser"));
44
44
  const error_1 = require("../error");
45
+ const toolbox_1 = require("@cloudbase/toolbox");
45
46
  const utils_1 = require("../utils");
47
+ const auth_1 = require("../auth");
46
48
  const registrableCommands = [];
47
49
  const cmdMap = new Map();
48
50
  const defaultCmdDecoratorOpts = {
@@ -162,8 +164,11 @@ class Command extends events_1.EventEmitter {
162
164
  }
163
165
  if (!withoutAuth && !loginState) {
164
166
  if (autoRunLogin) {
165
- console.log(chalk_1.default.bold.yellowBright('无有效身份信息,将自动为您打开授权页面。授权成功后请再重新执行命令'));
166
- yield commander_1.program.parseAsync(['node', 'tcb', 'login']);
167
+ console.log(chalk_1.default.bold.yellowBright('无有效身份信息,将自动为您打开授权页面。'));
168
+ yield (0, toolbox_1.execWithLoading)(() => (0, auth_1.login)(), {
169
+ startTip: '请在浏览器中打开的授权页面进行授权...',
170
+ successTip: '授权登录成功!'
171
+ });
167
172
  }
168
173
  else {
169
174
  throw new error_1.CloudBaseError('无有效身份信息,请使用 cloudbase login 登录');
@@ -70,6 +70,7 @@ const fs_extra_1 = __importDefault(require("fs-extra"));
70
70
  const os_1 = __importDefault(require("os"));
71
71
  const path_1 = __importDefault(require("path"));
72
72
  const dts_1 = require("../../utils/dts");
73
+ const cals_1 = require("@cloudbase/cals");
73
74
  let lowcodeCli;
74
75
  if (process.argv.includes('lowcode')) {
75
76
  (0, utils_1.getLowcodeCli)().then((_) => (lowcodeCli = _));
@@ -401,84 +402,239 @@ let TemplateSync = class TemplateSync extends common_1.Command {
401
402
  childCmd: 'sync',
402
403
  options: [
403
404
  {
404
- flags: '--envId <envId>',
405
- desc: '环境 ID'
405
+ flags: '--source <source>',
406
+ desc: '来源: 1-野鹤 2-自建模板'
406
407
  }
407
408
  ],
408
409
  desc: '同步官方模板应用内容',
409
- requiredEnvId: true
410
+ requiredEnvId: false
410
411
  };
411
412
  }
412
413
  execute(ctx, log, options) {
414
+ var _a;
413
415
  return __awaiter(this, void 0, void 0, function* () {
414
416
  log.info('同步中...');
417
+ const SourceType = {
418
+ YEHE: '1',
419
+ CUSTOM_MODULE: '2'
420
+ };
421
+ const source = Object.values(SourceType).includes(options.source)
422
+ ? options.source
423
+ : SourceType.YEHE;
415
424
  const envId = 'lowcode-5g5llxbq5bc9299e';
425
+ const cloudService = yield getCloudServiceInstance(ctx);
426
+ const files = [];
427
+ const limit = 50;
428
+ let count = 1;
416
429
  const fileDir = path_1.default.resolve(os_1.default.tmpdir(), 'templates');
417
430
  yield fs_extra_1.default.ensureDir(fileDir);
418
431
  yield fs_extra_1.default.rmdir(fileDir, { recursive: true });
419
432
  yield fs_extra_1.default.ensureDir(fileDir);
420
- const cloudService = yield getCloudServiceInstance(ctx);
421
- let total = Infinity;
422
- let currentTotal = 0;
423
- const limit = 50;
424
- const files = [];
425
- let count = 1;
426
- while (currentTotal < total) {
427
- const solutionListResult = yield cloudService.lowcode.request('DescribeSolutionList', {
428
- Limit: limit,
429
- Offset: currentTotal,
430
- KeyWords: '',
431
- EnvId: envId,
432
- TypeList: ['SELFBUILD', 'TPLEXPORT']
433
- });
434
- const solutionList = solutionListResult.SolutionInfoList;
435
- const handledSolutionList = yield Promise.all(solutionList.map((item) => __awaiter(this, void 0, void 0, function* () {
436
- const solution = yield cloudService.lowcode.request('DescribeSolution', {
433
+ if (source === SourceType.YEHE) {
434
+ const templates = yield fs_extra_1.default.readJSON('data.json', 'utf8');
435
+ console.log('模板总数:', templates.length);
436
+ while (templates.length > 0) {
437
+ const handledSolutionList = yield Promise.all(templates.splice(0, limit).map((item) => __awaiter(this, void 0, void 0, function* () {
438
+ if (item.appIds.length > 0) {
439
+ const apps = yield getAppsContent(item.appIds);
440
+ return {
441
+ name: item.templateName,
442
+ templateId: item.templateId,
443
+ status: item.status,
444
+ apps
445
+ };
446
+ }
447
+ else {
448
+ return null;
449
+ }
450
+ })));
451
+ const filePath = path_1.default.resolve(fileDir, `templates_${count}.json`);
452
+ yield fs_extra_1.default.writeFile(filePath, JSON.stringify(handledSolutionList, null, 2), 'utf8');
453
+ files.push(filePath);
454
+ count += 1;
455
+ }
456
+ }
457
+ else if (source === SourceType.CUSTOM_MODULE) {
458
+ let total = Infinity;
459
+ let currentTotal = 0;
460
+ while (currentTotal < total) {
461
+ const solutionListResult = yield cloudService.lowcode.request('DescribeSolutionList', {
462
+ Limit: limit,
463
+ Offset: currentTotal,
464
+ KeyWords: '',
437
465
  EnvId: envId,
438
- SolutionId: item.SolutionId
466
+ TypeList: ['SELFBUILD', 'TPLEXPORT']
439
467
  });
440
- if (solution.SolutionAppInfos.length > 0) {
441
- const appIds = solution.SolutionAppInfos.map((item) => item.AppId);
442
- const apps = yield Promise.all(appIds.map((appId) => __awaiter(this, void 0, void 0, function* () {
443
- var _a, _b, _c, _d, _e;
444
- try {
445
- let result = yield cloudService.lowcode.request('DescribeHistoryListByAppId', {
446
- WeAppId: appId,
447
- PageNum: 1,
448
- PageSize: 1
449
- });
450
- result = yield cloudService.lowcode.request('DescribeAppHistoryPreSignUrl', {
451
- HisIds: [(_c = (_b = (_a = result === null || result === void 0 ? void 0 : result.Data) === null || _a === void 0 ? void 0 : _a.List) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.Id],
452
- HttpMethod: 'get',
453
- WeAppsId: appId
454
- });
455
- result = yield (0, toolbox_1.fetch)((_e = (_d = result === null || result === void 0 ? void 0 : result.Data) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.UploadUrl);
456
- return { appId, content: result };
457
- }
458
- catch (e) {
459
- return { appId, error: e.message };
460
- }
461
- })));
462
- return {
463
- name: item.Name,
464
- solutionId: item.SolutionId,
465
- version: item.Version,
466
- apps
467
- };
468
+ const solutionList = solutionListResult.SolutionInfoList;
469
+ const handledSolutionList = yield Promise.all(solutionList.map((item) => __awaiter(this, void 0, void 0, function* () {
470
+ const solution = yield cloudService.lowcode.request('DescribeSolution', {
471
+ EnvId: envId,
472
+ SolutionId: item.SolutionId
473
+ });
474
+ if (solution.SolutionAppInfos.length > 0) {
475
+ const appIds = solution.SolutionAppInfos.map((item) => item.AppId);
476
+ const apps = yield getAppsContent(appIds);
477
+ return {
478
+ name: item.Name,
479
+ solutionId: item.SolutionId,
480
+ version: item.Version,
481
+ apps
482
+ };
483
+ }
484
+ else {
485
+ return null;
486
+ }
487
+ })));
488
+ const filePath = path_1.default.resolve(fileDir, `templates_${count}.json`);
489
+ yield fs_extra_1.default.writeFile(filePath, JSON.stringify(handledSolutionList, null, 2), 'utf8');
490
+ files.push(filePath);
491
+ total = solutionListResult.TotalCount;
492
+ currentTotal += limit;
493
+ count += 1;
494
+ }
495
+ }
496
+ log.success(`同步官方模板应用内容已完成. 文件路径:`);
497
+ log.success(files.join('\n'));
498
+ const appIdNameMap = {};
499
+ const appsPath = path_1.default.resolve(process.cwd(), 'apps');
500
+ try {
501
+ const items = yield fs_extra_1.default.readdir(appsPath);
502
+ const directories = [];
503
+ for (const item of items) {
504
+ const itemPath = path_1.default.join(appsPath, item);
505
+ const stats = yield fs_extra_1.default.stat(itemPath);
506
+ if (stats.isDirectory()) {
507
+ directories.push(item);
508
+ const appConfig = yield fs_extra_1.default.readJSON(path_1.default.resolve(itemPath, 'src', 'app-config.json'));
509
+ const appId = appConfig.id;
510
+ appIdNameMap[appId] = item;
511
+ }
512
+ }
513
+ }
514
+ catch (error) {
515
+ console.error('Error reading directory:', error);
516
+ }
517
+ for (let fileUrl of files) {
518
+ const items = yield fs_extra_1.default.readJSON(fileUrl);
519
+ for (let item of items) {
520
+ if (!item)
521
+ continue;
522
+ let done = false;
523
+ if (((_a = item === null || item === void 0 ? void 0 : item.apps) === null || _a === void 0 ? void 0 : _a.length) > 0) {
524
+ try {
525
+ yield Promise.all(item === null || item === void 0 ? void 0 : item.apps.map((app) => __awaiter(this, void 0, void 0, function* () {
526
+ var _b, _c;
527
+ if (app === null || app === void 0 ? void 0 : app.error) {
528
+ return null;
529
+ }
530
+ else {
531
+ if (appIdNameMap[app.appId]) {
532
+ app.name = appIdNameMap[app.appId];
533
+ }
534
+ else {
535
+ const result = yield (0, toolbox_1.fetch)('http://localhost:1234/v1/chat/completions', {
536
+ method: 'POST',
537
+ headers: {
538
+ 'Content-Type': 'application/json'
539
+ },
540
+ body: JSON.stringify({
541
+ model: 'TheBloke/Mistral-7B-Instruct-v0.2-GGUF',
542
+ messages: [
543
+ {
544
+ role: 'system',
545
+ content: '你是一个目录命名专家。目录名只包括小写字母,数字和下划线, 不能包含其它符号。\n正确的示例: community_deals_platform\n错误的示例: restaurant_company_website'
546
+ },
547
+ {
548
+ role: 'user',
549
+ content: `请为“${(_b = app === null || app === void 0 ? void 0 : app.content) === null || _b === void 0 ? void 0 : _b.label}”起一个目录名。请始终只返回一个目录名,其它不用返回`
550
+ }
551
+ ],
552
+ temperature: 1,
553
+ stream: false
554
+ }),
555
+ timeout: 30 * 60 * 1000
556
+ });
557
+ app.name = result.choices[0].message.content
558
+ .replace(/[\s\\]+/g, '')
559
+ .replace(/-/g, '_');
560
+ }
561
+ if (app.content) {
562
+ const calsAppData = app.content;
563
+ calsAppData.id = app.appId;
564
+ const { clientId, originHistoryId, mpAppId, uin } = app.extra;
565
+ calsAppData.extra.clientId = clientId;
566
+ calsAppData.extra.originHistoryId = `${originHistoryId}`;
567
+ calsAppData.extra.mpAppId = mpAppId;
568
+ calsAppData.extra.envId = envId;
569
+ calsAppData.extra.uin = uin;
570
+ calsAppData.extra.domain =
571
+ 'lowcode-5g5llxbq5bc9299e-1300677802.tcloudbaseapp.com';
572
+ calsAppData.extra.resourceAppId = '';
573
+ calsAppData.extra.endpointType = '';
574
+ const codeItems = (0, cals_1.calsToCode)(app.content, 'v0');
575
+ yield Promise.all(codeItems.map((codeItem) => __awaiter(this, void 0, void 0, function* () {
576
+ const codePath = path_1.default.resolve(appsPath, app.name, codeItem.path);
577
+ yield fs_extra_1.default.ensureFile(codePath);
578
+ yield fs_extra_1.default.writeFile(codePath, codeItem.code || '', 'utf8');
579
+ })));
580
+ console.log('写入成功:', app.appId, (_c = app === null || app === void 0 ? void 0 : app.content) === null || _c === void 0 ? void 0 : _c.label, app.name);
581
+ }
582
+ else {
583
+ console.error('写入异常:', app.appId, app.error);
584
+ }
585
+ }
586
+ })));
587
+ }
588
+ catch (e) {
589
+ console.error('异常:', item.templateId, e.message);
590
+ }
591
+ done = true;
468
592
  }
469
593
  else {
470
- return null;
594
+ done = true;
595
+ }
596
+ if (done) {
597
+ if (source === SourceType.YEHE) {
598
+ let templates = yield fs_extra_1.default.readJSON('data.json', 'utf8');
599
+ templates = templates.filter((tItem) => tItem.templateId !== item.templateId);
600
+ yield fs_extra_1.default.writeFile('data.json', JSON.stringify(templates, null, 2), 'utf8');
601
+ }
471
602
  }
472
- })));
473
- const filePath = path_1.default.resolve(fileDir, `templates_${count}.json`);
474
- yield fs_extra_1.default.writeFile(filePath, JSON.stringify(handledSolutionList, null, 2), 'utf8');
475
- files.push(filePath);
476
- total = solutionListResult.TotalCount;
477
- currentTotal += limit;
478
- count += 1;
603
+ }
604
+ yield fs_extra_1.default.writeFile(fileUrl, JSON.stringify(items, null, 2), 'utf8');
605
+ }
606
+ function getAppsContent(appIds) {
607
+ return __awaiter(this, void 0, void 0, function* () {
608
+ return yield Promise.all(appIds.map((appId) => __awaiter(this, void 0, void 0, function* () {
609
+ var _a, _b, _c;
610
+ try {
611
+ let result = yield cloudService.lowcode.request('DescribeAppDetail', {
612
+ WeAppId: appId
613
+ });
614
+ const { Title, LatestHistory, OwnerInfo, MpAppId, ClientId } = result.Data;
615
+ const label = Title;
616
+ const historyId = (_a = JSON.parse(LatestHistory)) === null || _a === void 0 ? void 0 : _a.id;
617
+ const extra = {
618
+ originHistoryId: historyId,
619
+ uin: OwnerInfo.Uin,
620
+ mpAppId: MpAppId,
621
+ clientId: ClientId
622
+ };
623
+ result = yield cloudService.lowcode.request('DescribeAppHistoryPreSignUrl', {
624
+ HisIds: [historyId],
625
+ HttpMethod: 'get',
626
+ WeAppsId: appId
627
+ });
628
+ result = yield (0, toolbox_1.fetch)((_c = (_b = result === null || result === void 0 ? void 0 : result.Data) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.UploadUrl);
629
+ result.label = label;
630
+ return { appId, content: result, extra };
631
+ }
632
+ catch (e) {
633
+ return { appId, error: e.message };
634
+ }
635
+ })));
636
+ });
479
637
  }
480
- log.success(`同步官方模板应用内容已完成. 文件路径:`);
481
- log.success(files.join('\n'));
482
638
  });
483
639
  }
484
640
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudbase/cli",
3
- "version": "2.5.1-alpha.4",
3
+ "version": "2.5.1-alpha.5",
4
4
  "description": "cli tool for cloudbase",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {