@capgo/cli 0.10.1 → 0.11.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.cz.toml CHANGED
@@ -1,7 +1,7 @@
1
1
  [tool.commitizen]
2
2
  name = "cz_conventional_commits"
3
3
  tag_format = "$major.$minor.$patch$prerelease"
4
- version = "0.10.1"
4
+ version = "0.11.2"
5
5
  version_files = [
6
6
  "package.json:version",
7
7
  ".cz.toml"
package/CHANGELOG.md CHANGED
@@ -1,3 +1,21 @@
1
+ ## 0.11.2 (2022-06-01)
2
+
3
+ ### Fix
4
+
5
+ - check if version exist
6
+
7
+ ## 0.11.1 (2022-06-01)
8
+
9
+ ### Fix
10
+
11
+ - add check if version exist
12
+
13
+ ## 0.11.0 (2022-06-01)
14
+
15
+ ### Feat
16
+
17
+ - add login system
18
+
1
19
  ## 0.10.1 (2022-06-01)
2
20
 
3
21
  ### Fix
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- (()=>{"use strict";var e={548:function(e,o,r){var t=this&&this.__awaiter||function(e,o,r,t){return new(r||(r=Promise))((function(a,n){function i(e){try{s(t.next(e))}catch(e){n(e)}}function p(e){try{s(t.throw(e))}catch(e){n(e)}}function s(e){var o;e.done?a(e.value):(o=e.value,o instanceof r?o:new r((function(e){e(o)}))).then(i,p)}s((t=t.apply(e,o||[])).next())}))};Object.defineProperty(o,"__esModule",{value:!0}),o.addApp=void 0;const a=r(304),n=r(113),i=r(147),p=r(470),s=r(11),d=r(233),l="assets/icon.png";o.addApp=(e,o)=>t(void 0,void 0,void 0,(function*(){var r,t;let{name:c,icon:u}=o;const{apikey:m}=o,g=yield(0,d.getConfig)();e=e||(null===(r=null==g?void 0:g.app)||void 0===r?void 0:r.appId),c=c||(null===(t=null==g?void 0:g.app)||void 0===t?void 0:t.appName)||"Unknown",u=u||"resources/icon.png",m||a.program.error("Missing API key, you need to provide a API key to add your app"),e&&c||a.program.error("Missing argument, you need to provide a appid and a name, or be in a capacitor project"),console.log(`Add ${e} to Capgo`);const v=(0,d.createSupabaseClient)(m),{data:f,error:y}=yield v.rpc("is_allowed_capgkey",{apikey:m,keymode:["write","all"]});if(!f||y)return void console.log("Invalid API key or insufisant rights");let h,_;console.log("Adding..."),u&&(0,p.existsSync)(u)?(h=(0,i.readFileSync)(u),_=(0,s.getType)(u)||"image/png",console.warn(`Found app icon ${u}`)):(0,p.existsSync)(l)?(h=(0,i.readFileSync)(l),_=(0,s.getType)(l)||"image/png",console.warn(`Found app icon ${l}`)):console.warn(`Cannot find app icon in any of the following locations: ${u}, ${l}`);const{data:k,error:b}=yield v.rpc("get_user_id",{apikey:m}),$=k?k.toString():"";$&&!b||a.program.error(`Cannot verify user ${(0,d.formatError)(b)}`);const{data:w,error:C}=yield v.rpc("exist_app",{appid:e,apikey:m});(w||C)&&a.program.error(`App already exists ${(0,d.formatError)(C)}`);const x=`icon_${(0,n.randomUUID)()}`;let I="https://xvwzpoazmxkqosrdewyv.supabase.co/storage/v1/object/public/images/capgo.png";if(h&&_){const{error:o}=yield v.storage.from(`images/${$}/${e}`).upload(x,h,{contentType:_});o&&a.program.error(`Could not add app ${(0,d.formatError)(o)}`);const{data:r}=yield v.storage.from(`images/${$}/${e}`).getPublicUrl(x);I=(null==r?void 0:r.publicURL)||I}const{error:q}=yield v.from("apps").insert({icon_url:I,user_id:$,name:c,app_id:e},{returning:"minimal"});q&&a.program.error(`Could not add app ${(0,d.formatError)(q)}`),console.log("App added to server, you can upload a version now")}))},453:function(e,o,r){var t=this&&this.__awaiter||function(e,o,r,t){return new(r||(r=Promise))((function(a,n){function i(e){try{s(t.next(e))}catch(e){n(e)}}function p(e){try{s(t.throw(e))}catch(e){n(e)}}function s(e){var o;e.done?a(e.value):(o=e.value,o instanceof r?o:new r((function(e){e(o)}))).then(i,p)}s((t=t.apply(e,o||[])).next())}))};Object.defineProperty(o,"__esModule",{value:!0}),o.deleteApp=void 0;const a=r(304),n=r(233);o.deleteApp=(e,o)=>t(void 0,void 0,void 0,(function*(){var r;const{apikey:t,version:i}=o,p=yield(0,n.getConfig)();e=e||(null===(r=null==p?void 0:p.app)||void 0===r?void 0:r.appId),t||a.program.error("Missing API key, you need to provide an API key to delete your app"),e||a.program.error("Missing argument, you need to provide a appid, or be in a capacitor project"),console.log(`Delete ${e} to Capgo`);const s=(0,n.createSupabaseClient)(t),{data:d,error:l}=yield s.rpc("is_allowed_capgkey",{apikey:t,keymode:["write","all"],app_id:e});if(!d||l)return void console.log("Invalid API key or insufisant rights");const{data:c,error:u}=yield s.rpc("get_user_id",{apikey:t}),m=c?c.toString():"";m&&!u||a.program.error(`Cannot verify user ${(0,n.formatError)(u)}`);const{data:g,error:v}=yield s.rpc("exist_app",{appid:e,apikey:t});if(g&&!v||a.program.error("No permission to delete"),i){const{data:o,error:r}=yield s.from("app_versions").select().eq("app_id",e).eq("user_id",m).eq("name",i).eq("deleted",!1);o&&o.length&&!r||a.program.error(`Version ${e}@${i} don't exist ${(0,n.formatError)(r)}`);const{data:t,error:p}=yield s.from("channels").select().eq("app_id",e).eq("created_by",m).eq("version",o[0].id);(t&&t.length||p)&&a.program.error(`Version ${e}@${i} is used in a channel, unlink it first ${(0,n.formatError)(p)}`);const{data:d,error:l}=yield s.from("devices_override").select().eq("app_id",e).eq("version",o[0].id);(d&&d.length||l)&&a.program.error(`Version ${e} @${i} is used in a device override, unlink it first ${(0,n.formatError)(l)}`);const{error:c}=yield s.storage.from("apps").remove([`${m} /${e}/versions / ${o[0].bucket_id} `]);c&&a.program.error(`Something went wrong when trying to delete ${e} @${i} ${c} `);const{error:u}=yield s.from("app_versions").update({deleted:!0}).eq("app_id",e).eq("name",i).eq("user_id",m);return u&&a.program.error(`App ${e} @${i} not found in database ${u} `),void console.log("App version deleted from server")}const{data:f,error:y}=yield s.from("app_versions").select().eq("app_id",e).eq("user_id",m);if(y&&a.program.error(`App ${e} not found in database ${y} `),f&&f.length){const o=f.map((o=>`${m} /${e}/versions / ${o.bucket_id} `)),{error:r}=yield s.storage.from("apps").remove(o);r&&a.program.error(`Cannot delete stored version for app ${e} from storage ${r} `)}const{error:h}=yield s.from("app_versions").delete().eq("app_id",e).eq("user_id",m);h&&a.program.error(`Cannot delete version for app ${e} from database ${h} `);const{error:_}=yield s.from("apps").delete().eq("app_id",e).eq("user_id",m);_&&a.program.error(`Cannot delete from database ${_} `),console.log("App deleted from server")}))},473:function(e,o,r){var t=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(o,"__esModule",{value:!0});const a=r(304),n=r(548),i=r(453),p=r(666),s=r(310),d=t(r(598));a.program.version(d.default.version).command("add [appid]").alias("a").action(n.addApp).option("-n, --name <name>","app name").option("-i, --icon <icon>","app icon path").option("-a, --apikey <apikey>","apikey to link to your account"),a.program.command("upload [appid]").alias("u").action(s.uploadVersion).option("-a, --apikey <apikey>","apikey to link to your account").option("-p, --path <path>","path of the file to upload").option("-c, --channel <channel>","channel to link to").option("-e, --external <url>","link to external url intead of upload to capgo cloud").option("-f, --format <base64|hex|binary|utf8>","choose the upload format default base64").option("-v, --version <version>","version number of the file to upload"),a.program.command("set [appid]").alias("s").action(p.setChannel).option("-c, --channel <channel>","channel to link to").option("-v, --version <version>","version number of the file to upload").option("-s, --state <state>","set the state of the channel, public or private").option("-a, --apikey <apikey>","apikey to link to your account"),a.program.description("Manage package and version in capgo Cloud").command("delete [appid]").alias("a").action(i.deleteApp).option("-a, --apikey <apikey>","apikey to link to your account").option("-v, --version <version>","version number of the app to delete"),a.program.parse(process.argv)},666:function(e,o,r){var t=this&&this.__awaiter||function(e,o,r,t){return new(r||(r=Promise))((function(a,n){function i(e){try{s(t.next(e))}catch(e){n(e)}}function p(e){try{s(t.throw(e))}catch(e){n(e)}}function s(e){var o;e.done?a(e.value):(o=e.value,o instanceof r?o:new r((function(e){e(o)}))).then(i,p)}s((t=t.apply(e,o||[])).next())}))};Object.defineProperty(o,"__esModule",{value:!0}),o.setChannel=void 0;const a=r(304),n=r(233);o.setChannel=(e,o)=>t(void 0,void 0,void 0,(function*(){var r,t,i;let{version:p}=o;const{apikey:s,state:d,channel:l="dev"}=o,c=yield(0,n.getConfig)();let u;e=e||(null===(r=null==c?void 0:c.app)||void 0===r?void 0:r.appId),p=p||(null===(i=null===(t=null==c?void 0:c.app)||void 0===t?void 0:t.package)||void 0===i?void 0:i.version),"public"!==d&&"private"!==d||(u="public"===d),s||a.program.error("Missing API key, you need to provide a API key to add your app"),e||a.program.error("Missing argument, you need to provide a appid, or be in a capacitor project"),p||u||a.program.error("Missing argument, you need to provide a state or a version"),p?console.log(`Set ${l} to @${p} in ${e}`):console.log(`Set${l} to @${d} in ${e}`);try{const o=(0,n.createSupabaseClient)(s),{data:r,error:t}=yield o.rpc("is_allowed_capgkey",{apikey:s,keymode:["write","all"],app_id:e});r&&!t||a.program.error("Invalid API key or insufisant rights");const{data:i,error:d}=yield o.rpc("get_user_id",{apikey:s}),c=i?i.toString():"";c&&!d||a.program.error("Cannot verify user");const m={created_by:c,app_id:e,name:l};if(p){const{data:r,error:t}=yield o.from("app_versions").select().eq("app_id",e).eq("name",p).eq("user_id",c).eq("deleted",!1);!t&&r&&r.length||a.program.error(`Cannot find version ${p}`),m.version=r[0].id}void 0!==u&&(m.public=u);try{const{error:e}=yield(0,n.updateOrCreateChannel)(o,m,s);e&&a.program.error(`Cannot set channel ${(0,n.formatError)(e)}`)}catch(e){a.program.error(`Cannot set channel ${(0,n.formatError)(e)}`)}}catch(e){a.program.error(`Unknow error ${(0,n.formatError)(e)}`)}p?console.log("Done ✅"):console.log(`You can use now is channel in your app with the url: ${n.host}/api/latest?appid=${e}&channel=${l}`)}))},310:function(e,o,r){var t=this&&this.__awaiter||function(e,o,r,t){return new(r||(r=Promise))((function(a,n){function i(e){try{s(t.next(e))}catch(e){n(e)}}function p(e){try{s(t.throw(e))}catch(e){n(e)}}function s(e){var o;e.done?a(e.value):(o=e.value,o instanceof r?o:new r((function(e){e(o)}))).then(i,p)}s((t=t.apply(e,o||[])).next())}))},a=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(o,"__esModule",{value:!0}),o.uploadVersion=void 0;const n=a(r(844)),i=r(304),p=r(113),s=a(r(295)),d=r(233);o.uploadVersion=(e,o)=>t(void 0,void 0,void 0,(function*(){var r,t,a,l;let{version:c,path:u,channel:m}=o;const{apikey:g,external:v}=o;m=m||"dev";const f=yield(0,d.getConfig)();e=e||(null===(r=null==f?void 0:f.app)||void 0===r?void 0:r.appId),c=c||(null===(a=null===(t=null==f?void 0:f.app)||void 0===t?void 0:t.package)||void 0===a?void 0:a.version),u=u||(null===(l=null==f?void 0:f.app)||void 0===l?void 0:l.webDir),g||i.program.error("Missing API key, you need to provide a API key to add your app"),e&&c&&u||i.program.error("Missing argument, you need to provide a appid and a version and a path, or be in a capacitor project"),console.log(`Upload ${e}@${c} started from path "${u}" to Capgo cloud`);const y=(0,d.createSupabaseClient)(g),h=new s.default.MultiBar({clearOnComplete:!1,hideCursor:!0},s.default.Presets.shades_grey),_=h.create(7,0,{format:"Uploading: [{bar}] {percentage}% | ETA: {eta}s | {value}/{total} Part"},s.default.Presets.shades_grey);_.start(7,0,{speed:"N/A"});const{data:k,error:b}=yield y.rpc("is_allowed_capgkey",{apikey:g,keymode:["upload","write","all"],app_id:e});k&&!b||(h.stop(),i.program.error(`Invalid API key or insufisant rights ${(0,d.formatError)(b)}`)),_.increment();const{data:$,error:w}=yield y.rpc("get_user_id",{apikey:g}),C=$?$.toString():"";C&&!w||(h.stop(),i.program.error(`Cannot verify user ${(0,d.formatError)(w)}`)),_.increment();const{data:x,error:I}=yield y.rpc("exist_app",{appid:e,apikey:g});if(x&&!I||(h.stop(),i.program.error(`Cannot find app ${e} in your account \n${(0,d.formatError)(I)}`)),_.increment(),v)v&&!v.startsWith("https://")&&(h.stop(),i.program.error(`External link should should start with "https://" current is "${v}"`));else{const o=new n.default;o.addLocalFolder(u);const r=o.toBuffer(),t=Math.floor(r.byteLength/1024/1024),a=`apps/${C}/${e}/versions`,s=(0,p.randomUUID)();_.increment(),t>30&&(h.stop(),i.program.error(`The app is too big, the limit is 30 Mb, your is ${t} Mb`)),t>25&&h.log(`WARNING !!\nThe app size is ${t} Mb, the limit is 30 Mb\n`);const{error:l}=yield y.storage.from(a).upload(s,r,{contentType:"application/zip"});l&&(h.stop(),i.program.error(`Cannot upload ${(0,d.formatError)(l)}`))}_.increment();const q=(0,p.randomUUID)(),{data:A,error:M}=yield(0,d.updateOrCreateVersion)(y,{bucket_id:v?void 0:q,user_id:C,name:c,app_id:e,external_url:v},g);if(M&&(h.stop(),i.program.error(`Cannot add version ${(0,d.formatError)(M)}`)),_.increment(),A&&A.length){const{error:o}=yield(0,d.updateOrCreateChannel)(y,{name:m,app_id:e,created_by:C,version:A[0].id},g);o&&h.log("Cannot set version with upload key, use key with more rights for that\n")}else h.log("Cannot set version with upload key, use key with more rights for that\n");_.increment(),h.stop(),console.log("App uploaded to server"),console.log(`Try it in mobile app: ${d.host}/app_mobile`),console.log(`Or set the channel ${m} as public here: ${d.hostWeb}/app/package/${e}`),console.log("To use with live update in your own app")}))},233:function(e,o,r){var t=this&&this.__awaiter||function(e,o,r,t){return new(r||(r=Promise))((function(a,n){function i(e){try{s(t.next(e))}catch(e){n(e)}}function p(e){try{s(t.throw(e))}catch(e){n(e)}}function s(e){var o;e.done?a(e.value):(o=e.value,o instanceof r?o:new r((function(e){e(o)}))).then(i,p)}s((t=t.apply(e,o||[])).next())}))},a=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(o,"__esModule",{value:!0}),o.updateOrCreateChannel=o.updateOrCreateVersion=o.getConfig=o.formatError=o.createSupabaseClient=o.supaAnon=o.hostSupa=o.hostWeb=o.host=void 0;const n=r(778),i=r(304),p=r(885),s=a(r(867));o.host="https://capgo.app",o.hostWeb="https://web.capgo.app",o.hostSupa="https://aucsybvnhavogdmzwtcw.supabase.co",o.supaAnon="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImF1Y3N5YnZuaGF2b2dkbXp3dGN3Iiwicm9sZSI6ImFub24iLCJpYXQiOjE2NTM1ODcxNjgsImV4cCI6MTk2OTE2MzE2OH0.8FKKJqiGgoVA3p9GH5wvnbWkWywIxVLqQyZFhupZ7C4",o.createSupabaseClient=e=>(0,p.createClient)(o.hostSupa,o.supaAnon,{headers:{capgkey:e}}),o.formatError=e=>e?`\n${s.default.render(e)}`:"",o.getConfig=()=>t(void 0,void 0,void 0,(function*(){let e;try{e=yield(0,n.loadConfig)()}catch(e){i.program.error("No capacitor config file found, run `cap init` first")}return e})),o.updateOrCreateVersion=(e,o,r)=>t(void 0,void 0,void 0,(function*(){const{data:t,error:a}=yield e.rpc("exist_app_versions",{appid:o.app_id,name_version:o.name,apikey:r});return t&&!a?(o.deleted=!1,e.from("app_versions").update(o,{returning:"minimal"}).eq("app_id",o.app_id).eq("name",o.name)):e.from("app_versions").insert(o,{returning:"minimal"})})),o.updateOrCreateChannel=(e,o,r)=>t(void 0,void 0,void 0,(function*(){if(!o.app_id||!o.name||!o.created_by)return console.error("missing app_id, name, or created_by"),Promise.reject(new Error("missing app_id, name, or created_by"));const{data:t,error:a}=yield e.rpc("exist_channel",{appid:o.app_id,name_channel:o.name,apikey:r});return t&&!a?e.from("channels").update(o,{returning:"minimal"}).eq("app_id",o.app_id).eq("name",o.name).eq("created_by",o.created_by):e.from("channels").insert(o,{returning:"minimal"})}))},778:e=>{e.exports=require("@capacitor/cli/dist/config")},885:e=>{e.exports=require("@supabase/supabase-js")},844:e=>{e.exports=require("adm-zip")},295:e=>{e.exports=require("cli-progress")},304:e=>{e.exports=require("commander")},470:e=>{e.exports=require("fs-extra")},11:e=>{e.exports=require("mime")},867:e=>{e.exports=require("prettyjson")},113:e=>{e.exports=require("crypto")},147:e=>{e.exports=require("fs")},598:e=>{e.exports=JSON.parse('{"name":"@capgo/cli","version":"0.10.1","description":"A CLI to upload to capgo servers","main":"dist/index.js","bin":{"capgo":"dist/index.js"},"repository":{"type":"git","url":"git+https://github.com/Cap-go/capgo-cli.git"},"bugs":{"url":"https://github.com/Cap-go/capgo-cli/issues"},"keywords":["appflow alternative","ionic","capacitor","auto update","live update","capgo","cli","upload","capgo-cli"],"scripts":{"dev":"set NODE_ENV=development&& npx webpack --config webpack.config.js","no-debug":"node dist/index.js","build":"set NODE_ENV=production&& npx webpack --config webpack.config.js","pack":"pkg","test_rls":"ts-node ./test/test_headers_rls.ts","lint":"eslint . --ext .ts --fix"},"author":"github.com/riderx","license":"Apache 2.0","dependencies":{"@capacitor/cli":"3.5.1","@supabase/supabase-js":"^1.35.3","adm-zip":"^0.5.9","cli-progress":"3.11.0","commander":"9.2.0","form-data":"^4.0.0","fs-extra":"10.1.0","mime":"^3.0.0","node-stream-zip":"^1.15.0","prettyjson":"^1.2.5","upath":"^2.0.1"},"devDependencies":{"@types/adm-zip":"0.5.0","@types/cli-progress":"^3.11.0","@types/fs-extra":"^9.0.13","@types/mime":"^2.0.3","@types/node":"^17.0.31","@types/prettyjson":"^0.0.30","@typescript-eslint/eslint-plugin":"5.22.0","@typescript-eslint/parser":"5.22.0","eslint":"8.14.0","eslint-config-airbnb-base":"^15.0.0","eslint-config-prettier":"^8.5.0","eslint-import-resolver-typescript":"2.7.1","eslint-plugin-import":"2.26.0","eslint-plugin-prettier":"^4.0.0","git-format-staged":"3.0.0","husky":"^7.0.4","nodemon":"2.0.16","pkg":"5.6.0","prettier":"2.6.2","ts-loader":"^9.3.0","ts-node":"^10.7.0","tsconfig-paths":"4.0.0","typescript":"4.6.4","webpack":"5.72.0","webpack-cli":"^4.9.2","webpack-node-externals":"^3.0.0"}}')}},o={};!function r(t){var a=o[t];if(void 0!==a)return a.exports;var n=o[t]={exports:{}};return e[t].call(n.exports,n,n.exports,r),n.exports}(473)})();
2
+ (()=>{"use strict";var e={548:function(e,o,r){var t=this&&this.__awaiter||function(e,o,r,t){return new(r||(r=Promise))((function(n,a){function i(e){try{s(t.next(e))}catch(e){a(e)}}function p(e){try{s(t.throw(e))}catch(e){a(e)}}function s(e){var o;e.done?n(e.value):(o=e.value,o instanceof r?o:new r((function(e){e(o)}))).then(i,p)}s((t=t.apply(e,o||[])).next())}))};Object.defineProperty(o,"__esModule",{value:!0}),o.addApp=void 0;const n=r(304),a=r(113),i=r(147),p=r(470),s=r(11),d=r(233),l="assets/icon.png";o.addApp=(e,o)=>t(void 0,void 0,void 0,(function*(){var r,t;let{name:c,icon:u}=o;const m=o.apikey||(0,d.findSavedKey)(),g=yield(0,d.getConfig)();e=e||(null===(r=null==g?void 0:g.app)||void 0===r?void 0:r.appId),c=c||(null===(t=null==g?void 0:g.app)||void 0===t?void 0:t.appName)||"Unknown",u=u||"resources/icon.png",m||n.program.error("Missing API key, you need to provide a API key to add your app"),e&&c||n.program.error("Missing argument, you need to provide a appid and a name, or be in a capacitor project"),console.log(`Add ${e} to Capgo`);const f=(0,d.createSupabaseClient)(m),{data:y,error:v}=yield f.rpc("is_allowed_capgkey",{apikey:m,keymode:["write","all"]});if(!y||v)return void console.log("Invalid API key or insufisant rights");let h,_;console.log("Adding..."),u&&(0,p.existsSync)(u)?(h=(0,i.readFileSync)(u),_=(0,s.getType)(u)||"image/png",console.warn(`Found app icon ${u}`)):(0,p.existsSync)(l)?(h=(0,i.readFileSync)(l),_=(0,s.getType)(l)||"image/png",console.warn(`Found app icon ${l}`)):console.warn(`Cannot find app icon in any of the following locations: ${u}, ${l}`);const{data:k,error:b}=yield f.rpc("get_user_id",{apikey:m}),$=k?k.toString():"";$&&!b||n.program.error(`Cannot verify user ${(0,d.formatError)(b)}`);const{data:w,error:x}=yield f.rpc("exist_app",{appid:e,apikey:m});(w||x)&&n.program.error(`App already exists ${(0,d.formatError)(x)}`);const C=`icon_${(0,a.randomUUID)()}`;let q="https://xvwzpoazmxkqosrdewyv.supabase.co/storage/v1/object/public/images/capgo.png";if(h&&_){const{error:o}=yield f.storage.from(`images/${$}/${e}`).upload(C,h,{contentType:_});o&&n.program.error(`Could not add app ${(0,d.formatError)(o)}`);const{data:r}=yield f.storage.from(`images/${$}/${e}`).getPublicUrl(C);q=(null==r?void 0:r.publicURL)||q}const{error:I}=yield f.from("apps").insert({icon_url:q,user_id:$,name:c,app_id:e},{returning:"minimal"});I&&n.program.error(`Could not add app ${(0,d.formatError)(I)}`),console.log("App added to server, you can upload a version now")}))},453:function(e,o,r){var t=this&&this.__awaiter||function(e,o,r,t){return new(r||(r=Promise))((function(n,a){function i(e){try{s(t.next(e))}catch(e){a(e)}}function p(e){try{s(t.throw(e))}catch(e){a(e)}}function s(e){var o;e.done?n(e.value):(o=e.value,o instanceof r?o:new r((function(e){e(o)}))).then(i,p)}s((t=t.apply(e,o||[])).next())}))};Object.defineProperty(o,"__esModule",{value:!0}),o.deleteApp=void 0;const n=r(304),a=r(233);o.deleteApp=(e,o)=>t(void 0,void 0,void 0,(function*(){var r;const{version:t}=o,i=o.apikey||(0,a.findSavedKey)(),p=yield(0,a.getConfig)();e=e||(null===(r=null==p?void 0:p.app)||void 0===r?void 0:r.appId),i||n.program.error("Missing API key, you need to provide an API key to delete your app"),e||n.program.error("Missing argument, you need to provide a appid, or be in a capacitor project"),console.log(`Delete ${e} to Capgo`);const s=(0,a.createSupabaseClient)(i),{data:d,error:l}=yield s.rpc("is_allowed_capgkey",{apikey:i,keymode:["write","all"],app_id:e});if(!d||l)return void console.log("Invalid API key or insufisant rights");const{data:c,error:u}=yield s.rpc("get_user_id",{apikey:i}),m=c?c.toString():"";m&&!u||n.program.error(`Cannot verify user ${(0,a.formatError)(u)}`);const{data:g,error:f}=yield s.rpc("exist_app",{appid:e,apikey:i});if(g&&!f||n.program.error("No permission to delete"),t){const{data:o,error:r}=yield s.from("app_versions").select().eq("app_id",e).eq("user_id",m).eq("name",t).eq("deleted",!1);o&&o.length&&!r||n.program.error(`Version ${e}@${t} don't exist ${(0,a.formatError)(r)}`);const{data:i,error:p}=yield s.from("channels").select().eq("app_id",e).eq("created_by",m).eq("version",o[0].id);(i&&i.length||p)&&n.program.error(`Version ${e}@${t} is used in a channel, unlink it first ${(0,a.formatError)(p)}`);const{data:d,error:l}=yield s.from("devices_override").select().eq("app_id",e).eq("version",o[0].id);(d&&d.length||l)&&n.program.error(`Version ${e} @${t} is used in a device override, unlink it first ${(0,a.formatError)(l)}`);const{error:c}=yield s.storage.from("apps").remove([`${m} /${e}/versions / ${o[0].bucket_id} `]);c&&n.program.error(`Something went wrong when trying to delete ${e} @${t} ${c} `);const{error:u}=yield s.from("app_versions").update({deleted:!0}).eq("app_id",e).eq("name",t).eq("user_id",m);return u&&n.program.error(`App ${e} @${t} not found in database ${u} `),void console.log("App version deleted from server")}const{data:y,error:v}=yield s.from("app_versions").select().eq("app_id",e).eq("user_id",m);if(v&&n.program.error(`App ${e} not found in database ${v} `),y&&y.length){const o=y.map((o=>`${m} /${e}/versions / ${o.bucket_id} `)),{error:r}=yield s.storage.from("apps").remove(o);r&&n.program.error(`Cannot delete stored version for app ${e} from storage ${r} `)}const{error:h}=yield s.from("app_versions").delete().eq("app_id",e).eq("user_id",m);h&&n.program.error(`Cannot delete version for app ${e} from database ${h} `);const{error:_}=yield s.from("apps").delete().eq("app_id",e).eq("user_id",m);_&&n.program.error(`Cannot delete from database ${_} `),console.log("App deleted from server")}))},473:function(e,o,r){var t=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(o,"__esModule",{value:!0});const n=r(304),a=r(548),i=r(453),p=r(666),s=r(310),d=t(r(598)),l=r(937);n.program.version(d.default.version).command("add [appid]").alias("a").action(a.addApp).option("-n, --name <name>","app name").option("-i, --icon <icon>","app icon path").option("-a, --apikey <apikey>","apikey to link to your account"),n.program.version(d.default.version).command("login [apikey]").alias("l").action(l.login).option("--local","Only save in local folder"),n.program.command("upload [appid]").alias("u").action(s.uploadVersion).option("-a, --apikey <apikey>","apikey to link to your account").option("-p, --path <path>","path of the file to upload").option("-c, --channel <channel>","channel to link to").option("-e, --external <url>","link to external url intead of upload to capgo cloud").option("-f, --format <base64|hex|binary|utf8>","choose the upload format default base64").option("-v, --version <version>","version number of the file to upload"),n.program.command("set [appid]").alias("s").action(p.setChannel).option("-c, --channel <channel>","channel to link to").option("-v, --version <version>","version number of the file to upload").option("-s, --state <state>","set the state of the channel, public or private").option("-a, --apikey <apikey>","apikey to link to your account"),n.program.description("Manage package and version in capgo Cloud").command("delete [appid]").alias("d").action(i.deleteApp).option("-a, --apikey <apikey>","apikey to link to your account").option("-v, --version <version>","version number of the app to delete"),n.program.parse(process.argv)},937:function(e,o,r){var t=this&&this.__awaiter||function(e,o,r,t){return new(r||(r=Promise))((function(n,a){function i(e){try{s(t.next(e))}catch(e){a(e)}}function p(e){try{s(t.throw(e))}catch(e){a(e)}}function s(e){var o;e.done?n(e.value):(o=e.value,o instanceof r?o:new r((function(e){e(o)}))).then(i,p)}s((t=t.apply(e,o||[])).next())}))},n=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(o,"__esModule",{value:!0}),o.login=void 0;const a=r(304),i=n(r(147)),p=n(r(37));o.login=(e,o)=>t(void 0,void 0,void 0,(function*(){const{local:r}=o;if(r)i.default.existsSync(".git")||a.program.error("To use local you should be in a git repository"),i.default.writeFileSync(".capgo",`${e}\n`),i.default.appendFileSync(".gitignore",".capgo\n");else{const o=p.default.homedir();i.default.writeFileSync(`${o}/.capgo`,`${e}\n`)}console.log(`login saved into .capgo file in ${r?"local":"home"} directory`)}))},666:function(e,o,r){var t=this&&this.__awaiter||function(e,o,r,t){return new(r||(r=Promise))((function(n,a){function i(e){try{s(t.next(e))}catch(e){a(e)}}function p(e){try{s(t.throw(e))}catch(e){a(e)}}function s(e){var o;e.done?n(e.value):(o=e.value,o instanceof r?o:new r((function(e){e(o)}))).then(i,p)}s((t=t.apply(e,o||[])).next())}))};Object.defineProperty(o,"__esModule",{value:!0}),o.setChannel=void 0;const n=r(304),a=r(233);o.setChannel=(e,o)=>t(void 0,void 0,void 0,(function*(){var r,t,i;let{version:p}=o;const{state:s,channel:d="dev"}=o,l=o.apikey||(0,a.findSavedKey)(),c=yield(0,a.getConfig)();let u;e=e||(null===(r=null==c?void 0:c.app)||void 0===r?void 0:r.appId),p=p||(null===(i=null===(t=null==c?void 0:c.app)||void 0===t?void 0:t.package)||void 0===i?void 0:i.version),"public"!==s&&"private"!==s||(u="public"===s),l||n.program.error("Missing API key, you need to provide a API key to add your app"),e||n.program.error("Missing argument, you need to provide a appid, or be in a capacitor project"),p||u||n.program.error("Missing argument, you need to provide a state or a version"),p?console.log(`Set ${d} to @${p} in ${e}`):console.log(`Set${d} to @${s} in ${e}`);try{const o=(0,a.createSupabaseClient)(l),{data:r,error:t}=yield o.rpc("is_allowed_capgkey",{apikey:l,keymode:["write","all"],app_id:e});r&&!t||n.program.error("Invalid API key or insufisant rights");const{data:i,error:s}=yield o.rpc("get_user_id",{apikey:l}),c=i?i.toString():"";c&&!s||n.program.error("Cannot verify user");const m={created_by:c,app_id:e,name:d};if(p){const{data:r,error:t}=yield o.from("app_versions").select().eq("app_id",e).eq("name",p).eq("user_id",c).eq("deleted",!1);!t&&r&&r.length||n.program.error(`Cannot find version ${p}`),m.version=r[0].id}void 0!==u&&(m.public=u);try{const{error:e}=yield(0,a.updateOrCreateChannel)(o,m,l);e&&n.program.error(`Cannot set channel ${(0,a.formatError)(e)}`)}catch(e){n.program.error(`Cannot set channel ${(0,a.formatError)(e)}`)}}catch(e){n.program.error(`Unknow error ${(0,a.formatError)(e)}`)}p?console.log("Done ✅"):console.log(`You can use now is channel in your app with the url: ${a.host}/api/latest?appid=${e}&channel=${d}`)}))},310:function(e,o,r){var t=this&&this.__awaiter||function(e,o,r,t){return new(r||(r=Promise))((function(n,a){function i(e){try{s(t.next(e))}catch(e){a(e)}}function p(e){try{s(t.throw(e))}catch(e){a(e)}}function s(e){var o;e.done?n(e.value):(o=e.value,o instanceof r?o:new r((function(e){e(o)}))).then(i,p)}s((t=t.apply(e,o||[])).next())}))},n=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(o,"__esModule",{value:!0}),o.uploadVersion=void 0;const a=n(r(844)),i=r(304),p=r(113),s=n(r(295)),d=r(233);o.uploadVersion=(e,o)=>t(void 0,void 0,void 0,(function*(){var r,t,n,l;let{version:c,path:u,channel:m}=o;const{external:g}=o,f=o.apikey||(0,d.findSavedKey)();m=m||"dev";const y=yield(0,d.getConfig)();e=e||(null===(r=null==y?void 0:y.app)||void 0===r?void 0:r.appId),c=c||(null===(n=null===(t=null==y?void 0:y.app)||void 0===t?void 0:t.package)||void 0===n?void 0:n.version),u=u||(null===(l=null==y?void 0:y.app)||void 0===l?void 0:l.webDir),f||i.program.error("Missing API key, you need to provide a API key to add your app"),e&&c&&u||i.program.error("Missing argument, you need to provide a appid and a version and a path, or be in a capacitor project"),console.log(`Upload ${e}@${c} started from path "${u}" to Capgo cloud`);const v=(0,d.createSupabaseClient)(f),h=new s.default.MultiBar({clearOnComplete:!1,hideCursor:!0},s.default.Presets.shades_grey),_=h.create(7,0,{format:"Uploading: [{bar}] {percentage}% | ETA: {eta}s | {value}/{total} Part"},s.default.Presets.shades_grey);_.start(7,0,{speed:"N/A"});const{data:k,error:b}=yield v.rpc("is_allowed_capgkey",{apikey:f,keymode:["upload","write","all"],app_id:e});k&&!b||(h.stop(),i.program.error(`Invalid API key or insufisant rights ${(0,d.formatError)(b)}`)),_.increment();const{data:$,error:w}=yield v.rpc("exist_app_versions",{apikey:f,name_version:c,appid:e});($||w)&&(h.stop(),i.program.error(`This app version already exist ${(0,d.formatError)(w)}`)),_.increment();const{data:x,error:C}=yield v.rpc("get_user_id",{apikey:f}),q=x?x.toString():"";q&&!C||(h.stop(),i.program.error(`Cannot verify user ${(0,d.formatError)(C)}`)),_.increment();const{data:I,error:S}=yield v.rpc("exist_app",{appid:e,apikey:f});if(I&&!S||(h.stop(),i.program.error(`Cannot find app ${e} in your account \n${(0,d.formatError)(S)}`)),_.increment(),g)g&&!g.startsWith("https://")&&(h.stop(),i.program.error(`External link should should start with "https://" current is "${g}"`));else{const o=new a.default;o.addLocalFolder(u);const r=o.toBuffer(),t=Math.floor(r.byteLength/1024/1024),n=`apps/${q}/${e}/versions`,s=(0,p.randomUUID)();_.increment(),t>30&&(h.stop(),i.program.error(`The app is too big, the limit is 30 Mb, your is ${t} Mb`)),t>25&&h.log(`WARNING !!\nThe app size is ${t} Mb, the limit is 30 Mb\n`);const{error:l}=yield v.storage.from(n).upload(s,r,{contentType:"application/zip"});l&&(h.stop(),i.program.error(`Cannot upload ${(0,d.formatError)(l)}`))}_.increment();const A=(0,p.randomUUID)(),{data:M,error:P}=yield(0,d.updateOrCreateVersion)(v,{bucket_id:g?void 0:A,user_id:q,name:c,app_id:e,external_url:g},f);if(P&&(h.stop(),i.program.error(`Cannot add version ${(0,d.formatError)(P)}`)),_.increment(),M&&M.length){const{error:o}=yield(0,d.updateOrCreateChannel)(v,{name:m,app_id:e,created_by:q,version:M[0].id},f);o&&h.log("Cannot set version with upload key, use key with more rights for that\n")}else h.log("Cannot set version with upload key, use key with more rights for that\n");h.stop(),console.log("App uploaded to server"),console.log(`Try it in mobile app: ${d.host}/app_mobile`),console.log(`Or set the channel ${m} as public here: ${d.hostWeb}/app/package/${e}`),console.log("To use with live update in your own app")}))},233:function(e,o,r){var t=this&&this.__awaiter||function(e,o,r,t){return new(r||(r=Promise))((function(n,a){function i(e){try{s(t.next(e))}catch(e){a(e)}}function p(e){try{s(t.throw(e))}catch(e){a(e)}}function s(e){var o;e.done?n(e.value):(o=e.value,o instanceof r?o:new r((function(e){e(o)}))).then(i,p)}s((t=t.apply(e,o||[])).next())}))},n=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(o,"__esModule",{value:!0}),o.updateOrCreateChannel=o.updateOrCreateVersion=o.getConfig=o.formatError=o.findSavedKey=o.createSupabaseClient=o.supaAnon=o.hostSupa=o.hostWeb=o.host=void 0;const a=r(778),i=r(304),p=r(885),s=n(r(867)),d=n(r(147)),l=n(r(37));o.host="https://capgo.app",o.hostWeb="https://web.capgo.app",o.hostSupa="https://aucsybvnhavogdmzwtcw.supabase.co",o.supaAnon="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImF1Y3N5YnZuaGF2b2dkbXp3dGN3Iiwicm9sZSI6ImFub24iLCJpYXQiOjE2NTM1ODcxNjgsImV4cCI6MTk2OTE2MzE2OH0.8FKKJqiGgoVA3p9GH5wvnbWkWywIxVLqQyZFhupZ7C4",o.createSupabaseClient=e=>(0,p.createClient)(o.hostSupa,o.supaAnon,{headers:{capgkey:e}}),o.findSavedKey=()=>{let e=`${l.default.homedir()}/.capgo`;return d.default.existsSync(e)?d.default.readFileSync(e,"utf8").trim():(e=".capgo",d.default.existsSync(e)?d.default.readFileSync(e,"utf8").trim():null)},o.formatError=e=>e?`\n${s.default.render(e)}`:"",o.getConfig=()=>t(void 0,void 0,void 0,(function*(){let e;try{e=yield(0,a.loadConfig)()}catch(e){i.program.error("No capacitor config file found, run `cap init` first")}return e})),o.updateOrCreateVersion=(e,o,r)=>t(void 0,void 0,void 0,(function*(){const{data:t,error:n}=yield e.rpc("exist_app_versions",{appid:o.app_id,name_version:o.name,apikey:r});return t&&!n?(o.deleted=!1,e.from("app_versions").update(o,{returning:"minimal"}).eq("app_id",o.app_id).eq("name",o.name)):e.from("app_versions").insert(o,{returning:"minimal"})})),o.updateOrCreateChannel=(e,o,r)=>t(void 0,void 0,void 0,(function*(){if(!o.app_id||!o.name||!o.created_by)return console.error("missing app_id, name, or created_by"),Promise.reject(new Error("missing app_id, name, or created_by"));const{data:t,error:n}=yield e.rpc("exist_channel",{appid:o.app_id,name_channel:o.name,apikey:r});return t&&!n?e.from("channels").update(o,{returning:"minimal"}).eq("app_id",o.app_id).eq("name",o.name).eq("created_by",o.created_by):e.from("channels").insert(o,{returning:"minimal"})}))},778:e=>{e.exports=require("@capacitor/cli/dist/config")},885:e=>{e.exports=require("@supabase/supabase-js")},844:e=>{e.exports=require("adm-zip")},295:e=>{e.exports=require("cli-progress")},304:e=>{e.exports=require("commander")},470:e=>{e.exports=require("fs-extra")},11:e=>{e.exports=require("mime")},867:e=>{e.exports=require("prettyjson")},113:e=>{e.exports=require("crypto")},147:e=>{e.exports=require("fs")},37:e=>{e.exports=require("os")},598:e=>{e.exports=JSON.parse('{"name":"@capgo/cli","version":"0.11.2","description":"A CLI to upload to capgo servers","main":"dist/index.js","bin":{"capgo":"dist/index.js"},"repository":{"type":"git","url":"git+https://github.com/Cap-go/capgo-cli.git"},"bugs":{"url":"https://github.com/Cap-go/capgo-cli/issues"},"keywords":["appflow alternative","ionic","capacitor","auto update","live update","capgo","cli","upload","capgo-cli"],"scripts":{"dev":"set NODE_ENV=development&& npx webpack --config webpack.config.js","no-debug":"node dist/index.js","build":"set NODE_ENV=production&& npx webpack --config webpack.config.js","pack":"pkg","test_rls":"ts-node ./test/test_headers_rls.ts","lint":"eslint . --ext .ts --fix"},"author":"github.com/riderx","license":"Apache 2.0","dependencies":{"@capacitor/cli":"3.5.1","@supabase/supabase-js":"^1.35.3","adm-zip":"^0.5.9","cli-progress":"3.11.0","commander":"9.2.0","form-data":"^4.0.0","fs-extra":"10.1.0","mime":"^3.0.0","node-stream-zip":"^1.15.0","prettyjson":"^1.2.5","upath":"^2.0.1"},"devDependencies":{"@types/adm-zip":"0.5.0","@types/cli-progress":"^3.11.0","@types/fs-extra":"^9.0.13","@types/mime":"^2.0.3","@types/node":"^17.0.31","@types/prettyjson":"^0.0.30","@typescript-eslint/eslint-plugin":"5.22.0","@typescript-eslint/parser":"5.22.0","eslint":"8.14.0","eslint-config-airbnb-base":"^15.0.0","eslint-config-prettier":"^8.5.0","eslint-import-resolver-typescript":"2.7.1","eslint-plugin-import":"2.26.0","eslint-plugin-prettier":"^4.0.0","git-format-staged":"3.0.0","husky":"^7.0.4","nodemon":"2.0.16","pkg":"5.6.0","prettier":"2.6.2","ts-loader":"^9.3.0","ts-node":"^10.7.0","tsconfig-paths":"4.0.0","typescript":"4.6.4","webpack":"5.72.0","webpack-cli":"^4.9.2","webpack-node-externals":"^3.0.0"}}')}},o={};!function r(t){var n=o[t];if(void 0!==n)return n.exports;var a=o[t]={exports:{}};return e[t].call(a.exports,a,a.exports,r),a.exports}(473)})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/cli",
3
- "version": "0.10.1",
3
+ "version": "0.11.2",
4
4
  "description": "A CLI to upload to capgo servers",
5
5
  "main": "dist/index.js",
6
6
  "bin": {
package/src/bin/add.ts CHANGED
@@ -4,7 +4,7 @@ import { readFileSync } from 'fs';
4
4
  import { existsSync } from 'fs-extra';
5
5
  import { getType } from 'mime';
6
6
  import { definitions } from './types_supabase'
7
- import { getConfig, createSupabaseClient, formatError } from './utils';
7
+ import { getConfig, createSupabaseClient, formatError, findSavedKey } from './utils';
8
8
 
9
9
  interface Options {
10
10
  apikey: string;
@@ -14,7 +14,7 @@ interface Options {
14
14
  const newIconPath = "assets/icon.png"
15
15
  export const addApp = async (appid: string, options: Options) => {
16
16
  let { name, icon } = options;
17
- const { apikey } = options;
17
+ const apikey = options.apikey || findSavedKey()
18
18
  const config = await getConfig();
19
19
  appid = appid || config?.app?.appId
20
20
  name = name || config?.app?.appName || 'Unknown'
package/src/bin/delete.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { program } from 'commander';
2
- import { getConfig, createSupabaseClient, formatError } from './utils';
2
+ import { getConfig, createSupabaseClient, formatError, findSavedKey } from './utils';
3
3
  import { definitions } from './types_supabase'
4
4
 
5
5
  interface Options {
@@ -8,7 +8,8 @@ interface Options {
8
8
  }
9
9
 
10
10
  export const deleteApp = async (appid: string, options: Options) => {
11
- const { apikey, version } = options;
11
+ const { version } = options;
12
+ const apikey = options.apikey || findSavedKey()
12
13
  const config = await getConfig();
13
14
  appid = appid || config?.app?.appId
14
15
  if (!apikey) {
package/src/bin/index.ts CHANGED
@@ -4,6 +4,7 @@ import { deleteApp } from './delete';
4
4
  import { setChannel } from './set';
5
5
  import { uploadVersion } from './upload';
6
6
  import pack from '../../package.json'
7
+ import { login } from './login';
7
8
 
8
9
  program
9
10
  .version(pack.version)
@@ -13,6 +14,13 @@ program
13
14
  .option('-i, --icon <icon>', 'app icon path')
14
15
  .option('-a, --apikey <apikey>', 'apikey to link to your account');
15
16
 
17
+ program
18
+ .version(pack.version)
19
+ .command('login [apikey]').alias('l')
20
+ .action(login)
21
+ .option('--local', 'Only save in local folder');
22
+
23
+
16
24
  program
17
25
  .command('upload [appid]').alias('u')
18
26
  .action(uploadVersion)
@@ -32,7 +40,7 @@ program
32
40
 
33
41
  program
34
42
  .description('Manage package and version in capgo Cloud')
35
- .command('delete [appid]').alias('a')
43
+ .command('delete [appid]').alias('d')
36
44
  .action(deleteApp)
37
45
  .option('-a, --apikey <apikey>', 'apikey to link to your account')
38
46
  .option('-v, --version <version>', 'version number of the app to delete');
@@ -0,0 +1,23 @@
1
+ import { program } from 'commander';
2
+ import fs from 'fs'
3
+ import os from 'os'
4
+
5
+ interface Options {
6
+ local: boolean;
7
+ }
8
+ export const login = async (apikey: string, options: Options) => {
9
+ // write in file .capgo the apikey in home directory
10
+ const { local } = options;
11
+
12
+ if (local) {
13
+ if (!fs.existsSync('.git')) {
14
+ program.error('To use local you should be in a git repository');
15
+ }
16
+ fs.writeFileSync('.capgo', `${apikey}\n`);
17
+ fs.appendFileSync('.gitignore', '.capgo\n');
18
+ } else {
19
+ const userHomeDir = os.homedir();
20
+ fs.writeFileSync(`${userHomeDir}/.capgo`, `${apikey}\n`);
21
+ }
22
+ console.log(`login saved into .capgo file in ${local ? 'local' : 'home'} directory`);
23
+ }
package/src/bin/set.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { program } from 'commander';
2
- import { getConfig, createSupabaseClient, updateOrCreateChannel, host, formatError } from './utils';
2
+ import { getConfig, createSupabaseClient, updateOrCreateChannel, host, formatError, findSavedKey } from './utils';
3
3
  import { definitions } from './types_supabase';
4
4
 
5
5
  interface Options {
@@ -11,7 +11,8 @@ interface Options {
11
11
 
12
12
  export const setChannel = async (appid: string, options: Options) => {
13
13
  let { version } = options;
14
- const { apikey, state, channel = 'dev' } = options;
14
+ const { state, channel = 'dev' } = options;
15
+ const apikey = options.apikey || findSavedKey()
15
16
  const config = await getConfig();
16
17
  appid = appid || config?.app?.appId
17
18
  version = version || config?.app?.package?.version
package/src/bin/upload.ts CHANGED
@@ -2,7 +2,10 @@ import AdmZip from 'adm-zip';
2
2
  import { program } from 'commander';
3
3
  import { randomUUID } from 'crypto';
4
4
  import cliProgress from 'cli-progress';
5
- import { host, hostWeb, getConfig, createSupabaseClient, updateOrCreateChannel, updateOrCreateVersion, formatError } from './utils';
5
+ import {
6
+ host, hostWeb, getConfig, createSupabaseClient,
7
+ updateOrCreateChannel, updateOrCreateVersion, formatError, findSavedKey
8
+ } from './utils';
6
9
 
7
10
  interface Options {
8
11
  version: string
@@ -17,7 +20,8 @@ const alertMb = 25;
17
20
 
18
21
  export const uploadVersion = async (appid: string, options: Options) => {
19
22
  let { version, path, channel } = options;
20
- const { apikey, external } = options;
23
+ const { external } = options;
24
+ const apikey = options.apikey || findSavedKey()
21
25
  channel = channel || 'dev';
22
26
  const config = await getConfig();
23
27
  appid = appid || config?.app?.appId
@@ -54,6 +58,16 @@ export const uploadVersion = async (appid: string, options: Options) => {
54
58
  }
55
59
  b1.increment();
56
60
 
61
+ // checking if user has access rights before uploading
62
+ const { data: versionExist, error: versionExistError } = await supabase
63
+ .rpc('exist_app_versions', { apikey, name_version: version, appid })
64
+
65
+ if (versionExist || versionExistError) {
66
+ multibar.stop()
67
+ program.error(`This app version already exist ${formatError(versionExistError)}`);
68
+ }
69
+ b1.increment();
70
+
57
71
  const { data, error: userIdError } = await supabase
58
72
  .rpc<string>('get_user_id', { apikey })
59
73
 
@@ -128,7 +142,6 @@ export const uploadVersion = async (appid: string, options: Options) => {
128
142
  } else {
129
143
  multibar.log('Cannot set version with upload key, use key with more rights for that\n');
130
144
  }
131
- b1.increment();
132
145
  multibar.stop()
133
146
  console.log("App uploaded to server")
134
147
  console.log(`Try it in mobile app: ${host}/app_mobile`)
package/src/bin/utils.ts CHANGED
@@ -2,6 +2,8 @@ import { loadConfig } from '@capacitor/cli/dist/config';
2
2
  import { program } from 'commander';
3
3
  import { createClient, SupabaseClient } from '@supabase/supabase-js'
4
4
  import prettyjson from 'prettyjson';
5
+ import fs from 'fs'
6
+ import os from 'os'
5
7
  import { definitions } from './types_supabase';
6
8
 
7
9
  export const host = 'https://capgo.app';
@@ -23,6 +25,22 @@ export const createSupabaseClient = (apikey: string) => createClient(hostSupa, s
23
25
  }
24
26
  })
25
27
 
28
+ export const findSavedKey = () => {
29
+ // search for key in home dir
30
+ const userHomeDir = os.homedir();
31
+ let keyPath = `${userHomeDir}/.capgo`;
32
+ if (fs.existsSync(keyPath)) {
33
+ const key = fs.readFileSync(keyPath, 'utf8');
34
+ return key.trim();
35
+ }
36
+ keyPath = `.capgo`;
37
+ if (fs.existsSync(keyPath)) {
38
+ const key = fs.readFileSync(keyPath, 'utf8');
39
+ return key.trim();
40
+ }
41
+ return null
42
+ }
43
+
26
44
  export const formatError = (error: any) => error ? `\n${prettyjson.render(error)}` : ''
27
45
 
28
46
  interface Config {