|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497 |
-
- // {{!-- https://corporatefinanceinstitute.com/resources/knowledge/accounting/general-ledger-gl/ --}}
-
- "use strict";
- // console.log([ 'If this is colorized we have color output...' ])
- // console.dir(process.env)
- var __g = require('global-this');
- var ENV = Object.assign({}, process.env); // Shallow clone so we can set values and reuse for reshelling spawns.
- __g.env = ENV
- ENV.NODE_ENV = (ENV.NODE_ENV && ENV.NODE_ENV.trim()) || 'development'
- const fs = require('fs')
- var path = require('path');
- var utils = require('bbhverse');
- var any = utils.any;
- var Tasq = utils.Tasq
- var statuslog = utils.Traq
- var Traq = utils.Traq
- // console.error('EEEEEEEEEEEEEEEEEEEE')
-
-
- var ENV = Object.assign({}, process.env); // Shallow clone so we can set values and reuse for reshelling spawns.
- const cliargs = utils.cliargs;
- const processedArgs = cliargs(process.argv.slice(2));
-
- const __ALIAS__STAMP__ = '9e7bebe0-1f57-11ec-8f88-778ffeea9d1b'
- const BUILD_VERSION = '[VI]Version: {version} - built on {date}[/VI]';
- const runtimestamp = processedArgs.runtimestamp ? (new Date(processedArgs.runtimestamp)).getTime() : (new Date()).getTime();
- function getVersion() { return BUILD_VERSION; }
-
- // 'use strict';
- // PB : TODO -- make sure folder context is proper coz we can now run elxr from anywhere.
-
- // PB : TODO --
- // runas bypass non elevated tasks when we request privelege
- // runas message is always error needs to be fixed.
- // runas wait needs to be parallelized.
- // suppress elevation check error messages
- // support runas lauched directly from shell.
- // pass in environment in hta to shellexecute.
-
- // PB : NOTE -- iife doesnt work if previous statement is not terminated by ;
- (function () {
- "use strict";
- if (!Array.prototype.earlyreduce) {
- Array.prototype.earlyreduce = function(eachcallback, initialValue, donecallback){
- var iterable = this;
- var donecallback = donecallback || (a=>a)
- var result = { value : initialValue === undefined ? iterable[0] : initialValue };
- initialValue === undefined ? null : result = eachcallback(result, element, i, iterable);
- // function *earlyReduceGenerator(acc, v, i, a) {
- for(var i = initialValue === undefined ? 0 : 1; i < iterable.length; i++){
- var element = iterable[i]
- result = eachcallback(result, element, i, iterable)
- if(result.done) return donecallback(result)
- }
- // }
- return donecallback(result)
- }
- }
- }());
-
- function isWin(){ return /^win/.test(process.platform) }
- if(isWin()) {
- var win_verse = require('./win_verse')
- var shell_verse = win_verse;
- }
- else {
- var lin_verse = require('./lin_verse')
- var shell_verse = lin_verse;
- }
-
- // PB : TODO -- In windows if we are run from an elevated shell we never move forward and simply exits !?.
- // -- Currently workaround in windows is to always run from a non-elevated shell.
- shell_verse.acquireElevationState().then((isElevated) => {
-
- // PB : TODO -- Implement queues using streams. Specially form bundling all elevated tasks as a single out of proc run with post elevated queue run postponed until the elevated stream and its dependencies are completed....
- const { existsSync } = require('fs');
- const existslink = function(name, cb){
- fs.lstat(name, (error, stats) => {
- if (error) {
- console.log(error);
- cb(error)
- }
- else {
- console.log("Stats object for: example_directory.txt");
- console.log(stats);
-
- // Using methods of the Stats object
- console.log("Path is file:", stats.isFile());
- console.log("Path is directory:", stats.isDirectory());
- cb(null, true)
- }
- });
- }
- const existsFolderSync = existsSync;
-
- var hasElxr = function(path, options, cb) {
- // PB : TOOD -- Navigate up the folder chain to discover the relevant .elxr directory.
- options = options || {};
-
- var tasks = [
- '/elxr' // Is there a subfolder named elxr
- , '/elxr/.git' // which is also a git repository
- // , '/elxr/.elxr' // and has .elxr subfolder we now store this in the instanceroot.
- , '/elxr/' + __ALIAS__STAMP__ // Which has our stamp.
- ]
-
- if(options.sync) { return tasks.earlyreduce((acc, tpath)=>{
- var value = existsFolderSync(path + tpath);
- return { value, done : acc && !value };
- }).value }
-
- if(cb) return cb(null, tasks.earlyreduce((acc, tpath)=>{
- var value = existsFolderSync(path + tpath);
- return { value, done : acc && !value };
- }).value );
-
- return Promise.resolve(tasks.earlyreduce((acc, tpath)=>{
- var value = existsFolderSync(path + tpath);
- return { value, done : acc && !value };
- }).value);
- }
-
- var hasElxrSync = function(path){ return hasElxr(path, { sync :true}); }
-
- var thisscriptdir = path.normalize(__dirname); // PB : TODO -- Thisscriptdir could be dislocated when run as a standalone file... We need to detect this where and how
- var root = process.cwd() // Root need not be the launchdir...
- // wd is our own environment variable probably corresponding to the root but need not be the same.
- // console.log(processedArgs.root)
-
- if(!processedArgs.root){
- if(!process.env.wd){
- console.log('discovery')
- // Script may exists in the same location where we were launched(PWD) but that doesn't mean we r running from elxr.
- // script could have been copied tosome random location and run from there.
- // check elxr is preinstalled in same location. If so we need to chdir to parent. Else promt for confirmation of root.
- var launchdir = /*process.env.PWD ||*/ process.cwd() // mingw + node --inspect-brk behaves differently has a PWD that windows commands cannot recognize /d instead of D:\ and process.chdir fails..
- var parentdir = path.dirname( launchdir )
- var parentFolderHasElxr = hasElxrSync(parentdir)
- var launchFolderHasElxr = hasElxrSync(launchdir)
- if( parentFolderHasElxr && (path.normalize(parentdir + '/elxr') === thisscriptdir)) {
- // We were run from the proper root with elxr cli in the subfolder.
- // PB : TODO -- Cleanup this should no longer be needed... selectedinstance is properly initialized...
-
- // instanceroot = root = selectedinstance.root;
- // instanceoptions.splice( 0, 0, detected = { root })
- // instanceoptions.splice( 0, 0, detectfromroot(root))
- root = ENV.wd = parentdir; // Default to the parent.
- }
- else {
- if( path.normalize(parentdir + '/elxr') === thisscriptdir ) {
- // launchFolderHasElxr is false -- and yet thiscriptdir is still proper.
- // PB : TODO -- Maybe a warning / abort if for some reason thisscriptdir should not be used...
- console.error('Warning : detected thisscriptdir as elxr subfolder but not recognized as elixir. git updates might fail.')
- // instanceoptions.splice( 0, 0, detected = { root })
- // instanceoptions.splice( 0, 0, detectfromroot(root))
- root = ENV.wd = parentdir; // Default to the parent.
- }
- else if (launchdir === thisscriptdir) {
- // Same directory doesn't mean we are being run from elxr sub directory. It could be a dislocated standalone elxr script.
- // PB : TODO -- In case elxr is not full elxr we need to locate it and relaunch to switch to the full version...
-
- // if(!parentFolderHasElxr) console.error('Invalid run location in subfolder that looks like elxr. We should probably abort.')
-
- if (BUILD_VERSION.indexOf('Version: {version} - built on {date}') > -1) {
- // Confirmed we were run from an Unbuilt ( meaning non standalone ) elxr therefore we are in the elxr sub directory.
- root = ENV.wd = parentdir; // Default to the parent.
-
- // instanceroot = root = path.normalize(selectedinstance.root + '/..');
- // instanceoptions.splice( 0, 0, detected = { root });
- // instanceoptions.splice( 0, 0, detectfromroot(root));
- }
- else {
- // In standalone build script we may or not be in the same location.
- if(!parentFolderHasElxr) {
- // We could have been run from the elxr subfolder. However we cant say for sure.
- // Most likely that the built version isn't the full elxr. We assume elxr doesnt exist and create an new elxr under..
- console.error('Warning : detected thisscriptdir as elxr subfolder but not recognized as elixir. git updates might fail.')
- root = ENV.wd = launchdir;
- }
- else {
- // Built version was run from the full elxr subfolder
- root = ENV.wd = parentdir; // Default to the parent.
- // instanceoptions.splice( 0, 0, detected = { root })
- // instanceoptions.splice( 0, 0, detectfromroot(root))
-
- // Assume current selectedinstance.root is a new instance and create.
- // Figure out the instnace name and environment from parent folders as an alternative option with confirmation if not provided in the arguments.
-
- // if(clioverrides.instanceName) {
- // if(clioverrides.node_env) {
- // instanceroot = root = path.normalize(selectedinstance.root + '/' + clioverrides.instanceName + '/' + clioverrides.node_env)
- // instanceoptions.splice( 0, 0, detected = { root, instanceName : clioverrides.instanceName, node_env : clioverrides.node_env })
- // // instanceoptions.splice( 0, 0, detectfromroot(root)) // This can be an option but is unnecessary unless a confirmation is provided.
- // // also folder names may have no relation to the actual instanceName and instanceType coz we need to have many
- // // eg : floder name can be elixir01 but instance name is elixr
- // }
- // else {
- // instanceroot = root = path.normalize(selectedinstance.root + '/' + clioverrides.instanceName + '/' + 'development')
- // instanceoptions.splice( 0, 0, detected = { root, instanceName : clioverrides.instanceName, node_env : 'development' })
- // instanceoptions.splice( 0, 0, detectfromroot(root)) // A recessive option only.
- // }
- // }
- // else {
- // instanceroot = root = selectedinstance.root;
- // if(clioverrides.node_env) {
- // instanceoptions.splice( 0, 0, detected = { root, node_env : clioverrides.node_env })
- // instanceoptions.splice( 0, 0, detectfromroot(root))
- // }
- // else {
- // // Nothing was specified... We only have one option from root.
- // instanceoptions.splice( 0, 0, detected = detectfromroot(launcpath))
- // }
- // }
- }
- }
- }
- else if(launchFolderHasElxr) {
- root = ENV.wd = launchdir;
- // instanceoptions.splice( 0, 0, detected = { root })
- // instanceoptions.splice( 0, 0, detectfromroot(root))
- }
- else {
- // Ambiguous but assume script is standalone in the instanceroot.
- root = ENV.wd = launchdir;
- // must be a new install in the selectedinstance.root with script invoked from some random location.
- }
- }
- }
- else root = ENV.wd = process.env.wd
- }
- else {
- if(process.env.wd && process.env.wd !== processedArgs.root) throw 'wd and root must match.'
- root = ENV.wd = process.env.wd = processedArgs.root
- }
- var instanceroot = root
- ENV.PWD = ENV.wd
- ENV.NODE_ENV = processedArgs.node_env || (ENV.NODE_ENV && ENV.NODE_ENV.trim()) || 'development'
-
- console.log('----------------------------------------------------------------')
- console.log('version : ' + getVersion());
- console.log('process.env.wd : ' + process.env.wd)
- console.log('processedArgs.root : ' + processedArgs.root)
- console.log('----------------------------------------------------------------')
- console.log('wd :' + ENV.wd)
- process.chdir(ENV.wd) // Ensure cwd is the actual working dir.
- console.log('PWD :' + ENV.PWD)
- console.log('cwd : ' + process.cwd())
- console.log(`thisscriptdir = ${thisscriptdir}`)
- console.log('----------------------------------------------------------------')
-
- var cli = require('./cliverse')
-
- // throw 'initbreak'
-
- // We no longer use this Folder name may be different from instanceName
- // var detectfromroot = function(root){
- // return { root, node_env : path.basename(root), instanceName : path.basename( path.dirname(root) ) }
- // }
-
- // independent elxr cli operations
- var noprerequisites = {
- add : true, 'set-url' : true, 'repo-relocate' : true
- , remote : true, 'c' : true, 'h' : true
- , httpget : true, getuser : true
- , 'switch user' : true
- , 'switch' : true
- // , 'undefined' : true
- }
-
- var __default = {
- // Common baseline repos for all chess chessinstances.
- repos : [
- { repo : 'ember-masonry-grid' /*, branch : master*/ } // Default need not be specified.
- , { repo : 'bbhverse' }
- , { repo : 'clientverse' }
- , { repo : 'serververse' }
- , { repo : 'elxr' }
- , { repo : 'ember-searchable-select' }
- , { repo : 'loopback-component-jsonapi' }
- , { repo : 'loopback-jsonapi-model-serializer' }
- , { repo : 'loopback-connector-mysql' }
- , { repo : 'loopback-connector-ds' }
- , { repo : 'ember-service-worker' }
- , { repo : 'ember-service-worker-asset-cache' }
- , { repo : 'ember-service-worker-cache-fallback' }
- , { repo : 'ember-service-worker-index' }
- , { repo : 'ember-sw-client-route' }
- , { repo : 'global-this' }
- ]
- // Requires elevation only in windows
- , elevated : [ { repo : 'chess-server-lib', requiresElevation : true } ]
- , exludeMergeRepos : { }
- }; // PB : TODO -- Use initialized instance instead of __default everywhere.
-
- var cmds = {
- remote : {
- interpret() { return utils.assign_core( { keycase: true, arraymergetype : utils.assign_core.DISTINCT_UNION }
- , { cmd : processedArgs._.slice(0, 2).join(' '), runchoice : 'c' }) }
- , noprerequisites : true
- , independentcmd : true
- }
- , start : {
- interpret() { return utils.assign_core( { keycase: true, arraymergetype : utils.assign_core.DISTINCT_UNION }
- , { cmd : 'start', runchoice : 'c'
- , node_env : ENV.NODE_ENV, root : ENV.wd, instanceType : ENV.NODE_ENV
- })}
- }
- , 'remote refresh' : {
- // return a interpreted set of arguments for this cmd run context.
- interpret() { return utils.assign_core( { keycase: true, arraymergetype : utils.assign_core.DISTINCT_UNION }
- , { cmd : 'remote refresh', runchoice : 'c' }) }
- , noprerequisites : true
- , independentcmd : true
- // , requires : [ generateDependencies ]
- , toArgs : function( o ){
- return o
- }
- }
- , 'remote set-url' : {
- // return a interpreted set of arguments for this cmd run context.
- interpret() { return utils.assign_core( { keycase: true, arraymergetype : utils.assign_core.DISTINCT_UNION }
- , { cmd : 'remote set-url', runchoice : 'c' }) }
- , noprerequisites : true
- , independentcmd : true
- // , requires : [ generateDependencies ]
-
- }
- , 'remote add' : {
- // return a interpreted set of arguments for this cmd run context.
- interpret() { return utils.assign_core( { keycase: true, arraymergetype : utils.assign_core.DISTINCT_UNION }
- , { cmd : 'remote add', runchoice : 'c' }) }
- , noprerequisites : true
- , independentcmd : true
- // , requires : [ generateDependencies ]
- }
- , 'remote remove' : {
- // return a interpreted set of arguments for this cmd run context.
- interpret() { return utils.assign_core( { keycase: true, arraymergetype : utils.assign_core.DISTINCT_UNION }
- , { cmd : 'remote remove', runchoice : 'c' }) }
- , noprerequisites : true
- , independentcmd : true
- }
- , 'addcollaborator' : {
- // Usage :
- // elxr addcollaborator developer
- cmdFn : function(args){
-
- var __each = function(args){
- var collaborator = args.collaborator;
- if(!collaborator) {console.error('Error : collaborator required'); throw 'Error : collaborator required' }
- var repo = args.repo
- var remotename = 'chess'
- var repodef = selectedinstance.reposindexed[repo];
- if(!repodef) return
-
- var remotenames = (selectedinstance.selectedremotes || []).concat( selectedinstance.permanentremotes )
- var remotes = selectedinstance.reposerverinstances[selectedinstance.reposerver].remotes
- // console.log('-----------------------------------------------------')
- // console.log(repo)
- // // console.dir(remotes)
- // console.log('-----------------------------------------------------')
-
- // PB : TODO -- pick up remote definitions per repository...
- var reposmanfiest = utils.assign_core( { keycase : true, arraymergetype : utils.assign_core.DISTINCT_UNION }
- , {}, selectedinstance, loadmanifest( selectedinstance.root + '/' + repo
- , { utils, username : selectedinstance.username, instanceName : selectedinstance.instanceName
- , node_env : selectedinstance.node_env, reposerver : selectedinstance.reposerver }
- ))
-
- if( !reposmanfiest.error ) {
- var reporemotenames = remotenames.concat(reposmanfiest.selectedremotes || []).concat( reposmanfiest.permanentremotes || [] )
- var reporemotes = utils.assign(remotes, reposmanfiest.reposerverinstances[selectedinstance.reposerver].remotes)
- }
- else {
- var reporemotenames = remotenames
- var reporemotes = remotes
- }
-
- var remote = reporemotes[remotename]
- var reposerver = repodef.server || remote.server || selectedinstance.reposerver
-
- // selectedinstance.reposerverinstances[reposerver]
- var auth = repodef.username ? repodef : remote.username ? remote : selectedinstance.reposerverinstances[reposerver].username ? reposerver : selectedinstance;
- var httpoptions = new URL(reposerver);
- httpoptions = {
- hostname: httpoptions.hostname
- , protocol : httpoptions.protocol
- }
- httpoptions.username = auth.username // PB : TODO -- Handle remotes with different username. Also cache git credentials either globally or per repo...
- // PB : TODO -- handle credentials properly. cant mix and match passwords..
- httpoptions.password = (auth.password || selectedinstance.password || '') // PB : NOTE URL has some kind of a setter for password which converts to string "undefined" as password which is undesirable.. so we pre resolve and send.
- // console.log(___args)
- // PB : TODO -- Check valid collaborator before adding...
- return GITEA.repository.addcollaborator(httpoptions, repodef, remote.owner, collaborator).then( v => {
- if(v === true) {
- console.log('-----------------------------------------------------')
- console.log('addcollaborator successful for ' + repo + ' ' + collaborator)
- console.log('-----------------------------------------------------')
- }
- } )
- }
-
- var promises = []
- var repos = Object.keys(selectedinstance.reposindexed)
- repos.forEach(repo => { promises.push(__each({ repo, collaborator : args._[1]})) })
- return Promise.all(promises).then(pr => { console.log('addcollaborator done') })
- }
- }
- , 'isurlaccessible' : {
- noprerequisites : true
- , independentcmd : true
- , toArgs : function( o ){
- return { url : o._[1] }
- }
- }
- , resetgitconfig : {
- // return a interpreted set of arguments for this comd run context.
- // cmdFn : resetgitconfig,
- interpret() { return { cmd : 'resetgitconfig' } }
- , noprerequisites : true
- , independentcmd : true
- }
- , reinit : {
- // return a interpreted set of arguments for this comd run context.
- // cmdFn : reinit
- // ,
- interpret() { return { cmd : 'reinit' } }
- , noprerequisites : true
- , independentcmd : true
- , requiresElevation : true
- // , requires : [ generateDependencies ]
- }
- , init : {
- // return a interpreted set of arguments for this comd run context.
- // cmdFn : init
- // ,
- interpret() { return { cmd : 'init' } }
- , noprerequisites : true
- , independentcmd : true
- , requiresElevation : true
- // , requires : [ generateDependencies ]
- }
- , switch : {
- interpret() {
- return { cmd : subcommandlabels['switch'], runchoice : 'c', username : processedArgs._[2]
- , reposerver : __default.reposerver // .?.?.?
- }
- }
- , getPossiblePrompts(){ return { username : true, reposerver : 'http://git.bbh' } } // Requires only one argument...
- }
- , 'switch user' : {
- interpret() {
- return { cmd : subcommandlabels['switch'], runchoice : 'c', username : processedArgs._[2] }
- }
- , getPossiblePrompts(){ return { username : true } } // Requires only one argument...
- }
-
- // Flat linear structure. Every subcommand should have a top level 'label'
- , 'users' : {
- // default to the users list
- subcommands : { list : {} }
- , cmdFn : ()=>getCredentials() // default
- , cmd : 'users'
- , noprerequisites : true
- }
- , 'users list' : {
- cmdFn : ()=>getCredentials()
- , cmd : 'users list'
- , noprerequisites : true
- }
- , 'get-upstream' :{
- // cmdFn : op['get-upstream'] // default
- // ,
- cmd : 'get-upstream'
- , noprerequisites : true
- , independentcmd : true
- , interpret() { return { cmd : 'get-upstream' } }
- }
- , 'g' : {
- cmd : 'g'
- , noprerequisites : true
- }
- , 'link' : {
- 'cmd' : 'link'
- // , cmdFn : function( sourecpath, filename, dir ){
- // var afterTargetPath = (dir)=>{
- // console.dir(dir);
-
- // // var tasks = [
- // // () => {
- // // // Use junctions to avoid npm package issues
- // // var p = nodeShellExec('mklink', ['/J', filename, sourecpath], {
- // // inherit: true, shell: true
- // // , cwd : dir || instanceroot
- // // , env: ENV
- // // }).catch((e) => { console.error(e) })
- // // return p;
- // // }
- // // ];
-
- // }
-
- // // console.dir(ENV)
- // if(!dir) getCurrentFolder().then(afterTargetPath);
- // else afterTargetPath(dir)
-
- // }
- , cmdFn : function( cmdopts ){
- // sourecpathandfile should include name of file name
- targetfilename = cmdopts.targetfilename || processedArgs._[1]
- // console.log(processedArgs)
- if(!targetfilename) return Promise.reject('error no file provided for linking')
- targetdir = cmdopts.targetdir || ENV.wd
- sourecpathandfile = cmdopts.sourecpathandfile || path.normalize(`../../node_modules/chess-server-lib/common/models/${targetfilename}`)
-
- var tasks = [
- () => {
- // Use junctions to avoid npm package issues
- var p = nodeShellExec('mklink', [path.normalize(`${targetdir + '/' + targetfilename}`), sourecpathandfile], {
- inherit: true, shell: true
- , cwd : targetdir
- , env: ENV
- }).catch((e) => { console.error(e) })
- return p;
- }
- ];
- var etask = ()=>{
- return any(tasks).then(() => {
- fs.writeFileSync('run.done', 'success')
- }).catch(() => {
- fs.writeFileSync('run.done', 'error')
- })
- }
- etask.statuslog = statuslog
- etask.selectedinstance = selectedinstance
- etask.processedArgs = processedArgs
- etask.runtimestamp = runtimestamp
- etask.ENV = ENV
- return shell_verse.runElevated(etask)
- }
- , noprerequisites : true
- , privileged : true
- }
- }
-
- function interpret() {
- var interpreted = this
- // Non custom command has universal positiona args.
- processedArgs._[1] ? interpreted.instanceName = processedArgs._[1] : null;
- interpreted.runchoice = interpreted.cmd ? 'c' : null
- // interpreted.runchoice = processedArgs._[0] || selectedinstance.runchoice;
- // return clioverrides
- // cmds[cmd] = {
- // interpret() {
- // return Object.assign(clioverrides, { cmd, runchoice : 'c' })
- // }
- // , getPossiblePrompts(){ return { cmd, username : true, password : true,
- // instanceName : true, instanceType : true, reposerver : true } }
- // }
- // ... PB : TODO -- use cmd.toArgs
- // interpreted.cmd = this.cmd
- return interpreted
- }
-
- var interpretrun = function(){
- var cmd = processedArgs._[0];
- return cmds[cmd] && cmds[cmd].interpret ? cmds[cmd].interpret() : (function(){
- return cmds[cmd] = utils.assign(cmds[cmd], interpret.call({ cmd, toArgs : function( o ){
- // PB : TODO -- need to call this before selectedinstance is built. The instanceName is dependent on the specific cmd.
- return o } }))
- })()
- }
-
- var chessinstances = { current_run : {} };
- var clioverrides = interpretrun()
- clioverrides = utils.assign( { cmd : processedArgs._[0], node_env : ENV.NODE_ENV, root : ENV.wd, instanceType : ENV.NODE_ENV }
- , clioverrides, cmds[clioverrides.cmd].toArgs(processedArgs), { root : ENV.wd } )
- function acquirelocalinstances(selected){
- // utils.assign is used to cleanup duplicates... assuming it is probably not clean.
- // PB : TODO -- if require fails decipher instanceName from findlocalinstances using the root...
-
- try { var existinglocalinstances = require(path.normalize(selected.root + '/chessinstances.js'))}
- catch(e){
- console.warn( 'Local instances not found.' ) // This is not an error. A new fresh instance is probably being setup.
- var existinglocalinstances = { current_run : {}, error : true }
- }
-
- chessinstances = utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }, existinglocalinstances);
- // console.dir(chessinstances)
- return chessinstances
- }
-
- function findlocalinstances(chessinstances, instanceoptions){
- // We can expect a .elxr at each level in folder and 2 levels up. higher up we will find a shared instance chessinstances.js
- ['' /* instanceroot */, '../' /* instanceTypes or node_env */, '../../' /* instanceNames */].
- earlyreduce( ( reduced, p, i, a )=>{
- var localinstancesPath = `${instanceroot}/${p}.elxr`;
- if(existsSync( localinstancesPath )) {
- try {
- acquirelocalinstances( { root : `${instanceroot}/${p}` } )
- if(chessinstances.error) {
- delete chessinstances.error; delete chessinstances.e
- return {}
- }
- return Object.keys(chessinstances).earlyreduce( ( reduced, instanceName) => {
- return Object.keys(chessinstances[instanceName]).earlyreduce( (reduced, instanceType) => {
- // Scan to find a matching instanceroot
- if( path.normalize(chessinstances[instanceName][instanceType].root) === path.normalize( instanceroot) ) {
- instanceoptions.splice( 0, 0, chessinstances[instanceName][instanceType])
- return {
- value : chessinstances[instanceName][instanceType]
- , done : true
- };
- }
- })
- })
- }
- catch(e){
- return { }
- }
- }
- else return { }
- }
- )
- }
- // PB : TODO -- Move this to utils assign... as an array merge option.
- var mergeObjByKey = function(arrOfObjs, keyName) {
-
- var keyedDistinct = {}
- var distinctArrOfObjs = []
-
- arrOfObjs.forEach( o => {
- if(o) (keyedDistinct[o[keyName]] || (keyedDistinct[o[keyName]] = []) ).push(o)
- })
- Object.keys(keyedDistinct).forEach(key => {
- distinctArrOfObjs.push( utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }
- , ...keyedDistinct[key] ) ) // PB : TODO -- Shallow use utils.assign
- })
-
- return distinctArrOfObjs;
- }
-
- var cacheWriteInstanceConfig = function(chessinstances, instanceroot, selectedinstance){
- if(selectedinstance) {
-
- var instanceName = selectedinstance.instanceName ;
- var node_env = selectedinstance.node_env;
- // PB : TODO -- handle current_run switch...
- // console.dir(chessinstances)
- // PB : TODO -- We should be able to do simply merge at a higher level using assign
- chessinstances[instanceName][node_env].repos = mergeObjByKey(chessinstances[instanceName][node_env].repos || [], 'repo') ;
- chessinstances[instanceName][node_env].elevated = mergeObjByKey(chessinstances[instanceName][node_env].elevated || [], 'repo') ;
- }
- fs.writeFileSync(instanceroot + '/chessinstances.js', 'module.exports = ' + JSON.stringify(chessinstances, null, 2) + '', { 'flag': 'w' })
- }
-
- var loadmanifest = function( mpath, moverrides ){
- try {
- // PB : TODO -- pick up remote definitions per repository...
- return utils.assign_core( { keycase : true, arraymergetype : utils.assign_core.DISTINCT_UNION }
- , {}, require(path.normalize( mpath + '/repo-manifest'))( null, moverrides ))
-
- }
- catch(e){
- // console.warn(e);
- return utils.assign_core( { keycase : true, arraymergetype : utils.assign_core.DISTINCT_UNION }, { error : true, e }, moverrides)
- }
- }
- var getReconfirmAll = ()=>{return { instanceName : true, instanceType : true, reposerver : true, username : true, password : true } }
-
- if(clioverrides.reconfirm) {
- var reconfirm = getReconfirmAll()
- }
- else { var reconfirm = {}; }
-
- var shouldPrompt = function(k, possiblePrompts, target){
- return ((possiblePrompts[k] !== undefined && possiblePrompts[k] !== null) && target[k] !== possiblePrompts[k]
- || (possiblePrompts[k] === undefined || possiblePrompts[k] === null) && (target[k] === undefined || target[k] === null)
- || reconfirm[k])
- }
-
- var getPromptableAsyncPropDescriptor = function(propName, promptable){
- return {
- get (){
- return any( promptable.dependencies || [] ).then(()=>{
-
- return cli.prompt( promptable.choices, promptable.label, promptable.defaultchoice, promptable.selectedchoice ).then(propValue => {
-
- var asyncprop = Promise.resolve(propValue)
- if(promptable.interpret){
- asyncprop = promptable.interpret(propValue)
- }
-
- return asyncprop.then(
- ()=>{
- Object.defineProperty(this, propName, {
- value: propValue,
- writable: true,
- configurable : true,
- enumerable : true
- });
- return propValue
- }
- )
- })
- } )
-
- }
- // , set (propValue){
- // Object.defineProperty(this, propName, {
- // value: propValue,
- // writable: true, // PB : TODO -- Use this to fix value permanently until run is over.
- // configurable : true,
- // enumerable : true
- // })
- // return propValue;
- // }
- , configurable : true
- , enumerable : true
- }
- }
-
- var getBoundEachPrompt = function(target, possiblePrompts, promptables, choices, promptsfilter) {
- return function(prompts, k, i, a){
- // Reducer for all prompts on targets.
-
- // No local instances config found. We use a default initialized instance available in selectedinstance
- // Confirm those that were not supplied as user choices in runtime args and proceed to reattempt.
- // PB : TODO -- selectedinstance === __default check to prompt everything...
- if( shouldPrompt(k, possiblePrompts, target) ) {
- delete reconfirm[k];
- // console.log(k)
- // console.dir(possiblePrompts); //console.dir(target)
- prompts.push(async ()=>{
- // PB : NOTE -- Important in async cases when this {{target}} needs to be in the same state as when it was invoked.
- // We need to take a snapshot... Shallow.. !! If required deep should be used based on use case.
- // var asyncthis = Object.assign(this);
- // By default Latest altered state is provided which is an implicit reference directly.
- console.log('----------------------------------------------------')
- console.log(k)
- console.dir(choices[k])
- if(!promptables[k]) promptables[k] = {}
- promptables[k].choices = choices[k]
- Object.defineProperty(target, k, getPromptableAsyncPropDescriptor(k, promptables[k]));
- return await target[k]
- })
- }
- delete possiblePrompts[k] // PB : TODO We should keep this around instead of deleting so we can do a second pass if required.
- return prompts
- }
- }
-
- // unbound. Bind before using.
- var unbound_interactionpoints = function(selectedinstance){
- var self = this;
- var _interactionpoints = {
- get runchoice() {
- return {
- label :
- `Choose an option :
- d) Install the default chess instance.
- => elxr i chess node_env=development --default
- n) Create your custom new instance interactively
- => elxr i {{instanceName}} node_env={{environment}}
- i) Choose an instance and environment to install
- => elxr i {{instanceName}} node_env={{environment}}
- c) Choose a command to run ( pull, use, i, npmi ... ) <= pull
- => elxr {{cmd}} {{instanceName}} node_env={{environment}}
- h) Help
- q) Quit
- : `
- , choices : []
- , defaultchoice : 'c'
- , selectedchoice : self.target.runchoice
- , interpret : function(choice){
- var interpret_map = {
- d : function(){
- processedArgs._[0] = 'i'
- self.target.instanceName = processedArgs._[1] = processedArgs._[1] || 'chess'
- self.target.node_env = processedArgs.node_env = (ENV.NODE_ENV && ENV.NODE_ENV.trim()) || processedArgs.node_env || 'development'
- self.target.reposerver = 'https://git.bbh.org.in' // PB : TODO -- Read from global defaults...
- }
- , n : function() { processedArgs._[0] = 'i' }
- , i : function() {
- processedArgs._[0] = 'i'
- // processedArgs._[1] = self.target.instanceName || processedArgs._[1] || 'chess';
- }
- , c : async function() {
- // self here will always be the self.target.
- Object.defineProperty(self, 'cmd', getPromptableAsyncPropDescriptor('cmd', {
- label : `Enter cmd :
- p) pull
- Default <= p
- : `
- , defaultchoice : 'pull'
- }
- ));
-
- var cmd = await self.target['cmd'];
- if (!cmd || cmd === 'p') { self.target['cmd'] = processedArgs._[0] = 'pull' }
- else self.target['cmd'] = processedArgs._[0] = cmd
- return cmd;
- }
- , h : function() { console.log(elxr.help()); process.exit() } // PB : TODO -- Why do we need log.
- , q : function() { process.exit() }
- }
-
- if(Promise.resolve(choice) === choice){
- return choice.then( resolvedchoice => {
- return (interpret_map[choice] || interpret_map['c']).call(self.target)
- })
- }
- else return Promise.resolve( (interpret_map[choice] || interpret_map['c']).call(self.target) )
-
- // var __interpreter = interpret_map[choice] || interpret_map['c']
- // if(!choice) return interpret_map['c']() // This should not happen prompter should always give us a default choice.
- // if(interpret_map[choice]) __interpreter = interpret_map[choice];
-
- // return __interpreter.call(self.target)
- }
- }
- }
- , get instanceName() {
- return { label : `Enter Instance Name ( <= ${self.target.instanceName || 'chess'} ) : `
- , choices : self.choices['instanceName'], defaultchoice : 'chess'
- , selectedchoice : self.target.instanceName
- }
- }
- , get instanceType() {
- return { label : `Enter Instance Type ( <= ${self.target.instanceType || 'development'} ) : `
- , choices : self.choices['instanceType'], defaultchoice : 'development'
- , selectedchoice : self.target.instanceType || 'development'
- }
- }
- , get reposerver() {
- return { label : `Enter Repo Server Base Url ( <= ${self.target.reposerver || 'https://git.bbh.org.in'} ) : `
- , get choices() {
- // self.choices['reposerver'].forEach( rs => {
- // var __rs = new URL(rs);
- // __rs.hostname = __rs.host;
- // __rs.path = '/'
- // __rs.method = 'GET'
- // RESTAPI.get(__rs, function(data){ rs.accessibility = 'accessible' }
- // , function(error){ rs.accessibility = 'unaccessible' } )
- // })
- return self.choices['reposerver']
- }
- , set choices(cs) {
- return self.choices['reposerver'].concat([selectedinstance.reposerver]).concat(cs)
- }
-
- , defaultchoice : selectedinstance.reposerver || 'https://git.bbh.org.in'
- , selectedchoice : self.target.reposerver
- }
- }
-
- // , 'upstream-remote' : { label : `Enter Remote Name ( <= ${self.target['upstream-remote'] || 'chess'} ) : `
- // , get choices() {
- // var reposerver = self.target['reposerver']
- // var remotes = []
- // var trs = self.target.reposervers || []
- // trs.forEach(rs => {
- // if(rs.server === reposerver){
- // remotes.push(remote)
- // // PB : TODO -- Sort and display highest priority self.target.remotes.sort( )
- // }
- // })
- // return remotes
- // }
- // , set choices(addlchoices){
- // var reposerver = self.target['reposerver']
- // var remotes = []
- // var trs = self.target.reposervers || []
- // trs.forEach(rs => {
- // if(rs.server === reposerver){
- // remotes.push(remote)
- // // PB : TODO -- Sort and display highest priority self.target.remotes.sort( )
- // }
- // })
- // return remotes.concat(addlchoices)
- // }
- // , defaultchoice : 'userfork'
- // , selectedchoice : self.target['upstream-remote'] || 'userfork'
- // // Just using getters resolves dependencies..., dependencies : [ ()=>{ return self.target['reposerver'] } ]
- // }
- // , 'remote-type' : { label : `Enter Remote Type ( <= ${self.target['remote-type'] || 'public'} ) : `
- // , get choices() {
- // return ['external', 'public', 'private', 'github', 'unc']
- // }
- // , set choices(addlchoices){
- // var reposerver = self.target['reposerver']
- // var remotes = []
- // var trs = self.target.reposervers || []
- // trs.forEach(rs => {
- // if(rs.server === reposerver){
- // remotes.push(remote)
- // // PB : TODO -- Sort and display highest priority self.target.remotes.sort( )
- // }
- // })
- // return ['external', 'public', 'private', 'github', 'unc'].concat(addlchoices)
- // }
- // , defaultchoice : 'public'
- // , selectedchoice : self.target['remote-type'] || 'public'
- // // Just using getters resolves dependencies..., dependencies : [ ()=>{ return self.target['reposerver'] } ]
- // }
- // , 'selectedremotes' : { label : `Chose Remote Names ( <= ${self.target['selectedremotes'] || 'chess'} ) : `
- // , get choices() {
- // var reposerver = self.target['reposerver'] // PB : TODO -- We need options to work with multiple selected reposervers at the same time..
- // var remotenames = []
- // Object.entries(self.target.remotes || []).forEach( ([rname, r]) => {
- // if(r.server === reposerver && r.accessibility.find( self.target[ 'remote-type' ] )){
- // remotes.push(rname)
- // // PB : TODO -- Sort and display highest priority self.target.remotes.sort( )
- // }
- // })
- // // PB : TODO -- Need to generate all possible permuted choices nP( 1 -> n )
- // // Currenty handles all combinations without any priority order.
- // var _remotechoices = [] // Array of arrays of choices.
- // remotenames.forEach( r => {
- // var __rcs = []
- // _remotechoices.forEach(rc => {
- // __rcs.push( rc.concat(r) )
- // })
- // Array.prototype.push.apply( _remotechoices, __rcs)
- // _remotechoices.push(r)
- // })
-
- // return _remotechoices
- // }
- // , defaultchoice : ['userfork', 'chess'] // userfork and chess should imlicitly point to userfork-external and chess-external when external connectivity is dtetected.
- // , selectedchoice : self.target['selectedremotes'] || ['userfork-external', 'chess-external']
- // // , defaultchoice : { 'userfork-external' : self.target.remotes['userfork-external'] , 'chess-external' : self.target.remotes['chess-external'] }
- // // , selectedchoice : self.target['selectedremotes'] || { 'userfork-external' : self.target.remotes['userfork-external'] , 'chess-external' : self.target.remotes['chess-external'] }
-
- // // Info : Just using getters resolves dependencies..., dependencies : [ ()=>{ return self.target['reposerver'] } ]
- // }
- , get username() { return { label : `Enter User Id for ${self.target.reposerver} ( <= ${self.target.username || 'chess'} ) : `
- , choices : self.choices['username'], defaultchoice : self.target.username || 'chess', selectedchoice : self.target.username } }
- , get password() { return { label : `Enter Password for ${self.target.username} @ ${self.target.reposerver} ( <= ${self.target.password || ''} ) : `
- , choices : self.choices['password'], defaultchoice : '***', selectedchoice : self.target.password } }
- , get email() { return { label : `Enter Email for ${self.target.username} @ ${self.target.reposerver} ( <= ${self.target.email || ''} ) : `
- , choices : self.choices['email'], defaultchoice : 'guest@bbh.org.in', selectedchoice : self.target.email } }
-
- }
- return _interactionpoints
- }
-
- var get_interactionpoints_template = function(){
- return {
- instanceName : { label : `Enter Instance Name ( <= ${this.target.instanceName || 'chess'} ) : `
- , choices : this.choices['instanceName'], defaultchoice : 'chess'
- , selectedchoice : this.target.instanceName
- }
- }
- }
-
- var pending = (function() {
-
- // PB : TODO -- Override cli prefs and call with undefined selectedinstance only if there is no cli overide or selection.
- acquirelocalinstances( { root : ENV.wd } )
-
- function createLocalChessInstance( cfg ){
- // return createInstance(cfg)
- reconfirm = getReconfirmAll()
- var inst = {};
- var __new = Object.assign({}, __default, cfg)
- inst[cfg.node_env] = __new; return inst;
- }
-
- if(chessinstances.error) {
- delete chessinstances.error
- // use installchoices only when we don't find chessinstances.
- try { var installchoices = require(path.normalize(ENV.wd + '/installchoices.js'))
- var instanceName = installchoices.instanceName;
- var reposerver = installchoices.reposerver
- var node_env = clioverrides.node_env || installchoices.node_env
- var __instance = (chessinstances[instanceName] = chessinstances[instanceName] || createLocalChessInstance( {
- instanceName, node_env, root, reposerver /* promptkeys['reposerver'] */ } ))[node_env];
- chessinstances['current_run'] = { instanceName, node_env, reposerver, root }
-
- // if(path.normalize(selectedinstance.root) !== path.normalize(chessinstances[selectedinstance.instanceName][selectedinstance.node_env].root)) {
- // throw "instanceName and instanceType specified doesn't match whats already present do you want to continue " + chessinstances[instanceName][node_env].root + ' does not match ' + selectedinstance.root
- // }
- // chessinstances[installchoices.instanceName] = chessinstances[installchoices.instanceName] || {}
- // chessinstances[installchoices.instanceName][installchoices.node_env || clioverrides.node_env] = installchoices
- __instance.username = installchoices.username = installchoices.gitUser
- __instance.email = installchoices.email = installchoices.gitEmail
- }
- catch(e){
- console.warn(e)
- console.warn( 'Install choices not found. WIll prompt for choices' ) // This is not an error. A new fresh instance is probably being setup.
- throw 'PROMPT FOR INSTLL CHOICES TO BE IMPLEMENTED'
- // chessinstances[installchoices.instanceName] = chessinstances[installchoices.instanceName] || {}
- // chessinstances[installchoices.instanceName][installchoices.node_env || clioverrides.node_env] = installchoices
- }
- }
-
- // PB : chessinstances.js update if we are switching repo servers, username etc... Myabe always refresh on each run.
- // root location for instance swithc ?? should not be allowd...
- var instanceName = clioverrides.instanceName || chessinstances.current_run.instanceName
- var instance = {}
- function oninstanceName(instanceName) {
-
- var instance_ = chessinstances[instanceName]
- if(instance_) {
- instance = instance_[clioverrides.node_env || chessinstances.current_run.node_env] || utils.assign(chessinstances['current_run'], clioverrides, { instanceName })
- }
- else instance = utils.assign(chessinstances['current_run'], clioverrides, { instanceName })
- // PB : TODO We should promt like instanceName create a new one instad of undefined ??
- return instance
- }
- if(!instanceName) {
- var interactionpoints = (function() { return { instanceName : get_interactionpoints_template.bind({target : {}, choices : {}})()['instanceName'] } })()
- var eachPrompt = getBoundEachPrompt(instance, utils.assign({}, interactionpoints), interactionpoints, {})
- var pendingtasks = any( Object.keys(interactionpoints).reduce(eachPrompt, []) ).then(()=>{
- return oninstanceName(instance.instanceName)
- })
- }
- else {
- var pendingtasks = Promise.resolve(oninstanceName(instanceName))
- }
-
- return pendingtasks.then((instance)=>{
-
- // PB : TODO -- Embed defaults in the build instead of inlining here...
-
- // PB : TODO -- All these values instanceName, node_env, username, reposervers should come from instace overridden by a finalized cliovrrides...
- // Also attepmt to load preconfig specially for new chessinstances from ../chess-config/...
-
- var __repo_manifest_elxr = loadmanifest( instance.root + '/elxr'
- , { utils, username : instance.username /** ??? TODO */ , instanceName : instance.instanceName /** ??? TODO */
- , node_env : clioverrides.node_env, reposerver : instance.reposerver /** ??? TODO */ } // options
- )
-
- if(__repo_manifest_elxr.error) console.log(__repo_manifest_elxr.e.message)
-
- var instance_specific_config_manifest = loadmanifest( instance.root + `/${instance.instanceName}-config-${clioverrides.node_env}`
- , { utils, username : instance.username /** ??? TODO */ , instanceName : instance.instanceName /** ??? TODO */
- , node_env : clioverrides.node_env, reposerver : instance.reposerver /** ??? TODO */ } // options
- )
- if(instance_specific_config_manifest.error) console.log(instance_specific_config_manifest.e.message)
- delete __repo_manifest_elxr.error
- delete __repo_manifest_elxr.e
- delete __repo_manifest_elxr.utils
- delete instance_specific_config_manifest.error
- delete instance_specific_config_manifest.e
- delete instance_specific_config_manifest.utils
-
- // selectedinstance is not complete until we load in the following order of priority...
- // 1) inline defaults
- // 2) elxr defaults & manifest
- // 3) ../chess-config/... defaults TODO
- // 4) local cached lastrun or cli selected instance from chessinstances
- // 5) instance specific manifest from {{instanceName}}-config-{{node_env}} ( can work with stale or on demand currently pulled updated version )
- // 5.5) runconfig ??? lastrun vs fixed runconfig for repetitive stuff. TODO -- runconfig overrides lastrun...
- // 6) cli overrides from command promnt
- // 7) promted selections and reconfirms.
- var __pvt = utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }
- , __default, __repo_manifest_elxr, instance
- , { root : ENV.wd, node_env : ENV.NODE_ENV
- , get instanceName() {
- // FInalize to prevent subsequent overrides...
- return instance.instanceName
- }
- }
- , instance_specific_config_manifest
- , clioverrides
- )
- // console.dir(__pvt)
- // Unmergable server overrides
- Object.keys(instance_specific_config_manifest).forEach( key => {
- __pvt[key] = instance_specific_config_manifest[key]
- })
-
- var __pub = utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }, {}, __pvt
- // Finalized immutables.
- // We don't need pub to prevent immutability.
- , { get root(){ return __pvt.root }
- , get node_env(){ return __pvt.node_env }
- , get instanceName() { return __pvt.instanceName }
- , get reposindexed(){
- // PB : TODO -- Implement -- One time gettor and then cached...
- var indexed = {}
- this.repos.forEach(r => { indexed[r.repo] = r })
- this.elevated.forEach(r => { indexed[r.repo] = r })
- Object.defineProperty(this, 'reposindexed', { value: indexed, writable: false });
- return indexed;
- }
- , set root(val){
- throw 'root changes not yet supported'
- // if the root changes while we r invoked from somewhere else... we need to reload everything from that location...
- if(__pvt.root === val){
-
- return __pvt.root = val
- }
- else __pvt.root
- }
- }
- )
-
- chessinstances[__pub.instanceName] = chessinstances[__pub.instanceName] || {}
- chessinstances[__pub.instanceName][__pub.instanceType] = __pub
- // PB : TODO initinstances is now obselete relocated here
- // Upgrade old formats..
- if(__pub?.repos && !__pub?.repos[0]?.repo) {
- console.warn('repo manifest has obsolete format. Attempting upgrade.')
- __pub.repos = __pub.repos.map(function(repo){ return { repo } })
- }
- if(__pub?.elevated && !__pub?.elevated[0]?.repo) {
- console.warn('elevated repo manifest has obsolete format. Attempting upgrade.')
- __pub.elevated = __pub.elevated.map(function(repo){ return { repo } })
- }
-
- // Config from server always override merges into selection except for the current selection.
- // PB : TODO -- utils.assign Array merges are non-distinct...
- // chessinstances[instanceName][node_env] = selectedinstance;
- // chessinstances[selectedinstance.instanceName][selectedinstance.node_env] = selectedinstance;
-
- // PB : TODO -- We should probably write the new server config also...
- // PB : TODO -- Attempt first one that is available and online...
- // PB : TODO -- system select may need a reconfirm...
- if(!__pub.reposerver) reconfirm['reposerver'] = true
- __pub.reposerver = __pub.reposerver || __pub.reposervers[0]
-
- cacheWriteInstanceConfig(chessinstances, __pub.root, __pub )
-
- Object.defineProperty(__g, 'selectedinstance', {
- get(){ return __pub}
- , set( inst ){
- utils.assign(__pub, inst); return __pub
- }
- })
- return __pub;
- })
-
- })()
-
- return pending.then(( selectedinstance)=>{
-
- var op = {
- 'h': () => { console.log(elxr.help()); return '-h' }
- , 'clean' : () => {
- // delete all node_module folders and links.
- var tasklist = [];
- dirs( (repodir)=> tasklist.push(getShellTask('rm',['-rf', 'node_modules'], { cwd : instanceroot + '/' + repodir.name })()), instanceroot )
- return Promise.all(tasklist)
- }
- // , 'undefined': () => { return acquireChoices( {} ); }
- , 'isurlaccessible' : function(args){
- // args.url... .. PB : TODO -- Extract from url
- args = args || {}
- var u = new URL(args.url || selectedinstance.reposerver)
- console.log(u)
- var httpoptions = { hostname : u.host || 'git.bbh.org.in', protocol : u.protocol || 'https'
- , method : 'GET'
- , username : args.username || selectedinstance.username || 'chess'
- , password : selectedinstance.password || 'chess'
- }
- httpoptions.path = args.path || `/`
- return new Promise(function( resolve, reject ){
- RESTAPI.get(httpoptions, function(){
- console.log('URL is accessible')
- resolve(true)
- }, function(){
- console.error('URL is not accessible')
- resolve(false)
- })
- })
- }
- , 'reset': () => {
-
- // Reset NPM packages semver so major versions can be updated.
-
- const fs = require('fs')
- const wipeDependencies = (__package) => {
- const file = fs.readFileSync(__package + '/package.json')
- const content = JSON.parse(file)
- for (var devDep in content.devDependencies) {
- if (content.devDependencies[devDep].match(/\W+\d+.\d+.\d+-?((alpha|beta|rc)?.\d+)?/g)) {
- content.devDependencies[devDep] = '*';
- }
- }
- for (var dep in content.dependencies) {
- if (content.dependencies[dep].match(/\W+\d+.\d+.\d+-?((alpha|beta|rc)?.\d+)?/g)) {
- content.dependencies[dep] = '*';
- }
- }
- fs.writeFileSync(__package + '/package.json', JSON.stringify(content))
- }
-
- var repos = ['client'];
- // repos = gitRepos;
- repos.forEach(wipeDependencies)
- // if (require.main === module) {
- // } else {
- // module.exports = wipeDependencies
- // }
-
- }
- , 'upgrade': () => {
- console.log('upgrade.......')
- var tasks = [
- () => {
- var p = nodeShellExec('npm', ['i', '-g', 'npm-upgrade'], {
- inherit: true, shell: true
- , env: ENV
- }).catch((e) => { console.error(e) })
- p.position = 1;
- console.log('One')
- return p;
- }
- , () => {
- var p = nodeShellExec('npm', ['cache', 'clean', '-f'], {
- inherit: true, shell: true
- , env: ENV
- }).catch((e) => { console.error(e) })
-
- p.position = 2;
- console.log('Two')
- return p;
- }
- , () => {
- var p = nodeShellExec('npm', ['install', '-g', 'n'], {
- inherit: true, shell: true
- , env: ENV
- }).catch((e) => { console.error(e) })
- p.position = 3;
- console.log('Three')
- return p;
- }
- , () => {
- var p = nodeShellExec('n', ['latest'], {
- inherit: true, shell: true
- , env: ENV
- }).catch((e) => { console.error(e) })
- p.position = 4;
- console.log('Four')
- return p;
- }
- ]
- any(tasks)
- console.log('.......done')
- console.log('Running exlr upgrade in : ' + path.dirname(__dirname))
- console.log('Currently only upgrades ember : ' + path.dirname(__dirname));
- console.info('Uninstalling existing ember globally');
- var step1 = nodeShellExec('cmd', ['/c', 'npm', 'uninstall', '-g', 'ember-cli'], {
- stdio: ['pipe', process.stdout, process.stderr],
- inherit: true,
- shell: true,
- cwd: path.dirname(__dirname),
- env: ENV
- })
- step1.on('close', () => {
- console.info('Installing ember globally');
- var step2 = nodeShellExec('cmd', ['/c', 'npm', 'install', '-g', 'ember-cli'], {
- stdio: ['pipe', process.stdout, process.stderr],
- inherit: true,
- shell: true,
- cwd: path.dirname(__dirname),
- env: ENV
- })
-
- step2.on('close', () => {
- nodeShellExec('cmd', ['/c', 'ember', '--version'], {
- stdio: ['pipe', process.stdout, process.stderr],
- inherit: true,
- shell: true,
- cwd: path.dirname(__dirname),
- env: ENV
- })
- })
- })
- }
- , 'push': () => {
- if (!processedArgs._[1]) { console.error('push all not supported. Specify repo name'); return }
- // init remote bare from local
- // pushandinitremotebare
- // https://www.jeffgeerling.com/blogs/jeff-geerling/push-your-git-repositories
- // connect to repo server -- net use 172.16.0.27
- // cd 172.16.0.27/repos/
- // mkdir repo.git
- // cd repo.git
- // git init --bare
- // cd localrepo
- // git remote rename origin githubclone
- // git remote add origin //172.16.0.27/repos/repo.git
- // git push origin master
-
- var repo = processedArgs._[1];
- var remote = 'origin'
-
- var sequentialTaskShellCommands = [];
- if (!existsSync(`Z:/${repo}.git`)) {
- sequentialTaskShellCommands = [
- // ['net', ['use', 'Z:', selectedinstance.reposerver.replace('/','\\')], {
- // inherit : true, shell: true
- // , env: ENV
- // }]
- ['pwd', { cwd: 'Z:', inherit: true }]
- , ['mkdir', [`${repo}.git`], {
- cwd: `Z:`
- , inherit: true, shell: true
- , env: ENV
- }]
- , ['pwd', { cwd: `Z:/${repo}.git`, inherit: true }]
- , ['git', ['init', '--bare'], {
- cwd: `Z:/${repo}.git`
- , inherit: true, shell: true
- , env: ENV
- }]
- // PB : TODO -- Do this conditionally only...
- , ['git', ['remote', 'rename', remote, 'githubclone'], { cwd: `${instanceroot + '/' + repo}` }, (err) => {
- console.log(`Ignoring ${remote} rename error : ` + err); return true; //return true to continue.
- }] // PB ; Todo -- new repositories created locally will not have origin. Handle this failure.
- , ['git', ['remote', 'add', remote, `${selectedinstance.reposerver}/${repo}.git`], { cwd: `${instanceroot + '/' + repo}` }]
- // PB : TODO -- If threre is a gitbubclone origin
- // Set the master to pull from the local repo.
- ]
- if (!existsSync(`Z:`)) {
- sequentialTaskShellCommands.splice(0, 0, ['net', ['use', 'Z:', selectedinstance.reposerver.replace(/\//gm, '\\')], {
- inherit: true, shell: true
- , env: ENV
- }])
- console.warn('Adding network drive z: for repo server. ' + sequentialTaskShellCommands[0])
- // throw 'done'
- }
- }
-
- sequentialTaskShellCommands.push(['git', ['push', remote, 'master'], { cwd: `${instanceroot + '/' + repo}` }])
- // console.dir(sequentialTaskShellCommands);
-
- var tasks = [];
- sequentialTaskShellCommands.forEach(shellcmd => {
- // console.log(shellcmd)
- tasks.push(() => {
-
- var p = nodeShellExec.apply(null, shellcmd.slice(0, 3)).catch((e) => { if (shellcmd[3]) { return shellcmd[3]() } else { console.error(e); } })
- return p;
- })
- })
- any(tasks);
-
- }
- , 'is-git-repo': (dir) => {
- return nodeShellExec('git', ['-C', dir.name, 'rev-parse'], { stdio: 'ignore' })
- }
- // git remote equivalents...
- // git branch --set-upstream-to=elixir-unc/master master
- // git push --set-upstream elixir-unc branch..
- , 'remote': (args) => {
-
- // Subcommands!
- // PB : TODO -- we can now pass in hypehnated args...
- if(!processedArgs.v) return false; // Only -v is supported presently..
-
- var serial_perform = (repo) => {
- var options = { cwd: instanceroot + '/' + repo }
-
- return [
- ['git', ['remote', '-v'], options]
- ]
- }
-
- var x = (args) => {
- var tasq = () => {
- // console.log(args)
- return nodeShellExec.apply(null, args).catch(e => {
- // We continue on failure.
- console.error(tasq.toString())
- })
- }
- tasq.toString = function(){
- return JSON.stringify(args)
- }
- return tasq;
- }
-
- var perform = (dir) => {
- return any(serial_perform(dir.name).map(x))
- }
-
- dirs(perform)
- }
-
- , 'remote set-url': (args) => {
- // git remote set-url elixir-unc //10.10.5.60/gitrepo/chess/bbhverse
- var __args = {
- remotename : args.remotename || processedArgs._[2]
- , url : args.url || processedArgs._[3]
- , branch : args.branch || processedArgs._[4] || 'master'
- }
- __args.upstream = args['upstream'] || processedArgs._[5] || `${__args.remotename}/${__args.branch}`
-
- // pushable doesn't mean the remote doesn't support being pushed to.
- // Over here it just means we are disabling pushing to that remote by setting the push portion of the url the a junk remote called no-pushing.
- // PB : TODO -- change this to enablepushing.
- // By default pushing should be disabled. Also developers permissions on the remote is a secondary check for pushing.
- var pushable = processedArgs.pushable || (args.remote ? args.remote.pushable : false) || args.pushable;
- var remotename = __args.remotename
- var url = __args.url
- var branch = __args.branch
- var upstream = __args.upstream
- console.log('remotename : ' + args.remotename)
-
- var serial_perform_git_seturl = (reponame) => {
- var options = { cwd: instanceroot + '/' + reponame }
- // console.log(reponame)
- var serialcmds = [
- ['git', ['remote', 'set-url', remotename, url + '/' + reponame], { cwd: instanceroot + '/' + reponame }]
- ]
- if (!pushable) {
- serialcmds.push(['git', ['remote', `set-url`, '--push', remotename, 'no-pushing'], { cwd: instanceroot + '/' + reponame }])
- }
-
- var requiresElevation = selectedinstance.elevated.find( r => r.repo === reponame)
- serialcmds.push(['git', ['pull', remotename, branch], { cwd: instanceroot + '/' + reponame, requiresElevation }]) // requires elevation based on repo
- serialcmds.push(['git', ['branch', `--set-upstream-to=${upstream}`, branch], { cwd: instanceroot + '/' + reponame }])
-
- return serialcmds
- }
-
- var x = (xargs) => {
- var tasq = () => {
- // console.log(args)
- var task = (onsuccess, onerror)=>{
- return ()=>{
- return nodeShellExec.apply(null, xargs).then( onsuccess ).catch(e => {
- // We continue on failure.
- // No such remote
- if(e.messages[0].search('No such remote') > -1 ){
- console.log(remotename + ' Remote doesnt exist will add.')
- return op['remote add']( args )
- }
- console.error(util.inspect(e) + ' ' + tasq.toString())
- })
- }
- }
-
- if(xargs[2].requiresElevation){
- if(!isElevated) {
- var etask = task( ()=>{ return remotename } )
- etask.statuslog = statuslog
- etask.selectedinstance = selectedinstance
- etask.processedArgs = processedArgs
- etask.runtimestamp = runtimestamp
- etask.ENV = ENV
- return shell_verse.runElevated(etask)
- }
- }
- return task(
- () => { fs.writeFileSync('run.done', 'success')}
- , () => { fs.writeFileSync('run.done', 'error') }
- )()
- }
- tasq.toString = function(){ return JSON.stringify(args) }
- return tasq;
- }
- var perform_git_seturl = (dir) => {
- return op['is-git-repo'](dir).then((logEntry) => {
- return any(serial_perform_git_seturl(dir.name).map(x))
- }).catch((e) => {
- console.log(e + ' Failed not recognized as a git repo : ' + dir.name)
- })
- }
-
- if(args.dir) return perform_git_seturl(args.dir) // do this for a specific repo
- else return dirs(perform_git_seturl) // Do for all
- }
- // , 'reset' : ()=>{
- // PB : TODO -- Cant have 2 resets !! | There is an npm reset that should be bundled.
- // // Reset the whole installation pertaining to this elxr folder.
-
- // }
-
- , 'remote exists': (args) => {
- var __args = {
- remotename : args.remotename|| processedArgs._[2]
- , url : args.url || processedArgs._[3]
- , branch : args.branch || processedArgs._[4]
- }
-
- var options = args.repo ? { cwd: instanceroot + '/' + args.repo } : {}
-
- // PB : TODO -- We should evaluate a whole list of remotes passed in from args instead of just one.
- var commands = [
- // git remote -v| while read remote; do "${remote#origin/}" "$remote"; done
- // ['git', ['remote', '-v', '| while read remote; do "${remote#origin/}" "$remote"; done'], utils.assign( {
- [
- // 'git', ['remote', '-v']
- 'git', ['config', `remote.${__args.remotename}.url`]
- , utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }
- , {
- benign : args.benign, ignorefailures : args.ignorefailures,
- evaluateResult : function(err, result){
- return [
- +result.code === 0
- , result
- ]
- }
- } , options) ]
- ]
- var mapped = commands.map(getshelltask) //.map( p => p.catch(e => e)) // Handle errors later.
- mapped.push( function(prevserialtaskresult) {
- var pt = [false , null]
- if(prevserialtaskresult.error) {
- // Previous task has failed.
- pt[1] = prevserialtaskresult.error[1]
- }
- else pt = prevserialtaskresult;
-
- console.dir(prevserialtaskresult)
- return getshelltask(['git', ['ls-remote', `${__args.url}`], utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }
- , {
- benign : args.benign, ignorefailures : args.ignorefailures,
- evaluateResult : function(err, result){
- if(+result.code !== 0) return [pt, [ false, result]]
- // var hasfailed = /^fatal: .*/.test(result.messages.join(' '))
- return [pt, [true, result] ]
- }
- } , options) ])().catch( e => {
- console.error(e)
- return [pt, [ false, null]]
- })
- })
- return any(mapped, true, false, { accumulatedresults : [] }).then( allresolved => {
-
- console.dir(allresolved)
- !allresolved[0][0] && !allresolved[1][0] ? console.log('was not added as a remote and url is currently inaccessible.')
- : allresolved[0][0] && allresolved[1][0] ? console.log('was added as a remote and url is accessible.')
- : allresolved[0][0] && !allresolved[1][0] ? console.log('was added as a remote but url is currently inaccessible.')
- : console.log('was not added as a remote but url is currently accessible.')
-
- return !allresolved[0][0] && !allresolved[1][0] ? [0, 0]
- : allresolved[0][0] && allresolved[1][0] ? [1, 1] // => was added as a remote and url is accessible.
- : allresolved[0][0] && !allresolved[1][0] ? [1, 0] // => was added as a remote but url is currently inaccessible.
- : [0, 1] // => was not added and remote and url is accessible.
- }).catch(e => {
- console.log(e)
- return [0, 0]
- })
- }
-
- , 'remote refresh' : (args) => {
-
- var url = args._[2] || selectedinstance.reposerver
- return op['isurlaccessible']({ url }).then(( baccess )=>{
- if(!baccess) {
- var reposerver = selectedinstance.reposerverinstances[url]
- if(!reposerver.external) {
- // Internal server not accessible ask to switch to external.
- console.log('Internal server not accsssible. Will switch to external')
- // console.dir(selectedinstance.reposerverinstances)
- url = reposerver.externalreposervers[0]
- }
- else {
- console.log('External server not accsssible. Will switch to interal')
- // PB : TODO -- Prompt and then switch.
- url = reposerver.internalreposervers[0]
- }
- return op['isurlaccessible']({ url }).then(( baccess )=>{
- if(!baccess) {
- console.log('Neither internal nor external servers are accessible.... Presenting choices...')
- console.log('PB : TODO -- Present appropriate choices...')
- // Redo whole command...
- }
- return dorefresh()
- })
- }
- dorefresh()
- console.log(baccess)
- }).catch((err)=>{ console.error(err) })
-
- function dorefresh(){
- // if(args.url === selectedinstance.reposerver) {
- // // No change. Just refresh with same url anyway. We do not know which of the repos is not up to date and is an outlier.
- // }
- // else {
- // // Also refreshs anyway
- // }
-
- args = args || {}
- var __args = {
- url : args.url || processedArgs._[2] || selectedinstance.reposerver
- // , branch : args.branch || processedArgs._[4]
- }
-
- var serial_perform = (repo) => {
- // if(repo === 'setup' || repo === 'chess-server-lib'){
-
- var options = { cwd: instanceroot + '/' + repo }
- console.log('serial_perform : ' + repo)
-
- return [
- ['git', ['remote', '-v'], options]
- ]
- // }
- // else return []
- }
-
- var x = (args) => {
-
- // console.log('MapX was called')
- var tasq = () => {
- console.log('--------------------------')
- console.log(args)
- return nodeShellExec.apply(null, args).catch(e => {
- console.log('caught error ------------------------' + JSON.stringify(e))
- // We continue on failure.
- console.error(tasq.toString() + args[2].cwd)
- })
- }
- tasq.toString = function(){
- // console.log('sadfasdf------------------------')
- return JSON.stringify(args) // PB : TODO -- Is this needed ?
- }
- return tasq;
- }
-
- var remotenames = (selectedinstance.selectedremotes || []).concat( selectedinstance.permanentremotes )
- var remotes = selectedinstance.reposerverinstances[__args.url].remotes
-
- // use the repo manifest to create missing remotes.
- function perform_remote_refresh(dir){
- var repo = dir.name
- var ___args = {url : __args.url, repo}
- if(repo === '.elxr') return Promise.resolve(true)
- if(repo === '.git') return Promise.resolve(true)
- if(repo === 'Downloads') return Promise.resolve(true)
- // if(repo !== 'elixir-config-development') {
-
- // PB : TODO -- pick up remote definitions per repository...
- var reposmanfiest = utils.assign_core( { keycase : true, arraymergetype : utils.assign_core.DISTINCT_UNION }
- , {}, selectedinstance, loadmanifest( selectedinstance.root + '/' + repo
- , { utils, username : selectedinstance.username, instanceName : selectedinstance.instanceName
- , node_env : selectedinstance.node_env, reposerver : selectedinstance.reposerver }
- ))
- // }
- // else {
- // var reposmanfiest= { error : 'Already lodeded elixir-config-development'}
- // }
-
-
- if( !reposmanfiest.error ) {
- var reporemotenames = remotenames.concat(reposmanfiest.selectedremotes || []).concat( reposmanfiest.permanentremotes || [] )
- var reporemotes = utils.assign([], remotes, reposmanfiest.reposerverinstances[___args.url].remotes)
- }
- else {
- var reporemotenames = remotenames
- var reporemotes = remotes
- }
-
- return (()=>{
- var promises = []
-
- return any(serial_perform(repo).map(x)).then(()=>{
- var branch = args.branch || 'master' // PB : TODO -- Dont default branch here.. Let each repos current state define this... ???
- reporemotenames.forEach(rm =>{
- console.log('Processing : ' + rm + ' ' + dir.name)
- // if(repo != 'elxr') return //test
- // if(repo != 'elixir-config-development') return //test
- // if(repo != 'elixir-server') return //test
-
- promises.push( ((rm) => {
- var remotename = rm
- var repodef = selectedinstance.reposindexed[repo];
- if(!repodef) return
-
- // if( repo !== 'cihsr-data') return
-
- // username and owner are two different things. The repository (possibly forked) under the username is when the {{owner}} portion of the repo path is the same as the user.
- // When one user or organiztion shares repos with others through a team without a fork a different user has access to a different {{owner}} path repo.
- var remote = reporemotes[remotename]
- // console.log(repodef)
- // console.log(remote)
- var owner = repodef.owner || remote.owner || defaultRepoOwner // should be based on the server, remote and repo combination.
-
- var reposerver = repodef.server || remote.server || selectedinstance.reposerver
- // selectedinstance.reposerverinstances[reposerver]
- var auth = repodef.username ? repodef : remote.username ? remote : selectedinstance.reposerverinstances[reposerver].username ? reposerver : selectedinstance;
-
- var httpoptions = new URL(reposerver);
- httpoptions = {
- hostname: httpoptions.hostname
- , method: 'GET'
- , protocol : httpoptions.protocol
- }
- httpoptions.username = auth.username // PB : TODO -- Handle remotes with different username. Also cache git credentials either globally or per repo...
- httpoptions.password = (auth.password || selectedinstance.password || '') // PB : NOTE URL has some kind of a setter for password which converts to string "undefined" as password which is undesirable.. so we pre resolve and send.
- // console.log(___args)
- var repourl = reporemotes[remotename].url + '/' + repo
- // PB : TOOD -- USE GITTEA API
- // op['isurlaccessible']({ url : repourl }).then((baccess)=>{
-
- return GITEA.repository.exists( httpoptions, repo, owner ).then((bexists)=>{
- console.log(remotename + ' ' + repourl)
- if(bexists) {
- console.log(remotename + ' ' + repourl + ' is accessible')
- return ((remotename)=>{
- return op['remote set-url']( { dir, remotename, url : reporemotes[remotename].url, remote
- , repodef : selectedinstance.reposindexed[repo] } ) // PB : TODO -- Handle add-url
- })(remotename)
- // get upstream remote choice from user !!!
- // PB : TODO -- Set upstream , ['git', ['branch', `--set-upstream-to=${remotename}/${branch}`, branch], { cwd: instanceroot + '/' + repo }]
- }
- }).catch(e => {
- console.warn(remotename + ' ' + repourl + ' ' + e) // Skip nonexistant not necessarily quietly
- })
- })(rm)
- )
- })
- return Promise.all(promises).then( accessibleremotes => {
- while(!accessibleremotes[accessibleremotes.length -1] && accessibleremotes.length) accessibleremotes.splice(accessibleremotes.length -1, 1)
-
- if(accessibleremotes.length) {
- ((remotename)=>{
- var upstream = `${remotename}/${branch}`
- var remote = reporemotes[remotename]
- return op['remote set-url']( { dir, remotename, url : reporemotes[remotename].url, remote
- , upstream
- , repodef : selectedinstance.reposindexed[repo] } ) // PB : TODO -- Handle add-url
- })( accessibleremotes[accessibleremotes.length -1] )
- }
- }).catch((e) => { console.log('Processing Error : ' + dir.name); console.error(e); return { error: true, message: repo } })
- })
-
- })()
- }
-
- if(isElevated) {
- var elevatedpromises = []
- selectedinstance.elevated.forEach( r => { elevatedpromises.push(perform_remote_refresh({ name : r.repo })) } )
- return Promise.all(elevatedpromises)
- }
- else return dirs(perform_remote_refresh)
- }
- }
-
- , 'remote add': (args) => {
- // PB : TODO -- set-upstream-to should be chosen and intentionally switched. Coz we can have multiple upstream remotes.
- // Also each upstream remote may need to have is own branch to switch to. We therefore need a branch nomenclature
- // That explicitly has a remote name prefix.
- // git branch branch_name --set-upstream-to your_new_remote/branch_name
- // git branch branch_name -u your_new_remote/branch_name -- older versions
- // git branch -vv -- Detect which branch are we tracking.
- // default upstream should be users own fork.
- // Also need alias branches to
- // define a git alias which would git pull master from one repo, and then git pull master from other remotes.
- // probably git merge from all remotes... and relevant branches...
- // alias pushall='for i in `git remote`; do git push $i; done;'
- // alias pullall='for i in `git remote`; do git pull $i; done;'
- // args === processedArgs ? args = {} : null;
- var __args = {
- remotename : args.remotename|| processedArgs._[2]
- , reponame : args.reponame || (args.repodef ? args.repodef.repo : undefined)
- , url : args.url || processedArgs._[3]
- , branch : args.branch || processedArgs._[4] || 'master'
- }
- __args.upstream = args['upstream'] || processedArgs._[5] || `${remotename}/${branch}`
-
- var remotename = __args.remotename
- var url = __args.url
- var branch = __args.branch
- var upstream = __args.upstream
-
- var pushable = processedArgs.pushable || (args.remote ? args.remote.pushable : false) || args.pushable;
-
- var serial_perform_git_add = (reponame) => {
- var options = { cwd: instanceroot + '/' + reponame }
- // console.log(reponame)
- if (pushable) {
- var gacmds = [
- ['git', ['remote', 'add', remotename, url + '/' + reponame], { cwd: instanceroot + '/' + reponame }]
- , ['git', ['pull', remotename, branch], { cwd: instanceroot + '/' + reponame }]
- // , ['git', ['branch', `--set-upstream-to=${upstream}`, branch], { cwd: instanceroot + '/' + reponame }]
- ]
- }
- else {
-
- var gacmds = [
- ['git', ['remote', 'add', remotename, url + '/' + reponame], { cwd: instanceroot + '/' + reponame }]
- , ['git', ['remote', `set-url`, '--push', remotename, 'no-pushing'], { cwd: instanceroot + '/' + reponame }]
- , ['git', ['pull', remotename, branch], { cwd: instanceroot + '/' + reponame }]
- // When we add a remote it should not automatically become the upstream. The upstream should be a chosen remote else we do not change
- // , ['git', ['branch', `--set-upstream-to=${upstream}`, branch], { cwd: instanceroot + '/' + reponame }]
- ]
- }
- if(__args['upstream']) gacmds.push(['git', ['branch', `--set-upstream-to=${upstream}`, branch], { cwd: instanceroot + '/' + reponame }])
- return gacmds
- }
-
- // PB : TODO -- Accumulate using yield... and finally return a set of tasks or promises...
- var perform_git_add = (dir) => {
- return op['is-git-repo'](dir).then((logEntry) => {
- // console.log(code)
- if (logEntry.success) {
- return nodeShellExec('git', ['remote', 'get-url', remotename], { cwd: dir.name, stdio: 'ignore' }).then(() => {
- console.log('skipped : ' + dir.name + ', reason : A remote with same name already exists.')
- return remotename;
- })
- .catch((e) => {
- return perform( serial_perform_git_add, dir.name ).then(()=>{
- return remotename
- })
- })
- }
- // else console.log('Skipped : Not a Git Repo : ' + dir.name)
- }).catch((e) => {
- console.log('Failed : ' + dir.name)
- })
- }
-
- if(__args.reponame) { return perform_git_add( { name : __args.reponame} ) }
- else return dirs(perform_git_add)
- }
- , 'remote remove': (args) => {
-
- var __args = {
- remotename : args.remotename|| processedArgs._[2]
- }
- var remotename = __args.remotename
- var serial_perform_git_remove = (repo) => {
- var options = { cwd: instanceroot + '/' + repo }
- // console.log(repo)
- return [
- ['git', ['remote', 'remove', remotename], { cwd: instanceroot + '/' + repo }]
- ]
- }
-
- var perform_git_remove = (dir) => {
- op['is-git-repo'](dir).then((logEntry) => {
- // console.log(code)
- if (logEntry.success) {
- nodeShellExec('git', ['remote', 'get-url', remotename], { cwd: dir.name, stdio: 'ignore' }).then(() => {
- any(serial_perform_git_remove(dir.name).map(getnodeshellexectask))
- })
- .catch((e) => {
- console.log('skipped : ' + dir.name + `, reason : No remote named ${remotename}`)
- })
- }
- // else console.log('Skipped : Not a Git Repo : ' + dir.name)
- }).catch((e) => {
- // console.log('Failed : ' + dir.name)
- })
- }
-
- dirs(perform_git_remove)
- }
- , 'get-remotes' : (args) => {
- // var __args = {
- // repo : args?.repo || processedArgs._[1]
- // }
-
- var repo = args.repo
-
- var serial_perform = (repo) => {
- return [
- ['git', ['remote', '-vvvv'], { cwd: instanceroot + '/' + repo,
- evaluateResult : function( issuccess, result){
- if(!issuccess || +result.code !== 0) throw Object.assign( new Error('git branch -vvvv crashed'), result);
-
- var lines = result.messages.join('').split('\n')
- var remotes = {}
- lines.forEach(line => {
- // pattern = origin https://git.bbh.org.in/chess/elxr (fetch)
- var match = new RegExp(`(\\S+)\\s(\\S+)/${repo}.git\\s\\((\\S+)\\)`).exec(line)
- if(match) {
- var r = remotes[match[1]] || (remotes[match[1]] = { title : match[1] })
- if(match[3] === 'push') { r.push = match[2] }
- else r.url = match[2]
- }
- })
- return remotes
- }}]
- ]
- }
-
- return perform( serial_perform, repo )
- }
- , 'get-remote' : (upstream) => {
- // var __args = {
- // repo : args?.repo || processedArgs._[1]
- // }
-
- var repo = upstream.repo
-
- var serial_perform = (repo) => {
- return [
- ['git', ['remote', '-vvvv'], { cwd: instanceroot + '/' + repo,
- evaluateResult : function( issuccess, result){
- if(!issuccess || +result.code !== 0) throw Object.assign( new Error('git branch -vvvv crashed'), result);
-
- // origin https://git.bbh.org.in/chess/elxr (fetch)
- var match = new RegExp(`${upstream.remotename}\\\s(\\S+)/${repo}.git\\s\\(fetch\\)`).exec(result.messages.join(' '))
- if(match) { upstream.url = match[1] }
- else throw Object.assign( new Error('No upstream found for active branch'), result)
- return upstream
- }}]
- ]
- }
-
- return perform( serial_perform, repo )
- }
- , 'get-upstream' : (args) => {
-
- var __args = {
- repo : args?.repo || processedArgs._[1]
- }
-
- var repo = __args.repo
-
- var serial_perform = (repo) => {
- return [
- ['git', ['branch', '-vvvv'], { cwd: instanceroot + '/' + repo,
- evaluateResult : function( issuccess, result){
- if(!issuccess || +result.code !== 0) throw Object.assign( new Error('git branch -vvvv crashed'), result);
-
- var match = /\*\s(\S+)\s\S+\s\[(\S+)\/(\S+).*?\].*?/.exec(result.messages.join(' '))
- if(match) { var upstream = { remotename : match[2], branch : match[1], remotebranch : match[3] } }
- else throw Object.assign( new Error('No upstream found for active branch'), result)
-
- if(args?.remotes) {
- var r = args.remotes[upstream.remotename];
- if(!r) return op['get-remote'](upstream)
-
- r.branch = upstream.branch
- r.remotebranch = upstream.remotebranch
- return r
- }
- return op['get-remote'](upstream)
- }}]
- ]
- }
-
- return perform( serial_perform, repo )
- }
- , 'init-gitea': (username) => {
- // Change user to an object that has user.username and use here..
- username = username || processedArgs._[1]
- if (!username) throw 'User name required'
- var serial_perform_init_gitea = (repo) => {
- var options = { cwd: instanceroot + '/' + repo }
- // console.log(repo)
- return [
- ['git', ['remote', 'add', 'chess', `${selectedinstance.reposerver}/${username}/${repo}.git`], { cwd: instanceroot + '/' + repo }]
- , ['git', ['remote', 'set-url', '--push', 'chess', 'no-pushing'], { cwd: instanceroot + '/' + repo }]
- , ['git', ['remote', 'set-url', 'userfork', `${selectedinstance.reposerver}/${username}/${repo}.git`], { cwd: instanceroot + '/' + repo }]
- ]
- }
-
- var perform_init_gitea = (dir) => {
- op['is-git-repo'](dir).then((logEntry) => {
- // console.log(code)
- if (logEntry.success) {
- nodeShellExec('git', ['remote', 'get-url', 'chess'], { cwd: dir.name, stdio: 'ignore' }).then(() => {
- console.log('skipped : ' + dir.name + ', reason : Already has remote chess ')
- })
- .catch((e) => {
- perform( serial_perform_init_gitea, dir.name )
- })
- }
- // else console.log('Skipped : Not a Git Repo : ' + dir.name)
- }).catch((e) => {
- // console.log('Failed : ' + dir.name)
- })
- }
-
- dirs(perform_init_gitea)
- }
- , 'syncmaster': (label) => {
- // Usage :
- // elxr pull -- Defaults to run config
-
- console.log('Running exlr pull : ' + path.dirname(__dirname))
-
- if (!processedArgs.runas) gitRepos.map((def) => performPull(def, def.branch || 'master'))
- var etask = ()=>{
- return any(elevatedRunasRepos.map((def) => performPull(def, def.branch || 'master'))).then(() => {
- fs.writeFileSync('run.done', 'success')
- }).catch(() => {
- fs.writeFileSync('run.done', 'error')
- })
- }
- etask.statuslog = statuslog
- etask.selectedinstance = selectedinstance
- etask.processedArgs = processedArgs
- etask.runtimestamp = runtimestamp
- etask.ENV = ENV
- return shell_verse.runElevated(etask)
- }
-
- , 'repo-relocate' : function(args){
- // relocate a subfolder in source repo to same subfolder in target repo with history.
-
- var args = args || {
- remotebase : processedArgs._[4] || 'http://git.bbh/chess/'
- , folder : processedArgs._[3] || 'server'
- , sourcerepo : processedArgs._[2] || 'client'
- , targetrepo : processedArgs._[1] || 'elixir-server'
- }
- var sourcerepooptions = { cwd: `${instanceroot}/relocate/${args.sourcerepo}`
- // , stdio: ['pipe', process.stdout, process.stderr]
- , inherit: true,
- shell: true,
- env: ENV
- , runas: processedArgs.runas
- }
-
- return op['repo-split'](args).then(()=>{
- return op['repo-merge'](args).then(()=>{
-
- // cleanup folder from source.
- var cmdseq = [
- ['rm', ['-rf', `${args.folder}`], sourcerepooptions ]
- // commits have to intentional...
- // , ['git', ['commit', '-a', '-m', `relocate folder ${args.folder} to ${targetrepo}`], sourcerepooptions ]
- ]
-
- return any(cmdseq.map(getshelltask))
- })
- }).catch(e=>{console.error(e)})
- }
-
- , 'repo-split' : function(args) {
-
- // https://gist.github.com/smdabdoub/17065c348289158277b5
-
- /**
- * Eg : folder, remotebase, sourcerepo, targetrepo
- * args.folder = 'server'
- * args.remotebase = 'https://git.bbh/chess/'
- * args.sourcerepo = 'client'
- * args.targetrepo = 'elixir-server'
- */
-
- var args = args || {
- folder : 'server'
- , remotebase : 'http://git.bbh/chess/'
- , sourcerepo : 'client'
- , targetrepo : 'elixir-server'
- }
-
- var options = { cwd: `${instanceroot}/relocate`
- // , stdio: ['pipe', process.stdout, process.stderr]
- , inherit: true,
- shell: true,
- env: ENV
- , runas: processedArgs.runas
- }
-
- var cmdseq = [
- // create a temporary working dir in current folder where we started execution.
- ['mkdir', [ '-p', `${instanceroot}/relocate`]]
- , ['git', ['clone', `${args.remotebase}${args.sourcerepo}`], options ]
- , ['git', ['clone', `${args.sourcerepo}`, `relocate-${args.sourcerepo}-${args.folder}`], options]
- , ['git', ['clone', `${args.remotebase}${args.targetrepo}`, `relocate-${args.targetrepo}-${args.folder}`], options]
- ]
-
- return any(cmdseq.map(getshelltask)).then(() => {
- // , ['git', ['subtree', 'split', '-P', `${args.folder}`, '-b', `relocate-${args.sourcerepo}-${args.folder}`], sourcerepooptions]
- // split doesnt retain folder structure we need to move and commit ourselves through a branch to retain history...
- // , ['git', [`checkout relocate-${args.sourcerepo}-${args.folder}`], sourcerepooptions]
- // , ['mkdir', [`${args.folder}`], sourcerepooptions]
- // , ['git', ['mv', `!(${args.folder})`, `${args.folder}`], sourcerepooptions ] // requires shopt -s extglob
- // , ['git', ['commit', `-m`, 'Moved to same subfolder after subtree split in branch'] , sourcerepooptions]
-
-
- // subtree split and filter-branch are options. We currently use repo-filter.
- return op['filter-repo'](args)
- }).catch(e=>{console.error(e); throw 'failed'})
- }
-
- , 'repo-merge' : function(args) {
- // Merge source repo into target repo
- var args = args || {
- folder : 'server'
- , remotebase : 'http://git.bbh/chess/'
- , sourcerepo : 'client'
- , targetrepo : 'elixir-server'
- }
-
- var sourcerepooptions = { cwd: `${instanceroot}/relocate/relocate-${args.sourcerepo}-${args.folder}`
- // , stdio: ['pipe', process.stdout, process.stderr]
- , inherit: true,
- shell: true,
- env: ENV
- , runas: processedArgs.runas
- }
- var targetrepooptions = { cwd: `${instanceroot}/relocate/relocate-${args.targetrepo}-${args.folder}`
- // , stdio: ['pipe', process.stdout, process.stderr]
- , inherit: true,
- shell: true,
- env: ENV
- , runas: processedArgs.runas
- }
-
- var cmdseq = [
- ['git', ['pull', `../relocate-${args.sourcerepo}-${args.folder}`, `master`, '--allow-unrelated-histories']
- , targetrepooptions ]
- // , ['git', ['push', targetrepooptions ] // manual push for now..
- ]
- return any(cmdseq.map(getshelltask)).catch(e=>{console.error(e)})
- }
-
- , 'filter-repo' : function(args){
- // Merge source repo into target repo
- var args = args || {
- folder : 'server'
- , remotebase : 'http://git.bbh/chess/'
- , targetrepo : 'elixir-server'
- }
-
- var targetrepooptions = { cwd: `${instanceroot}/relocate/relocate-${args.sourcerepo}-${args.folder}`
- , inherit: true, shell: true
- , env: ENV
- , runas: processedArgs.runas
- }
-
- var cmdseq = [
- // git checkout -b feature/merge-old-project
- // git remote add src-project ../src-project
- ['git', ['filter-repo', '--path', `${args.folder}`], targetrepooptions ]
- ]
- return any(cmdseq.map(getshelltask)).catch(e=>{console.error(e); throw 'failed' })
- }
-
- , 'filter-branch' : function(preservefolder, repo){
- // https://stackoverflow.com/questions/359424/detach-move-subdirectory-into-separate-git-repository
- // git subtree split -P <name-of-folder> -b <name-of-new-branch>
- // Preserve a specific folder.
- // PB : TODO -- filter-branch has known issues. Explore subtree and filter-repo...
- /*
- git clone repo reposplit
- git filter-branch --tag-name-filter cat --prune-empty --subdirectory-filter {{folder}} -- --all
- git remote rm origin
- */
-
- throw 'filter-repo-history not yet implmented pls refer to manual commands in code.'
- }
-
- // , 'repo-merge-folder' : function(mergetarget, mergesource, mergefolder){
- // // Merge repo2 into a subfolder in repo1
- // // https://alexharv074.github.io/puppet/2017/10/04/merge-a-git-repository-and-its-history-into-a-subdirectory-of-a-second-git-repository.html
- // // https://thoughts.t37.net/merging-2-different-git-repositories-without-losing-your-history-de7a06bba804
- // /*
- // cd repo1
- // git remote add -f tempmergesource ../{{mergesource}}/.git
- // git merge -s ours --no-commit tempmergesource/master --allow-unrelated-histories
- // git read-tree --prefix={{mergefolder}} -u tempmergesource/master:
- // git commit
-
- // // Repair History
- // // === git filter-branch --tree-filter '(echo === $GIT_COMMIT:; git ls-tree $GIT_COMMIT) >> /tmp/tree.log'
-
- // #!/usr/bin/env bash
-
- // first=ed4f16becc2f03820c164e0617bb98f12ff49ef0
- // last=038e26e21bd60fa265d6637261f3bc918780d2e8
- // subdir=server
-
- // git filter-branch --tree-filter '
- // first='"$first"'
- // last='"$last"'
-
- // subdir='"$subdir"'
- // log_file=/tmp/filter.log
-
- // [ "$GIT_COMMIT" = "$first" ] && seen_first=true
-
- // if [ "$seen_first" = "true" ] && [ "$seen_last" != "true" ]; then
- // echo "=== $GIT_COMMIT: making changes"
- // files=$(git ls-tree --name-only $GIT_COMMIT)
- // mkdir -p $subdir
- // for i in $files; do
- // mv $i $subdir || echo "ERR: mv $i $subdir failed"
- // done
- // else
- // echo "=== $GIT_COMMIT: ignoring"
- // fi \
- // >> $log_file
-
- // [ "$GIT_COMMIT" = "$last" ] && seen_last=true
-
- // status=0 # tell tree-filter never to fail
- // '
-
- // git remote rm tempmergesource
- // */
- // // better alternative for history
- // /**
- // *
- // git clone git@server.com:old-project.git
- // git clone git@server.com:new-project.git
-
- // cd old-project
- // mkdir old-project
- // git mv !(old-project) old-project
- // ls -la and gt mv hidden files ....
- // git commit -a -S -m “Moving old project into its own subdirectory”
-
- // cd ../new-project
- // git remote add old-project ../old-project
- // git fetch old-project
- // git checkout -b feature/merge-old-project
- // git merge -S --allow-unrelated-histories old-project/master
- // git push origin feature/merge-old-project
- // git remote rm old-project
- // */
-
- // throw 'merge-repos not yet implmented pls refer to manual commands in code.'
- // }
-
- , 'pull' : (label) => {
- // Usage :
- // elxr pull -- Defaults to run config
-
- return elxr.getpulltask(selectedinstance)
- }
- , 'isInstalled': () => {
- return nodeShellExec('where', [processedArgs._[1]], { inherit: true }).then(() => {
- console.log(processedArgs._[1] + ' exists.')
- return true;
- });
- }
- , 'c' : ()=>{
-
- }
- , 'i' : () => {
- var tasks = []
- // tasks.push(op['pull']);
- tasks.push(getShellTask.apply(null, ['rm', [instanceroot + '/run.js'], { ignorefailures: true }]))
- tasks.push(op['use']) // Will pull repos...
- op['npmi'].statuslog = statuslog
- tasks.push(shell_verse.getNonElevatedTask(op['npmi'])) // We do not run npm i with elevated privilege for security reasons.
-
-
- // var tasksdefs = [
- // ['elxr', ['pull']]
- // , ['elxr', ['use', 'elixir']]
- // , ['elxr', ['npmi']]
- // ]
-
-
- // tasksdefs.forEach(tasksdef=>{
- // tasks.push( ()=> nodeShellExec.apply(null, tasksdef) )
- // })
-
- return any(tasks);
-
- }
- , 'npmi': () => {
- var tasks = [];
-
- // Build fails without babel...
- tasks.push(getShellTask(
- 'npm',[ 'i', '-g', 'babel-cli' ]
- ) )
-
- var npmbuildrepos = ['loopback-jsonapi-model-serializer']
- // npmbuildrepos = []
- npmbuildrepos.forEach(repo => {
- tasks.push(() => {
- return nodeShellExec('npm', ['i --force'], {
- inherit: true, shell: true
- , cwd: instanceroot + '/' + repo
- , env: ENV
- , title: `npm i for ${repo}`
- }).catch((e) => {
- console.error('Ignoring Benign Error'); console.error(e);
- }).then(() => {
- console.log(`--npm run build for ${repo}--------------------`)
- return nodeShellExec('npm', ['run build'], {
- inherit: true, shell: true
- , cwd: instanceroot + '/' + repo
- , env: ENV
- , title: `npm run build for ${repo}`
- }).then(Tasq.then).catch(Tasq.catch)
- })
- })
- })
-
- return any(tasks).then(() => {
-
- gitRepos.push({ repo : 'chess-server-lib/server'});
- gitRepos = gitRepos.concat(elevatedRunasRepos);
- // console.dir(gitRepos)
- // throw ""
-
- // gitRepos = [
- // // 'bbhverse', 'serververse', 'elixir-server',
- // // 'clientverse',
- // 'client'
- // ];
- var rmtasks = []
- var repotasks = []
- var env = Object.assign({}, ENV)
- delete env.NODE_ENV // PB : TODO -- Why delete ??? npm i fails with NODE_ENV...
- gitRepos.forEach(repodef => {
- rmtasks.push(
- // () => {
- // console.log(`--rm package-lock.json for ${repodef.repo}--------------------`)
- // return nodeShellExec(`"${gitbash}"`, ['-c', '"rm package-lock.json"'], {
- callshelltask(['rm', ['package-lock.json'], {
- inherit: true, shell: true
- , cwd: instanceroot + '/' + repodef.repo
- , env: ENV
- , title: `rm 'package-lock.json' for ${repodef.repo}`
- }])
- .then(()=>{
- console.log(`--rm package-lock.json for ${repodef.repo}--------------------`)
- }).catch((e) => { console.error(e) })
- // }
- )
-
- if (npmbuildrepos.indexOf(repodef.repo) < 0) {
- repotasks.push(
- () => {
- console.log(`--npm i for ${repodef.repo}--------------------`)
- var p = nodeShellExec('npm', ['i', '--force'], {
- inherit: true, shell: true
- , cwd: instanceroot + '/' + repodef.repo
- , env
- , title: `npm i for ${repodef.repo}`
- }).then(Tasq.then).catch(Tasq.catch)
- return p;
- })
- }
- })
-
- // NODE_ENV=development DEBUG=loopback:connector:mssql node --tls-min-v1.0 --inspect elixir/server.js
- // PB : NOTE -- npm i for client does not complete when NODE_ENV=production
- // therefore bower doesn't get installed !!! which then fails installing the bower dependenciew !!!
- // We work around this by running npm i for client without any NODE_ENV which probably defualts to development.
- // PB : TODO -- Investigate why NODE_ENV has an impact on npm i !??
- // Second time try also doesnt work.
- // repotasks.push(
- // () => {
- // var env = Object.assign({}, process.env)
- // delete env.NODE_ENV
- // console.log(`--------------------second time npm i for client--------------------`)
- // return nodeShellExec(`"${gitbash}"`, ['-c', '"npm i --force"'], {
- // // return nodeShellExec('npm', ['i --force'], {
- // inherit: true, shell: true
- // , cwd: instanceroot + '/' + 'client'
- // , env
- // , title: `npm i for client`
- // }).then(Tasq.then).catch(Tasq.catch)
- // })
-
- var bowerRepos = [{ repo : 'client'}]
- var bowertasks = []
- bowerRepos.forEach(repodef => {
- bowertasks.push(() => {
- console.log(instanceroot + '/' + repodef.repo + '/node_modules/bower/bin/bower')
- // var p = nodeShellExec('node_modules/bower/bin/bower', ['install'], {
- var p = nodeShellExec(`"${gitbash}"`, ['-c', '"node_modules/bower/bin/bower i"'], {
-
- inherit: true, shell: true
- , cwd: instanceroot + '/' + repodef.repo
- , env: ENV
- , title: `bower i for ${repodef.repo}`
- }).then(Tasq.then).catch(Tasq.catch)
- return p;
- })
- })
- // console.log('rmtasks.length : ' + rmtasks.length)
- return Promise.all(rmtasks).then(() => any(repotasks)).then(()=>any(bowertasks));
- }).catch(e => {
- console.error(e)
- }).finally(statuslog.finally)
-
- }
- , 'start': (args) => {
- // Usage : elxr start {{instanceName=elixir}} {{all||server||client||{{microserviceName=elixir||express}}}}
-
- var label = clioverrides.cmd
- var env = Object.assign({}, ENV); // Shallow clone it.
- var nodecmd = clioverrides.node_env === 'development' ? 'nodemon' : 'node';
-
- function startFn( o ){
- var cmd = [nodecmd, [`--inspect=${o.debugport}`, '--preserve-symlinks', o.script]]
- console.log(`Starting ${o.name}.`);
-
- if(shell_verse.iswin()){
- var __cmd = 'cmd'
- var a1 = '/k'
- }
- else if(shell_verse.islin()) {
- var __cmd = 'sh'
- var a1 = '-c'
- }
-
- if(o.cmd) {
- cmd = o.cmd
- var childPromise = nodeShellExec(...o.cmd, {
- // inherit : true,
- shell: true,
- detached: true,
- stdio: 'ignore',
- cwd: instanceroot + o.path
- , env
- })
- }
- else {
- var childPromise = nodeShellExec(__cmd, [a1, getCmdString(cmd) ], {
- // inherit : true,
- shell: true,
- detached: true,
- stdio: 'ignore',
- cwd: instanceroot + o.path
- , env
- })
- }
-
- var child = childPromise.process;
- var cpid = child.pid
- childPromise.then(()=>{
- console.log(` *** started ${o.name} PID(${cpid}) ${cmd}`);
- fs.writeFileSync(o.pidstore, '' + cpid)
- })
- .catch(e => console.error(e) )
- }
-
- if(clioverrides.node_env === 'development') {
- env.DEBUG = 'loopback:connector:' + dbForLabel(label)
-
- if(!processedArgs._[2]) processedArgs._[2] = 'all'
- if(processedArgs._[2] === 'server' || processedArgs._[2] === 'all'
- || processedArgs._[2] === 'elixir' // specific microservice name.
- ) {
- startFn( { debugport : 9228, script : 'elixir/server.js', name : 'Elixir Loopback Server', path : '/' + 'elixir-server'
- , pidstore : '.elixir-server.elixir.server.pid'
- } )
- }
- if(processedArgs._[2] === 'server' || processedArgs._[2] === 'all'
- || processedArgs._[2] === 'express' // specific microservice name.
- ) {
- startFn( { debugport : 9227, script : 'bin/www', name : 'Express Server', path : '/' + 'chess-server-lib/server'
- , pidstore : '.express.server.pid'
- } )
- }
-
- if(processedArgs._[2] === 'client' || processedArgs._[2] === 'all') {
- startFn( {
- name : 'Elixir Ember Client', path : '/' + 'client'
- , cmd : ['node_modules/ember-cli/bin/ember', ['s']]
- , pidstore : '.client.server.pid'
- } )
- }
- }
-
- }
- , 'stop': (label) => {
- const kill = require('tree-kill');
-
- try{
- var serverPid = fs.readFileSync('.elixir-server.elixir.server.pid', { encoding: 'utf8'})
- fs.unlinkSync('.elixir-server.elixir.server.pid')
- console.log(serverPid)
- kill(serverPid)
- }
- catch(e){ console.error(e) }
-
- try{
- serverPid = fs.readFileSync('.express.server.pid', { encoding: 'utf8' })
- fs.unlinkSync('.express.server.pid')
- console.log(serverPid)
- kill(serverPid)
- }
- catch(e){ console.error(e) }
-
- try{
- serverPid = fs.readFileSync('.client.server.pid', { encoding: 'utf8' })
- fs.unlinkSync('.client.server.pid')
- console.log(serverPid)
- kill(serverPid)
- }
- catch(e){ console.error(e) }
- }
- , 'model' : () => {
- var etask = ()=>{
- var tasks = [
- () => {
- var p = nodeShellExec('mklink', [ `${processedArgs._[2]}.json`
- , `..\\..\\node_modules\\chess-server-lib\\common\\models\\${processedArgs._[2]}.json`], {
- inherit: true, shell: true
- , cwd : instanceroot + `/${selectedinstance.instanceName}-server/${selectedinstance.instanceName}/models`
- , title: `mklink ${processedArgs._[2]}.json ..\\..\\node_modules\\chess-server-lib\\common\\models\\${processedArgs._[2]}.json`
- , env: ENV
- }).catch((e) => { console.error(e) })
- return p;
- }
- ];
- return any(tasks).then(() => {
- fs.writeFileSync('run.done', 'success') // PB : TODO -- IPC through files is needed only for windows.
- }).catch(() => {
- fs.writeFileSync('run.done', 'error')
- })
- }
- etask.statuslog = statuslog
- etask.selectedinstance = selectedinstance
- etask.processedArgs = processedArgs
- etask.runtimestamp = runtimestamp
- etask.ENV = ENV
- return shell_verse.runElevated(etask)
- }
-
-
- , 'use' : () => {
- // use a certain named instance.
- // Eg :
- // 1) elxr use elixir
- // 2) elxr use cihsr
- // If environment is not specified defaults to development.
- // 1) NODE=test elxr use elixir
-
- /*// Steps
- 1) Delete Config and Data symlinks
- 2) Make Links for config ({{name}}-config-{{node_env}}) and data with the NODE_ENV specified or default to dev
- 3) Iterates all repos and pull all. 'git', ['pull', '--all'].
- 4) Iterates all repos and checkout to the ENV specified. 'git', ['checkout', checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV]
- 5) Iterates all repos and merge from source configured in mergesource. 'git', ['merge', mergesource],
- */
- // We no longer need to check ruans.. ??? if we were initiated from self invoked privileged shell ?
- if (( /*processedArgs.runas && processedArgs.runas !== 'self' &&*/ !processedArgs.force) &&
- runconfig.NODE_ENV && runconfig.NODE_ENV === (selectedinstance.node_env || runconfig.NODE_ENV) &&
- selectedinstance.instanceName && runconfig.use === selectedinstance.instanceName) {
-
- console.log(`No change detected. Already using requested specs : ${runconfig.NODE_ENV} ${runconfig.use}`)
- if (processedArgs.runas) { fs.writeFileSync('run.done', 'success') } // PB : TODO -- IPC through files is needed only for windows.
- if (existsSync('config') && existsSync('data')){
- return
- }
- }
-
- var tasks = [
- () => {
- var promise = new Promise((resolve, reject)=>{
- existslink('config', function(err, data){
- if(data) {
- var p = shell_verse.removeJuncionOrLink('config')
- .then(()=>{resolve(true)})
- .catch((err) => { console.log('Ignoring benign error : ' + err); return resolve(true); })
- }
- else resolve(false)
- })
- })
- return promise;
- },
- () => {
- var promise = new Promise((resolve, reject)=>{
- existslink('data', function(err, data){
- if(data) {
- var p = shell_verse.removeJuncionOrLink('data')
- .then(()=>{resolve(true)})
- .catch((err) => { console.log('Ignoring benign error : ' + err); return resolve(true); })
- }
- else resolve(false)
- })
- })
- return promise;
- },
- ];
- runconfig.NODE_ENV = ENV.NODE_ENV = ENV.NODE_ENV || runconfig.NODE_ENV || 'development';
- if (processedArgs._[1] && runconfig.use !== processedArgs._[1]) runconfig.use = processedArgs._[1];
- if (!runconfig.use && selectedinstance.instanceName) runconfig.use = selectedinstance.instanceName;
- if (!runconfig.use) { throw 'unspecifed use not allowed. Please specify chess instance name.' }
- fs.writeFileSync(instanceroot + '/run.js', 'module.exports = ' + JSON.stringify(runconfig))
-
- var branch = checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV
-
- var performPullOrCloneForBranch = (def)=>{
- var promise = Promise.resolve(true)
- if (!branch) {
- var dscoverbranchcmd = gitops.getdiscoverbranchcmd(repo)
-
- promise = nodeShellExec.apply(null, dscoverbranchcmd).then(__branch=>{ branch = __branch})
- .catch((e) => { console.error(e); return { error: true, message: repo } })
-
- }
-
- return promise = promise.then(()=>{ return elxr.getpulltask(def)() }).catch(e => { console.error( 'performPullOrCloneForBranch : Failed ' + e )})
- }
-
- var performCloneAndCheckout = null;
- // cant use git checkout -b it fails when branch already exists.
- var performCheckout = performCloneAndCheckout = (def) => {if (excludeCheckouts[def.repo]) return Promise.resolve({ 'skipped': true })
-
- return performPullOrCloneForBranch(def).then(()=>{
- nodeShellExec('git', ['checkout', def.branch || checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV], {
- // return nodeShellExec('git', ['switch', '-m', '-C', checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV], {
- // inherit : true, shell: true,
- cwd: instanceroot + '/' + def.repo
- // , stdio : ignore // Use when we want to silence output completely.
- , runas: processedArgs.runas
- , title: `git checkout ${def.branch || checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV} for ${def.repo}`
- }).then(()=>{ console.log( `SUCCESS : git checkout ${def.branch || checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV} for ${def.repo}` ) }).catch((e) => { console.error(e); return { error: true, message: def.repo } })
- })
- }
-
- // var enqueueCheckout = function(def){ enqueue(elevatedqueue, performCheckout, def) }
- // var enqueueMerge = function(def){ enqueue(elevatedqueue, performMerge, def) }
-
- var elevatedqueue = [];
- var enqueueOnce = (queue, task, def) => {
- var found = queue.find(element => {
- var keys = Object.keys( element )
- for(var k=0; k < keys.length; k++) {
- if(element[keys[k]] !== def[k]) return false;
- }
- })
- queue.push(function(){ return task(this)}.bind(def));
- }
- var enqueue = (queue, task, def) => {
- queue.push(function(){ return task(def)}.bind(def));
- }
-
- var mergeSources = {
- 'development': null,
- 'test': 'master',
- 'production': 'master'
- }
- var mergesource = mergeSources[checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV]
-
- // Checkout is reduced to pull provided the current branch is the targetbranch
- if(branch === mergesource) performCheckout = (def) => {
- var dscoverbranchcmd = gitops.getdiscoverbranchcmd(repo)
- return nodeShellExec.apply(null, dscoverbranchcmd).then(__branch=>{
-
- if(branch === __branch) return performCloneAndCheckout(def)
- return performPullOrCloneForBranch(def)
- })
- .catch((e) => { console.error(e); return { error: true, message: repo } })
- }
- // else performCheckout = (def) => { return performPullOrCloneForBranch(def) }
-
- var performPullAll = (def) => {
- if (excludeCheckouts[def.repo]) return Promise.resolve({ 'skipped': true })
- return nodeShellExec('git', ['pull', '--all'], {
- // inherit : true, shell: true,
- cwd: instanceroot + '/' + def.repo
- , runas: processedArgs.runas
- , title: `git pull -all for ${checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV} ${def.repo}`
- }).catch((e) => { console.error(e); return { error: true, message: def.repo } })
- }
-
- var excludeCheckouts = Object.assign(exludeMergeRepos)
- delete excludeCheckouts[`elixir-config-${runconfig.NODE_ENV}`]
- delete excludeCheckouts[`cihsr-config-${runconfig.NODE_ENV}`]
-
- var performMerge = (def) => {
- if (exludeMergeRepos[def.repo]) return Promise.resolve({ 'skipped': true })
- return nodeShellExec('git', ['merge', mergesource], {
- inherit: true, shell: true,
- cwd: instanceroot + '/' + def.repo
- , runas: processedArgs.runas
- }).catch((e) => { console.error(e) })
- }
- if(!mergesource || branch === mergesource) performMerge = () => { return Promise.resolve(true) }
-
- // var performRepoOperation = function(def) {
- // elevatedRunasRepos.map((repo) => enqueueCheckout({ repo, branch: def.branch, requiresElevation : true }))
- // return any(gitRepos.map((repo) => performCheckout({ repo, branch: def.branch})))
- // }
-
- return any(tasks).then(() => {
-
- var task = ()=>{
-
- // Default target is developent. if some other target is specified in NODE_ENV in for shell call for the 'use' command we will switch to that target..
- //Switch to target branch
- return any(gitRepos.map((repodef) => { return performCheckout({ repo : repodef.repo, branch}).catch(e=>{ console.log(e); }) } ))
- // pull or clone target branch
- .then(() => {
- if(!mergesource || branch === mergesource) return Promise.resolve(true)
- return any(gitRepos.map((repo) => performPullAll(repo)))
- })
- // switch to source branch
- .then( () => {
- if(!mergesource || branch === mergesource ) return Promise.resolve(true) // Dont do anything if there is no source to merge from.
- return any(gitRepos.map((repodef) => performCheckout({ repo : repodef.repo, branch: mergesource})))
- })
- //Pull on merge source branch
- .then( () => {
- if(!mergesource || branch === mergesource ) return Promise.resolve(true)
- return any(gitRepos.map((repo) => performPullAll(repo)))
- })
- //Switch to target branch
- .then( () => {
- if(!mergesource || branch === mergesource ) return Promise.resolve(true)
- return any(gitRepos.map((repodef) => performCheckout({ repo : repodef.repo, branch})))
- })
- .then( //Merge source branch to target branch
- () => {
- if(!mergesource || branch === mergesource) return Promise.resolve(true)
- return any(gitRepos.map((repo) => performMerge( repo ))).catch(err => { console.error('error in performMerge ' + err) })
- })
- }
- task.statuslog = statuslog
- var netask = shell_verse.getNonElevatedTask(task)
-
- var eltask = ()=> {
- var opts = {
- inherit: true, shell: true
- , cwd : instanceroot
- , env: ENV
- }
- var tasks = [
- () => {
- // Use junctions to avoid npm package issues
- var target = runconfig.use + '-' + 'config' + '-' + ENV.NODE_ENV
- var p = shell_verse.createJuntionOrLink('config', target, opts)
- return p;
- }
- ];
- // if (processedArgs._[1]) {
- tasks = tasks.concat( [
- () => {
- var dataToLink = runconfig.use + '-data' + '-' + ENV.NODE_ENV
- if(!existsSync(dataToLink)) dataToLink = runconfig.use + '-data';
- var p = shell_verse.createJuntionOrLink('data', dataToLink, opts)
- return p;
- }
- ]
- )
- // }
-
- return any(tasks).then(()=>{
- // checkout target branch
- return any(elevatedRunasRepos.map((repodef) => performCheckout({ repo : repodef.repo, branch, requiresElevation : true})))
- // pull or clone target branch
- .then(() => {
- if(!mergesource || branch === mergesource) return Promise.resolve(true)
- return any(elevatedRunasRepos.map((repo) => performPullAll(repo)))
- })
- // switch to source branch
- .then( () => {
- if(!mergesource || branch === mergesource) return Promise.resolve(true)
- return any(elevatedRunasRepos.map((repodef) => performCheckout({ repo : repodef.repo, branch: mergesource, requiresElevation : true})))
- })
- //Pull on merge source branch
- .then( () => {
- if(!mergesource || branch === mergesource) return Promise.resolve(true)
- return any(elevatedRunasRepos.map((repodef) => performPullAll({repo : repodef.repo, requiresElevation : true })))
- })
- //Switch to target branch
- .then( () => {
- if(!mergesource || branch === mergesource) return Promise.resolve(true)
- return any(elevatedRunasRepos.map((repodef) => performCheckout({ repo : repodef.repo, branch, requiresElevation : true})))
- })
- .then( //Merge source branch to target branch
- () => {
- if(!mergesource || branch === mergesource) return Promise.resolve(true)
- return any(elevatedRunasRepos.map((repodef) => performMerge({ repo : repodef.repo, requiresElevation : true }))).catch(err => { console.error('error in performMerge ' + err) })
- })
- .then(() => {
- fs.writeFileSync('run.done', 'success')
- }).catch(() => {
- fs.writeFileSync('run.done', 'error')
- })
- })
- }
- eltask.statuslog = statuslog
- eltask.selectedinstance = selectedinstance
- eltask.processedArgs = processedArgs
- eltask.runtimestamp = runtimestamp
- var eltask = shell_verse.getElevatedTask(eltask)
-
- return eltask().then( ()=>{ return netask() })
-
- }).catch((e) => {
- fs.writeFileSync('run.done', 'error : ' + e)
- })
-
- // Antibiotic stewardship program.
- // 1st use is fine.
- // Max vials dispense
- // 2nd use Pharmacy needs justification Form.
- // Approval after a certain period of time.
- }
- , 'g' : () => {
- if (processedArgs.h) {
-
- console.log('elxr g model [modelname] => generate a model named [modelname]');
- console.log('elxr g vmodel [modelname] => generate a model named [modelname]');
- console.log('elxr g vmodel [modelname] --server=true => generate only a server model named [modelname]');
- console.log('elxr g vmodel [modelname] --all=true => default generate server and client model named [modelname]');
- console.log('elxr g => regenerate all known models');
- return
- }
- // var child = nodeShellExec('mkdir', ['-p', label], { inherit : true} );
- // try {
- // child = child.on('close', () => { process.chdir(label) } );
- // }
- // catch (err) {
- // console.log('chdir: ' + err);
- // }
- // child.on('close', function(){
- // var options = {
- // shell : true
- // , inherit : true
- // // , cwd : '' + process.cwd
- // // , env : ENV
- // };
- // nodeShellExec('git', ['init'], { inherit : true});
- if (0) {
- // PB : TODO -- Special google chrome profile for tests etc.
- nodeShellExec('pwd', { inherit: true });
- // /c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe --user-data-dir=/c/chess/instances/elixir_01/data/Google/Chrome/User\ Data --profile-directory="chess"
- // "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --user-data-dir="C:\chess\instances\elixir_01\data\Google\Chrome\User Data" --profile-directory="chess" http://localhost:4200/admin/crud/create/item
- // "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --user-data-dir="C:\chess\instances\elixir_01\data\Google\Chrome\User Data" --profile-directory="chess" http://localhost:4200/tests/index.html?grep=loopback
- nodeShellExec("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe", [
- "--profile-directory=Profile 1"
- , 'http://localhost:4200/tests/index.html?grep=model convert ember to loopback' + '&filter=none' /*+ '&filter=model convert ember to loopback'*/]);
-
- }
- // nodeShellExec('npm', ['init', '-y'], options);
- // nodeShellExec('npm', ['init', '-y'], options);
- // })
-
- var g = {
- 'client': () => {
- // ember new --skip-bower --no-welcome --skip-git -b {{blueprint}}
-
- console.info('Creating new ember client named : ' + processedArgs._[2]);
- var step1 = nodeShellExec('cmd', ['/c', 'ember', 'new', processedArgs._[2]], {
- stdio: ['pipe', process.stdout, process.stderr],
- inherit: true,
- shell: true,
- cwd: path.dirname(__dirname),
- env : ENV
- })
- },
-
- 'modelr': () => {
- var tasks = [
- () => {
- var p = nodeShellExec('"ember"', [
- 'g'
- , 'modelr'
- , processedArgs._[2]], {
- inherit: true, shell: true, env: ENV
- }).then(() => {
- console.log('Blueprint generation complete for : ' + processedArgs._[2])
- return true;
- }).catch((e) => { console.error(e) })
- return p;
- },
- () => {
- var chromePrefsFile = "C:\\chess\\instances\\elixir_01\\data\\Google\\Chrome\\User Data\\chess\\Preferences";
- var chromeprefs = fs.readFileSync(chromePrefsFile, { encoding: 'utf8' })
- chromeprefs = JSON.parse(chromeprefs)
- var previous = chromeprefs.download.default_directory;
- var parentDir = path.dirname(__dirname);
- chromeprefs.download.default_directory = parentDir + "\\client\\app\\templates\\components\\resource";
- fs.writeFileSync(chromePrefsFile, JSON.stringify(chromeprefs))
- // PB : TODO -- detect where chrome is installed.
- // PB : TODO -- set the download dir to the place where files are needed.
- var p = nodeShellExec('"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"', [
- '--user-data-dir="C:\\chess\\instances\\elixir_01\\data\\Google\\Chrome\\User Data"'
- , '--profile-directory="chess"'
- , 'http://localhost:4200/admin/crud/create/' + processedArgs._[2]], {
- inherit: true, shell: true
- , env: ENV
- }).then(() => {
- chromeprefs.download.default_directory = previous;
- fs.writeFileSync(chromePrefsFile, JSON.stringify(chromeprefs))
- return true;
- }).catch((e) => { console.error(e) })
- return p;
- }
- ,
- () => {
- console.log('Browser process should have closed here....')
- return true;
- }
- ]
- any(tasks)
-
- }
-
- , 'undefined' : ()=>{
- console.warn('Are you sure you want to refresh all the models.');
- throw 'NOT YET IMPLMENTED'
- }
-
- , 'vmodel' : (options)=>{
- // node --inspect-brk elxr/index.js g vmodel qlabresults --server=true
- options = options || {
- generate : processedArgs['all'] ? 'all' : processedArgs['server'] ? 'server' : processedArgs['client'] ? 'client' : null
- }
-
- if(!options.generate) return 'Nothing specified for generation'
-
- var name = processedArgs._[2];
- if(!name) return 'No name specified for generation'
- var templateverse = `${selectedinstance.root}/bbhverse/__universe__/vmodel`
- // var templateverse = `${selectedinstance.root}/bbhverse/__universe__/vmodel/__base__-server-lib/common/lib`
-
- var workingtarget = `${selectedinstance.root}/.elxr/run-${runtimestamp}/temp/vmodelworkingdir`
- var destination = `${selectedinstance.root}`
-
- // Create a tmp working copy for staging the changes.
- return copyrecursive(templateverse, workingtarget).then( r => {
-
- var base = 'chess' // -- The server base from where this instance was dervied from.
- var patternsubstitutions = [
- { strOrregexp : /(__name__)/g, substitutes : [name]}
- , { strOrregexp : /(__instance__)/g, substitutes : [selectedinstance.instanceName]}
- , { strOrregexp : /(__base__)/g, substitutes : [base]}
- , { strOrregexp : /(__microservice__)/g, substitutes : [selectedinstance.microservice || selectedinstance.instanceName]}
- , { strOrregexp : /(__microclient__)/g, substitutes : [selectedinstance.microclient || `${selectedinstance.instanceName}-client` || 'client']} // PB : TODO -- elixir-client etc... use a config to find the microclient..
- ]
- var options = { source : templateverse, target : workingtarget
- , base , destinationroot : destination, workingtarget
- , processingtype : 'inplace', sourcetype : 'filesystem', patternsubstitutions }
-
- var actions = [
- fswalk( workingtarget, options
- , [ function(t, o){
-
- // A series of tasks to be executed on each pattern in patternsubstitutions.
- return any([
- // PB : TODO -- Contentreplace is better addressed in memory instead of file IO so do all regexps in sequence in one shot and then rename.
- ()=>{ return fscontentreplace( t, o ) }
- , ()=>{ return fsrename( t, o ) }
- ])
- }] )
- // Post process links in place.
- , () => dirs((innernode, s, t, o ) => fswalk( path.join(workingtarget, innernode.name), utils.assign({}, o, { innernode }), [ templatelink ] )
- , workingtarget, null, options)
- // Move to the final destination. Whole tree should move only top level folders are required.
- // , () => fswalk( workingtarget, options, [ fsrename ])
- // , () => dirs( ( child, src, target, options )=>{ return fs.renameSync(path.join(src, child.name), path.join(target, child.name) ) }
- // , workingtarget, destination )
- ]
-
- return any(actions);
-
- })
-
- }
- }
-
- g[processedArgs._[1]]();
- }
-
- , 'create' : () => { return createInstance(selectedinstance) }
-
- , 'httpget' : () => {
- // RESTAPI.get({
- // hostname: 'git.bbh',
- // // port: 443,
- // protocol : 'http:',
- // path: '/',
- // method: 'GET'
- // }, function(data){ console.log(data)}, function(error){ console.log(error)} )
- return createInstance(selectedinstance)
- }
-
- , 'getuser' : ()=>{ return prerequisites.git.getuser().then(u=>{ console.log(u)}) }
- , 'switch user' : (tousername)=>{ return GIT['switch user'](tousername) }
- }
-
- Object.keys(op).forEach( oper => {
- if(cmds[oper]) return
-
- cmds[oper] = {}
- // cmds[oper] = {
- // // return a interpreted set of arguments for this cmd run context.
- // interpret() { return { cmd : oper, runchoice : 'c' } }
- // }
- })
-
- var elxrcmd = (function(){
-
- var __cmdprototype = function(){}
- function subcommandlabelFor(cmd, sub){ return (`${cmd} ${sub || ''}`).trim() }
-
- var __cmd = {
- interpret() {
- var interpreted = {}
- // Non custom command has universal positiona args.
- processedArgs._[1] ? interpreted.instanceName = processedArgs._[1] : null;
- interpreted.runchoice = processedArgs._[0] || selectedinstance.runchoice;
- // return clioverrides
- // cmds[cmd] = {
- // interpret() {
- // return Object.assign(clioverrides, { cmd, runchoice : 'c' })
- // }
- // , getPossiblePrompts(){ return { cmd, username : true, password : true,
- // instanceName : true, instanceType : true, reposerver : true } }
- // }
- // ... PB : TODO -- use cmd.toArgs
- interpreted.cmd = this.cmd
- return interpreted
- }
- , getPossiblePrompts(){ return { username : true, password : true, instanceName : true, instanceType : true, reposerver : true } }
- , cmdFn : ()=>{ throw "Elxr Unknown command."}
- , finalized : true
- , toArgs : function( o ){
- // PB : TODO -- need to call this before selectedinstance is built. The instanceName is dependent on the specific cmd.
- return o }
- }
-
- function __createO(o){
- if(o.finalized) return o;
- var subs = Object.keys(cmds[o.cmd].subcommands || {})
- // console.log(subs)
- // console.log(o)
- subs.forEach(sub=>elxrcmd.create( cmds[subcommandlabelFor(o.cmd, sub)] ))
-
- var created = utils.assign_core( { keycase: true, arraymergetype : utils.assign_core.DISTINCT_UNION }
- , {}, __cmd, cmds[o.cmd], o)
- cmds[o.cmd] = created;
- created.cmdFn = op[o.cmd] || o.cmdFn || created.cmdFn
- op[o.cmd] = created.cmdFn;
- noprerequisites[o.cmd] = created.noprerequisites
- return created.cmdFn;
- }
-
- function create(o){ return __createO(o) }
- __cmdprototype.create = create;
-
- return __cmdprototype;
- })()
-
- var cmdFns = {
- resetgitconfig, reinit, init, g : op['g'], 'get-upstream' : op['get-upstream']
- }
-
- Object.entries(cmds).forEach( ca => {
- ca[1].cmd = ca[0]
- ca[1].cmdFn = ca[1].cmdFn || cmdFns[ca[0]]
- elxrcmd.create( ca[1] )
- })
-
- var __default = selectedinstance; // PB : TODO -- Eliminate __default references.
-
- Tasq.addlistener(statuslog.statuslog)
-
- // Instead of waiting for the root to be establised we start working at the current locatoin and then relocate when root changes.
- var runlogjson = `${selectedinstance.root}/.elxr/run-${runtimestamp}/run.log`
- ensureDirectoryExistence(runlogjson)
- fs.writeFileSync(runlogjson, JSON.stringify( { message : `Started ${runtimestamp}`, success:true})) // Initialize a new log file with "logrotate" for every run
- Tasq.addlistener((e)=>{ fs.writeFileSync(runlogjson, ', ' + JSON.stringify( e ), { 'flag': 'a+' }) })
-
- var nodeShellExec = cli.nodeShellExec;
- var chalk = require('chalk')
- const homedir = require('os').homedir();
-
- function sysAddPathVar(addpath){
- // Object.assign({
- // inherit: true, shell: true, env: ENV, title: `${command} ${args}`
- // }, options)
-
- // powershell $env:Path -split ';'
- var etask = ()=>{
- return nodeShellExec('Powershell.exe', [
- `$path = [Environment]::GetEnvironmentVariable('PATH', 'Machine')
- $newpath = $path + ';${addpath}'
- [Environment]::SetEnvironmentVariable("PATH", $newpath, 'Machine')`
- ]).then(() => {
- fs.writeFileSync('run.done', 'success') // PB : TODO -- IPC through files is needed only for windows.
- }).catch(() => {
- fs.writeFileSync('run.done', 'error')
- })
- }
- etask.statuslog = statuslog
- etask.selectedinstance = selectedinstance
- etask.processedArgs = processedArgs
- etask.runtimestamp = runtimestamp
- etask.ENV = ENV
- return shell_verse.runElevated(etask)
-
-
- // var newpath = addpath.split(';');
- // newpath.push(addpath)
- // newpath = Array.from(new Set(newpath)).join(';')
- // // addpath.split(';').forEach(pel => { var kv = pel.split('='); kv[0] === key ? null : newpath.push(pel); } )
- // // setx /M PATH "%PATH%;<path-to-executable-folder>"
- // // %AppData%\npm
- // return nodeShellExec('setx', ['/m', 'PATH', `%PATH%;${addpath}` ]);
- }
-
- // singleton one time lazy
- var getCredentials = function(){
-
- // try {
- // First call
- var creds = fs.readFileSync(path.normalize(`${homedir}/.elxrcredentials`), { encoding: 'utf8' });
- var creds = creds.split('\n').map( c => c.trim() && new URL(c));
- console.log(creds);
- creds.forEach(cred => {console.log( cred.hostname ); console.log( cred.username )} );
- // }
- // catch(e){
- // console.warn('Credentials not cached. You will be prompted for username and password.')
- // }
-
- // Subsequent calls
- getCredentials = ()=>{ return creds }
- return getCredentials();
- }
-
- // console.dir(processedArgs)
- // PB : TODO -- defaults for valuless arguments if passed.
- // Object.keys(processedArgs).forEach(a=>{
- // if(Object.prototype.toString.call(processedArgs[a]) === '[object Undefined]' || !processedArgs[a]) || trim(processedArgs[a])) == '') {
-
- // }
- // })
-
- const { readdir } = require("fs").promises
-
- // Directory shallow walk and do perform on each dir.
- const dirs = async (perform, src, target, options) => {
- var rs = []
- for (const dir of await readdir(src || selectedinstance.root, { withFileTypes: true })) {
- if (dir.isDirectory()) rs.push( perform(dir, src, target, options) )
- }
- return any(rs)
- }
-
- var fsMove = async function(from, to, options) {
-
- if(!options?.innernode) {
- var exists = fs.existsSync(from);
- var stats = exists && fs.statSync(from);
- var isDirectory = exists && stats.isDirectory();
- }
- else {
- var isDirectory = options.innernode.isDirectory()
- }
-
- if(!isDirectory) return fs.renameSync(from, to)
-
- fs.mkdirSync(to, { recursive : true })
-
- var rs = [];
- for (const innernode of await readdir(from, { withFileTypes: true })) {
- rs.push( fsMove( path.join(from, innernode.name), path.join(to, innernode.name), { innernode } ) )
- }
-
- return any(rs)
- };
-
-
-
- // PB : TODO -- Should return a bunch of promises to wait for...
- var copyrecursive = async function(src, target, options) {
-
- if(!options?.innernode) {
- var exists = fs.existsSync(src);
- var stats = exists && fs.statSync(src);
- var isDirectory = exists && stats.isDirectory();
- }
- else {
- var isDirectory = options.innernode.isDirectory()
- }
-
- if(!isDirectory) return fs.copyFileSync(src, target)
-
- fs.mkdirSync(target, { recursive : true })
-
- var rs = [];
- for (const innernode of await readdir(src, { withFileTypes: true })) {
- rs.push( copyrecursive( path.join(src, innernode.name), path.join(target, innernode.name), { innernode } )
- // .then(()=>{
- // return any(actions.map( a => ()=>a(dir, { isDirectory : true}) ))
- // })
- )
- }
-
- return any(rs)
-
-
- // return fswalk( src, {}, [
- // (src, options)=> {
- // if(options.innernode.isDirectory()) fs.mkdirSync(options.target, { recursive : true })
- // else fs.copyFileSync(src, options.target)
- // }
- // ])
-
- // var exists = fs.existsSync(src);
- // var stats = exists && fs.statSync(src);
- // var isDirectory = exists && stats.isDirectory();
- // if (isDirectory) {
- // fs.mkdirSync(dest, { recursive : true });
- // return dirs( function(childItemName) {
- // return copyrecursive(path.join(src, childItemName.name),
- // path.join(dest, childItemName.name));
- // }, src )
- // } else {
- // fs.copyFileSync(src, dest);
- // }
- };
-
- // var renamerecursive = function(from, to){
- // var exists = fs.existsSync(from);
- // var stats = exists && fs.statSync(from);
- // var isDirectory = exists && stats.isDirectory();
- // if (isDirectory) {
- // fs.mkdirSync(to);
- // dirs( function(childItemName) {
- // renamerecursive(path.join(from, childItemName),
- // path.join(to, childItemName));
- // }, from )
- // // fs.readdirSync(from).forEach(function(childItemName) {
- // // copyrecursive(path.join(from, childItemName),
- // // path.join(to, childItemName));
- // // });
- // } else {
- // fsrename(from, to);
- // }
- // }
-
-
- var regexreplaceall = function(content, ps){
- var repmatches
- var replaced = content;
- while (repmatches = ps.strOrregexp.exec(content)) {
-
- if(content.length > 0) {
- var replacement = repmatches[0]
- for(var m = 1; m < repmatches.length; m++) {
- replacement = replacement.replace(repmatches[m], ps.substitutes[m-1])
- }
- replaced = replaced.replace( repmatches[0], replacement )
- }
- }
- return replaced
- }
-
- // filesystem walk
- const fswalk = async (pathToWalk, options, actions) => {
- // options = options || { depthfirst : true }
-
- if(!options.innernode) {
- var exists = fs.existsSync(pathToWalk);
- var stats = exists && fs.statSync(pathToWalk);
- var isDirectory = exists && stats.isDirectory();
- }
- else {
- var isDirectory = options.innernode.isDirectory()
- }
-
- if(!isDirectory) return any(actions.map( a => ()=>a( pathToWalk, utils.assign({}, options
- , { pathToWalk, innernode : options.innernode || { isDirectory : function(){ return true } } }) ) ))
-
- var rs = [];
- for (const innernode of await readdir(pathToWalk, { withFileTypes: true })) {
- rs.push( fswalk( path.join(pathToWalk, innernode.name), utils.assign({}, options, {pathToWalk, innernode }), actions )
- // .then(()=>{
- // return any(actions.map( a => ()=>a(innernode, { isDirectory : true}) ))
- // })
- )
- }
- // Wait for the walk to complete before renaming directories.
- Array.prototype.push.apply(rs, actions.map( a => ()=>a( pathToWalk, utils.assign({}, options
- , { pathToWalk, innernode : options.innernode || { isDirectory : function(){ return true } } } ) ) ))
-
- return any(rs)
- }
-
- var fsrecurse = function(target, options, actions, root) {
- var exists = fs.existsSync(target);
- var stats = exists && fs.statSync(target);
- var isDirectory = exists && stats.isDirectory();
- if (isDirectory) {
- var tasks = []
- tasks.push( dirs( function(childItemName) {
- return fsrecurse(path.join(target, childItemName.name)
- , options, actions, root);
- }, target ))
- return any(tasks)
- } else {
- var prevActionResult = null;
- return any( actions.map( (action) => prevActionResult = action.apply(null, target, options) ))
- }
- };
-
- // var fslink = function(to, options){ try { fs.linkSync( to, options.from ) } catch(e) { throw e } }
- var fslink = function(to, options){ fs.symlinkSync( to, options.from /*, type = "junction"*/ ) }
-
- var fsrename = function(target, options){
- // PB : TODO -- streaming and async.. target could be a stream a string or a filesystem item or a tree node...
-
- var targetname = options.innernode?.name || path.parse(target).base;
- var replaced = targetname;
-
- options.patternsubstitutions.forEach( ps => { replaced = regexreplaceall( replaced, ps ) })
- if(replaced && replaced !== targetname) {
- var tr = target.replace( targetname, replaced )
- fs.renameSync(target, tr)
- return tr;
- }
- else return replaced
- }
-
- var fscontentreplace = function( target, options ){
- // do many replacements in one shot in the file.
- if(options.processingtype === 'inplace' && options.sourcetype === 'filesystem' && !options.innernode.isDirectory() ) {
- var content = fs.readFileSync(target, { encoding: 'utf8' }) // PB : TODO -- streaming and async..
- options.patternsubstitutions.forEach( ps => { content = regexreplaceall( content, ps ) })
- fs.writeFileSync(target, content)
- }
- }
-
- var templatelink = function(target, options){
- var targetSubPath = target.replace(options.workingtarget + '/', '' );
- var targetSubPathsplit = targetSubPath.split("/")
- var targetPathBaseDir = `${targetSubPathsplit[0]}/${targetSubPathsplit[1]}`
-
- var linkToSubPathReplacement = `${options.base}-server-lib/common`
-
- // var rgxp = new RegExp(`(__link__)${options.base}-server-lib`, "g")
- var rgxp = new RegExp(`(__link__${options.base}-server-lib)`, "g")
- var linkToSubPath = targetSubPath
- var b = target.match(rgxp)
- var linkFromName = regexreplaceall( target, { strOrregexp : rgxp, substitutes : [ '' ] } )
- linkFromName = linkFromName.replace(options.workingtarget, options.destinationroot)
-
- if( b ) {
- // Only if the pattern matches which is link instruction.
- linkToSubPath = regexreplaceall( linkToSubPath, { strOrregexp : rgxp, substitutes : [ '' ] } )
- }
-
- options.patternsubstitutions.forEach( ps => { linkToSubPath = regexreplaceall( linkToSubPath, ps ) })
- var linkToName = `${options.destinationroot}/${linkToSubPath}`
-
- if(b){
- linkToSubPath = linkToSubPath.replace( targetPathBaseDir, linkToSubPathReplacement)
-
- // PB : DONT DELETE EVERYTHING ??? Only the linkable ones... Or use an add strategy without deleting...
- fs.unlinkSync( target );
- fslink( `${options.destinationroot}/${linkToSubPath}`, { from : linkFromName } )
- }
- else {
- fsMove( `${options.workingtarget}/${linkToSubPath}`, linkToName )
- // fsMove( `${options.workingtarget}/${linkToSubPath}`, linkFromName )
- }
- }
-
- // Source and Target are files or folders. We need another version or a unifed version for strings..
- var processpatterns = function(target, options){
- // Accept a set of patterns and substitutions and operate on a target and apply all patterns substitutions.
-
- // options = options || { targettype : 'inplace', sourcetype : 'filesystem' }
- // options.source, options.target sourcetype = 'string' | 'filesystem' options.targettype = 'inplace' | 'copy'
-
- if(options.processingtype === 'inplace' && options.sourcetype === 'filesystem' ) {
-
- options.patternsubstitutions.forEach( ps => {
- // var replacement = regexreplaceall( from, ps.strOrregexp, ps.substitutes )
- // if(targetname) { // Porcess all files. Even if file names dont have patterns the content needs to be replaced.
- return options.tasks.map( t => { return t( target, ps ) } )
- // }
-
- // return str.replace(regex, function(match, key) {
- // console.log(`Found match, group ${match}: ${key}`);
- // return substitutes[key] || match;
- // })
- })
- }
-
- // while ((m = regex.exec(str)) !== null) {
- // // This is necessary to avoid infinite loops with zero-width matches
- // if (m.index === regex.lastIndex) {
- // regex.lastIndex++;
- // }
-
- // var rstr = str;
- // // The result can be accessed through the `m`-variable.
- // m.forEach((match, groupIndex) => {
-
- // // rstr = rstr.replace( groupIndex
- // console.log(`Found match, group ${groupIndex}: ${match}`);
- // if(groupIndex === 0) return
-
- // });
-
-
- // }
-
- }
-
- function copyFileSync( source, target , options) {
-
- var targetFile = target;
-
- // If target is a directory, a new file with the same name will be created
- if ( fs.existsSync( target ) ) {
- if ( fs.lstatSync( target ).isDirectory() ) {
- targetFile = path.join( target, path.basename( source ) );
- }
- }
-
- fs.writeFileSync(targetFile, fs.readFileSync(source));
- }
-
- function copyFolderRecursiveSync( source, target, options ) {
- var files = [];
-
- // Check if folder needs to be created or integrated
- var targetFolder = path.join( target, path.basename( source ) );
- if ( !fs.existsSync( targetFolder ) ) {
- fs.mkdirSync( targetFolder );
- }
-
- // Copy
- if ( fs.lstatSync( source ).isDirectory() ) {
- files = fs.readdirSync( source );
- files.forEach( function ( file ) {
- var curSource = path.join( source, file );
- if ( fs.lstatSync( curSource ).isDirectory() ) {
- copyFolderRecursiveSync( curSource, targetFolder );
- } else {
- copyFileSync( curSource, targetFolder );
- }
- } );
- }
- }
-
- var getShellTask = (command, args, options) => {
- options = options || {}
- var callshell = command === 'rm' ? getgitbashtask : getshelltask;
- return () => {
- var p = callshell( [command, args, Object.assign({
- inherit: true, shell: true, env: ENV, title: `${command} ${args}`
- }, options)
- ])()
- if (options.ignorefailures) {
- return p.catch(e => {
- // Ignore. Not a major error.
- })
- }
- else return p;
- }
- }
-
- var callshelltask = (args) => {
- // console.dir(args)
- if( Object.prototype.toString.call(args) === '[object Function]' ) {
- return args;
- }
- return getshelltask(args)()
- }
-
- var getCmdString = function(args){ return `"${args[0]} ${args[1].join(' ')}"` }
-
- var getshelltask = (args) => {
- return args[0] === 'rm' ? getgitbashtask(args) : () => {
- return nodeShellExec.apply(null, args).catch(function(e){
- e.benign = args[2].benign;
- if(!e.benign) { console.error(e); }
- throw e
- })
- }
- }
- var getgitbashtask = (args, onEachError) => { return () => {
- return nodeShellExec( `"${gitbash}"`, ['-c', getCmdString(args)], args[2]).catch( onEachError || function(e){ console.error(e) }) }
- }
-
- var getnodeshellexectask = (args) => { return () => { return nodeShellExec.apply(null, args) }}
- var perform = function( serailtasks, taskvector ){ return any( serailtasks(taskvector).map(getnodeshellexectask)) }
-
- var getTaskCheckExists = shell_verse.getTaskCheckExists
- // var getTaskWithElevation = function(tasdef){ return shell_verse.getElevatedTask( tasdef.elevatedpulltasks ) }
-
- // var getTaskWithoutElevation = function(tasdef){
- // return ()=>{
- // if(!processedArgs.runas) { return tasdef.regularpulltasks(); }
- // else Promise.resolve(true)
- // }
- // }
-
- var gitops = {
-
- getdiscoverbranchcmd : function(repo){
-
- var parameters = ['-c', 'branch=`git rev-parse --abbrev-ref HEAD`;for i in `git remote`; do git pull $i $branch; done;']
- var cmd = [gitbash
- , ['-c', 'branch=`git rev-parse --abbrev-ref HEAD`;for i in `git remote`; do git pull $i $branch; done;']
- , { cwd: instanceroot + '/' + repo, title: 'discoverbranch for ' + repo + ' ' + parameters.join(' ') }]
- return cmd
- }
- }
-
- var getPullCmd = (repodef, branch) => {
- // console.log(useGitPull)var getPullCmd = (repo, branch) => {
- // console.log(useGitPull)
- var repo = repodef.repo
- var pullCmd = []
- if(!branch) {
- // console.warn('No branch was specified detecting from working client.')
- // First check if working client exists.
- // if (existsSync(instanceroot + '/' + repo)) {
- pullCmd= gitops.getdiscoverbranchcmd(repo)
- // }
- // else performpull
- }
- // var pullCmd = [gitInstallDir
- // , ['-c', 'branch=`git rev-parse --abbrev-ref HEAD`;for i in `git remote`; do git pull $i $branch; done;']
- // , { cwd: instanceroot + '/' + repo, title: 'pull all remotes for ' + repo }]
- if(branch) {
- var parameters = ['-c', 'for i in `git remote`; do git pull $i ' + branch + '; done;']
- var pullCmd = [ gitbash
- , ['-c', 'for i in `git remote`; do git pull $i ' + branch + '; done;']
- , { cwd: instanceroot + '/' + repo, title : 'pull all remotes for ' + branch + ' ' + repo + ' ' + parameters.join(' ') }]
- }
- // var pullCmd = ['pullall', [], { cwd : repo }]
- if (useGitPull) pullCmd = ['git', ['pull'], {
- inherit: true, shell: true,
- cwd: instanceroot + '/' + repo
- // , env: ENV
- , runas: processedArgs.runas
- , title: `git pull ${repo}`
- }]
- return pullCmd
- }
-
- // PB : TODO -- Use queueing with async tasks..
- var performPull = (repodef, branch, repoowner, errHandler, elevatedBatch, regularBatch) => {
- var __inelevatedBatch = elevatedBatch, __inregularBatch = regularBatch;
- var pT = getPullTask(repodef, branch, repoowner, errHandler, elevatedBatch, regularBatch)
- // var waitForBatchAdditions = function(){
- // return pT;
- // }
- // Execute is implied.
- // if(false && (__inelevatedBatch || __inregularBatch) ) return waitForBatchAdditions() // PB : Todo Implemnt a trigger which starts execution.
- // else
- return pT() // Execute it.
- }
-
- var getPullTask = (repodef, branch, repoowner, errHandler, elevatedBatch, regularBatch) => {
- // PB : TODO -- Handle no branch passed in case.
- // if(!branch) { throw 'No branch specified' }
- var repo = repodef.repo;
- elevatedBatch = elevatedBatch || [];
- regularBatch = regularBatch || [];
- try{
- // PB : TODO -- local repo folder named the same as the remote repository name is not enough to establish existence.
- // This remote repo and branch may have been or may need to be added as a local tracking branch
- // to any other local repo folder (with a different name)
- var exists = existsSync(instanceroot + '/' + repo)
- }
- catch(e){
- exists = false
- console.log(e)
- }
-
- function initTask(etask){
- etask.info = { repo }
- etask.errHandler = errHandler
- etask.statuslog = statuslog
- etask.processedArgs = processedArgs
- etask.selectedinstance = selectedinstance
- etask.runtimestamp = runtimestamp
- etask.ENV = ENV
- }
-
- function initElevatedBatch(){
- elevatedBatch.info = { repo }
- elevatedBatch.errHandler = errHandler
- elevatedBatch.statuslog = statuslog
- elevatedBatch.processedArgs = processedArgs
- elevatedBatch.selectedinstance = selectedinstance
- elevatedBatch.runtimestamp = runtimestamp
- elevatedBatch.ENV = ENV
- }
-
- if (exists) {
-
- var tasks = [];
- return (()=>{
- // PB : TODO -- We need a non cacheable set of properties that need to default to something but then need to escplicitly specified in each run.
- if(selectedinstance.addremotes) {
- return op['get-remotes']({repo}).then(( remotes )=>{
- return op['get-upstream']({repo, remotes}).then(( upstream ) => {
- return __addremotesandpull({ upstream, remotes})
- })
- })
- }
- else return __pull();
- })()
-
- function __pull(){
- var branchprint = branch ? ' branch :' + branch : '';
- var task = ()=>{
- console.log('pulling ' + instanceroot + '/' + repo + branchprint )
- return any(tasks).then( ()=>{
- return nodeShellExec.apply(null, getPullCmd(repodef, branch)).then(() => {
- return true;
- })
- })
- }
- initTask(task)
-
- if(repodef.requiresElevation) {
- elevatedBatch.push(shell_verse.getElevatedTaskInBatch( task ));
- if(elevatedBatch.length === 1) { initElevatedBatch(); initTask(elevatedBatch[0]) } // PB : TDOO -- Make sure first task also has run context. May need to be moved to win_verse
- else initTask(elevatedBatch[elevatedBatch.length-1])
- return elevatedBatch[elevatedBatch.length-1]
- }
- else {
- regularBatch.push(shell_verse.getNonElevatedTask( task ))
- return regularBatch[regularBatch.length-1]
- }
- }
-
- function __addremotesandpull(options){
- // Add the remotes
- var tasks = []
-
- // selectedinstance.selectedremotes are optional and need not be enforced to be chosen. The selection for hte selected remotes
- // could also be derived from a crossfilter of remote-type selected for this run and the current chosen and operating reposervers.
- var selectedremotes = Array.from( new Set(selectedinstance.selectedremotes.concat(Object.keys(repodef.remotes || {}))));
- if(selectedremotes.length > 0) var selectedremoteFilter = function(rs) { return selectedremotes.find( rs.server ) }
- else var selectedremoteFilter = function(){ return true }
-
- var repoRemotes = Object.assign( {}, selectedinstance.remotes, options.remotes, options.upstream, repodef.remotes );
-
- // We need a most permissive filtered list to use as an efficient starting point to further reduce to whats actually needed.
- // However if such a cached list is not available we need to build it by scaning and apply all the filters anyway on the whole world.
-
- Object.keys(repoRemotes).forEach((remotename)=>{
- // PB : TODO -- ai prefix for all apis that require an answer amongst many choices.
- var ai_RemoteAddNeeded = (r)=>{
- if(r.server === reposerver && selectedremoteFilter() && r.accessibility.find( selectedinstance[ 'remote-type' ] )){ // currently chosen remotetype...
- var exists = false;
- if(exists = (options.remotes[r.remotename] || Object.keys(options.remotes).find( lrn => {
- var exists = options.remotes[lrn].server === r.server && !options.remotes[r.remotename]
- if(exists) console.warn('Found duplicate remote with a different name for the same server...')
- return exists
- } )) ) {
- return false // Already a remote not required.
- }
- return true // PB : TODO -- Sort and display highest priority target.remotes.sort( )
- }
- return false
- };
- tasks.push(
- ()=>{
- return op['remote exists']({
- remotename, repo, benign : true, ignorefailures : true
- , url : repoRemotes[remotename].url + repo
- , branch : checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV
- }).then( (r) => {
- if(!r[1]) return false; // PB : TODO -- Not accessible skip for now probably should remove.
- else if(r[0]) return true; // Already added nothing to do
- else if(ai_RemoteAddNeeded( repoRemotes[remotename] )) return op['remote add']( { remotename,
- 'set-upstream' : (selectedinstance['upstream'] || `${remotename}/${branch}`) // ??? when url accessible... in order of highest priority
- , reponame : repo
- , url : repoRemotes[remotename].url , branch : checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV
- })
- // else skipped as remote is not relevant for this repo...
- })
- }
- )
- })
-
- __pull()
- }
- }
- else {
- // PB : TODO -- detect if a clonable repo exists within the context of currentGitAuthUser on remote reposerver...
- console.log('cloning ' + repo)
- // PB : TODO -- add additional remotes after cloning and pull again...
-
- var task = ()=>{
- return nodeShellExec('git', ['clone', '-c', 'core.symlinks=true', selectedinstance.reposerver + `/${repodef.owner || repoowner || defaultRepoOwner}/` + repo + '.git'],
- {
- inherit: true, shell: true,
- env: ENV
- , cwd : instanceroot
- , runas: processedArgs.runas
- }).then(() => {
-
- var task = ()=>{
- return nodeShellExec('git', ['config', '--replace-all', 'core.symlinks', true],
- {
- inherit: true, shell: true,
- env: ENV
- , cwd: instanceroot + '/' + repo
- , runas: processedArgs.runas
- , title: `'git', ${['config', '--replace-all', 'core.symlinks', selectedinstance.username].join(' ')}`
- })
- }
-
- initTask(task)
-
- if(repodef.requiresElevation) {
- elevatedBatch.push(shell_verse.getElevatedTaskInBatch( task ));
- return elevatedBatch[elevatedBatch.length-1]
- }
- else {
- regularBatch.push(shell_verse.getNonElevatedTask( task ))
- return regularBatch[regularBatch.length-1]
- }
- })
- }
-
- initTask(task)
- if(repodef.requiresElevation) {
- elevatedBatch.push(shell_verse.getElevatedTaskInBatch( task ));
- return elevatedBatch[elevatedBatch.length-1]
- }
- else {
- regularBatch.push(shell_verse.getNonElevatedTask( task ))
- return regularBatch[regularBatch.length-1]
- }
- }
- }
-
- var currentGitAuthUser; // nodeShellExec('git', ['config', 'user.email']) ... PB : TODO-- get the current GITEA username
- var defaultRepoOwner = 'chess';
- var elevatedRunasRepos = null
- var gitRepos = null
-
- // grep -qxF 'alias elxr="node elxr/index.js"' ~/.bash_profile || echo 'alias elxr="node elxr/index.js"' >> ~/.bash_profile
- // nodeShellExec('echo', ['elxr'], { inherit : true}) //, {stdio: "inherit"}
-
- var dbForLabel = function (label) {
- var dbsForLabel = {
- devmysql: 'mysql'
- , development: 'mssql'
- , production: 'mssql'
- }
- return dbsForLabel[label] || 'mysql'
- }
-
- // Maps an environment to a branch. Not required if the branch is appropriately named.
- var checkoutMap = { 'development': 'master' }
-
-
- // SAM : TODO Use nodeshellexec where to detect git installation dir
- var gitbash = shell_verse.getbash()
- // var gitbash = "G:\\Installed\\Git\\bin\\sh.exe"
- // Relevant git repos
- var exludeMergeRepos = [];
- var useGitPull = processedArgs.useGitPull || false;
- var configPromise = null
-
- var reconfirmcmds = { create : true }
-
- var subcommandlabels = {
- switch : (`switch ${processedArgs._[1] || ''}`).trim()
- }
-
- var independentcmd = function(){
- return false;
- }
-
- var util = require('util');
- var cliname = 'elxr';
- var ver = '1.1';
- var readme = `
- Command syntax examples
-
- elxr
- Displays help. Same as elxr h
-
- elxr i
- Runs interactive propmts to help choose installation options.
-
- NODE_ENV={{yourenvironment}} elxr {{cmd}} {{instancename}} {{otheroptions}}
- General command syntax.
- Eg: NODE_ENV=development elxr pull elixir
-
- Note : Although {{instancename}} is generally a positional parameter supplied immediately after the cmd for most cmd's.
- The specific cmd defines what the value is interpreted as.
- There are several cmds that do not require an {{instancename}} parameter.
- Eg: elxr remote remove origin
- Git operations or passthrough external commands on all repository folders in you working directory.
-
- Main objectives.
- elxr cli is a wrapper around other shell commands and api based operations.
- One of the main objetives it achives is ease of use of repeating an individual cmd multiple times on all targets.
- Eg: A git operation like pull can be repeated consistently on a set of git repositories.
-
- `;
- var help = `
- ${readme}
-
-
- # list of commands... please refer dveloper documentation for ${cliname}
- \r\n\t${
- Object.keys(op).join('\r\n\t')
- }
- `;
-
- var elxr = {
- help() {
- return chalk.cyanBright(`
- -------------------------------------------------------------------------------
- *** BBH Elixir ***
- -------------------------------------------------------------------------------
- elxr ${ver}
- A cli tool for chess
- ${help}
- -------------------------------------------------------------------------------
- `)
- }
- , info() {
- return chalk.cyanBright(`
- -------------------------------------------------------------------------------
- *** BBH Elixir ***
- -------------------------------------------------------------------------------
- elxr ${ver}
- A cli tool for chess
- -------------------------------------------------------------------------------
- `)
- }
- , getpulltask(args){
-
- // def can be an instance config
- // Or an object with many repos and elevated repos
- // Or a single repo ( Either Elevated or normal. )
-
-
- // USAGE
- // elxr pull elixir origin master
- // elxr pull
-
- // not all remote branches are setup to be tracked locally (respective remotes and branches).
- // pull --all is useless. As it fetches all remotes and branches but only merges current branch.
- // apart from remote tracking branches We are interested in pulling from all remotes and relavant branches.
- // The branch pipline should feed back to master
- // master -> test -> stage -> release[nnnn] -> production
- // (master) <= git merge [all remotes] production && git merge [all remotes] release[nnnn]
- // git merge [all remotes] stage && git merge [all remotes] test && git merge [all remotes] master
-
- var __args = {
- remotename : args.remotename|| processedArgs._[2]
- , url : args.url || processedArgs._[3]
- , branch : args.branch || processedArgs._[4] // If branch not specified opearte on the current branch of each working directorys
- }
-
- // var commands = [
- // // ['git', ['checkout', __args.branch || checkoutMap[runconfig.NODE_ENV] || runconfig.NODE_ENV]]
- // ,
- // ]
-
- // var parameters = ['-c', 'for i in `git remote`; do git pull $i ' + branch + '; done;']
- // var pullCmd = [ gitbash
- // , ['-c', 'for i in `git remote`; do git pull $i ' + branch + '; done;']
- // , { cwd: instanceroot + '/' + repo, title : 'pull all remotes for ' + branch + ' ' + repo + ' ' + parameters.join(' ') }]
-
-
-
- var def = args || {
- repos : selectedinstance.repos,
- elevated : selectedinstance.elevated
- }
-
- var elevatedpulltasks = null;
- if(def.repo) {
- // Single repo case.
- if(def.repo.requiresElevation) {
- var t1 = function() {
- return performPull(def).then(() => {
- return true;
- }).catch((e) => {
- console.error(e)
- }).finally(Traq.finally)
- }
- t1.statuslog = statuslog
- t1.selectedinstance = selectedinstance
- t1.processedArgs = processedArgs
- elevatedpulltasks = shell_verse.getElevatedTask( t1 )
- }
- else {
- var t2 = function(){
- return performPull(def).then(() => {
- console.log('pulled ' + JSON.stringify(def.repo))
- return true;
- }).catch((e) => {
- console.error(e)
- })
- // .finally(Traq.finally)
- }
- t2.statuslog = statuslog
- var regularpulltasks = shell_verse.getNonElevatedTask( t2 )
- }
- if(elevatedpulltasks) return elevatedpulltasks //getTaskWithElevation( { elevatedpulltasks, regularpulltasks} )
- else return regularpulltasks // getTaskWithoutElevation({ regularpulltasks})
- }
-
- // console.log(`-------------------Processing pull for : ${def.repo} ${def.branch}`)
- console.dir(def)
- console.log('Running exlr pull : ' + path.dirname(__dirname))
-
- var useGitPull = processedArgs.useGitPull || false;
-
- if(def.elevated || def.repos) {
- if(def.elevated) def.requiresElevation = true;
- else delete def.requiresElevation;
- }
- else {
- if(def.requiresElevation) def.elevated = [def]
- else def.repos = [def]
- }
-
- var regularpulltasks = function(){ return Promise.resolve(true) }
-
- if(def.elevated){
- elevatedpulltasks = function() {
- var eBatch = []
- def.elevated.map((def) => getPullTask(def, null, null, null, eBatch))
- if(eBatch.length<1) return Promise.resolve(true);
- eBatch[0].processedArgs = processedArgs
- return shell_verse.runElevatedBatch(eBatch).then(() => {
- return true;
- }).catch((e) => {
- console.error(e)
- })
- }
- elevatedpulltasks.statuslog = statuslog
- }
- if(def.repos) {
- var regularpulltasks = function(){
- var pendingpulls = [];
- def.repos.forEach((def) => {
- pendingpulls.push(
- performPull(def).then(()=>{ console.log('pulled ' + JSON.stringify(def.repo)) })
- .catch(e => { console.log(e); throw e })
- )
- })
- return any(pendingpulls).finally(Traq.finally)
- }
- regularpulltasks.statuslog = statuslog
- }
- // if(elevatedpulltasks) return getTaskWithElevation( { elevatedpulltasks, regularpulltasks} )
- // else return getTaskWithoutElevation({ regularpulltasks})
- return elevatedpulltasks().then(()=>{
- if(isWin() && isElevated) return Promise.resolve(true);
- return regularpulltasks()
- })
- }
- }
-
- function preworkerconfig(){
- // Everything runs after this check is completed. Elevation occurs out of process when needed.
- gitRepos = selectedinstance.repos
- // gitRepos = ['chess-server-lib'];
-
- // Repositiories that have symlinks that require elevated priviletes in windows to create symlinks
- elevatedRunasRepos = selectedinstance.elevated
- // Repos that should excluded from merge for releases...
- exludeMergeRepos = selectedinstance.exludeMergeRepos
-
- // mysqldump --add-drop-table --no-data -u root -p db_name | grep 'DROP TABLE' ) > drop_all_tables.sql
- // mysql -u root -p db_name < drop_all_tables.sql
-
- var mysql = '../xampp/mysql/bin/mysql'
- var mysqldump = '../xampp/mysql/bin/mysqldump'
- }
-
-
- // The main elxr cli process
- function elxrworker() {
-
- var __runcmd = function (label) {
- var distinquishedlabel = subcommandlabels[label] || label
- return op[distinquishedlabel] ? op[distinquishedlabel](processedArgs) : null;
- }
- return __runcmd(processedArgs.label || processedArgs._[0] || 'undefined');
- }
-
- // function acquireChoices(selectedinstance) {
-
- // var hasconfig = false;
- // console.warn(chalk.cyan(`
- // -------------------------------------------------------------------------------
- // Could not locate your preferred configuration (you may not specified it)
- // You should fork the default chess configuration to customize and make it
- // your own instance with a named config as
- // {{yourowninstancename}}-config-{{yourchosenenvironment}}
- // And then run this tool as follows
- // NODE_ENV={{yourchosenenvironment}} elxr i {{yourowninstancename}}
- // OR
- // Run this tool with the following command to use a quick start default.
- // elxr --default
- // OR
- // Choose one of the options below to run interactively.
-
- // We will run your choice at the next prompt.
- // -------------------------------------------------------------------------------
- // `))
-
- // var choices = getInteractionPoints(detectedinstanceoptions, __interactive_prompts)
- // var todo = any( __interactive_prompts(selectedinstance, choices) ).then(()=>{
- // return initinstances(selectedinstance)
- // })
-
- // // var prompt = cli.prompter;
- // // return prompt.ask(`Choose an option :
- // // d) Install the default chess instance.
- // // => elxr i chess node_env=development --default
- // // n) Create your custom new instance interactively
- // // => elxr i {{instanceName}} node_env={{environment}}
- // // i) Choose an instance and environment to install
- // // => elxr i {{instanceName}} node_env={{environment}}
- // // c) Choose a command to run ( pull, use, i, npmi ... ) <= pull
- // // => elxr {{cmd}} {{instanceName}} node_env={{environment}}
- // // h) Help
- // // q) Quit
- // // Default <= d
- // // : `).then((choice) => {
- // // if (choice && choice === 'd' || !choice) {
- // // processedArgs._[0] = 'i'
- // // selectedinstance.instanceName = processedArgs._[1] = processedArgs._[1] || 'chess'
- // // selectedinstance.node_env = processedArgs.node_env = (ENV.NODE_ENV && ENV.NODE_ENV.trim()) || processedArgs.node_env || 'development'
- // // selectedinstance.reposerver = 'https://git.bbh.org.in'
- // // }
- // // else if (choice === 'h') {
- // // processedArgs._[0] = 'h'
- // // fs.writeFileSync('run.done', 'noop help');
- // // console.log(elxr.help()); process.exit()
- // // }
- // // else if (choice === 'n' || choice === 'i') {
- // // var p1 = cli.prompter;
- // // return p1.ask(`Enter Instance Name ( <= ${selectedinstance.instanceName} ) : `).then(function (instanceName) {
- // // processedArgs._[0] = 'i'
- // // selectedinstance.instanceName = processedArgs._[1] = instanceName || selectedinstance.instanceName;
- // // return p1.ask(`Enter Environment ( <= ${selectedinstance.node_env} ) : `).then(function (node_env) {
- // // selectedinstance.node_env = processedArgs.node_env = node_env || selectedinstance.node_env
- // // if (choice === 'n') {
- // // selectedinstance.reposerver = 'https://git.bbh.org.in'
- // // console.warn(
- // // chalk.magenta('No Option Available. Your account may not have privileges. You can request here http://git.bbh.org.in/chess'))
- // // process.exit();
- // // }
-
- // // return p1.ask(`Enter preferred repo server ( <= ${selectedinstance.reposerver || selectedinstance.reposervers[0]} ) : `).then(function (reposerver) {
- // // selectedinstance.reposerver = reposerver || selectedinstance.reposervers[0] || 'https://git.bbh.org.in'
- // // })
- // // })
- // // })
- // // } else if (choice === 'c') {
- // // var p1 = cli.prompter;
- // // return p1.ask(`Enter Instance Name ( <= ${selectedinstance.instanceName} ) : `).then(function (instanceName) {
- // // selectedinstance.instanceName = processedArgs._[1] = instanceName || selectedinstance.instanceName
- // // return p1.ask(`Enter Environment ( <= ${selectedinstance.node_env} ) : `).then(function (node_env) {
- // // selectedinstance.node_env = processedArgs.node_env = node_env || selectedinstance.node_env
- // // return p1.ask(`Enter cmd :
- // // p) pull
- // // Default <= p
- // // : `).then(function (cmd) {
- // // if (!cmd || cmd === 'p') {
- // // processedArgs._[0] = 'pull'
- // // }
- // // else processedArgs._[0] = cmd
- // // return p1.ask(`Enter preferred repo server ( <= ${selectedinstance.reposerver || selectedinstance.reposervers[0]} ) : `).then(function (reposerver) {
- // // selectedinstance.reposerver = reposerver || selectedinstance.reposerver || selectedinstance.reposervers[0] || 'https://git.bbh.org.in'
- // // })
- // // })
- // // })
- // // })
- // // } else {
- // // console.log(chalk.gray(`Default option not exercised. Please follow manual instructions to customize your instance here http://git.bbh.org.in/chess and try again.`));
- // // fs.writeFileSync('run.log', ', ' + JSON.stringify({ success: 'quit without execution' }), { 'flag': 'a+' })
- // // fs.writeFileSync('run.done', 'noop quit');
- // // process.exit()
- // // }
- // // })
- // }
-
-
- var os = require("os");
- var hostname = os.hostname();
- var clustername = null; // PB : TODO -- Prompt to acquire.
- var configs = (function(){
- // We need to discover and use the most specialized config available.
- // Two passes are required as the config currently cloned may be switched by the discovery.
- // If the repo name is the same but has different remotes the newly acquired config from multiple remotes may
- // iteself redefine the remotes, repos and servers for the config which we need to honor..
- return {
- ownerClusterNodeConfig(selected) {
- // Hostname and therefore clusternodename can be anything and can participate in many clusters.
- // Also many cluster node instance can be running as a set of services on the same host on the same port with ( specific domain names )
- var clusternodename = hostname || `CHESS0001${selected.username}`; // Cluster node names need to be unique accross all clusters to avoid collitions.
- clustername = clustername || `${selected.username}-CHESS-${selected.node_env}`;
- return __acquireConfig(selected, { remote : 'userfork' } // PB : TODO -- accessiblity state should switch to public etc..
- , selected.instanceName + '-config-' + selected.node_env + `-${clusternodename}`
- , function(e){ console.info('Customized node level config not found. This is not an Error. Will attempt with owner level config.');
- e.benign = true
- return e; }
- )
- }
- , ownerInstnace(selected) { return __acquireConfig(selected, { remote : 'userfork' }
- , selected.instanceName + '-config-' + selected.node_env
- , function(e){ console.info('Customized user level config not found. This is not an Error. Will attempt global common instance config.');
- e.benign = true
- return e }
- )
- }
- // PB : TODO -- Use the ORG level instance before falling back to common Instance coz common instance may not exist for certain orgs.
- , commonInstance(selected) { return __acquireConfig(selected, { repoOwner : defaultRepoOwner }
- , undefined
- , function(e){
- // PB : TODO -- console.info('This is probably an error unless the user is asking to create a new instance with this name.')
- statuslog.statuslog(e, e)
- var manifestpath = path.normalize(selected.root + '/' + selected.instanceName + '-config-' + selected.node_env + '/repo-manifest');
- utils.assign_core( { keycase : true, arraymergetype : utils.assign_core.DISTINCT_UNION }
- , selectedinstance, require(manifestpath)( null, { utils, username : selectedinstance.username, instanceName : selectedinstance.instanceName
- , node_env : selectedinstance.node_env, reposerver : 'https://git.bbh.org.in' } ))
- console.dir(selectedinstance.repos)
- // Config from server always override merges into selection except for the current selection.
- // PB : TODO -- utils.assign Array merges are non-distinct...
- if(selectedinstance?.repos && !selectedinstance.repos[0].repo) {
- console.warn('repo manifest has obsolete format. Attempting upgrade.')
- selectedinstance.repos = selectedinstance.repos.map(function(repo){ return { repo } })
- }
- if(selectedinstance?.elevated && !selectedinstance.elevated[0]?.repo) {
- console.warn('elevated repo manifest has obsolete format. Attempting upgrade.')
- selectedinstance.elevated = selectedinstance.elevated.map(function(repo){ return { repo } })
- }
- chessinstances[selected.instanceName][selected.node_env] = selectedinstance = __g.selectedinstance = utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }
- , selected, selectedinstance)
- chessinstances[selected.instanceName][selected.node_env].reposervers = Array.from(new Set(chessinstances[selected.instanceName][selected.node_env].reposervers))
- selectedinstance.reposerver = selectedinstance.reposerver || selectedinstance.reposervers[0] // PB : TODO -- Attempt first one that is available and online...
- cacheWriteInstanceConfig(chessinstances, selectedinstance.root, selectedinstance)
- // ENV.NODE_ENV = selectedinstance.node_env;
- throw e
- }
- ) }
- // , genericChessInstance(selected) { return __acquireConfig(selected, { repoOwner : defaultRepoOwner }) }
- }
- })()
-
- var eNotImplemented = function(){
- console.error('Not yet implemented')
- process.exit()
- }
-
- // PB : TODO -- accept additional arg - an array specifying custom configPriority.
- var acquireConfig = function(slections){
- var configPriority = [ 'ownerClusterNodeConfig', 'ownerInstnace', 'commonInstance' /*, 'genericChessInstance'*/ ]
- return any(configPriority.map(cfg => { return function() { return configs[cfg](slections) } } ), true, true).then(()=>{
- return acquireData(slections)
- })
- }
-
- var instanceData = (function(){
- return {
- ownerClusterNodeData(selected) {
- var clusternodename = hostname || `CHESS0001${selected.username}`; // Cluster node names need to be unique accross all clusters to avoid collitions.
- clustername = clustername || `${selected.username}-CHESS-${selected.node_env}`;
- return __acquireData(selected, { remote : 'userfork' }
- , selected.instanceName + '-data-' + selected.node_env + `-${clusternodename}`
- , function(e){ console.info('Customized node level data not found. This is not an Error. Will attempt with owner level config.');
- e.benign = true
- return e; }
- )
- }
- , ownerInstnace(selected) { return __acquireData(selected, { remote : 'userfork' }
- , selected.instanceName + '-data-' + selected.node_env
- , function(e){ console.info('Customized user level data not found. This is not an Error. Will attempt global common instance config.');
- e.benign = true
- return e }
- )
- }
- // PB : TODO -- Use the ORG level instance before falling back to common Instance coz common instance may not exist for certain orgs.
- , commonInstance(selected) { return __acquireData(selected, { repoOwner : defaultRepoOwner }
-
- // , function(e){ console.info('This is probably an error unless the user is asking to create a new instance with this name.') }
- ) }
- , genericChessInstance(selected) { return __acquireData(selected, { repoOwner : defaultRepoOwner }) }
- }
- })()
-
- var acquireData = function(slections){
- var configPriority = [ 'ownerClusterNodeData', 'ownerInstnace', 'commonInstance' /*, 'genericChessInstance'*/ ]
- return any(configPriority.map(cfg => { return function() { return instanceData[cfg](slections) } } ), true, true)
- }
-
-
- function reinit(){
- sysAddPathVar('%AppData%\\npm')
- resetgitconfig()
- }
-
- function init(){
- sysAddPathVar('%AppData%\\npm')
- resetgitconfig()
- }
-
-
- function resetgitconfig(){
-
- // https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage
- // Git Credential storage...
- // git config --global credential.helper 'store --file ~/.gitcredentials'
- // git credential fill
- // protocol=https
- // host=git.bbh.org.in
- // username=bob
- // password=pppp
- // git credential-store --file ~/.gitcredentials store
-
- // // notepad C:/Program Files/Git/etc/gitconfig
- // git config --edit --system
-
- // git credential-store --file ~/git.store store
-
- // Find system git config
- // git config --global --edit
- // git config --list --show-origin
- // git config --list --show-origin --show-scope
-
-
- // https://stackoverflow.com/questions/35942754/how-can-i-save-username-and-password-in-git
- // Recipie
- // git config --global --unset credentials.helper
- // cd /path/to/my/repo
- // git config --unset credential.helper
- // git config credential.helper 'store --file ~/.git_repo_credentials'
- // git config credential.*.username my_user_name
- // git config credential.https://gitlab.com.username my_user_name
-
- // git credential fill
- // git config --global credential.modalprompt false // doesnst work.
- // core askpass = ;;; https://stackoverflow.com/questions/37182847/how-do-i-disable-git-credential-manager-for-windows
- var options = {
- inherit: true, shell: true, env: ENV
- , cwd: instanceroot
- , runas: processedArgs.runas
- }
-
- var task = ['git', ['config'], options]
-
- var tasklist = [
- // ['--global', '--unset credentials.helper']
- // ['--unset', 'credentials.helper']
- // ,
- // ['credential.helper', `'store --file git_repo_credentials'`]
- ['--global', 'credential.helper', "'store --file ~/.git_repo_credentials'"]
- , ['--global', '--replace-all', 'user.name', 'pb']
- , ['--global', '--replace-all', 'user.email', 'pradeep@bbh.org.in']
-
- // , ['--list']
- // , ['credential', 'fill']
- ]
- var onEachError = e => console.error( e.messages.join('\n') + e.result + '\n' + util.inspect(e) + '\n')
- var shellT = (args) => { return getgitbashtask(args, onEachError)
- // .catch(e => console.error( e.messages.join('\n') + e.result + '\n' + util.inspect(e) + '\n' + tasq.toString()) )
- }
- execserial(tasklist, task, shellT, onEachError )
- }
-
- var createTasq = (args, shellT, onEachError) => {
- var tasq = shellT ? shellT(args) : (() => { return nodeShellExec.apply(null, args).catch( onEachError || function(e){ console.error(e) } ) })
- tasq.toString = function(){ return JSON.stringify(args)}
- return tasq;
- }
-
- var execserial = function(tasklist, task, shellT){
- var exec = (taskArgs)=>{
- var thistask = task.concat();
- thistask[1] = thistask[1].concat()
- thistask[1].push.apply(thistask[1], taskArgs)
- return createTasq(thistask, shellT)
- }
-
- return any(tasklist.map(exec))
- }
-
- var execonce = function(taskArgs, task){ return any([task[1].push.apply(task[1], taskArgs)].map(createTasq)) }
-
-
- var __acquireConfig = function (selected, options, configrepo, errHandler) {
-
- configrepo = configrepo || selected.instanceName + '-config-' + selected.node_env;
- var repoOwner = options.repoOwner || selected.username || options.defaultRepoOwner
-
- var errorHandler = (e) => {
- if(e.messages.join(' ').match(new RegExp (`fatal: unable to access '${selectedinstance.reposerver}/${repoOwner}/${configrepo}.git/': Failed to connect to .*? port .*? after .*? ms: Timed out`))){
- // console.error('Could not connect to repo server. Timed Out')
- return cli.prompt( ['(y)es', '(n)o', '(r)etry'], 'Could not connect to repo server. Timed Out. Would you like to switch server ? (y/n) ', 'y' ).then(propValue => {
-
- if(propValue === 'y') {
- reconfirm = getReconfirmAll()
- return startElxr()
- }
- else if(propValue === 'r'){
- return acquireConfig(selected)
- }
- else process.exit()
- })
- }
-
- if(e.messages.join(' ').match(new RegExp (`fatal: repository '${selectedinstance.reposerver}/${repoOwner}/${configrepo}.git/' not found`))){
- var choices = {
- t : `install a new temporary local instance with this name ( will not persist ).
- Use your own username for additional options. You can request for a username at chess@bbh.org.in )`
- , e : 'exit' }
-
- if(selectedinstance.username !== 'guest' && selectedinstance.username !== 'demo') {
- choices = utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }
- , {
- i : 'create a new instance with this name => will fork the default config under your username'
- , f : 'fork a new instance with this name for yourself for this node from another instance'
- , o : 'fork a new instance with this name for your organization from another instance' // prompt organization name...
- , c : 'create a custom config for yourself for this node' // prompt hostname as nodename
- , p : 'create a custom config for yourself '
- }, choices)
- }
-
- return cli.prompt( choices
- , 'Config for instance not found. Would you like to ', 'e' ).then(propValue => {
-
- if(propValue === 't') {
- selectedinstance.local = true; return createInstance(selectedinstance)
- }
- if(propValue === 'i') return eNotImplemented()
- if(propValue === 'f') return eNotImplemented()
- if(propValue === 'o') return eNotImplemented()
- if(propValue === 'c') return eNotImplemented() // return createLocalChessInsance(selectedinstance)
- if(propValue === 'p') return eNotImplemented() // return createChessInstance(selectedinstance)
- // if(propValue === 'o') createChessInstance(selectedinstance, orgname)
- else process.exit()
- })
- }
-
- console.warn(e)
- throw e; //('Config acquisition failed.')
- }
-
- var successHandler = () => {
-
- var manifestpath = path.normalize(selected.root + '/' + selected.instanceName + '-config-' + selected.node_env + '/repo-manifest');
- utils.assign_core( { keycase : true, arraymergetype : utils.assign_core.DISTINCT_UNION }
- , selectedinstance, require(manifestpath)( null, { utils, username : selectedinstance.username, instanceName : selectedinstance.instanceName
- , node_env : selectedinstance.node_env, reposerver : selectedinstance.reposerver }))
- console.dir(selectedinstance.repos)
- // Config from server always override merges into selection except for the current selection.
- // PB : TODO -- utils.assign Array merges are non-distinct...
- if(!selectedinstance.repos[0].repo) {
- console.warn('repo manifest has obsolete format. Attempting upgrade.')
- selectedinstance.repos = selectedinstance.repos.map(function(repo){ return { repo } })
- }
- if(selectedinstance.elevated[0] && !selectedinstance.elevated[0].repo) {
- console.warn('elevated repo manifest has obsolete format. Attempting upgrade.')
- selectedinstance.elevated = selectedinstance.elevated.map(function(repo){ return { repo } })
- }
- chessinstances[selected.instanceName][selected.node_env] = selectedinstance = __g.selectedinstance = utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }
- , selected, selectedinstance)
- chessinstances[selected.instanceName][selected.node_env].reposervers = Array.from(new Set(chessinstances[selected.instanceName][selected.node_env].reposervers))
- selectedinstance.reposerver = selectedinstance.reposerver || selectedinstance.reposervers[0] // PB : TODO -- Attempt first one that is available and online...
- cacheWriteInstanceConfig(chessinstances, selectedinstance.root)
- ENV.NODE_ENV = selectedinstance.node_env;
- }
-
- return performPull( selected.reposindexed[configrepo] || { repo : configrepo }
- , null, repoOwner, errHandler || errorHandler || ((e)=>{ throw e })).then( successHandler )
- .catch( (e)=>{
- // if(e){
- if(Promise.resolve(e) === e) return e;
- // console.error(e)
- e.benign = true;
- throw e; // Not a hard error but we need this for bcontinueonfailure as we still want to stop on firstsuccess.
- // }
- })
- }
-
- var __acquireData = function (selected, options, datarepo, errHandler) {
-
- datarepo = datarepo || selected.instanceName + '-data';
- var repoOwner = options.repoOwner || selected.username || options.defaultRepoOwner
-
- var errorHandler = (e) => {
- if(e.messages.join(' ').match(new RegExp (`fatal: unable to access '${selectedinstance.reposerver}/${repoOwner}/${datarepo}.git/': Failed to connect to .*? port .*? after .*? ms: Timed out`))){
- // console.error('Could not connect to repo server. Timed Out')
- return cli.prompt( ['(y)es', '(n)o', '(r)etry'], 'Could not connect to repo server. Timed Out. Would you like to switch server ? (y/n) ', 'y' ).then(propValue => {
-
- if(propValue === 'y') {
- reconfirm = getReconfirmAll()
- return startElxr()
- }
- else if(propValue === 'r'){
- return acquireConfig(selected)
- }
- else process.exit()
- })
- }
-
- if(e.messages.join(' ').match(new RegExp (`fatal: repository '${selectedinstance.reposerver}/${repoOwner}/${datarepo}.git/' not found`))){
- var choices = {
- t : `install a temporary local data folder.
- For more options. Request and use a personal username at chess@bbh.org.in )`
- , e : 'exit' }
-
- if(selectedinstance.username !== 'guest' && selectedinstance.username !== 'demo') {
- choices = utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }
- , {
- i : 'create a new instance with this name => will fork the default config under your username'
- , f : 'fork a new instance with this name for yourself for this node from another instance'
- , o : 'fork a new instance with this name for your organization from another instance' // prompt organization name...
- , c : 'create a custom config for yourself for this node' // prompt hostname as nodename
- , p : 'create a custom config for yourself '
- }, choices)
- }
-
- return cli.prompt( choices
- , 'Data repo for instance not found. Would you like to ', 'e' ).then(propValue => {
-
- if(propValue === 't') {
- selectedinstance.local = true; return createInstanceData(selectedinstance)
- }
- if(propValue === 'i') return eNotImplemented()
- if(propValue === 'f') return eNotImplemented()
- if(propValue === 'o') return eNotImplemented()
- if(propValue === 'c') return eNotImplemented() // return createLocalChessInsance(selectedinstance)
- if(propValue === 'p') return eNotImplemented() // return createChessInstance(selectedinstance)
- // if(propValue === 'o') createChessInstance(selectedinstance, orgname)
- else process.exit()
- })
- }
-
- console.warn(e)
- throw e; //('Config acquisition failed.')
- }
-
- var successHandler = () => {
-
- }
-
- return performPull( selected.reposindexed[datarepo] || { repo : datarepo }
- , null, repoOwner, errHandler || errorHandler || ((e)=>{ throw e })).then( successHandler )
- .catch( (e)=>{
- // if(e){
- if(Promise.resolve(e) === e) return e;
- // console.error(e)
- e.benign = true;
- throw e; // Not a hard error but we need this for bcontinueonfailure
- // }
- })
- }
-
- // ----------------------------------------------------------------
- // PB : TODO -- Review and obsolete...
- // ----------------------------------------------------------------
- // var launchpath = path.resolve(path.normalize(ENV.wd))
- var launchpath = selectedinstance.root
- // // we were run.
- // // The easisest would be to ask for a target directory and default to current dir....
- // // path.dirname(launchpath).split(path.sep).pop()
-
- // var parent = path.dirname(launchpath);
- // var pp = launchpath;
- // var instancediscoverytasks = [];
- // while(parent !== pp){
- // instancediscoverytasks.push(
- // ((pa)=>{
- // var p = pa;
- // return function(){
- // return hasElxr(p).then((value)=>{
- // if(value) return p
- // // PB : TODO -- Using throw like this is not friendly with any...
- // throw Object.assign( new Error('Benign Failure in Batch')
- // , { value : false, benign : true });
- // })
- // }
- // })(pp)
- // )
- // pp = parent;
- // parent = path.dirname(parent);
- // }
-
- // var instanceroot = null
- // var detectInstanceRoot = any(instancediscoverytasks, true, true).then( ir => {
- // if(ir.error) { instanceroot = path.normalize(thisscriptdir) === path.normalize(launchpath) ? path.normalize(thisscriptdir + '/..') : launchpath ; }
- // else instanceroot = ir.pVal
- // return instanceroot
- // }).catch(()=>{
- // instanceroot = path.normalize(thisscriptdir) === path.normalize(launchpath) ? path.normalize(thisscriptdir + '/..') : launchpath ;
- // })
-
- // We first load the default and then override with a runconfig if it exists else we override with the interactive prompts.
- // Then acquire and reload and replace this default.
-
- var __interactive_prompts = function( target, choices, promptsfilter ){
-
- Object.defineProperty(target, 'node_env', { get : function(){ return this.instanceType } });
-
- var interactionpoints = unbound_interactionpoints.bind({target, choices})(selectedinstance)
-
- function getPromptKeys() {
- // PB : TODO -- get interactionpoints
- if(! (processedArgs.label || processedArgs._[0]) ) return Object.assign({}, interactionpoints);
-
- return promptkeys // PB : TODO __ Convert pormptkeys to filtered interactionpoints.
- }
- var eachPrompt = getBoundEachPrompt( target, getPromptKeys() , interactionpoints, choices, promptsfilter)
- return Object.keys(promptsfilter || interactionpoints).reduce(eachPrompt, [])
- }
-
-
- var downloadsdir = '../Downloads';
- var prerequisites = [
- {
- shellcmd: 'git',
- name : 'git',
- url: 'https://github.com/git-for-windows/git/releases/download/v2.31.0.windows.1/Git-2.31.0-64-bit.exe'
- , installer: 'Git-2.31.0-64-bit.exe'
- , installcmd: ['cmd', ['/c', 'start',
- '/WAIT', path.resolve(downloadsdir + '/' + 'Git-2.31.0-64-bit.exe')
- , '/VERYSILENT'
- // , '/MERGETASKS=!runcode' // This is required only for vscode...
- ]]
- , preinstallsteps: function() {
- var steps = [
- () => {
- if (!existsSync(downloadsdir + '/' + this.installer)) {
- return nodeShellExec(`${selectedinstance.root}/.elxr/run-${runtimestamp}/download.bat`, [this.url, downloadsdir + '/' + this.installer])
- }
- else return Promise.resolve(true)
- }
- ]
- var prompts = this.getuser(null, ()=>{
- // console.log('preinstallsteps')
- // var gitUser = 'guest';
- // var gitEmail = 'guest@bbh.org.in';
- // var prompts = [];
- // prompts.push( ()=>{ return cli.prompt(choices['username'], 'git user name').then(gituser => gitUser = gituser) } )
- // prompts.push( ()=>{ return cli.prompt(choices['useremail'], 'git user email').then(gitemail => gitEmail = gitemail) } )
-
- // console.log('prompting in preinstallsteps')
- // return any(prompts).then(()=>{
- // var steps = [
- // ['git', ['config', '--global', '--add', 'user.name', `${gitUser}`]]
- // , ['git', ['config', '--global', '--add', 'user.email', `${gitEmail}`]]
- // ]
- // return any(steps.map(getshelltask)).then(() => {
-
- // })
- // });
-
- return Promise.resolve(true)
- })
-
- return any([any(steps), prompts])
- }
- , installsteps: function () {
- return any([this.installcmd].map(getshelltask))
- }
- , postinstallsteps: function(err, users){
- return this.getuser(null, (err, data)=>{
- // ignore err and proceed with data as guest.
- console.dir(data)
- if(data && data.length === 1 && !shouldPrompt('username', promptkeys, selectedinstance) ) {
- return username = data[0];
- }
-
- data = data || ['guest']
- choices['username'] = Array.from( new Set( data.concat( (choices['username'] || []) )) )
-
- console.log('prompting in postinstallsteps')
-
- var username = 'guest';
- var email = 'guest@bbh.org.in';
- var password = '***';
-
- return any( getInteractionPoints([selectedinstance], promptkeys, { username, password, email}) ).then(()=>{
- if(!data.find(e => e === selectedinstance.username)) {
- var steps = [
- ['git', ['config', '--global', '--add', 'user.name', `${username}`]]
- , ['git', ['config', '--global', '--add', 'user.email', `${email}`]]
- ]
- return any(steps.map(getshelltask)).then(() => {
-
- })
- }
- else {
- return Promise.resolve(true)
- }
- });
- })
- }
- , install: function () {
- return any([ /*this.preinstallsteps,*/ this.installsteps.bind(this), this.postinstallsteps.bind(this)])
- }
- , verifyAndInstall : function(){
- return getTaskCheckExists(this.shellcmd, { ignorefailures: true })().then((exists) => {
- if(exists) {
- // return any(['git', ['config', '--global', '-l']].map(getshelltask))
- return this.postinstallsteps.bind(this)()
- }
- return this.install();
- });
- }
- , getuser : function(repo, onResult){
-
- var __onResult = onResult || function(e, data){
- data = data || []
- choices['username'] = Array.from( new Set( data.concat( (choices['username'] || []) )) )
- // console.dir(choices)
- return data[0]
- }
-
- var globalOrLocal = '--global';
- if(repo) globalOrLocal = '--local'
-
- return any([['git', ['config', globalOrLocal, '--get-all', 'user.name']]].map(getshelltask)).then((result)=>{
- // not yet configured.
- if(!result.success) return __onResult(result)
- else {
- var users = result.messages[0].trim().split('\n');
- if(users.length === 0 ||
- users.length === 1 && users[0] === 'guest') {
-
- return __onResult(result)
- }
- else {
- return __onResult(null, Array.from( new Set( users ) ) ); }// PB : TODO == We should probably prompt with all the users available for selection !
- }
- })
- .catch((e)=>{
- console.log(e)
- return __onResult(e)
- })
- }
- }
- ,
- {
- shellcmd: 'node',
- name : 'node',
- url: 'https://nodejs.org/dist/v14.16.0/node-v14.16.0-x64.msi'
- , installer: 'node-v14.16.0-x64.msi'
- , installcmd: ['MSIEXEC.exe', ['/i'
- , path.resolve(downloadsdir + '/' + 'node-v14.16.0-x64.msi')
- , 'ACCEPT=YES', '/passive']]
- , install : function() { return any([this.installcmd].map(getshelltask)).then(() => { }) }
- }
- ]
- for(var i=0; i<prerequisites.length; i++) {
- prerequisites[prerequisites[i].name] = prerequisites[i];
- }
-
- prerequisites.forEach(p=>{ prerequisites[p.shellcmd] = p })
-
- function ensureDirectoryExistence(filePath) {
- var dirname = path.dirname(filePath);
- if (fs.existsSync(dirname)) {
- return filePath;
- }
- ensureDirectoryExistence(dirname);
- fs.mkdirSync(dirname);
- return filePath;
- }
-
- var mainTasks = [];
- function verifyAndInstallPrerequisites() {
- var downloadtasks = [];
- var installtasks = [];
- prerequisites.forEach(preq => {
- downloadtasks.push(getTaskCheckExists(preq.shellcmd, { ignorefailures: true })().then((exists) => {
- if (exists) console.log(`${preq.shellcmd} exists`)
- else {
- console.log(`${preq.shellcmd} is not installed`)
- return preq.preinstallsteps.call(preq).then(() => {
- installtasks.push(preq.install.bind(preq))
- })
- }
- }))
- })
- return Promise.all(downloadtasks).then(() => { return any(installtasks) })
- }
-
- // function updateselection(selected) { selectedinstance = utils.assign(selectedinstance, selected) }
- var runconfig = null;
-
- var promptkeys = {
- cmd : processedArgs._[0] || 'pull'
- // Try not to prompt anything unless absolutely necessary or reconfirm is forced.
- // 'instanceName' : true
- // , 'node_env' : true
- // , username : ''
- // , runchoice : 'c'
- }
- // promptkeys.runchoice = promptkeys.cmd ? 'c' : undefined
-
- var choices = {
- 'instanceName' : []
- , 'reposerver' : []
- , 'upstream-remote' : []
- , 'instanceType' : []
- , username : ['guest', 'chessdemo', 'demo']
- }
-
- var getInteractionPoints = function(detectedinstanceoptions, possiblePrompts, promptsfilter){
-
- var instances = []
- var reposervers = [];
- var remotes = [];
- var instanceNames = []
- var instanceTypes = ['development', 'production'];
-
- // Accumulate all known choices from every possible instance...
- Object.keys( chessinstances).forEach(instanceName => {
- if(instanceName === 'current_run') return ;
- Object.keys( chessinstances[instanceName] ).forEach(node_env=>{
- var instance = chessinstances[instanceName][node_env];
- reposervers = reposervers.concat(instance.reposervers)
- if(instance.reposerver) reposervers.push(instance.reposerver)
- instances.push(instance)
- instanceTypes.push(instance.node_env)
- instanceNames.push(instance.instanceName)
- var otherremotes = instance?.reposerverinstances
- if(otherremotes) otherremotes = otherremotes[instance?.reposerver]?.remotes
- otherremotes = otherremotes || {}
- Array.prototype.push.apply(remotes, Object.keys(otherremotes))
- })
- })
- instances = instances.concat(detectedinstanceoptions)
-
- if(selectedinstance['instanceName']) instanceNames.push(selectedinstance['instanceName'])
- if(possiblePrompts['instanceName']) instanceNames.push(possiblePrompts['instanceName'])
- if(selectedinstance['reposervers']) reposervers = reposervers.concat(selectedinstance['reposervers'])
-
- choices['instanceName'] = Array.from( new Set(instanceNames.concat(choices['instanceName'])) )
- choices['reposerver'] = Array.from( new Set(reposervers.concat(choices['reposerver'])) )
- // choices['upstream-remote'] = Array.from( new Set(remotes.concat(choices['upstream-remote'])) )
- choices['instanceType'] = Array.from( new Set(instanceTypes.concat(choices['instanceType'])) )
-
- return __interactive_prompts(selectedinstance, choices, promptsfilter)
- }
-
- var detection_state = {
- localInstanceDetected : false
- }
-
- const https = require('https')
- const http = require('http');
- const { Console } = require('console');
- const { env } = require('process');
- const RESTAPI = (function(){
-
- // Singleton
- function RESTAPI(){}
- // RESTAPI.create = RESTAPI; // Returns the one singe instance which is the class itself
- // const httpsoptions = {
- // hostname: 'whatever.com',
- // port: 443,
- // path: '/todos',
- // method: 'POST',
- // headers: {
- // 'Content-Type': 'application/json',
- // 'Content-Length': data.length
- // }
- // }
-
- RESTAPI.method = function(options, resolve, reject){
- options.headers = options.headers || { 'Content-Type': 'application/json' }
-
- const data = options.payload ? new TextEncoder().encode( JSON.stringify(options.payload) ) : null
- options.headers = options.payload ? (options.headers || {
- 'Content-Type': 'application/json',
- 'Content-Length': data.length
- }) : (options.headers || {})
- !options.headers.Authorization && usertokens[options.username] ? (()=>{
- options.headers.Authorization = `token ${usertokens[options.username]}`
- delete options.username
- onauthenticated()
- })() : (()=>{
- if(!usertokens[options.username] && !options.isAuthCall) {
- RESTAPI.authenticate( utils.assign( {isAuthCall : true}, options ) , onauthenticated, function(err){
- // PB : TODO -- Retry without auth...
- console.error('Auth failed or not accessible')
- reject(err)
- })
- }
- else onauthenticated()
- })()
-
- function onauthenticated(){
-
- var acquirer = getHTTPorS(options)
- const req = acquirer.request(options, res => {
- if (res.statusCode < 200 || res.statusCode >= 300) {
- // new Error('statusCode=' + res.statusCode)
- return reject('statusCode = ' + res.statusCode);
- }
- var body = [];
- // res.setEncoding('utf8');
- res.on('data', (chunk)=>{ body.push(chunk); });
- res.on('end', ()=>{
- try {
- if(res.headers['content-type'] && res.headers['content-type'].split(';').find( (i)=> i.trim() === 'application/json' )) body = JSON.parse(Buffer.concat(body).toString());
- else body = Buffer.concat(body).toString();
- } catch(e) { return reject(e); }
- resolve(body);
- });
- });
-
- req.on('error', error => { reject(error) })
- if(options.payload) req.write(data)
- req.end()
- }
- }
-
- var usertokens = {}
-
- RESTAPI.authenticate = function(options, resolve, reject){
- options.headers = options.headers || { 'Content-Type': 'application/json' }
-
- if(!usertokens[options.username]) {
- // Authenticate and acquire token.
- // https://git.bbh/api/v1/users/<username>/tokens
- // curl -XPOST -H "Content-Type: application/json" -k -d '{"name":"demo"}' -u demo:demo123 http://git.bbh/api/v1/users/demo/tokens
- var _options = Object.assign({}, options)
- // _options.username = 'demo'
- // _options.password = 'demo123'
- _options.method = 'POST'
- _options.headers.Authorization = `Basic ${Buffer.from(`${_options.username}:${_options.password}`).toString('base64')}`
- _options.path = `/api/v1/users/${_options.username}/tokens`
-
- var postoptions = { name : _options.username }
- delete _options.username
- delete _options.password
- _options.payload = postoptions
- RESTAPI.post( _options, function(tokenresp){
- // tokenresp = JSON.parse(tokenresp)
- usertokens[options.username] = tokenresp.sha1
- resolve(tokenresp)
- },
- function(err){ reject(err) }
- )
- }
- else resolve(tokenresp.sha1)
- }
-
- RESTAPI.put = RESTAPI.get = RESTAPI.post = RESTAPI.method
-
- return RESTAPI
- })();
-
- var getHTTPorS = function(options){ return options.protocol.startsWith('https') ? https : http; }
-
- const GITEA = (function(){
-
- function GITEA(){}
- GITEA.APIROOT = '/api/v1'
- GITEA.repository = {
- fork( httpoptions, cmdoptions, giteaoptions, resolve, reject ){
- // forkoptions = { owner : httpoptions.username, repo : {{reoptoFork}} }
- // giteaoptions = {
- // organization string
- // --- organization name, if forking into an organization
- // }
- // http://try.gitea.io/api/v1/repos/{owner}/{repo}/forks
- httpoptions.path = `${GITEA.APIROOT}/repos/${cmdoptions.owner}/${cmdoptions.repo}/forks`
- httpoptions.method = 'POST'
- httpoptions.payload = giteaoptions;
- return RESTAPI.post(httpoptions, resolve || function(){}, reject || function(){} )
- }
- , updateattributes( httpoptions, cmdoptions, giteaoptions, resolve, reject ){
- httpoptions.path = `${GITEA.APIROOT}/repos/${cmdoptions.owner}/${cmdoptions.repo}`
- httpoptions.method = 'PATCH'
- httpoptions.payload = giteaoptions;
- return RESTAPI.post(httpoptions, resolve || function(){}, reject || function(){} )
- }
- , exists( httpoptions, repo, owner ) {
- // /users/{username} // Get a user
- httpoptions.path = `${GITEA.APIROOT}/repos/${owner}/${repo}`
- httpoptions.method = 'GET'
- return new Promise( (resolve, reject) => {
- RESTAPI.get(httpoptions, function(o){
- if(o.id) return resolve(true)
- return resolve(false) }, reject || function(){ return false } )
- })
- }
- , addcollaborator( httpoptions, repodef, owner, collaborator, permission ) {
- permission = permission || 'Read'
- httpoptions.payload = { permission }
- httpoptions.path = `${GITEA.APIROOT}/repos/${owner}/${repodef.repo}/collaborators/${collaborator}`
- httpoptions.method = 'PUT'
- return new Promise( (resolve, reject) => {
- RESTAPI.put(httpoptions, function(o){
- //
- if(o === "") return resolve(true)
- return resolve(false) }, reject || function(){ return false } )
- })
- }
- }
-
- GITEA.user = {
- getuser( httpoptions, cmdoptions, giteaoptions, resolve, reject ){
- // /users/{username} // Get a user
- httpoptions.path = `${GITEA.APIROOT}/users/${httpoptions.username}`
- httpoptions.method = 'GET'
- return RESTAPI.get(httpoptions, giteaoptions, resolve || function(){}, reject || function(){} )
- }
- }
-
- return GITEA
- })();
-
- // Wrapper for Git shell operations. Some meta operations will map to a bunch of GIT commands.
- const GIT = (function(){
- function GIT(){}
-
- Object.assign(GIT, {
- 'switch user'(username){
- var httpoptions = new URL(selectedinstance.reposerver);
- httpoptions.username = selectedinstance.username
- httpoptions.password = selectedinstance.password
-
- return GITEA.user.getuser(httpoptions).then(()=>{
- return nodeShellExec('git', ['config', '--replace-all', 'user.name', username],
- {
- inherit: true, shell: true,
- env: ENV
- , cwd: instanceroot + '/' + repo
- , runas: processedArgs.runas
- , title: `'git', ${['config', '--replace-all', 'user.name', username].join(' ')}`
- })
- }
- )
- .catch(e => {
- console.error(e + 'Could not switch. Probably no such user.')
- })
-
- }
- })
-
- return GIT
- })();
-
- function createInstanceData(target, source) {
-
- var sourceinstance = source || target;
- console.dir(sourceinstance)
- var args = {
- remotebase : sourceinstance.reposerver + '/chess/'
- , sourcerepo : sourceinstance.repo || 'chess-data'
- , targetrepo : `${target.instanceName}-data-${target.instanceType}${target.nodeName ? ('-' + target.nodeName) : ''}`
- }
-
- if(sourceinstance.local) {
-
- var options = {
- inherit: true, shell: true,
- env: ENV
- , cwd : instanceroot
- , runas: processedArgs.runas
- }
- var cmdseq = [
- ['git', ['clone', `${args.remotebase}${args.sourcerepo}`, `${args.targetrepo}`], options]
- ]
- return any(cmdseq.map(getshelltask)).then(() => { return true })
- }
- else {
- // http://try.gitea.io/api/v1/org/{org}/repos
-
- if(source.reposerver !== target.reposerver && source.username !== target.username) {
- throw 'createInstanceData is possible only within the same repository server.'
- }
-
- var server = new URL(target.reposerver);
- return GITEA.repository.fork( { hostname : server.host, protocol : server.protocol
- , username : target.username, password : target.password
- }
- // , { repo : `${selectedinstance.instanceName}-config-${selectedinstance.instanceType}`}
- , { repo : `${args.sourcerepo}`, owner : `${target.username}` }, {}, function( repository ){
- return GITEA.repository.updateattributes( {
- hostname : server.host, protocol : server.protocol
- , username : target.username, password : target.password
- }
- , { repo : `${args.sourcerepo}`, owner : `${target.username}` }
- , { name : `${args.targetrepo}`}
- )
- }
- )
- }
-
- // GITEA.repository.updateattributes( {
- // hostname : server.host, protocol : server.protocol
- // , username : selectedinstance.username, password : selectedinstance.password
- // }
- // , { repo : `chess-config`, owner : selectedinstance.username }
- // , { name : `${selectedinstance.instanceName}-config-${selectedinstance.instanceType}${selectedinstance.nodeName ? '-' + selectedinstance.nodeName : ''}`}
- // )
-
- // return selectedinstance
- }
-
-
- function createInstance(target, source) {
-
- var sourceinstance = source || target;
- console.dir(sourceinstance)
- var args = {
- remotebase : sourceinstance.reposerver + '/chess/'
- , sourcerepo : sourceinstance.repo || 'chess-config'
- , targetrepo : `${target.instanceName}-config-${target.instanceType}${target.nodeName ? ('-' + target.nodeName) : ''}`
- }
-
- if(sourceinstance.local) {
-
- var options = {
- inherit: true, shell: true,
- env: ENV
- , cwd : instanceroot
- , runas: processedArgs.runas
- }
- var cmdseq = [
- ['git', ['clone', `${args.remotebase}${args.sourcerepo}`, `${args.targetrepo}`], options]
- ]
- return any(cmdseq.map(getshelltask)).then(() => { return true })
- }
- else {
- // http://try.gitea.io/api/v1/org/{org}/repos
-
- if(source.reposerver !== target.reposerver && source.username !== target.username) {
- throw 'createInstance is possible only within the same repository server.'
- }
-
- var server = new URL(target.reposerver);
- return GITEA.repository.fork( { hostname : server.host, protocol : server.protocol
- , username : target.username, password : target.password
- }
- // , { repo : `${selectedinstance.instanceName}-config-${selectedinstance.instanceType}`}
- , { repo : `${args.sourcerepo}`, owner : `${target.username}` }, {}, function( repository ){
- return GITEA.repository.updateattributes( {
- hostname : server.host, protocol : server.protocol
- , username : target.username, password : target.password
- }
- , { repo : `${args.sourcerepo}`, owner : `${target.username}` }
- , { name : `${args.targetrepo}`}
- )
- }
- )
- }
-
- // GITEA.repository.updateattributes( {
- // hostname : server.host, protocol : server.protocol
- // , username : selectedinstance.username, password : selectedinstance.password
- // }
- // , { repo : `chess-config`, owner : selectedinstance.username }
- // , { name : `${selectedinstance.instanceName}-config-${selectedinstance.instanceType}${selectedinstance.nodeName ? '-' + selectedinstance.nodeName : ''}`}
- // )
-
- // return selectedinstance
- }
-
- var skipprerequisites = false;
- function initinstances() {
- // var root = selectedinstance.root
-
- var instanceName = selectedinstance.instanceName
- var node_env = selectedinstance.node_env
- var reposerver = selectedinstance.reposerver
-
- // var instanceName = selectedinstance.instanceName
- // || clioverrides.instanceName
- // // || processedArgs._[1]
- // || chessinstances.current_run.instanceName
-
- // var node_env = selectedinstance.node_env
- // || clioverrides.node_env
- // // || processedArgs.node_env
- // || chessinstances.current_run.node_env
-
- // var reposerver = selectedinstance.reposerver
- // || clioverrides.reposerver
- // // || processedArgs.node_env
- // || chessinstances.current_run.reposerver
-
- // if(!instanceName) {
- // promptkeys['instanceName'] = instanceName = chessinstances.current_run.instanceName = promptkeys['instanceName'] || __default.instanceName;
- // promptkeys['node_env'] = node_env = chessinstances.current_run.node_env = promptkeys['node_env'] || __default.node_env;
- // promptkeys['reposerver'] = reposerver = chessinstances.current_run.reposerver = promptkeys['reposerver'] || __default.reposervers[0];
- // }
-
- // if(!node_env) {
- // promptkeys['node_env'] = node_env = chessinstances.current_run.node_env = promptkeys['node_env'] || __default.node_env;
- // promptkeys['reposerver'] = reposerver = chessinstances.current_run.reposerver = promptkeys['reposerver'] || __default.reposervers[0];
- // }
-
- // if(!reposerver) {
- // promptkeys['reposerver'] = reposerver = chessinstances.current_run.reposerver = promptkeys['reposerver'] || __default.reposervers[0];
- // }
-
- // chessinstances[instanceName] = chessinstances[instanceName] || createLocalChessInstance( {
- // instanceName, node_env, root : selectedinstance.root, reposerver : selectedinstance.reposerver /* promptkeys['reposerver'] */ } );
- // chessinstances['current_run'] = { instanceName: selectedinstance.instanceName, node_env : selectedinstance.node_env, reposerver : selectedinstance.reposerver, root }
-
- // if(path.normalize(selectedinstance.root) !== path.normalize(chessinstances[selectedinstance.instanceName][selectedinstance.node_env].root)) {
- // throw "instanceName and instanceType specified doesn't match whats already present do you want to continue " + chessinstances[instanceName][node_env].root + ' does not match ' + selectedinstance.root
- // }
- // Override sequence.
- // __default, chessinstances[current_run], instanceName-config-development, cliargs, interactve_promts
- // PB : TODO -- Undefined keys are overriding and deleting values. We should not allow that.
- // This is ordinary utils.assign behavior. The key should not exist as undefined in the override.
- // PB : TODO -- We now have options that can be passed into assign_core to control this behavior.
- if(selectedinstance.node_env === undefined) delete selectedinstance.node_env
-
- // selectedinstance = __g.selectedinstance = utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }
- // , __default
- // , chessinstances[instanceName][node_env]
- // , clioverrides
- // , selectedinstance
- // // , __interactive_prompts -- Cant just override. Also need selectedinstance to be ready...
- // );
-
- // chessinstances[instanceName] = chessinstances[instanceName] || {}
- // chessinstances[instanceName][node_env] = chessinstances[instanceName][node_env] || {}
-
- // if(!selectedinstance.repos || selectedinstance.instanceName) {
- // // Brand New.
- // selectedinstance = Object.assign( __default, selectedinstance )
- // }
-
- // Upgrade old formats..
- if(selectedinstance?.repos && !selectedinstance?.repos[0]?.repo) {
- console.warn('repo manifest has obsolete format. Attempting upgrade.')
- selectedinstance.repos = selectedinstance.repos.map(function(repo){ return { repo } })
- }
- if(selectedinstance?.elevated && !selectedinstance?.elevated[0]?.repo) {
- console.warn('elevated repo manifest has obsolete format. Attempting upgrade.')
- selectedinstance.elevated = selectedinstance.elevated.map(function(repo){ return { repo } })
- }
-
- // Config from server always override merges into selection except for the current selection.
- // PB : TODO -- utils.assign Array merges are non-distinct...
- // chessinstances[instanceName][node_env] = selectedinstance;
- // chessinstances[selectedinstance.instanceName][selectedinstance.node_env] = selectedinstance;
- cacheWriteInstanceConfig(chessinstances, selectedinstance.root)
-
- // PB : TODO -- We should probably write the new server config also...
- selectedinstance.reposerver = selectedinstance.reposerver || selectedinstance.reposervers[0] // PB : TODO -- Attempt first one that is available and online...
-
- return chessinstances
- }
-
- var skipprereqs = {}
- var maintask = () => {
-
- // Default cmd to run !
- processedArgs._[0] === processedArgs._[0] || 'pull';
- // selectedinstance.reposerver = selectedinstance.reposervers[0] // PB : TODO -- Attempt first one that is available and online from all that are available.
-
- if(!noprerequisites[processedArgs._[0]]
- && !skipprereqs[processedArgs._[0]]
- ) {
- return prerequisites.git.verifyAndInstall().then(()=>{
- var e = { message : 'verifyAndInstall', success : true}
- var inittasks = []
- var commontask = () => { preworkerconfig(); return elxrworker(true) }
- if(!detection_state.localInstanceDetected) {
- var t1 = ()=>{ return createInstance(selectedinstance) };
- t1.statuslog = statuslog
- var specifictask = shell_verse.getNonElevatedTask(t1)
- }
- else {
- var t2 = ()=>{ return acquireConfig(selectedinstance) };
- t2.statuslog = statuslog
- var specifictask = shell_verse.getNonElevatedTask(t2)
- }
- inittasks.push( specifictask().catch((err) => {
- e = err;
- console.error('Chosen cofiguraton failed or not found. Fix config and rerun or chose another.')
- console.error(err)
- }).then( commontask )
- // .finally(()=>{
- // fs.writeFileSync('run.log', ', ' + JSON.stringify({ error: e.message }), { 'flag': 'a+' })
- // if(!e.success) fs.writeFileSync('run.done', 'error');
- // // return process.exit()
- // }))
- )
-
- return any(inittasks)
- })
- }
- else {
- console.log('cmd has no preqs or has been configured to skip preqs')
- preworkerconfig()
- return elxrworker()
- }
-
- }
-
- function generateDependencies(){
- // PB : TODO -- Keep only the last n runs...
- // Currently it retains 2*n when proc needs to be relaunched in elevated mode !!!
-
- ensureDirectoryExistence(`${selectedinstance.root}/.elxr/run-${runtimestamp}/download.bat`)
- fs.writeFileSync(ensureDirectoryExistence(path.normalize(`${selectedinstance.root}/${downloadsdir}/readme.txt`)), `${getVersion()} Your local downloads for this instance`)
-
- // PB : TODO include and build from files... using rollup..
- var downloadbatch =
- `::**************************************************************************
- :Download_ <url> <File>
- Powershell.exe ^
- $AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'; ^
- [System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols; ^
- (New-Object System.Net.WebClient).DownloadFile('%1','%2')
- exit /b
- ::**************************************************************************`
- fs.writeFileSync(`${selectedinstance.root}/.elxr/run-${runtimestamp}/download.bat`, downloadbatch)
-
-
- var windowselevate =
- `
- <html><HTA:APPLICATION ID="windowselevate" icon="#"/>
- <script language="vbscript">
- document.title = "elxr control panel"
- self.ResizeTo 200,600
-
- Sub Window_Onload
- self.MoveTo (screen.availWidth - (document.body.clientWidth + 40)),10
- End Sub
-
- Set objShell = CreateObject("WScript.Shell")
- Set objENV = objShell.Environment("Process")
- dim NODE_ENV
- NODE_ENV = objENV("NODE_ENV")
-
- </script>
-
- <script language="javascript">
- //WINDOWSTATE="minimize" SHOWINTASKBAR="no" SYSMENU="no" CAPTION="no"
- // https://devblogs.microsoft.com/scripting/how-can-i-pass-command-line-variables-to-an-hta-when-it-starts/
- // alert(windowselevate.commandLine)
- var args = windowselevate.commandLine.split('"').slice(3);
- // alert(args)
- var processedArgs = { _ : [] }
- var namedArgs = [];
- namedArgs.push('--wd=' + objENV('wd'))
- // alert(namedArgs)
- for(var item in args){
- if(args[item].charAt(0) === '-'){
- namedArgs.push(args[item])
- var split = args[item].split('=');
- processedArgs[split[0].slice(2)] = split[1] || true;
- }
- else processedArgs._.push(args[item]);
- }
- // args = args.forEach(function(item){ })
- // alert('processedArgs._ : ' + processedArgs._);
- // alert(processedArgs.runas);
- // alert(objENV('wd'))
- // PB : TODO -- Convert all the cli args back to string.
- // __filename will sure we are launhed using the same entry point.
- var cargs = (processedArgs.debug ? '--inspect-brk=9226' : '') + ' ${__filename.replace(/\\/g, '\\\\')} ' + processedArgs._.join(' ') + ' ' + namedArgs.join(' ');
- var shell = new ActiveXObject('shell.application');
- // alert('launching node privilged. ' + processedArgs['nodepath'])
- // shell.ShellExecute('cmd.exe', '/k where node', '', '', 10);
- // shell.ShellExecute('cmd.exe', '/k notepad.exe', '', 'runas', 1);
- // shell.ShellExecute('cmd.exe ', '/k node ', '', 'runas', 1);
- // shell.ShellExecute('cmd.exe ', '/k node ' + cargs + '', '', 'runas', 1);
- shell.ShellExecute('node', cargs, '', 'runas', 1);
- // alert('/k node ' + cargs + '')
- // shell.ShellExecute(processedArgs['nodepath'], cargs, '', 'runas', 1);
- var fso = new ActiveXObject('Scripting.FileSystemObject');
-
- window.onload = function() {
- document.body.style.backgroundColor = 'black';
- document.body.style.fontFamily = 'arial';
- var log = document.createElement('div');
- log.innerHTML='Please Wait';
- function l(msg){ log.innerHTML+= msg; };
- log.style.color = 'blue';
- log.style.width = '95%';
- log.id = 'log';
- document.body.appendChild(log);
-
- l('<Br/>Current config : ')
- l('<Br/>NODE_ENV = ' + NODE_ENV)
- l('<Br/>cmd = ' + processedArgs._[0])
- processedArgs._[1] === 'use' ? l('<Br/>using = ' + processedArgs._[2]) : null;
- l('<Br/><Br/>')
- // alert(fso.GetAbsolutePathName("."))
-
- var timer = function(){
- l('.');
- if(fso.FileExists("run.done")) {
- fso.DeleteFile('run.done') // PB : TODO -- IPC through files is needed only for windows we need to do it per run...
- close();
- }
- else window.setTimeout(timer, 1000);
- };
- window.setTimeout(timer, 3000);
- };
-
- </script>
- </html>
-
- `
- fs.writeFileSync(`${selectedinstance.root}/.elxr/run-${runtimestamp}/windowselevate.hta`, windowselevate)
- }
-
- var startElxr = function() {
- const retaincount = 2
- var min = runtimestamp;
- var collect = []
-
- if(reconfirmcmds[processedArgs.label || processedArgs._[0]]) {
- reconfirm = getReconfirmAll()
- }
-
- // detectedinstanceoptions.splice(0,0, __default) // PB : TODO -- Merge multiple...
- var detectedinstanceoptions = [selectedinstance]
-
- var cmdinstance = cmds[clioverrides.cmd]
- var cmdprompts = cmdinstance.getPossiblePrompts()
- // selectedinstance.node_env ? selectedinstance.node_env : selectedinstance.node_env = clioverrides.node_env
- // PB : TODO -- Most recent should be at the tip ! at index 0 so utils.reverseassign is required !!!
- utils.assign_core( { arraymergetype : utils.assign_core.DISTINCT_UNION }, selectedinstance, promptkeys )
- // promptkeys = utils.assign(promptkeys, clioverrides)
-
- // startElxr requires instance // Independent cmds should have already been bypassed.
-
- // if(cmdprompts.instanceName) {
- // not an instanceless cmd.
- // console.dir(selectedinstance)
- try {
-
- // chessinstances = acquirelocalinstances(selectedinstance);
- // findlocalinstances(chessinstances, detectedinstanceoptions)
- initinstances(selectedinstance) // use the local instances for defaults if at all possible.
- var todo = any( getInteractionPoints(detectedinstanceoptions, promptkeys) ).then(()=>{
- var inst = initinstances(selectedinstance)
- detection_state.localInstanceDetected = true;
- return inst;
- })
- }
- catch (e) {
- // PB : TODO -- verbose mode warning.. console.warn(e) // Missing chessinstances is not an error...
- var todo = any( getInteractionPoints(detectedinstanceoptions, promptkeys) ).then(()=>{
- return initinstances(selectedinstance)
- })
-
- // if(!processedArgs._[0] || !selectedinstance.node_env || !selectedinstance.instanceName){
- // // Weve not been told what to do.
- // todo = todo.then(() => { return acquireChoices(selectedinstance) })
- // }
-
- todo = todo.then(() => {
- try {
- // chessinstances = acquirelocalinstances(selectedinstance)
- // findlocalinstances(chessinstances, detectedinstanceoptions)
- // detectedinstanceoptions.splice(0,0, __default)
- initinstances(selectedinstance)
- detection_state.localInstanceDetected = true;
- }
- catch (e) {
- // console.error(e)
- console.log('No local instances config found in current root = ' + selectedinstance.root);
- console.log('A config will be createed with the instance and environment chosen...')
- // return (async ()=>{return await __default.reposerver})().then(()=>{
- // // selectedinstance = Object.assign(detectedInstance, clioverrides);
- // return selectedinstance = Object.assign(__default, selectedinstance);
- // })
- detection_state.localInstanceDetected = false;
- return selectedinstance
- }
- })
- }
- return todo.then( ()=>{
- // PB : TODO -- Embed this in the build instead of inlining it.
- // PB : TODO -- Also attepmt to load from ../chess-config/... as base for new instances...
-
- initinstances(selectedinstance) // PB : TODO Review Initinstancess... and cleanup
- // ---------------------------
- runconfig = { NODE_ENV: selectedinstance.node_env }
- try { runconfig = Object.assign(runconfig, require(instanceroot + '/run.js')) } catch (e) { }
- generateDependencies();
- if(noprerequisites[processedArgs._[0]]
- || skipprereqs[processedArgs._[0]]
- ) {
- return elxrworker()
- }
-
- var neTask = ()=>{
- ensureDirectoryExistence(`${selectedinstance.root}/.elxr/${__ALIAS__STAMP__}`)
- // collect garbage
- return dirs( (dir)=>{
- var matches = /run-(.*)/gm.exec(dir.name)
- if(matches) {
- if(+(matches[1]) < min) {
- min = matches[1]
- collect.splice( 0, 0, matches[1] )
- }
- else collect.push(matches[1])
- }
- return Promise.resolve(collect);
- }, `${selectedinstance.root}/.elxr` )
- .then(()=>{
- // delete garbage
- if(collect.length > retaincount) {
-
- var asyncs = [];
- while((collect.length - asyncs.length) > retaincount) {
- asyncs.push(getShellTask('rm',['-rf', `run-${collect[asyncs.length]}`], { cwd : `${selectedinstance.root}/.elxr` })());
- }
- return Promise.all(asyncs)
- }
- else return true
- })
- .catch(e => {
- console.error
- })
- }
- neTask.statuslog = statuslog
-
- shell_verse.getNonElevatedTask( neTask )()
-
- var commonTask = ()=>{
- verifyAndInstallPrerequisites.statuslog = statuslog
- if((!skipprerequisites || processedArgs.forceprereqs)) mainTasks.push(verifyAndInstallPrerequisites);
- mainTasks.push(maintask)
- return any(mainTasks);
- }
- return commonTask()
-
- })
- // }
- // else return Promise.resolve(true)
- }
-
-
- // return detectInstanceRoot.then(()=>{
- var cmdobj = cmds[selectedinstance.cmd]
- return Promise.all((cmdobj.requires || []).map( (r) => utils.promisify(null, r) ) ).then(()=>{
- if(cmdobj.independentcmd) {
- if(cmdobj.requiresElevation) {
- return cmdobj.cmdFn(cmdobj.toArgs(processedArgs))
- }
- else return cmdobj.cmdFn(cmdobj.toArgs(processedArgs))
- }
- else return startElxr()
- })
- .catch(e => {
- if(typeof e === 'symbol') console.log('error : ' + 'symbol')
- else console.log('error : ' + e)
- })
- // })
-
-
-
- })
- })
- // .then( () => {
- // console.log(process._getActiveHandles());
- // console.log(process._getActiveRequests());
- // })
-
-
- // detect if alread installed -> Take no action.
- // download if no installer avalable -> next()
- // install
- function checkandinstall(items) {
- var tasks = []
- items.forEach(item => {
- tasks.push(getTaskCheckExists(item.shellcommand || prerequisites[item]))
- })
-
- return Promise.all(tasks).then(existances => {
- existances.forEach((exists, i) => {
- if (!exists) {
- return downloadandinstall([items[i]])
- }
- })
- })
- }
-
- function downloadandinstall(items) {
- var tasks = []
- items.forEach(item => {
- tasks.push(getTaskDownload(item))
- })
- return Promise.all(tasks)
- }
-
-
-
-
- // Sample instances config.
- // var instances = {
- // "elixir": {
- // "production": {
- // "reposervers": ["http://git.bbh", "https://git.bbh.org.in"]
- // , "repos": ["ember-masonry-grid", "client", "elixir-client"]
- // , "exludeMergeRepos": {
- // "elixir-config-development": true, "elixir-config-test": true
- // , "elixir-config-production": true, "elixir-data": true
- // }
- // , "instanceName": "elixir", "node_env": "production"
- // }
- // },
- // "current_run": { "instanceName": "elixir", "node_env": "production" }
- // }
-
- // ,([^\}^\S\r]*?\}) // Regexp to eliminate extra comma at the end of an array or an object...
|