Skip to content
This repository was archived by the owner on May 6, 2025. It is now read-only.

Commit 41913a5

Browse files
committed
support function code download
1 parent 44443a9 commit 41913a5

40 files changed

Lines changed: 1167 additions & 261 deletions

lib/commands/doc.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
const commander_1 = __importDefault(require("commander"));
7+
const open_1 = __importDefault(require("open"));
8+
commander_1.default
9+
.command('logout')
10+
.description('查看 CLI 文档')
11+
.action(() => {
12+
open_1.default('');
13+
});

lib/commands/env/base.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
var __importDefault = (this && this.__importDefault) || function (mod) {
12+
return (mod && mod.__esModule) ? mod : { "default": mod };
13+
};
14+
Object.defineProperty(exports, "__esModule", { value: true });
15+
const commander_1 = __importDefault(require("commander"));
16+
const env_1 = require("../../env");
17+
const utils_1 = require("../../utils");
18+
const error_1 = require("../../error");
19+
const logger_1 = require("../../logger");
20+
commander_1.default
21+
.command('env:list')
22+
.description('展示云开发环境信息')
23+
.action(function () {
24+
return __awaiter(this, void 0, void 0, function* () {
25+
const data = yield env_1.listEnvs();
26+
const head = [
27+
'Alias',
28+
'EnvId',
29+
'PackageName',
30+
'Source',
31+
'CreateTime',
32+
'Status'
33+
];
34+
const sortData = data.sort((prev, next) => {
35+
if (prev.Alias > next.Alias) {
36+
return 1;
37+
}
38+
if (prev.Alias < next.Alias) {
39+
return -1;
40+
}
41+
return 0;
42+
});
43+
const tableData = sortData.map(item => [
44+
item.Alias,
45+
item.EnvId,
46+
item.PackageName,
47+
item.Source === 'miniapp' ? '小程序' : '云开发',
48+
item.CreateTime,
49+
item.Status === 'NORMAL' ? '正常' : '不可用'
50+
]);
51+
utils_1.printCliTable(head, tableData);
52+
const unavailableEnv = data.find(item => item.Status === 'UNAVAILABLE');
53+
if (unavailableEnv) {
54+
logger_1.warnLog(`您的环境中存在不可用的环境:[${unavailableEnv.EnvId}],请留意!`);
55+
}
56+
});
57+
});
58+
function checkEnvAvailability(envId) {
59+
return __awaiter(this, void 0, void 0, function* () {
60+
const MAX_TRY = 10;
61+
let retry = 0;
62+
return new Promise((resolve, reject) => {
63+
const timer = setInterval(() => __awaiter(this, void 0, void 0, function* () {
64+
const envInfo = yield env_1.getEnvInfo(envId);
65+
if (envInfo.Status === 'NORMAL') {
66+
clearInterval(timer);
67+
resolve();
68+
}
69+
else {
70+
retry++;
71+
}
72+
if (retry > MAX_TRY) {
73+
reject(new error_1.CloudBaseError('环境初始化查询超时,请稍后通过 cloudbase env:list 查看环境状态'));
74+
}
75+
}), 1000);
76+
});
77+
});
78+
}
79+
commander_1.default
80+
.command('env:create <alias>')
81+
.description('创建新的云开发环境')
82+
.action(function (alias) {
83+
return __awaiter(this, void 0, void 0, function* () {
84+
if (!alias) {
85+
throw new error_1.CloudBaseError('环境名称不能为空!');
86+
}
87+
const loading = utils_1.loadingFactory();
88+
loading.start('创建环境中');
89+
const res = yield env_1.createEnv({
90+
alias
91+
});
92+
loading.succeed('创建环境成功!');
93+
loading.start('环境初始化中');
94+
if (res.Status === 'NORMAL') {
95+
loading.start('环境初始化成功');
96+
return;
97+
}
98+
try {
99+
yield checkEnvAvailability(res.EnvId);
100+
loading.succeed('环境初始化成功');
101+
}
102+
catch (e) {
103+
loading.fail(e.message);
104+
}
105+
});
106+
});
107+
commander_1.default
108+
.command('env:rename <name> [envId]')
109+
.description('重命名云开发环境')
110+
.action(function (name, envId, options) {
111+
return __awaiter(this, void 0, void 0, function* () {
112+
if (!name) {
113+
throw new error_1.CloudBaseError('环境名称不能为空!');
114+
}
115+
const { configFile } = options.parent;
116+
const assignEnvId = yield utils_1.getEnvId(envId, configFile);
117+
yield env_1.updateEnvInfo({
118+
envId: assignEnvId,
119+
alias: name
120+
});
121+
logger_1.successLog('更新环境名成功 !');
122+
});
123+
});

