@portalsdk/cli 0.1.2 → 0.3.1
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/README.md +49 -3
- package/dist/index.js +10 -5
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @portalsdk/cli
|
|
2
2
|
|
|
3
|
-
The Portal command line
|
|
3
|
+
The Portal command line: sign in, deploy your configuration, and set secrets.
|
|
4
4
|
|
|
5
5
|
```bash
|
|
6
6
|
npm install -g @portalsdk/cli
|
|
@@ -10,12 +10,58 @@ npx @portalsdk/cli deploy
|
|
|
10
10
|
|
|
11
11
|
## Authenticate
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
There are two kinds of credential, for two kinds of command.
|
|
14
|
+
|
|
15
|
+
**Your account** — `portal login` signs you in as you, for commands that work
|
|
16
|
+
across your projects:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
portal login
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
Your code: K7WQ-3MTD
|
|
24
|
+
Approve at: https://app.useportal.co/authorize-cli?code=K7WQ3MTD
|
|
25
|
+
|
|
26
|
+
Waiting for approval…
|
|
27
|
+
✓ Signed in as Sam Rivera (sam@example.com).
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Your browser opens to the approval page — confirm the code matches the one in
|
|
31
|
+
your terminal, and the CLI is signed in. The credential is stored in
|
|
32
|
+
`~/.portal/credentials.json`, readable only by you.
|
|
33
|
+
|
|
34
|
+
**A project key** — `deploy` and `secrets set` act on one environment, so they
|
|
35
|
+
take that environment's **secret** key (`sk_...`):
|
|
14
36
|
|
|
15
37
|
```bash
|
|
16
38
|
export PORTAL_SECRET=sk_your_secret_key
|
|
17
39
|
```
|
|
18
40
|
|
|
41
|
+
## `portal login`
|
|
42
|
+
|
|
43
|
+
Signs in to your Portal account. Options:
|
|
44
|
+
|
|
45
|
+
- `--name <name>` — label for this machine, so you can tell your devices apart
|
|
46
|
+
(default: its hostname).
|
|
47
|
+
|
|
48
|
+
## `portal whoami`
|
|
49
|
+
|
|
50
|
+
Shows who you are signed in as.
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
portal whoami
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
Sam Rivera (sam@example.com)
|
|
58
|
+
Acme
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## `portal logout`
|
|
62
|
+
|
|
63
|
+
Signs out on this machine and revokes the credential.
|
|
64
|
+
|
|
19
65
|
## `portal deploy`
|
|
20
66
|
|
|
21
67
|
Deploys the `portal.config.ts` in the current directory (or `--config <path>`).
|
|
@@ -60,7 +106,7 @@ portal secrets set OPENAI_API_KEY
|
|
|
60
106
|
|
|
61
107
|
| Variable | Purpose |
|
|
62
108
|
| --- | --- |
|
|
63
|
-
| `PORTAL_SECRET` | Your project secret key (`sk_...`). Required
|
|
109
|
+
| `PORTAL_SECRET` | Your project secret key (`sk_...`). Required by `deploy` and `secrets set`. |
|
|
64
110
|
| `PORTAL_API_URL` | Override the API base URL (for local development). Optional. |
|
|
65
111
|
|
|
66
112
|
## License
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{Command as
|
|
3
|
-
|
|
2
|
+
import{createRequire as at}from"module";import{Command as ct}from"commander";import c from"picocolors";import{spawn as we}from"child_process";import ve from"os";var d=class extends Error{constructor(t,r,o){super(t);this.status=r;this.code=o;this.name="ApiError"}},x=class{constructor(n,t){this.secret=t;this.baseUrl=n.replace(/\/+$/,"")}baseUrl;async createDeploy(n){let{status:t,data:r}=await this.send("POST","/v1/deploys",n);if(t>=200&&t<300)return{versionId:H(r),alreadyDeployed:!1};if(t===409)return{versionId:H(r),alreadyDeployed:!0};throw this.fail(t,r)}async activate(n){let{status:t,data:r}=await this.send("POST",`/v1/deploys/${encodeURIComponent(n)}/activate`);if(t<200||t>=300)throw this.fail(t,r)}async setSecret(n,t){let{status:r,data:o}=await this.send("PUT",`/v1/secrets/${encodeURIComponent(n)}`,{value:t});if(r<200||r>=300)throw this.fail(r,o)}async send(n,t,r){let o;try{o=await fetch(`${this.baseUrl}${t}`,{method:n,headers:{authorization:`Bearer ${this.secret}`,"content-type":"application/json"},body:r===void 0?void 0:JSON.stringify(r)})}catch(a){throw new d(`Could not reach the Portal API at ${this.baseUrl}: ${a instanceof Error?a.message:String(a)}`,0)}let s=await o.text(),i={};if(s)try{i=JSON.parse(s)}catch{i={reason:s}}return{status:o.status,data:i}}fail(n,t){let r=t.code;return n===401||r==="unauthorized"?new d("Your PORTAL_SECRET was not accepted. It must be a valid project secret key (sk_...).",n,r):new d(t.reason||`The Portal API rejected the request (${r??n}).`,n,r)}};function H(e){if(!e.versionId)throw new d("The Portal API did not return a version id for this deploy.",502);return e.versionId}var u=class{baseUrl;constructor(n){this.baseUrl=n.replace(/\/+$/,"")}async startDeviceLogin(n){let{status:t,data:r}=await this.send("POST","/v1/device/start",{...n?{name:n}:{}});if(t<200||t>=300)throw this.fail(t,r);let o=r;if(!o.deviceCode||!o.userCode||!o.verificationUri)throw new d("The Portal API did not return a usable login code.",502);return{deviceCode:o.deviceCode,userCode:o.userCode,verificationUri:o.verificationUri,interval:typeof o.interval=="number"?o.interval:5,expiresIn:typeof o.expiresIn=="number"?o.expiresIn:600}}async pollDeviceLogin(n){let{status:t,data:r}=await this.send("POST","/v1/device/poll",{deviceCode:n});if(t===429)return{status:"slow_down"};if(t===404)return{status:"expired"};if(t<200||t>=300)throw this.fail(t,r);let o=r;switch(o.status){case"approved":return{status:"approved",token:o.token,expiresAt:o.expiresAt};case"denied":return{status:"denied"};case"expired":return{status:"expired"};default:return{status:"pending"}}}async fetchIdentity(n){let{status:t,data:r}=await this.send("GET","/v1/me",void 0,n);if(t<200||t>=300)throw this.fail(t,r);return r}async revokeCurrentToken(n){let{status:t,data:r}=await this.send("DELETE","/v1/cli-tokens/current",void 0,n);if(t<200||t>=300)throw this.fail(t,r)}async revokeAllTokens(n){let{status:t,data:r}=await this.send("DELETE","/v1/cli-tokens",void 0,n);if(t<200||t>=300)throw this.fail(t,r);let o=r;return typeof o.revoked=="number"?o.revoked:0}async createProject(n,t,r){let{status:o,data:s}=await this.send("POST","/v1/projects",{organizationId:t,name:r},n);if(o<200||o>=300)throw this.fail(o,s);let i=s;if(!i.project?.id||!Array.isArray(i.environments))throw new d("The Portal API did not return a usable project.",502);return{project:i.project,environments:i.environments}}async createApiKey(n,t,r){let{status:o,data:s}=await this.send("POST",`/v1/environments/${encodeURIComponent(t)}/api-keys`,{keyType:r},n);if(o<200||o>=300)throw this.fail(o,s);let i=s;if(!i.apiKey?.value)throw new d("The Portal API did not return a usable API key.",502);return i.apiKey}async createAllowedOrigin(n,t,r){let{status:o,data:s}=await this.send("POST",`/v1/environments/${encodeURIComponent(t)}/allowed-origins`,{origin:r},n);if(o<200||o>=300)throw this.fail(o,s);let i=s;if(!i.origin?.id)throw new d("The Portal API did not return a usable origin.",502);return i.origin}async listAllowedOrigins(n,t){let{status:r,data:o}=await this.send("GET",`/v1/environments/${encodeURIComponent(t)}/allowed-origins`,void 0,n);if(r<200||r>=300)throw this.fail(r,o);let s=o;return Array.isArray(s.origins)?s.origins:[]}async deleteAllowedOrigin(n,t){let{status:r,data:o}=await this.send("DELETE",`/v1/allowed-origins/${encodeURIComponent(t)}`,void 0,n);if(r<200||r>=300)throw this.fail(r,o)}async send(n,t,r,o){let s;try{s=await fetch(`${this.baseUrl}${t}`,{method:n,headers:{"content-type":"application/json",...o?{authorization:`Bearer ${o}`}:{}},body:r===void 0?void 0:JSON.stringify(r)})}catch(l){throw new d(`Could not reach the Portal API at ${this.baseUrl}: ${l instanceof Error?l.message:String(l)}`,0)}let i=await s.text(),a={};if(i)try{a=JSON.parse(i)}catch{a={reason:i}}return{status:s.status,data:a}}fail(n,t){let r=typeof t.code=="string"?t.code:void 0,o=typeof t.reason=="string"?t.reason:void 0;return n===401?new d("Your login is no longer valid.",n,r):new d(o||`The Portal API rejected the request (${r??n}).`,n,r)}};import k from"fs/promises";import P from"os";import F from"path";var ye=448,z=384;function W(e=P.homedir()){return F.join(e,".portal")}function $(e=P.homedir()){return F.join(W(e),"credentials.json")}async function S(e=P.homedir()){let n;try{n=await k.readFile($(e),"utf8")}catch{return null}try{let t=JSON.parse(n);return typeof t.token!="string"||!t.token?null:{token:t.token,expiresAt:typeof t.expiresAt=="string"?t.expiresAt:void 0}}catch{return null}}async function B(e,n=P.homedir()){let t=W(n),r=$(n);return await k.mkdir(t,{recursive:!0,mode:ye}),await k.writeFile(r,`${JSON.stringify(e,null,2)}
|
|
3
|
+
`,{mode:z}),await k.chmod(r,z),r}async function j(e=P.homedir()){try{await k.unlink($(e))}catch{}}var xe=e=>new Promise(n=>setTimeout(n,e));function ke(e){return e.length===8?`${e.slice(0,4)}-${e.slice(4)}`:e}function Pe(){try{return ve.hostname()||void 0}catch{return}}function be(e){let n=process.platform==="darwin"?"open":process.platform==="win32"?"cmd":"xdg-open",t=process.platform==="win32"?["/c","start","",e]:[e];try{let r=we(n,t,{stdio:"ignore",detached:!0});r.on("error",()=>{}),r.unref()}catch{}}async function J(e){let n=e.sleep??xe,t=e.openBrowser??be,r=e.log??(p=>console.log(p)),o=new u(e.apiUrl),s=e.name??Pe(),i=await o.startDeviceLogin(s),a=`${i.verificationUri}?code=${encodeURIComponent(i.userCode)}`;r(""),r(` Your code: ${ke(i.userCode)}`),r(` Approve at: ${a}`),r(""),r(" Waiting for approval\u2026"),t(a);let l=Date.now()+i.expiresIn*1e3,f=i.interval*1e3;for(;Date.now()<l;){await n(f);let p=await o.pollDeviceLogin(i.deviceCode);if(p.status==="slow_down"){f+=5e3;continue}if(p.status==="pending")continue;if(p.status==="denied")throw new Error("The login request was denied.");if(p.status==="expired")throw new Error("The login request expired. Run `portal login` to try again.");if(!p.token)throw new Error("This login code was already used. Run `portal login` to start a new one.");let w=await B({token:p.token,expiresAt:p.expiresAt}),O=null;try{O=await o.fetchIdentity(p.token)}catch{O=null}return{identity:O,credentialsPath:w}}throw new Error("The login request expired. Run `portal login` to try again.")}async function G(e){let n=await S();if(!n)return{loggedIn:!1};try{return{loggedIn:!0,identity:await new u(e.apiUrl).fetchIdentity(n.token)}}catch(t){if(t instanceof d&&t.status===401)return await j(),{loggedIn:!1};throw t}}async function V(e){let n=await S();if(!n)return{wasLoggedIn:!1,revoked:!1};let t=!1,r;try{let o=new u(e.apiUrl);e.all?r=await o.revokeAllTokens(n.token):await o.revokeCurrentToken(n.token),t=!0}catch{}return await j(),{wasLoggedIn:!0,revoked:t,...r!==void 0?{revokedCount:r}:{}}}async function Ee(){return(await S())?.token??null}async function v(){let e=await Ee();if(!e)throw new Error("Not signed in. Run: portal login");return e}import Je from"path";import{existsSync as X}from"fs";import T from"path";import{mkdtemp as Se,rm as Ce,writeFile as Ae}from"fs/promises";import{tmpdir as Ie}from"os";import C from"path";import{pathToFileURL as Oe}from"url";import Y from"esbuild";var U=class extends Error{};async function A(e){let n;try{n=(await Y.build({entryPoints:[e],bundle:!0,write:!1,format:"esm",platform:"node",target:"node18",logLevel:"silent",absWorkingDir:C.dirname(e)})).outputFiles[0].text}catch(o){throw new U($e(o,e))}let t=await Se(C.join(Ie(),"portal-cli-")),r=C.join(t,"module.mjs");try{return await Ae(r,n,"utf8"),await import(Oe(r).href)}finally{await Ce(t,{recursive:!0,force:!0})}}function $e(e,n){if(e!=null&&typeof e=="object"&&"errors"in e&&Array.isArray(e.errors)){let r=Y.formatMessagesSync(e.errors,{kind:"error",color:!1}).join(`
|
|
4
|
+
`).trimEnd();if(r)return r}return`Failed to compile ${C.basename(n)}: ${e instanceof Error?e.message:String(e)}`}var b=class extends Error{},je=["portal.config.ts","portal.config.mts","portal.config.js","portal.config.mjs"];function Q(e,n){if(n){let t=T.resolve(e,n);if(!X(t))throw new b(`No config file found at ${n}`);return t}for(let t of je){let r=T.join(e,t);if(X(r))return r}throw new b(`No portal.config.ts found in ${e}. Create one, or point at it with --config <path>.`)}async function Z(e){let t=(await A(e)).default;if(t==null||typeof t!="object")throw new b(`${T.basename(e)} must \`export default defineConfig({ ... })\`.`);return t}function L(e,n){let t={project:{webhooks:null},channels:{}};e.auth&&(t.project.auth={issuer:e.auth.issuer,jwksUrl:e.auth.jwksUrl,claimMap:{...e.auth.claimMap}});for(let[r,o]of Object.entries(e.channels??{}))t.channels[r]=Ue(o,n.get(r)??[]);return t}function Ue(e,n){let t={};e.authz&&(t.authz=!0),e.onPublish&&e.onPublish.length>0&&(t.onPublish=e.onPublish.length),e.onDisconnect&&e.onDisconnect.length>0&&(t.onDisconnect=e.onDisconnect.length),e.notify&&(t.notify=!0);let r={};for(let o of n)r[o.handle]={namespace:o.namespace,transport:o.transport};return{mode:e.mode??"standard",anonymous:e.anonymous??!0,hooks:t,extensions:r}}import{mkdtemp as Re,rm as De,writeFile as Me}from"fs/promises";import{tmpdir as Ne}from"os";import I from"path";import qe from"esbuild";var y=class extends Error{};function R(e,n){if(e.webhooks!=null)throw new y("The `webhooks` block is reserved for a future release. Remove it before deploying.");for(let[t,r]of Object.entries(e.channels??{})){if(r&&"onMiddlewareFailure"in r)throw new y(`Channel "${t}" sets \`onMiddlewareFailure\`, which is no longer supported \u2014 message handling always fails closed. Remove it.`);Te(t),Le(t,n.get(t)??[])}}function Te(e){if(e.includes("*")&&!/^[^*]+\*$/.test(e))throw new y(`Unsupported channel pattern "${e}": a template must end with a single "*" over a fixed prefix (e.g. "room-*").`)}function Le(e,n){let t=new Map;for(let r of n){let o=t.get(r.namespace);if(o)throw new y(`Channel "${e}" has two extensions using the namespace "${r.namespace}" ("${o}" and "${r.handle}"). Each namespace must be unique within a channel.`);t.set(r.namespace,r.handle)}}var Ke="hooks";function te(e){return`import config from ${JSON.stringify(e)};
|
|
4
5
|
|
|
5
6
|
const channels = config.channels || {};
|
|
6
7
|
|
|
@@ -49,6 +50,10 @@ export async function run(request) {
|
|
|
49
50
|
}
|
|
50
51
|
|
|
51
52
|
export default { run };
|
|
52
|
-
`}function
|
|
53
|
-
`),n(
|
|
54
|
-
`),process.exit(1)),
|
|
53
|
+
`}function ne(e){return Object.values(e.channels??{}).some(n=>n.authz!=null||(n.onPublish?.length??0)>0||(n.onDisconnect?.length??0)>0||n.notify!=null)}async function D(e,n,t){let r=I.dirname(n),o;if(ne(e)){let i=await He(n);o={name:Ke,content:i}}let s=[];for(let[i,a]of t)for(let l of a){let f=await re(l.path,r);s.push({key:i,handle:l.handle,name:ze(i,l.handle),content:f})}return _e(s),{hookScript:o,extensionScripts:s}}function _e(e){let n=new Map;for(let t of e){let r=n.get(t.name);if(r)throw new y(`Extensions "${r.handle}" (channel "${r.key}") and "${t.handle}" (channel "${t.key}") both map to the upload name "${t.name}". Rename a handle or channel so they differ by more than punctuation.`);n.set(t.name,t)}}function M(e,n){for(let t of n){let r=e.channels[t.key]?.extensions[t.handle];r&&(r.script=t.name)}}async function He(e){let n=await Re(I.join(Ne(),"portal-cli-hooks-")),t=I.join(n,"hooks.mjs");try{return await Me(t,te(e),"utf8"),await re(t,I.dirname(e))}finally{await De(n,{recursive:!0,force:!0})}}async function re(e,n){return(await qe.build({entryPoints:[e],bundle:!0,write:!1,format:"esm",target:"es2022",platform:"neutral",logLevel:"silent",absWorkingDir:n})).outputFiles[0].text}function ze(e,n){return`ext-${ee(e)}-${ee(n)}`}function ee(e){return e.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"")}import{existsSync as Fe}from"fs";import We from"path";var E=class extends Error{};async function N(e,n){let t=new Map;for(let[r,o]of Object.entries(e.channels??{})){let s=o.extensions;if(!s||Object.keys(s).length===0)continue;let i=[];for(let[a,l]of Object.entries(s)){let f=We.resolve(n,l);if(!Fe(f))throw new E(`Channel "${r}" attaches extension "${a}" from "${l}", but that file does not exist.`);let p=await A(f),w=Be(p.default);if(!w)throw new E(`Extension "${a}" (${l}) must \`export default defineExtension(...)\` with a static manifest.`);i.push({handle:a,source:l,namespace:w.namespace,transport:w.transport,path:f})}t.set(r,i)}return t}function Be(e){if(typeof e!="function"&&typeof e!="object")return null;let n=e?.manifest;if(n==null||typeof n!="object")return null;let{namespace:t,transport:r}=n;return typeof t!="string"||!t.endsWith(".")||r!=="ws"&&r!=="http"?null:{namespace:t,transport:r}}async function oe(e){let n=Q(e.cwd,e.configPath),t=await Z(n),r=Je.dirname(n),o=await N(t,r);R(t,o);let s=L(t,o),i=await D(t,n,o);M(s,i.extensionScripts);let a=new x(e.apiUrl,e.secret),{versionId:l,alreadyDeployed:f}=await a.createDeploy({extract:s,hookScript:i.hookScript,extensionScripts:i.extensionScripts.length>0?i.extensionScripts:void 0});return await a.activate(l),{configPath:n,versionId:l,alreadyDeployed:f,channels:Object.keys(s.channels),hasHookScript:!!i.hookScript,extensionCount:i.extensionScripts.length}}async function ie(e){await new x(e.apiUrl,e.secret).setSecret(e.name,e.value)}import se from"path";var Ge="https://api.useportal.co";function ae(e,n){let t=se.relative(n,e);return t&&!t.startsWith("..")&&!se.isAbsolute(t)?t:e}function g(){return(process.env.PORTAL_API_URL||Ge).replace(/\/+$/,"")}function q(){let e=process.env.PORTAL_SECRET;if(!e)throw new Error("Set PORTAL_SECRET to your project's secret key (sk_...) before running this command.");return e}async function ce(e){if(e!=null&&e!=="")return e;if(process.stdin.isTTY){let t=await nt("Value: ");if(!t)throw new Error("No value entered.");return t}let n=await Ve();if(!n)throw new Error("No value provided. Pass --value <value> or pipe the value on stdin.");return n}async function Ve(){let e=[];for await(let n of process.stdin)e.push(n);return Buffer.concat(e).toString("utf8").trim()}var Ye=10,Xe=13,Qe=4,Ze=3,et=8,tt=127;function nt(e){return new Promise(n=>{let t=process.stdin;process.stdout.write(e),t.setRawMode?.(!0),t.resume(),t.setEncoding("utf8");let r="",o=s=>{for(let i of s){let a=i.charCodeAt(0);if(a===Ye||a===Xe||a===Qe){t.setRawMode?.(!1),t.pause(),t.removeListener("data",o),process.stdout.write(`
|
|
54
|
+
`),n(r);return}a===Ze&&(t.setRawMode?.(!1),t.pause(),t.removeListener("data",o),process.stdout.write(`
|
|
55
|
+
`),process.exit(1)),a===et||a===tt?r=r.slice(0,-1):r+=i}};t.on("data",o)})}async function le(e){let n=await v();return new u(e.apiUrl).createApiKey(n,e.environmentId,e.keyType)}import{Portal as rt}from"@portalsdk/core";import{WebSocket as ot}from"ws";typeof globalThis.WebSocket>"u"&&(globalThis.WebSocket=ot);function it(e){let n=new Date(e.timestamp).toISOString(),t=e.sender.anon?`anon:${e.sender.id}`:e.sender.username??e.sender.id,r=typeof e.content=="string"?e.content:JSON.stringify(e.content);return`[${n}] ${t}: ${r}`}function pe(e){let n=e.log??(s=>console.log(s)),t=e.info??(s=>console.error(s)),o=new rt({apiKey:e.apiKey,...e.apiUrl?{apiUrl:e.apiUrl}:{},...e.realtimeUrl?{realtimeUrl:e.realtimeUrl}:{}}).channel(e.channelId,{history:"none"});return new Promise(s=>{let i=!1,a=()=>{i||(i=!0,process.off("SIGINT",a),l(),f(),o.release(),s())},l=o.on("message",p=>{n(it(p))}),f=o.on("status",(p,w)=>{p==="ready"&&t(`Listening on ${e.channelId} (Ctrl-C to exit)\u2026`),p==="blocked"&&(t(`Connection blocked${w?`: ${w.message}`:""}.`),a())});process.on("SIGINT",a),o.acquire()})}var K='Origin must be a bare "scheme://host[:port]" \u2014 e.g. https://app.example.com (no path, no trailing slash).';function st(e){let n=e.trim().replace(/\/+$/,""),t;try{t=new URL(n)}catch{return{ok:!1,message:K}}return t.protocol!=="http:"&&t.protocol!=="https:"?{ok:!1,message:K}:t.origin!==n?{ok:!1,message:K}:{ok:!0,origin:n}}async function de(e){let n=st(e.origin);if(!n.ok)throw new Error(n.message);let t=await v();return new u(e.apiUrl).createAllowedOrigin(t,e.environmentId,n.origin)}async function ue(e){let n=await v();return new u(e.apiUrl).listAllowedOrigins(n,e.environmentId)}async function fe(e){let n=await v();await new u(e.apiUrl).deleteAllowedOrigin(n,e.originId)}async function ge(e){let n=await v(),t=new u(e.apiUrl),r=await t.fetchIdentity(n),{organizations:o}=r;if(o.length===0)throw new Error("Your account has no organization yet \u2014 create one in the dashboard first.");if(o.length>1){let s=o.map(i=>i.name).join(", ");throw new Error(`Your account belongs to multiple organizations (${s}). portal projects create only supports a single organization right now.`)}return t.createProject(n,o[0].id,e.name)}var lt=at(import.meta.url),pt=lt("../package.json").version,m=new ct;m.name("portal").description("Sign in, deploy your Portal configuration, and manage secrets.").addHelpText("after",`
|
|
56
|
+
Authentication:
|
|
57
|
+
login signs you in as a user (for account-level commands)
|
|
58
|
+
PORTAL_SECRET your project's secret key (sk_...), used by deploy and secrets
|
|
59
|
+
`).version(pt);m.command("login").description("Sign in to Portal.").option("--name <name>","Label for this machine (default: its hostname)").action(async e=>{await h(async()=>{let n=await J({apiUrl:g(),name:e.name}),t=n.identity?he(n.identity):null;console.log(`${c.green("\u2713")} Signed in${t?` as ${c.bold(t)}`:""}.`)})});m.command("whoami").description("Show who you are signed in as.").action(async()=>{await h(async()=>{let e=await G({apiUrl:g()});if(!e.loggedIn){console.log("Not signed in. Run: portal login");return}console.log(he(e.identity));let{organizations:n}=e.identity;n.length>0&&console.log(c.dim(` ${n.map(t=>t.name).join(", ")}`))})});m.command("logout").description("Sign out of Portal on this machine.").option("--all","Revoke every CLI session for your account, not just this machine").action(async e=>{await h(async()=>{let n=await V({apiUrl:g(),all:e.all});if(!n.wasLoggedIn){console.log("You were not signed in.");return}if(n.revokedCount!==void 0){let t=n.revokedCount;console.log(`${c.green("\u2713")} Signed out. Revoked ${t} CLI session${t===1?"":"s"}.`);return}console.log(`${c.green("\u2713")} Signed out.`)})});m.command("deploy").description("Deploy your portal.config.ts.").option("-c, --config <path>","Path to your config file (default: portal.config.ts)").action(async e=>{await h(async()=>{let n=q(),t=await oe({cwd:process.cwd(),configPath:e.config,apiUrl:g(),secret:n});gt(t)})});var dt=m.command("secrets").description("Manage your project secrets.");dt.command("set").argument("<name>",'Secret name, referenced in your config with env("NAME")').description("Set a project secret.").option("--value <value>","The secret value (otherwise read from stdin)").action(async(e,n)=>{await h(async()=>{let t=q(),r=await ce(n.value);await ie({name:e,value:r,apiUrl:g(),secret:t}),console.log(`${c.green("\u2713")} Secret ${c.bold(e)} set.`)})});var ut=m.command("projects").description("Manage your Portal projects.");ut.command("create").argument("<name>","Project name").description("Create a project (auto-picks your organization) and its two environments.").action(async e=>{await h(async()=>{let n=await ge({apiUrl:g(),name:e});console.log(`${c.green("\u2713")} Created project ${c.bold(n.project.name)}`),console.log(` Project: ${n.project.id}`);for(let t of n.environments)console.log(` ${mt(t.type)}: ${t.id}`)})});var ft=m.command("keys").description("Manage environment API keys.");ft.command("create").description("Create an API key for an environment. The value is printed once.").requiredOption("--env <envId>","Environment id").requiredOption("--type <type>","public|secret").action(async e=>{await h(async()=>{if(e.type!=="public"&&e.type!=="secret")throw new Error("--type must be public or secret.");let n=await le({apiUrl:g(),environmentId:e.env,keyType:e.type});console.log(`${c.green("\u2713")} Created ${n.keyType} key.`),console.log(` ${n.value}`),console.log(c.dim(" This value is shown once \u2014 store it now."))})});var _=m.command("origins").description("Manage an environment's allowed browser origins.");_.command("add").argument("<origin>","Browser origin, e.g. https://app.example.com").description("Register an origin so browsers on it can use a pk_ from that environment.").requiredOption("--env <envId>","Environment id").action(async(e,n)=>{await h(async()=>{let t=await de({apiUrl:g(),environmentId:n.env,origin:e});console.log(`${c.green("\u2713")} Registered ${c.bold(t.origin)}`),console.log(c.dim(" localhost / 127.0.0.1 never need registering \u2014 loopback is always allowed."))})});_.command("list").description("List an environment's registered origins.").requiredOption("--env <envId>","Environment id").action(async e=>{await h(async()=>{let n=await ue({apiUrl:g(),environmentId:e.env});if(n.length===0){console.log("No origins registered (loopback is always allowed regardless).");return}for(let t of n)console.log(`${t.id} ${t.origin}`)})});_.command("remove").argument("<id>","Origin id, from `portal origins list`").description("Remove a registered origin.").action(async e=>{await h(async()=>{await fe({apiUrl:g(),originId:e}),console.log(`${c.green("\u2713")} Removed.`)})});m.command("listen").argument("<channelId>","Channel id to listen on").description("Connect anonymously and print every incoming message as one line.").requiredOption("--key <key>","Publishable key (pk_...)").action(async(e,n)=>{await h(async()=>{await pe({apiKey:n.key,channelId:e})})});m.parseAsync(process.argv);async function h(e){try{await e()}catch(n){console.error(`${c.red("Error:")} ${n instanceof Error?n.message:String(n)}`),process.exitCode=1}}function gt(e){e.alreadyDeployed?console.log(`${c.green("\u2713")} Already up to date \u2014 version ${c.bold(e.versionId)}`):(console.log(`${c.green("\u2713")} Deployed ${c.bold(ae(e.configPath,process.cwd()))}`),console.log(` Version ${e.versionId}`));let{channels:n}=e;console.log(` ${n.length} channel ${me(n.length,"override","overrides")}: ${n.join(", ")}`);let t=[];e.hasHookScript&&t.push("hooks"),e.extensionCount>0&&t.push(`${e.extensionCount} ${me(e.extensionCount,"extension","extensions")}`),t.length>0&&console.log(` Uploaded: ${t.join(", ")}`),console.log(c.dim(" Channels with active connections keep their current configuration until they restart; new connections use this version right away."))}function me(e,n,t){return e===1?n:t}function mt(e){return e.charAt(0).toUpperCase()+e.slice(1)}function he(e){let{firstName:n,lastName:t,email:r}=e.user,o=[n,t].filter(Boolean).join(" ").trim();return o?`${o} (${r})`:r}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@portalsdk/cli",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "The Portal command line: deploy your configuration and manage secrets.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -23,12 +23,15 @@
|
|
|
23
23
|
"realtime"
|
|
24
24
|
],
|
|
25
25
|
"dependencies": {
|
|
26
|
+
"@portalsdk/core": "^0.1.3",
|
|
26
27
|
"commander": "^14.0.2",
|
|
27
28
|
"esbuild": "^0.25.0",
|
|
28
|
-
"picocolors": "^1.1.1"
|
|
29
|
+
"picocolors": "^1.1.1",
|
|
30
|
+
"ws": "^8.18.0"
|
|
29
31
|
},
|
|
30
32
|
"devDependencies": {
|
|
31
33
|
"@types/node": "^24",
|
|
34
|
+
"@types/ws": "^8.5.13",
|
|
32
35
|
"tsup": "^8.5.0",
|
|
33
36
|
"tsx": "^4.7.1",
|
|
34
37
|
"typescript": "^5.8.3",
|