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

Commit 630bd33

Browse files
committed
fix hosting bugs
1 parent 7b82805 commit 630bd33

4 files changed

Lines changed: 41 additions & 133 deletions

File tree

lib/commands/hosting.js

Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,79 +14,36 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
1414
Object.defineProperty(exports, "__esModule", { value: true });
1515
const chalk_1 = __importDefault(require("chalk"));
1616
const commander_1 = __importDefault(require("commander"));
17-
const inquirer_1 = __importDefault(require("inquirer"));
1817
const hosting_1 = require("../hosting");
1918
const error_1 = require("../error");
2019
const utils_1 = require("../utils");
21-
const logger_1 = require("../logger");
2220
const HostingStatusMap = {
2321
init: '初始化中',
2422
process: '处理中',
25-
online: '上线',
23+
online: '已上线',
2624
destroying: '销毁中',
27-
offline: '下线',
25+
offline: '已下线',
2826
create_fail: '初始化失败',
2927
destroy_fail: '销毁失败'
3028
};
31-
commander_1.default
32-
.command('hosting:enable')
33-
.option('-e, --envId [envId]', '环境 Id')
34-
.description('开启静态网站服务')
35-
.action((options) => __awaiter(void 0, void 0, void 0, function* () {
36-
const { parent: { configFile }, envId } = options;
37-
const assignEnvId = yield utils_1.getEnvId(envId, configFile);
38-
const res = yield hosting_1.enableHosting({ envId: assignEnvId });
39-
if (res.code === 0) {
40-
logger_1.successLog('静态网站服务开启成功!');
41-
}
42-
else {
43-
throw new error_1.CloudBaseError('静态网站服务失败!');
44-
}
45-
}));
4629
commander_1.default
4730
.command('hosting:detail')
48-
.option('-e, --envId [envId] [envId]', '环境 Id')
31+
.option('-e, --envId [envId]', '环境 Id')
4932
.description('查看静态网站服务信息')
5033
.action((options) => __awaiter(void 0, void 0, void 0, function* () {
5134
const { parent: { configFile }, envId } = options;
5235
const assignEnvId = yield utils_1.getEnvId(envId, configFile);
5336
const res = yield hosting_1.getHostingInfo({ envId: assignEnvId });
5437
const website = res.data && res.data[0];
5538
if (!website) {
56-
throw new error_1.CloudBaseError('你还没有开启静态网站服务,请使用 cloudbase hosting:enable 命令启用静态网站服务!');
39+
throw new error_1.CloudBaseError('您还没有开启静态网站服务,请先到云开发控制台开启静态网站服务!\n 👉 https://console.cloud.tencent.com/tcb');
5740
}
5841
const url = `https://${website.cdnDomain}`;
59-
console.log(`静态网站域名:${chalk_1.default.bold.underline(url)}\n静态网站状态:${HostingStatusMap[website.status]}`);
60-
}));
61-
commander_1.default
62-
.command('hosting:destroy')
63-
.option('-e, --envId [envId]', '环境 Id')
64-
.description('关闭静态网站服务')
65-
.action((options) => __awaiter(void 0, void 0, void 0, function* () {
66-
const { parent: { configFile }, envId } = options;
67-
const assignEnvId = yield utils_1.getEnvId(envId, configFile);
68-
const { confirm } = yield inquirer_1.default.prompt({
69-
type: 'confirm',
70-
name: 'confirm',
71-
message: '确定要关闭静态网站服务吗,关闭后您的所有静态网站资源将被销毁,无法恢复!',
72-
default: false
73-
});
74-
if (!confirm) {
75-
throw new error_1.CloudBaseError('操作终止!');
42+
if (website.status === 'offline') {
43+
console.log(`静态网站状态:${HostingStatusMap[website.status]}`);
7644
}
77-
const loading = utils_1.loadingFactory();
78-
loading.start('静态网站销毁中');
79-
try {
80-
const res = yield hosting_1.destroyHosting({ envId: assignEnvId });
81-
if (res.code === 0) {
82-
loading.succeed('静态网站服务销毁中...');
83-
}
84-
else {
85-
loading.fail('静态网站服务销毁失败!');
86-
}
87-
}
88-
catch (e) {
89-
loading.fail(e.message || '静态网站服务销毁失败!');
45+
else {
46+
console.log(`静态网站域名:${chalk_1.default.bold.underline(url)}\n静态网站状态:${HostingStatusMap[website.status]}`);
9047
}
9148
}));
9249
commander_1.default

lib/hosting.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function checkHostingStatus(envId) {
5555
return __awaiter(this, void 0, void 0, function* () {
5656
const hostings = yield getHostingInfo({ envId });
5757
if (!hostings.data || !hostings.data.length) {
58-
throw new error_1.CloudBaseError('静态网站服务未开启!', {
58+
throw new error_1.CloudBaseError('您还没有开启静态网站服务,请先到云开发控制台开启静态网站服务!\n 👉 https://console.cloud.tencent.com/tcb', {
5959
code: 'INVALID_OPERATION'
6060
});
6161
}
@@ -74,8 +74,8 @@ function enableHosting(options) {
7474
const hostings = yield getHostingInfo(options);
7575
if (hostings.data && hostings.data.length) {
7676
const website = hostings.data[0];
77-
if (website.status !== 'destroy' || website.status !== 'destroy_fail') {
78-
throw new error_1.CloudBaseError('静态网站服务已开启!');
77+
if (website.status !== 'offline') {
78+
throw new error_1.CloudBaseError('静态网站服务已开启,请勿重复操作!');
7979
}
8080
}
8181
const res = yield tcbService.request('CreateStaticStore', {

src/commands/hosting.ts

Lines changed: 15 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,22 @@
11
import chalk from 'chalk'
22
import program from 'commander'
3-
import inquirer from 'inquirer'
4-
import {
5-
enableHosting,
6-
getHostingInfo,
7-
destroyHosting,
8-
hostingDeploy,
9-
hostingDelete,
10-
hostingList
11-
} from '../hosting'
3+
import { getHostingInfo, hostingDeploy, hostingDelete, hostingList } from '../hosting'
124
import { CloudBaseError } from '../error'
135
import { getEnvId, loadingFactory, isDirectory, printHorizontalTable, formatDate } from '../utils'
14-
import { successLog } from '../logger'
156

167
const HostingStatusMap = {
178
init: '初始化中',
189
process: '处理中',
19-
online: '上线',
10+
online: '已上线',
2011
destroying: '销毁中',
21-
offline: '下线',
12+
offline: '已下线',
2213
create_fail: '初始化失败', // eslint-disable-line
2314
destroy_fail: '销毁失败' // eslint-disable-line
2415
}
2516

26-
program
27-
.command('hosting:enable')
28-
.option('-e, --envId [envId]', '环境 Id')
29-
.description('开启静态网站服务')
30-
.action(async (options: any) => {
31-
const {
32-
parent: { configFile },
33-
envId
34-
} = options
35-
const assignEnvId = await getEnvId(envId, configFile)
36-
const res = await enableHosting({ envId: assignEnvId })
37-
if (res.code === 0) {
38-
successLog('静态网站服务开启成功!')
39-
} else {
40-
throw new CloudBaseError('静态网站服务失败!')
41-
}
42-
})
43-
4417
program
4518
.command('hosting:detail')
46-
.option('-e, --envId [envId] [envId]', '环境 Id')
19+
.option('-e, --envId [envId]', '环境 Id')
4720
.description('查看静态网站服务信息')
4821
.action(async (options: any) => {
4922
const {
@@ -54,51 +27,23 @@ program
5427
const res = await getHostingInfo({ envId: assignEnvId })
5528

5629
const website = res.data && res.data[0]
30+
5731
if (!website) {
5832
throw new CloudBaseError(
59-
'你还没有开启静态网站服务,请使用 cloudbase hosting:enable 命令启用静态网站服务!'
33+
'您还没有开启静态网站服务,请先到云开发控制台开启静态网站服务!\n 👉 https://console.cloud.tencent.com/tcb'
6034
)
6135
}
6236
const url = `https://${website.cdnDomain}`
63-
console.log(
64-
`静态网站域名:${chalk.bold.underline(url)}\n静态网站状态:${
65-
HostingStatusMap[website.status]
66-
}`
67-
)
68-
})
6937

70-
program
71-
.command('hosting:destroy')
72-
.option('-e, --envId [envId]', '环境 Id')
73-
.description('关闭静态网站服务')
74-
.action(async (options: any) => {
75-
const {
76-
parent: { configFile },
77-
envId
78-
} = options
79-
const assignEnvId = await getEnvId(envId, configFile)
80-
// 危险操作,再次确认
81-
const { confirm } = await inquirer.prompt({
82-
type: 'confirm',
83-
name: 'confirm',
84-
message: '确定要关闭静态网站服务吗,关闭后您的所有静态网站资源将被销毁,无法恢复!',
85-
default: false
86-
})
87-
if (!confirm) {
88-
throw new CloudBaseError('操作终止!')
89-
}
90-
91-
const loading = loadingFactory()
92-
loading.start('静态网站销毁中')
93-
try {
94-
const res = await destroyHosting({ envId: assignEnvId })
95-
if (res.code === 0) {
96-
loading.succeed('静态网站服务销毁中...')
97-
} else {
98-
loading.fail('静态网站服务销毁失败!')
99-
}
100-
} catch (e) {
101-
loading.fail(e.message || '静态网站服务销毁失败!')
38+
// offline 状态不展示域名
39+
if (website.status === 'offline') {
40+
console.log(`静态网站状态:${HostingStatusMap[website.status]}`)
41+
} else {
42+
console.log(
43+
`静态网站域名:${chalk.bold.underline(url)}\n静态网站状态:${
44+
HostingStatusMap[website.status]
45+
}`
46+
)
10247
}
10348
})
10449

src/hosting.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import path from 'path'
22
import CloudBase from '@cloudbase/manager-node'
33
import { StorageService } from '@cloudbase/manager-node/types/storage'
44
import { CloudApiService, firstLetterToLowerCase, checkPathExist, isDirectory } from './utils'
5-
import { list } from './storage'
65
import { CloudBaseError } from './error'
76
import { checkAndGetCredential, getProxy } from './utils'
87

@@ -58,17 +57,23 @@ async function checkHostingStatus(envId: string) {
5857
const hostings = await getHostingInfo({ envId })
5958

6059
if (!hostings.data || !hostings.data.length) {
61-
throw new CloudBaseError('静态网站服务未开启!', {
62-
code: 'INVALID_OPERATION'
63-
})
60+
throw new CloudBaseError(
61+
'您还没有开启静态网站服务,请先到云开发控制台开启静态网站服务!\n 👉 https://console.cloud.tencent.com/tcb',
62+
{
63+
code: 'INVALID_OPERATION'
64+
}
65+
)
6466
}
6567

6668
const website = hostings.data[0]
6769

6870
if (website.status !== 'online') {
69-
throw new CloudBaseError(`静态网站服务【${HostingStatusMap[website.status]}】,请稍后重试!`, {
70-
code: 'INVALID_OPERATION'
71-
})
71+
throw new CloudBaseError(
72+
`静态网站服务【${HostingStatusMap[website.status]}】,请稍后重试!`,
73+
{
74+
code: 'INVALID_OPERATION'
75+
}
76+
)
7277
}
7378

7479
return website
@@ -79,8 +84,9 @@ export async function enableHosting(options: IBaseOptions) {
7984
const hostings = await getHostingInfo(options)
8085
if (hostings.data && hostings.data.length) {
8186
const website = hostings.data[0]
82-
if (website.status !== 'destroy' || website.status !== 'destroy_fail') {
83-
throw new CloudBaseError('静态网站服务已开启!')
87+
// offline 状态的服务可重新开启
88+
if (website.status !== 'offline') {
89+
throw new CloudBaseError('静态网站服务已开启,请勿重复操作!')
8490
}
8591
}
8692

0 commit comments

Comments
 (0)