lib/commands/env/index.js

Lines changed: 2 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,5 @@
11
"use strict";
2-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4-
return new (P || (P = Promise))(function (resolve, reject) {
5-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8-
step((generator = generator.apply(thisArg, _arguments || [])).next());
9-
});
10-
};
11-
var __importDefault = (this && this.__importDefault) || function (mod) {
12-
return (mod && mod.__esModule) ? mod : { "default": mod };
13-
};
142
Object.defineProperty(exports, "__esModule", { value: true });
15-
const commander_1 = __importDefault(require("commander"));
16-
const env_1 = require("../../env");
17-
const utils_1 = require("../../utils");
18-
const error_1 = require("../../error");
19-
const logger_1 = require("../../logger");
20-
require("./domain");
3+
require("./base");
214
require("./login");
22-
commander_1.default
23-
.command('env:list')
24-
.description('列出云开发所有环境')
25-
.action(function () {
26-
return __awaiter(this, void 0, void 0, function* () {
27-
const data = yield env_1.listEnvs();
28-
const head = [
29-
'Alias',
30-
'EnvId',
31-
'PackageName',
32-
'Source',
33-
'CreateTime',
34-
'Status'
35-
];
36-
const sortData = data.sort((prev, next) => {
37-
if (prev.Alias > next.Alias) {
38-
return 1;
39-
}
40-
if (prev.Alias < next.Alias) {
41-
return -1;
42-
}
43-
return 0;
44-
});
45-
const tableData = sortData.map(item => [
46-
item.Alias,
47-
item.EnvId,
48-
item.PackageName,
49-
item.Source === 'miniapp' ? '小程序' : '云开发',
50-
item.CreateTime,
51-
item.Status === 'NORMAL' ? '正常' : '不可用'
52-
]);
53-
utils_1.printCliTable(head, tableData);
54-
const unavailableEnv = data.find(item => item.Status === 'UNAVAILABLE');
55-
if (unavailableEnv) {
56-
logger_1.warnLog(`您的环境中存在不可用的环境:[${unavailableEnv.EnvId}],请留意!`);
57-
}
58-
});
59-
});
60-
function checkEnvAvailability(envId) {
61-
return __awaiter(this, void 0, void 0, function* () {
62-
const MAX_TRY = 10;
63-
let retry = 0;
64-
return new Promise((resolve, reject) => {
65-
const timer = setInterval(() => __awaiter(this, void 0, void 0, function* () {
66-
const envInfo = yield env_1.getEnvInfo(envId);
67-
if (envInfo.Status === 'NORMAL') {
68-
clearInterval(timer);
69-
resolve();
70-
}
71-
else {
72-
retry++;
73-
}
74-
if (retry > MAX_TRY) {
75-
reject(new error_1.CloudBaseError('环境初始化查询超时,请稍后通过 cloudbase env:list 查看环境状态'));
76-
}
77-
}), 1000);
78-
});
79-
});
80-
}
81-
commander_1.default
82-
.command('env:create <alias>')
83-
.description('创建新的云环境')
84-
.action(function (alias) {
85-
return __awaiter(this, void 0, void 0, function* () {
86-
if (!alias) {
87-
throw new error_1.CloudBaseError('环境名称不能为空!');
88-
}
89-
const loading = utils_1.loadingFactory();
90-
loading.start('创建环境中');
91-
const res = yield env_1.createEnv({
92-
alias
93-
});
94-
loading.succeed('创建环境成功!');
95-
loading.start('环境初始化中');
96-
if (res.Status === 'NORMAL') {
97-
loading.start('环境初始化成功');
98-
return;
99-
}
100-
try {
101-
yield checkEnvAvailability(res.EnvId);
102-
loading.succeed('环境初始化成功');
103-
}
104-
catch (e) {
105-
loading.fail(e.message);
106-
}
107-
});
108-
});
109-
commander_1.default
110-
.command('env:rename <name> [envId]')
111-
.description('重命名环境')
112-
.action(function (name, envId, options) {
113-
return __awaiter(this, void 0, void 0, function* () {
114-
if (!name) {
115-
throw new error_1.CloudBaseError('环境名称不能为空!');
116-
}
117-
const { configFile } = options.parent;
118-
const assignEnvId = yield utils_1.getEnvId(envId, configFile);
119-
yield env_1.updateEnvInfo({
120-
envId: assignEnvId,
121-
alias: name
122-
});
123-
logger_1.successLog('更新环境名成功 !');
124-
});
125-
});
5+
require("./domain");
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
var __importDefault = (this && this.__importDefault) || function (mod) {
12+
return (mod && mod.__esModule) ? mod : { "default": mod };
13+
};
14+
Object.defineProperty(exports, "__esModule", { value: true });
15+
const path_1 = __importDefault(require("path"));
16+
const fs_extra_1 = __importDefault(require("fs-extra"));
17+
const inquirer_1 = __importDefault(require("inquirer"));
18+
const error_1 = require("../../error");
19+
const code_1 = require("../../function/code");
20+
const utils_1 = require("../../utils");
21+
function codeDownload(ctx, dest, options) {
22+
return __awaiter(this, void 0, void 0, function* () {
23+
const { name, envId, config } = ctx;
24+
const { codeSecret } = options;
25+
if (!name) {
26+
throw new error_1.CloudBaseError('请指定云函数名称!');
27+
}
28+
let destPath = dest;
29+
if (!destPath) {
30+
destPath = path_1.default.resolve(config.functionRoot, name);
31+
if (utils_1.checkPathExist(destPath)) {
32+
const { override } = yield inquirer_1.default.prompt({
33+
type: 'confirm',
34+
name: 'override',
35+
message: '函数已经存在,是否覆盖原文件?',
36+
default: false
37+
});
38+
if (!override) {
39+
throw new error_1.CloudBaseError('下载终止!');
40+
}
41+
utils_1.delSync([destPath]);
42+
}
43+
yield fs_extra_1.default.ensureDir(destPath);
44+
}
45+
const loading = utils_1.loadingFactory();
46+
loading.start('文件下载中...');
47+
yield code_1.downloadFunctionCode({
48+
envId,
49+
functionName: name,
50+
destPath: destPath,
51+
codeSecret: codeSecret,
52+
unzip: true
53+
});
54+
loading.succeed(`[${name}] 云函数代码下载成功!`);
55+
});
56+
}
57+
exports.codeDownload = codeDownload;

