UNPKG

7.67 kBJavaScriptView Raw
1"use strict";
2var __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};
11var __importDefault = (this && this.__importDefault) || function (mod) {
12 return (mod && mod.__esModule) ? mod : { "default": mod };
13};
14Object.defineProperty(exports, "__esModule", { value: true });
15const path_1 = __importDefault(require("path"));
16const manager_node_1 = __importDefault(require("@cloudbase/manager-node"));
17const utils_1 = require("./utils");
18const error_1 = require("./error");
19function getStorageService(envId) {
20 return __awaiter(this, void 0, void 0, function* () {
21 const { secretId, secretKey, token } = yield utils_1.checkAndGetCredential(true);
22 const app = new manager_node_1.default({
23 secretId,
24 secretKey,
25 token,
26 envId,
27 proxy: utils_1.getProxy()
28 });
29 return app.storage;
30 });
31}
32const HostingStatusMap = {
33 init: '初始化中',
34 process: '处理中',
35 online: '上线',
36 destroying: '销毁中',
37 offline: '下线',
38 create_fail: '初始化失败',
39 destroy_fail: '销毁失败'
40};
41const tcbService = utils_1.CloudApiService.getInstance('tcb');
42function getHostingInfo(options) {
43 return __awaiter(this, void 0, void 0, function* () {
44 const { envId } = options;
45 const res = yield tcbService.request('DescribeStaticStore', {
46 EnvId: envId
47 });
48 const data = utils_1.firstLetterToLowerCase(res);
49 return data;
50 });
51}
52exports.getHostingInfo = getHostingInfo;
53function checkHostingStatus(envId) {
54 return __awaiter(this, void 0, void 0, function* () {
55 const hostings = yield getHostingInfo({ envId });
56 const link = utils_1.genClickableLink('https://console.cloud.tencent.com/tcb');
57 if (!hostings.data || !hostings.data.length) {
58 throw new error_1.CloudBaseError(`您还没有开启静态网站服务,请先到云开发控制台开启静态网站服务!\n👉 ${link}`, {
59 code: 'INVALID_OPERATION'
60 });
61 }
62 const website = hostings.data[0];
63 if (website.status !== 'online') {
64 throw new error_1.CloudBaseError(`静态网站服务【${HostingStatusMap[website.status]}】,无法进行此操作!`, {
65 code: 'INVALID_OPERATION'
66 });
67 }
68 return website;
69 });
70}
71function enableHosting(options) {
72 return __awaiter(this, void 0, void 0, function* () {
73 const { envId } = options;
74 const hostings = yield getHostingInfo(options);
75 if (hostings.data && hostings.data.length) {
76 const website = hostings.data[0];
77 if (website.status !== 'offline') {
78 throw new error_1.CloudBaseError('静态网站服务已开启,请勿重复操作!');
79 }
80 }
81 const res = yield tcbService.request('CreateStaticStore', {
82 EnvId: envId
83 });
84 const code = res.Result === 'succ' ? 0 : -1;
85 return {
86 code,
87 requestId: res.RequestId
88 };
89 });
90}
91exports.enableHosting = enableHosting;
92function hostingList(options) {
93 return __awaiter(this, void 0, void 0, function* () {
94 const { envId } = options;
95 const hosting = yield checkHostingStatus(envId);
96 const { bucket, regoin } = hosting;
97 const storageService = yield getStorageService(envId);
98 const list = yield storageService.walkCloudDirCustom({
99 prefix: '',
100 bucket,
101 region: regoin
102 });
103 return list;
104 });
105}
106exports.hostingList = hostingList;
107function destroyHosting(options) {
108 return __awaiter(this, void 0, void 0, function* () {
109 const { envId } = options;
110 const files = yield hostingList(options);
111 if (files === null || files === void 0 ? void 0 : files.length) {
112 throw new error_1.CloudBaseError('静态网站文件不为空,无法销毁!', {
113 code: 'INVALID_OPERATION'
114 });
115 }
116 const hostings = yield getHostingInfo(options);
117 if (!hostings.data || !hostings.data.length) {
118 throw new error_1.CloudBaseError('静态网站服务未开启!', {
119 code: 'INVALID_OPERATION'
120 });
121 }
122 const website = hostings.data[0];
123 if (website.status !== 'online' && website.status !== 'destroy_fail') {
124 throw new error_1.CloudBaseError(`静态网站服务【${HostingStatusMap[website.status]}】,无法进行此操作!`, {
125 code: 'INVALID_OPERATION'
126 });
127 }
128 const res = yield tcbService.request('DestroyStaticStore', {
129 EnvId: envId
130 });
131 const code = res.Result === 'succ' ? 0 : -1;
132 return {
133 code,
134 requestId: res.RequestId
135 };
136 });
137}
138exports.destroyHosting = destroyHosting;
139function hostingDeploy(options) {
140 return __awaiter(this, void 0, void 0, function* () {
141 const { envId, filePath, cloudPath, onProgress, onFileFinish } = options;
142 const resolvePath = path_1.default.resolve(filePath);
143 utils_1.checkReadable(resolvePath, true);
144 const hosting = yield checkHostingStatus(envId);
145 const { bucket, regoin } = hosting;
146 const storageService = yield getStorageService(envId);
147 if (utils_1.isDirectory(resolvePath)) {
148 yield storageService.uploadDirectoryCustom({
149 localPath: resolvePath,
150 cloudPath,
151 bucket,
152 region: regoin,
153 onProgress,
154 onFileFinish,
155 fileId: false
156 });
157 }
158 else {
159 const assignCloudPath = cloudPath || path_1.default.parse(resolvePath).base;
160 yield storageService.uploadFileCustom({
161 localPath: resolvePath,
162 cloudPath: assignCloudPath,
163 bucket,
164 region: regoin,
165 onProgress,
166 fileId: false
167 });
168 }
169 });
170}
171exports.hostingDeploy = hostingDeploy;
172function hostingDelete(options) {
173 return __awaiter(this, void 0, void 0, function* () {
174 const { envId, cloudPath, isDir } = options;
175 const hosting = yield checkHostingStatus(envId);
176 const { bucket, regoin } = hosting;
177 const storageService = yield getStorageService(envId);
178 if (isDir) {
179 yield storageService.deleteDirectoryCustom({
180 cloudPath,
181 bucket,
182 region: regoin
183 });
184 }
185 else {
186 yield storageService.deleteFileCustom([cloudPath], bucket, regoin);
187 }
188 });
189}
190exports.hostingDelete = hostingDelete;
191function walkLocalDir(envId, dir) {
192 return __awaiter(this, void 0, void 0, function* () {
193 const storageService = yield getStorageService(envId);
194 return storageService.walkLocalDir(dir);
195 });
196}
197exports.walkLocalDir = walkLocalDir;