@altostra/core
Version:
Core library for shared types and logic
1 lines • 6.71 kB
JavaScript
;var __awaiter=this&&this.__awaiter||function(e,t,r,i){return new(r||(r=Promise))((function(o,s){function a(e){try{c(i.next(e))}catch(e){s(e)}}function n(e){try{c(i.throw(e))}catch(e){s(e)}}function c(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,n)}c((i=i.apply(e,t||[])).next())}))},__classPrivateFieldSet=this&&this.__classPrivateFieldSet||function(e,t,r,i,o){if("m"===i)throw new TypeError("Private method is not writable");if("a"===i&&!o)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!o:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===i?o.call(e,r):o?o.value=r:t.set(e,r),r},__classPrivateFieldGet=this&&this.__classPrivateFieldGet||function(e,t,r,i){if("a"===r&&!i)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!i:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?i:"a"===r?i.call(e):i?i.value:t.get(e)},__rest=this&&this.__rest||function(e,t){var r={};for(var i in e)Object.prototype.hasOwnProperty.call(e,i)&&t.indexOf(i)<0&&(r[i]=e[i]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(i=Object.getOwnPropertySymbols(e);o<i.length;o++)t.indexOf(i[o])<0&&Object.prototype.propertyIsEnumerable.call(e,i[o])&&(r[i[o]]=e[i[o]])}return r};Object.defineProperty(exports,"__esModule",{value:!0}),exports.ProjectValidatingServiceClient=exports.backofficeUrlSym=void 0;const fs_1=require("fs"),NonEmptyString_1=require("../../../common/CustomTypes/NonEmptyString"),Errors_1=require("../../../common/Errors"),Url_1=require("../../../common/Utils/Url"),type_validations_1=require("@altostra/type-validations"),from_1=require("@reactivex/ix-es2015-cjs/iterable/from"),map_1=require("@reactivex/ix-es2015-cjs/iterable/operators/map"),orderby_1=require("@reactivex/ix-es2015-cjs/iterable/operators/orderby"),git_1=require("../../git"),Utils_1=require("../../ProjectManager/Utils"),Types_1=require("../../Types"),Types_2=require("../BuildsManager/Types");function ProjectValidatingServiceClient(e){var t,r,i,o,s,a;return r=new WeakMap,i=new WeakMap,t=new WeakSet,o=function(e,r){return __awaiter(this,void 0,void 0,(function*(){const{bucketName:i,projectId:o,projectType:a}=yield __classPrivateFieldGet(this,t,"m",s).call(this,e,r);return{bucket:i,newProjectId:o,projectType:null!=a?a:"altostra"}}))},s=function(e,r){return __awaiter(this,void 0,void 0,(function*(){const o=r?{initializeProject:!0,projectDescription:(0,NonEmptyString_1.maybeNonEmptyString)(e.description),projectName:(0,NonEmptyString_1.nonEmptyString)(e.name),remotes:yield __classPrivateFieldGet(this,t,"m",a).call(this,r.projectDir)}:{initializeProject:!1,projectDescription:(0,NonEmptyString_1.maybeNonEmptyString)(e.description),projectName:(0,NonEmptyString_1.maybeNonEmptyString)(e.name)};return yield this._request({request:t=>__awaiter(this,void 0,void 0,(function*(){return yield t.post(getBucketPathUrl(__classPrivateFieldGet(this,i,"f"),e.id),o,{headers:yield this._getHeaders()})})),dataValidation:Types_2.isOrganizationBucket,errorWrapping:{}})}))},a=function(e){return __awaiter(this,void 0,void 0,(function*(){const t=new git_1.Git(e);if(!(yield t.isRepoExists))throw Errors_1.UserFacingError.create("The current project is not linked to a Git repository.\nPlease initialize a new Git repository in the project directory and push it to the Git account you have connected in Altostra.\nHow to initialize a Git repository: https://git-scm.com/docs/git-init\nHow to connect your Git account in Altostra: https://docs.altostra.com/integrations/Git/git-integration.html",{category:"user-fault",type:"no-project-git"});const r=yield t.getRemotes(),i=[...(0,from_1.from)(Object.entries(r)).pipe((0,orderby_1.orderBy)(([e])=>"origin"===e?0:1),(0,map_1.map)(([,e])=>e))];if(0===i.length)throw Errors_1.UserFacingError.create("The current project's Git repository doesn't have any remotes configured.\nPlease add a remote Git repository and push the local changes to it.\n\nHow to add a Git remote repository: https://git-scm.com/docs/git-remote",{category:"user-fault",type:"git-repo-has-no-remotes"});return i}))},class extends e{constructor(...e){const o=e[0],s=exports.backofficeUrlSym,a=o[s],n=__rest(o,["symbol"==typeof s?s:s+""]);e[0]=n,super(...e),t.add(this),r.set(this,void 0),i.set(this,void 0),__classPrivateFieldSet(this,i,a,"f"),__classPrivateFieldSet(this,r,(0,type_validations_1.assertBy)(Types_1.isProject,(e,t)=>(this._logger.verbose("Project rejection reasons: ",t),Errors_1.AltoError.create("The project file (.altostra/project.json) seems to be corrupt.\nPlease review the latest changes made to it and restore it if required.\nAvoid editing the file by hand, unless absolutely necessary.",{data:{invalidProject:e,rejections:t}}))),"f")}validateProjectByType(e,r,{initializeProject:i=!1}={}){return __awaiter(this,void 0,void 0,(function*(){const s=yield this._withLogging("Loading project",()=>this.getProject(e)),a=yield this._withLogging("Getting project information",()=>__awaiter(this,void 0,void 0,(function*(){return yield __classPrivateFieldGet(this,t,"m",o).call(this,s,i?{projectDir:e}:void 0)})));return a.newProjectId===s.id&&a.projectType===s.type||(yield this._withLogging("Updating project file",()=>__awaiter(this,void 0,void 0,(function*(){s.id=a.newProjectId,s.type=a.projectType,yield Errors_1.AltoError.wrapPromise(fs_1.promises.writeFile((0,Utils_1.altostraProject)(e),JSON.stringify(s,null,2)),{message:"Storing updated project file failed",data:{publicationArgs:{projectDir:e,projectType:a.projectType}}})})))),yield this._withLogging("Validating project",()=>__awaiter(this,void 0,void 0,(function*(){const e=displayNamesByProjectType[r],t=displayNamesByProjectType[a.projectType];if(a.projectType!==r){const r="altostra"===a.projectType?"an":"a";throw Errors_1.AltoError.create(`This command is supported for ${e} projects only. This is ${r} ${t} project.`)}}))),{project:s,publicationPath:a}}))}getProject(e){return __awaiter(this,void 0,void 0,(function*(){const t=(0,Utils_1.altostraProject)(e),i=yield Errors_1.AltoError.wrapPromise(fs_1.promises.readFile(t)),o=JSON.parse(i.toString());return __classPrivateFieldGet(this,r,"f")(o),o}))}}}exports.backofficeUrlSym=Symbol("backofficeUrl"),exports.ProjectValidatingServiceClient=ProjectValidatingServiceClient;const displayNamesByProjectType={altostra:"Altostra","serverless-framework":"Serverless Framework",cloudformation:"AWS CloudFormation"};function getBucketPathUrl(e,t){return e+Url_1.asUrl`/projects/${t}/images`}