lib/commands/functions/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const trigger_create_1 = require("./trigger-create");
2626
const trigger_delete_1 = require("./trigger-delete");
2727
const invoke_1 = require("./invoke");
2828
const copy_1 = require("./copy");
29+
const code_download_1 = require("./code-download");
2930
function getFunctionContext(name, envId, configPath) {
3031
return __awaiter(this, void 0, void 0, function* () {
3132
const cloudBaseConfig = yield utils_1.resolveCloudBaseConfig(configPath);
@@ -60,6 +61,22 @@ const commands = [
6061
yield list_1.list(ctx, options);
6162
})
6263
},
64+
{
65+
cmd: 'functions:download <functionName> [dest] [envId]',
66+
options: [
67+
{ flags: '-l, --limit <limit>', desc: '返回数据长度,默认值为 20' },
68+
{
69+
flags: '--code-secret <codeSecret>',
70+
desc: '代码加密的函数的 CodeSecret'
71+
}
72+
],
73+
desc: '下载云函数代码',
74+
handler: (name, dest, envId, options) => __awaiter(void 0, void 0, void 0, function* () {
75+
const { configFile } = options.parent;
76+
const ctx = yield getFunctionContext(name, envId, configFile);
77+
yield code_download_1.codeDownload(ctx, dest, options);
78+
})
79+
},
6380
{
6481
cmd: 'functions:deploy [functionName] [envId]',
6582
options: [

lib/error.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ class CloudBaseError extends Error {
55
super();
66
this.name = 'CloudBaseError';
77
this.message = message;
8-
this.original = options.original;
9-
this.code = options.code;
10-
this.requestId = options.requestId;
8+
const { code = '', action = '', original = null, requestId = '' } = options;
9+
this.original = original;
10+
this.code = code;
11+
this.requestId = requestId;
12+
this.action = action;
1113
}
1214
}
1315
exports.CloudBaseError = CloudBaseError;

0 commit comments

Comments
 (0)