@@ -15,7 +15,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
1515const path_1 = __importDefault ( require ( "path" ) ) ;
1616const manager_node_1 = __importDefault ( require ( "@cloudbase/manager-node" ) ) ;
1717const utils_1 = require ( "./utils" ) ;
18- const storage_1 = require ( "./storage" ) ;
1918const error_1 = require ( "./error" ) ;
2019const utils_2 = require ( "./utils" ) ;
2120function getStorageService ( envId ) {
@@ -31,21 +30,16 @@ function getStorageService(envId) {
3130 return app . storage ;
3231 } ) ;
3332}
33+ const HostingStatusMap = {
34+ init : '初始化中' ,
35+ process : '处理中' ,
36+ online : '上线' ,
37+ destroying : '销毁中' ,
38+ offline : '下线' ,
39+ create_fail : '初始化失败' ,
40+ destroy_fail : '销毁失败'
41+ } ;
3442const tcbService = new utils_1 . CloudApiService ( 'tcb' ) ;
35- function enableHosting ( options ) {
36- return __awaiter ( this , void 0 , void 0 , function * ( ) {
37- const { envId } = options ;
38- const res = yield tcbService . request ( 'CreateStaticStore' , {
39- EnvId : envId
40- } ) ;
41- const code = res . Result === 'succ' ? 0 : - 1 ;
42- return {
43- code,
44- requestId : res . RequestId
45- } ;
46- } ) ;
47- }
48- exports . enableHosting = enableHosting ;
4943function getHostingInfo ( options ) {
5044 return __awaiter ( this , void 0 , void 0 , function * ( ) {
5145 const { envId } = options ;
@@ -61,34 +55,69 @@ function checkHostingStatus(envId) {
6155 return __awaiter ( this , void 0 , void 0 , function * ( ) {
6256 const hostings = yield getHostingInfo ( { envId } ) ;
6357 if ( ! hostings . data || ! hostings . data . length ) {
64- throw new error_1 . CloudBaseError ( '静态托管服务未开启 !' , {
58+ throw new error_1 . CloudBaseError ( '静态网站服务未开启 !' , {
6559 code : 'INVALID_OPERATION'
6660 } ) ;
6761 }
68- return hostings ;
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 ;
6969 } ) ;
7070}
71- function destroyHosting ( options ) {
71+ function enableHosting ( options ) {
7272 return __awaiter ( this , void 0 , void 0 , function * ( ) {
7373 const { envId } = options ;
74- const files = yield storage_1 . list ( {
75- envId,
76- cloudPath : ''
77- } ) ;
7874 const hostings = yield getHostingInfo ( options ) ;
79- if ( ! hostings . data || ! hostings . data . length ) {
80- throw new error_1 . CloudBaseError ( '静态托管服务未开启!' , {
75+ if ( hostings . data && hostings . data . length ) {
76+ const website = hostings . data [ 0 ] ;
77+ if ( website . status !== 'destroy' || website . status !== 'destroy_fail' ) {
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+ }
91+ exports . enableHosting = enableHosting ;
92+ function 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 ( '' , bucket , regoin ) ;
99+ return list ;
100+ } ) ;
101+ }
102+ exports . hostingList = hostingList ;
103+ function destroyHosting ( options ) {
104+ return __awaiter ( this , void 0 , void 0 , function * ( ) {
105+ const { envId } = options ;
106+ const files = yield hostingList ( options ) ;
107+ if ( files && files . length ) {
108+ throw new error_1 . CloudBaseError ( '静态网站文件不为空,无法销毁!' , {
81109 code : 'INVALID_OPERATION'
82110 } ) ;
83111 }
84- const website = hostings . data [ 0 ] ;
85- if ( website . status !== 'online' ) {
86- throw new error_1 . CloudBaseError ( '静态托管服务状态异常,无法销毁 !' , {
112+ const hostings = yield getHostingInfo ( options ) ;
113+ if ( ! hostings . data || ! hostings . data . length ) {
114+ throw new error_1 . CloudBaseError ( '静态网站服务未开启 !' , {
87115 code : 'INVALID_OPERATION'
88116 } ) ;
89117 }
90- if ( files . length ) {
91- throw new error_1 . CloudBaseError ( '静态托管文件不为空,无法销毁!' , {
118+ const website = hostings . data [ 0 ] ;
119+ if ( website . status !== 'online' && website . status !== 'destroy_fail' ) {
120+ throw new error_1 . CloudBaseError ( `静态网站服务【${ HostingStatusMap [ website . status ] } 】,无法处理请求!` , {
92121 code : 'INVALID_OPERATION'
93122 } ) ;
94123 }
@@ -108,41 +137,30 @@ function hostingDeploy(options) {
108137 const { envId, filePath, cloudPath } = options ;
109138 const resolvePath = path_1 . default . resolve ( filePath ) ;
110139 utils_1 . checkPathExist ( resolvePath , true ) ;
111- const hostings = yield checkHostingStatus ( envId ) ;
112- const { bucket, region } = hostings . data [ 0 ] ;
140+ const hosting = yield checkHostingStatus ( envId ) ;
141+ const { bucket, regoin } = hosting ;
113142 const storageService = yield getStorageService ( envId ) ;
114143 if ( utils_1 . isDirectory ( resolvePath ) ) {
115- storageService . uploadDirectoryCustom ( resolvePath , cloudPath , bucket , region ) ;
144+ storageService . uploadDirectoryCustom ( resolvePath , cloudPath , bucket , regoin ) ;
116145 }
117146 else {
118- storageService . uploadFileCustom ( resolvePath , cloudPath , bucket , region ) ;
147+ storageService . uploadFileCustom ( resolvePath , cloudPath , bucket , regoin ) ;
119148 }
120149 } ) ;
121150}
122151exports . hostingDeploy = hostingDeploy ;
123152function hostingDelete ( options ) {
124153 return __awaiter ( this , void 0 , void 0 , function * ( ) {
125154 const { envId, cloudPath, isDir } = options ;
126- const hostings = yield checkHostingStatus ( envId ) ;
127- const { bucket, region } = hostings . data [ 0 ] ;
155+ const hosting = yield checkHostingStatus ( envId ) ;
156+ const { bucket, regoin } = hosting ;
128157 const storageService = yield getStorageService ( envId ) ;
129158 if ( isDir ) {
130- storageService . deleteDirectoryCustom ( cloudPath , bucket , region ) ;
159+ storageService . deleteDirectoryCustom ( cloudPath , bucket , regoin ) ;
131160 }
132161 else {
133- storageService . deleteFileCustom ( [ cloudPath ] , bucket , region ) ;
162+ storageService . deleteFileCustom ( [ cloudPath ] , bucket , regoin ) ;
134163 }
135164 } ) ;
136165}
137166exports . hostingDelete = hostingDelete ;
138- function hostingList ( options ) {
139- return __awaiter ( this , void 0 , void 0 , function * ( ) {
140- const { envId } = options ;
141- const hostings = yield checkHostingStatus ( envId ) ;
142- const { bucket, region } = hostings . data [ 0 ] ;
143- const storageService = yield getStorageService ( envId ) ;
144- const list = yield storageService . walkCloudDirCustom ( '' , bucket , region ) ;
145- return list ;
146- } ) ;
147- }
148- exports . hostingList = hostingList ;
0 commit comments