@altostra/core
Version:
Core library for shared types and logic
1 lines • 5.29 kB
JavaScript
"use strict";var _Git_instances,_a,_Git_simpleGit,_Git_isRepoExists,_Git_validateExistence,_Git_commitMessageByPaths,_Git_restorationPathsError,__awaiter=this&&this.__awaiter||function(t,e,i,o){return new(i||(i=Promise))((function(r,s){function a(t){try{l(o.next(t))}catch(t){s(t)}}function n(t){try{l(o.throw(t))}catch(t){s(t)}}function l(t){var e;t.done?r(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(a,n)}l((o=o.apply(t,e||[])).next())}))},__classPrivateFieldSet=this&&this.__classPrivateFieldSet||function(t,e,i,o,r){if("m"===o)throw new TypeError("Private method is not writable");if("a"===o&&!r)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!r:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===o?r.call(t,i):r?r.value=i:e.set(t,i),i},__classPrivateFieldGet=this&&this.__classPrivateFieldGet||function(t,e,i,o){if("a"===i&&!o)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!o:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===i?o:"a"===i?o.call(t):o?o.value:e.get(t)},__importDefault=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.Git=void 0;const AltoError_1=require("../../common/Errors/AltoError"),Object_1=require("../../common/Utils/Object"),simple_git_1=__importDefault(require("simple-git")),Common_1=require("./Common"),pathsGlobByRestorationPath={[Common_1.RestorationPaths.none]:"!./**",[Common_1.RestorationPaths.code]:"**",[Common_1.RestorationPaths.altostra]:".altostra/**",[Common_1.RestorationPaths.all]:"**"};class Git{constructor(t){this.path=t,_Git_instances.add(this),_Git_simpleGit.set(this,void 0),__classPrivateFieldSet(this,_Git_simpleGit,(0,simple_git_1.default)(t),"f")}get isRepoExists(){return __classPrivateFieldGet(this,_Git_instances,"m",_Git_isRepoExists).call(this)}getRemoteUrl(t="origin"){return __awaiter(this,void 0,void 0,(function*(){const{[t]:e}=yield this.getRemotes();return e}))}getRemotes(){return __awaiter(this,void 0,void 0,(function*(){const t=yield __classPrivateFieldGet(this,_Git_instances,"m",_Git_validateExistence).call(this),e=yield t.getRemotes(!0);return(0,Object_1.fromEntries)(e.map(t=>[t.name,t.refs.fetch]))}))}getStatus(){return __awaiter(this,void 0,void 0,(function*(){const t=yield __classPrivateFieldGet(this,_Git_instances,"m",_Git_validateExistence).call(this);return yield t.status()}))}isDirty(){return __awaiter(this,void 0,void 0,(function*(){return!(yield this.getStatus()).isClean()}))}headHash(){return __awaiter(this,void 0,void 0,(function*(){const t=yield __classPrivateFieldGet(this,_Git_instances,"m",_Git_validateExistence).call(this);return yield AltoError_1.AltoError.wrapPromise(t.revparse(["HEAD"]),{message:"Could not get HEAD hash"})}))}restoreFromCommit(t,e,i){return __awaiter(this,void 0,void 0,(function*(){if(e&=Common_1.RestorationPaths.all,Common_1.RestorationPaths.none===e)return;if(yield this.isDirty())throw new AltoError_1.AltoError("Cannot restore while repository is dirty");const o=yield __classPrivateFieldGet(this,_Git_instances,"m",_Git_validateExistence).call(this);let r=void 0;if(e===Common_1.RestorationPaths.code&&(r=yield this.headHash()),yield AltoError_1.AltoError.wrapPromise(o.checkout([t,"--no-overlay","--",pathsGlobByRestorationPath[e]]),{message:"Failed to checkout files"}),e===Common_1.RestorationPaths.code){if(void 0===r)throw new AltoError_1.AltoError("currentHead is undefined but is shouldn't");yield AltoError_1.AltoError.wrapPromise(o.checkout([r,"--no-overlay","--",pathsGlobByRestorationPath[Common_1.RestorationPaths.altostra]]),{message:"Failed restore altostra project from HEAD"})}(yield this.isDirty())&&(yield AltoError_1.AltoError.wrapPromise(o.commit(null!=i?i:__classPrivateFieldGet(Git,_a,"m",_Git_commitMessageByPaths).call(Git,t,e)),{message:"Failed to commit restored files"}))}))}}exports.Git=Git,_a=Git,_Git_simpleGit=new WeakMap,_Git_instances=new WeakSet,_Git_isRepoExists=function(){return __awaiter(this,void 0,void 0,(function*(){try{return yield __classPrivateFieldGet(this,_Git_simpleGit,"f").checkIsRepo()}catch(t){throw AltoError_1.AltoError.wrapError(t,{message:"Failed to open git repo. Please check that git is installed and accessible"})}}))},_Git_validateExistence=function(){return __awaiter(this,void 0,void 0,(function*(){if(!(yield this.isRepoExists))throw new AltoError_1.AltoError("Missing Git repository. Please initialize a new repository and commit all changes.");return __classPrivateFieldGet(this,_Git_simpleGit,"f")}))},_Git_commitMessageByPaths=function(t,e){switch(e&Common_1.RestorationPaths.all){case Common_1.RestorationPaths.all:return"Repository restored to commit "+t;case Common_1.RestorationPaths.altostra:return"Altostra project restored to commit "+t;case Common_1.RestorationPaths.code:return"All code restored to commit "+t;default:throw __classPrivateFieldGet(this,_a,"m",_Git_restorationPathsError).call(this,e)}},_Git_restorationPathsError=function(t){return new AltoError_1.AltoError(`Invalid paths [${Common_1.RestorationPaths[t]||t}]`)};