All files cws.js

75.94% Statements 101/133
68.87% Branches 73/106
90.91% Functions 30/33
100% Lines 1/1

Press n or j to go to the next uncovered block, b, p or k for the previous block.

187x
"use strict";var __awaiter=this&&this.__awaiter||function(e,i,r,a){function s(t){return t instanceof r?t:new r(function(e){e(t)})}return new(r||(r=Promise))(function(t,r){function n(e){try{u(a.next(e))}catch(e){r(e)}}function o(e){try{u(a["throw"](e))}catch(e){r(e)}}function u(e){e.done?t(e.value):s(e.value).then(n,o)}u((a=a.apply(e,i||[])).next())})};var __generator=this&&this.__generator||function(e,r){var n={label:0,sent:function(){Iif(i[0]&1)throw i[1];return i[1]},trys:[],ops:[]},o,u,i,t;return t={next:a(0),throw:a(1),return:a(2)},typeof Symbol==="function"&&(t[Symbol.iterator]=function(){return this}),t;function a(t){return function(e){return s([t,e])}}function s(t){Iif(o)throw new TypeError("Generator is already executing.");while(n)try{Iif(o=1,u&&(i=t[0]&2?u["return"]:t[0]?u["throw"]||((i=u["return"])&&i.call(u),0):u.next)&&!(i=i.call(u,t[1])).done)return i;Iif(u=0,i)t=[t[0]&2,i.value];switch(t[0]){case 0:case 1:i=t;break;case 4:n.label++;return{value:t[1],done:false};case 5:n.label++;u=t[1];t=[0];continue;case 7:t=n.ops.pop();n.trys.pop();continue;default:if(!(i=n.trys,i=i.length>0&&i[i.length-1])&&(t[0]===6||t[0]===2)){n=0;continue}Eif(t[0]===3&&(!i||t[1]>i[0]&&t[1]<i[3])){n.label=t[1];break}if(t[0]===6&&n.label<i[1]){n.label=i[1];i=t;break}if(i&&n.label<i[2]){n.label=i[2];n.ops.push(t);break}if(i[2])n.ops.pop();n.trys.pop();continue}t=r.call(e,n)}catch(e){t=[6,e];u=0}finally{o=i=0}Iif(t[0]&5)throw t[1];return{value:t[0]?t[1]:void 0,done:true}}};Object.defineProperty(exports,"__esModule",{value:true});exports.publish=exports.upload=void 0;var request=require("superagent");var google_auth_library_1=require("google-auth-library");var fs=require("fs");var dictionary={zipError:"FATAL: zip file not found or not readable",authError:"FATAL: authentication failed. At least 1 of arguments: client_id, client_secret, refresh_token; is invalid."};var checkApiResponse=function(e){return!!(e&&e.body)};var uploadSuccess=function(e){return checkApiResponse(e)&&(e.body.uploadState==="SUCCESS"||e.body.uploadState==="IN_PROGRESS")};var publishSuccess=function(e){return checkApiResponse(e)&&Array.isArray(e.body.status)&&(e.body.status.includes("OK")||e.body.status.includes("ITEM_PENDING_REVIEW"))};var getFileBlob=function(e){try{return fs.readFileSync(e)}catch(e){return null}};var getAccessToken=function(t,n,o){return new Promise(function(r){var e=new google_auth_library_1.OAuth2Client(n,o,"urn:ietf:wg:oauth:2.0:oob");e.setCredentials({access_token:null,refresh_token:t});e.refreshAccessToken(function(e,t){return r(!e&&t?t.access_token:undefined)})})};var uploadFile=function(e,t,n){return new Promise(function(r){return request.put("https://www.googleapis.com/upload/chromewebstore/v1.1/items/"+e).query({uploadType:"media"}).set({Authorization:"Bearer "+n}).send(t).end(function(e,t){return r([e,t])})})};var publishExtension=function(e,t,n){return new Promise(function(r){return request.post("https://www.googleapis.com/chromewebstore/v1.1/items/".concat(e,"/publish")).query({publishTarget:n?"trustedTesters":"default"}).set({Authorization:"Bearer "+t}).end(function(e,t){return r([e,t])})})};var handleResult=function(e,t){var r=typeof t==="object"?t.body:t;if(e)console.log(r);else{console.error(r);process.exit(1)}};var upload=function(a,s,l,c,f){return __awaiter(void 0,void 0,void 0,function(){var t,r,n,o,u,i;return __generator(this,function(e){switch(e.label){case 0:t=getFileBlob(c);if(!t)return[2,handleResult(false,dictionary.zipError)];return[4,getAccessToken(l,a,s)];case 1:r=e.sent();if(!r)return[2,handleResult(false,dictionary.authError)];return[4,uploadFile(f,t,r)];case 2:n=e.sent(),o=n[0],u=n[1];i=!o&&uploadSuccess(u);handleResult(i,u);return[2,i?r:undefined]}})})};exports.upload=upload;var publish=function(u,i,a,s,l,c){return __awaiter(void 0,void 0,void 0,function(){var t,r,n,o;return __generator(this,function(e){switch(e.label){case 0:return[4,(0,exports.upload)(u,i,a,s,l)];case 1:t=e.sent();if(!t)return[3,3];return[4,publishExtension(l,t,c)];case 2:r=e.sent(),n=r[0],o=r[1];handleResult(!n&&publishSuccess(o),o);e.label=3;case 3:return[2]}})})};exports.publish=publish;