usebecky 0.0.0-stage → 0.2.0
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 +701 -2
- package/dist/analytics/client.d.ts +219 -0
- package/dist/analytics/client.js +923 -0
- package/dist/analytics/cmp.d.ts +20 -0
- package/dist/analytics/cmp.js +166 -0
- package/dist/analytics/context.d.ts +29 -0
- package/dist/analytics/context.js +97 -0
- package/dist/auth/client.d.ts +401 -0
- package/dist/auth/client.js +1081 -0
- package/dist/auth/webauthn.d.ts +18 -0
- package/dist/auth/webauthn.js +138 -0
- package/dist/cli/ai.d.ts +54 -0
- package/dist/cli/ai.js +202 -0
- package/dist/cli/analytics.d.ts +53 -0
- package/dist/cli/analytics.js +357 -0
- package/dist/cli/config.d.ts +75 -0
- package/dist/cli/config.js +718 -0
- package/dist/cli/db.d.ts +48 -0
- package/dist/cli/db.js +215 -0
- package/dist/cli/deploy-api.d.ts +168 -0
- package/dist/cli/deploy-api.js +177 -0
- package/dist/cli/deploy-ci.d.ts +15 -0
- package/dist/cli/deploy-ci.js +54 -0
- package/dist/cli/deploy-detect.d.ts +64 -0
- package/dist/cli/deploy-detect.js +249 -0
- package/dist/cli/deploy-files.d.ts +112 -0
- package/dist/cli/deploy-files.js +778 -0
- package/dist/cli/deploy.d.ts +10 -0
- package/dist/cli/deploy.js +430 -0
- package/dist/cli/emails.d.ts +29 -0
- package/dist/cli/emails.js +122 -0
- package/dist/cli/framework.d.ts +135 -0
- package/dist/cli/framework.js +459 -0
- package/dist/cli/functions.d.ts +59 -0
- package/dist/cli/functions.js +426 -0
- package/dist/cli/hosting.d.ts +4 -0
- package/dist/cli/hosting.js +532 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +9 -0
- package/dist/cli/payments.d.ts +35 -0
- package/dist/cli/payments.js +176 -0
- package/dist/cli/push.d.ts +36 -0
- package/dist/cli/push.js +172 -0
- package/dist/cli/run.d.ts +65 -0
- package/dist/cli/run.js +850 -0
- package/dist/cli/social.d.ts +52 -0
- package/dist/cli/social.js +532 -0
- package/dist/cli/tokens.d.ts +28 -0
- package/dist/cli/tokens.js +114 -0
- package/dist/cli/ui.d.ts +38 -0
- package/dist/cli/ui.js +140 -0
- package/dist/client.d.ts +94 -0
- package/dist/client.js +156 -0
- package/dist/db/query.d.ts +162 -0
- package/dist/db/query.js +336 -0
- package/dist/db/types.d.ts +93 -0
- package/dist/db/types.js +4 -0
- package/dist/forms/client.d.ts +65 -0
- package/dist/forms/client.js +106 -0
- package/dist/functions/client.d.ts +37 -0
- package/dist/functions/client.js +76 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +16 -0
- package/dist/lib/dashboard-paths.d.ts +4 -0
- package/dist/lib/dashboard-paths.js +8 -0
- package/dist/lib/env.d.ts +9 -0
- package/dist/lib/env.js +55 -0
- package/dist/lib/errors.d.ts +22 -0
- package/dist/lib/errors.js +90 -0
- package/dist/lib/hosted.d.ts +2 -0
- package/dist/lib/hosted.js +7 -0
- package/dist/lib/http.d.ts +59 -0
- package/dist/lib/http.js +172 -0
- package/dist/lib/jwt.d.ts +1 -0
- package/dist/lib/jwt.js +31 -0
- package/dist/lib/locale.d.ts +12 -0
- package/dist/lib/locale.js +51 -0
- package/dist/lib/pkce.d.ts +20 -0
- package/dist/lib/pkce.js +124 -0
- package/dist/lib/storage-adapter.d.ts +15 -0
- package/dist/lib/storage-adapter.js +91 -0
- package/dist/lib/types.d.ts +339 -0
- package/dist/lib/types.js +1 -0
- package/dist/newsletter/client.d.ts +140 -0
- package/dist/newsletter/client.js +85 -0
- package/dist/payments/client.d.ts +352 -0
- package/dist/payments/client.js +323 -0
- package/dist/push/client.d.ts +110 -0
- package/dist/push/client.js +179 -0
- package/dist/realtime/client.d.ts +131 -0
- package/dist/realtime/client.js +513 -0
- package/dist/server/analytics.d.ts +176 -0
- package/dist/server/analytics.js +49 -0
- package/dist/server/cache.d.ts +74 -0
- package/dist/server/cache.js +154 -0
- package/dist/server/emails.d.ts +142 -0
- package/dist/server/emails.js +123 -0
- package/dist/server/events.d.ts +357 -0
- package/dist/server/events.js +219 -0
- package/dist/server/functions.d.ts +40 -0
- package/dist/server/functions.js +1 -0
- package/dist/server/http-utils.d.ts +10 -0
- package/dist/server/http-utils.js +115 -0
- package/dist/server/index.d.ts +195 -0
- package/dist/server/index.js +314 -0
- package/dist/server/payments.d.ts +218 -0
- package/dist/server/payments.js +150 -0
- package/dist/server/push.d.ts +85 -0
- package/dist/server/push.js +37 -0
- package/dist/server/usage.d.ts +43 -0
- package/dist/server/usage.js +8 -0
- package/dist/storage/client.d.ts +143 -0
- package/dist/storage/client.js +224 -0
- package/package.json +52 -4
- package/skill/usebecky/SKILL.md +339 -0
- package/skill/usebecky/references/access-rules.md +307 -0
- package/skill/usebecky/references/addons.md +68 -0
- package/skill/usebecky/references/analytics.md +166 -0
- package/skill/usebecky/references/android.md +434 -0
- package/skill/usebecky/references/auth.md +379 -0
- package/skill/usebecky/references/cache.md +51 -0
- package/skill/usebecky/references/components.md +127 -0
- package/skill/usebecky/references/emails.md +105 -0
- package/skill/usebecky/references/expo.md +147 -0
- package/skill/usebecky/references/functions.md +99 -0
- package/skill/usebecky/references/hosting.md +119 -0
- package/skill/usebecky/references/http-api.md +233 -0
- package/skill/usebecky/references/ios.md +355 -0
- package/skill/usebecky/references/payments.md +493 -0
- package/skill/usebecky/references/realtime.md +143 -0
- package/skill/usebecky/references/scheduled-jobs.md +82 -0
- package/skill/usebecky/references/sdk.md +838 -0
- package/skill/usebecky/references/storage.md +210 -0
- package/skill/usebecky/references/templates.md +191 -0
- package/skill/usebecky/references/wordpress.md +87 -0
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
// `npx usebecky hosting <list|deploys|rollback|promote|env|domains|logs|github>`: everything about your sites after
|
|
2
|
+
// `npx usebecky deploy`. Deploys, rollback and promote also work with a deploy token (USEBECKY_DEPLOY_TOKEN); the
|
|
3
|
+
// rest needs `npx usebecky login`. Guide: docs/guide/guides/hosting.md.
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { DOMAIN_STATUS, dnsToken, KIND_LABEL, MB, explain, hostingPath, limitOf, noProject, parseHostingArgs, productionUrlFrom, projectRef, readLink, reportDns, sitePath, siteStatus, tokenFor, when, } from "./deploy-api.js";
|
|
7
|
+
import { DEPLOY_TOKEN_SECRET, WORKFLOW_PATH, githubWorkflow } from "./deploy-ci.js";
|
|
8
|
+
import { detectPackageManager } from "./deploy-detect.js";
|
|
9
|
+
import { c } from "./ui.js";
|
|
10
|
+
export const HOSTING_HELP = `
|
|
11
|
+
${c.bold('npx usebecky hosting')}: your sites on usebecky Hosting (Business plan). Deploy with ${c.cyan('npx usebecky deploy')}.
|
|
12
|
+
|
|
13
|
+
${c.cyan('hosting list')} Your sites: kind, address, status
|
|
14
|
+
${c.cyan('hosting deploys')} A site's deploys, newest first (preview addresses, which one is live)
|
|
15
|
+
${c.cyan('hosting rollback [deploy]')} Put an earlier deploy back live (no deploy given: the one before the current one)
|
|
16
|
+
${c.cyan('hosting promote <deploy>')} Put a deploy (a preview) live
|
|
17
|
+
${c.cyan('hosting env list')} Environment variables of a Node.js app (names only)
|
|
18
|
+
${c.cyan('hosting env set A=1 B=2')} Set variables (your app reads process.env.A)
|
|
19
|
+
${c.cyan('hosting env unset A')} Remove a variable
|
|
20
|
+
${c.cyan('hosting domains list')} The site's own domains and their status
|
|
21
|
+
${c.cyan('hosting domains add <domain>')} Connect a domain you own (shows the DNS records to add; --dns cloudflare
|
|
22
|
+
writes them there with CLOUDFLARE_API_TOKEN)
|
|
23
|
+
${c.cyan('hosting domains check <domain>')} Check the DNS records now
|
|
24
|
+
${c.cyan('hosting domains remove <domain>')} Disconnect a domain
|
|
25
|
+
${c.cyan('hosting logs [--follow]')} What your Node.js app printed (--follow keeps showing new lines)
|
|
26
|
+
${c.cyan('hosting github')} Write a GitHub Actions workflow that deploys on every push
|
|
27
|
+
|
|
28
|
+
--site <name> Which site (default: usebecky/hosting.json, or your only site)
|
|
29
|
+
--project <ref> Which project
|
|
30
|
+
`;
|
|
31
|
+
const ENV_NAME_RE = /^[A-Z_][A-Z0-9_]{0,63}$/;
|
|
32
|
+
const BUILT_IN_ENV = ['USEBECKY_URL', 'USEBECKY_PUBLISHABLE_KEY', 'USEBECKY_SECRET_KEY', 'PORT'];
|
|
33
|
+
const defaultSleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
34
|
+
export async function hostingCommand(ctx) {
|
|
35
|
+
const { ui } = ctx;
|
|
36
|
+
const a = parseHostingArgs(ctx.args);
|
|
37
|
+
const sub = a.positional[0];
|
|
38
|
+
if (!sub || sub === 'help') {
|
|
39
|
+
ui.write(HOSTING_HELP);
|
|
40
|
+
return sub ? 0 : 1;
|
|
41
|
+
}
|
|
42
|
+
const known = ['list', 'ls', 'sites', 'deploys', 'rollback', 'promote', 'env', 'domains', 'domain', 'logs', 'github'];
|
|
43
|
+
if (!known.includes(sub)) {
|
|
44
|
+
ui.error(`Unknown command "hosting ${sub}".`);
|
|
45
|
+
ui.write(HOSTING_HELP);
|
|
46
|
+
return 1;
|
|
47
|
+
}
|
|
48
|
+
if (a.unknown.length) {
|
|
49
|
+
ui.error(`Unknown option ${a.unknown.join(', ')}.`);
|
|
50
|
+
ui.write(HOSTING_HELP);
|
|
51
|
+
return 1;
|
|
52
|
+
}
|
|
53
|
+
if (sub === 'github')
|
|
54
|
+
return github(ctx, a);
|
|
55
|
+
const link = readLink(ctx.cwd);
|
|
56
|
+
const ref = projectRef(ctx, link);
|
|
57
|
+
if (!ref)
|
|
58
|
+
return noProject(ctx);
|
|
59
|
+
// A deploy token may list and promote deploys; everything else needs the signed-in session.
|
|
60
|
+
const auth = await tokenFor(ctx, ['deploys', 'rollback', 'promote'].includes(sub));
|
|
61
|
+
if (!auth)
|
|
62
|
+
return 1;
|
|
63
|
+
if (sub === 'list' || sub === 'ls' || sub === 'sites')
|
|
64
|
+
return list(ctx, ref, auth);
|
|
65
|
+
const name = await whichSite(ctx, a, link?.site, ref, auth);
|
|
66
|
+
if (!name)
|
|
67
|
+
return 1;
|
|
68
|
+
switch (sub) {
|
|
69
|
+
case 'deploys':
|
|
70
|
+
return deploys(ctx, ref, name, auth);
|
|
71
|
+
case 'rollback':
|
|
72
|
+
case 'promote':
|
|
73
|
+
return promote(ctx, ref, name, auth, a.positional[1], sub);
|
|
74
|
+
case 'env':
|
|
75
|
+
return env(ctx, ref, name, auth, a.positional.slice(1));
|
|
76
|
+
case 'domains':
|
|
77
|
+
case 'domain':
|
|
78
|
+
return domains(ctx, ref, name, auth, a.positional.slice(1), a.dns);
|
|
79
|
+
default:
|
|
80
|
+
return logs(ctx, ref, name, auth, a.follow);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** --site, the folder's usebecky/hosting.json, or the project's only site (asks when there are several). */
|
|
84
|
+
async function whichSite(ctx, a, linked, ref, auth) {
|
|
85
|
+
if (a.site)
|
|
86
|
+
return a.site;
|
|
87
|
+
if (linked)
|
|
88
|
+
return linked;
|
|
89
|
+
if (ctx.deployToken && auth === ctx.deployToken) {
|
|
90
|
+
ctx.ui.error('Which site? Pass --site <name>.');
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
const o = await ctx.api('GET', hostingPath(ref), undefined, auth);
|
|
94
|
+
if (o.error) {
|
|
95
|
+
await explain(ctx, o.error, ref);
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
const sites = o.data?.sites ?? [];
|
|
99
|
+
if (!sites.length) {
|
|
100
|
+
ctx.ui.error('This project has no sites yet.');
|
|
101
|
+
ctx.ui.write(` ${c.dim('Put your app online: npx usebecky deploy')}`);
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
if (sites.length === 1)
|
|
105
|
+
return sites[0].name;
|
|
106
|
+
if (ctx.yes) {
|
|
107
|
+
ctx.ui.error(`Which site? Pass --site <name> (${sites.map((s) => s.name).join(', ')}).`);
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
return ctx.ui.choose('Which site?', sites.map((s) => ({ label: `${s.name} ${c.dim(s.url)}`, value: s.name })));
|
|
111
|
+
}
|
|
112
|
+
// ---- list / deploys / rollback / promote --------------------------------------------------------------------
|
|
113
|
+
async function list(ctx, ref, auth) {
|
|
114
|
+
const { ui } = ctx;
|
|
115
|
+
const r = await ctx.api('GET', hostingPath(ref), undefined, auth);
|
|
116
|
+
if (r.error || !r.data)
|
|
117
|
+
return explain(ctx, r.error ?? { code: 'unknown', message: 'Could not load your sites.', status: 500 }, ref);
|
|
118
|
+
const o = r.data;
|
|
119
|
+
if (o.available_on_plan === false)
|
|
120
|
+
return explain(ctx, { code: 'plan_required', message: '', status: 402, details: { upgrade_url: o.upgrade_url } }, ref);
|
|
121
|
+
if (!o.sites.length) {
|
|
122
|
+
ui.info('No sites yet. Put your app online: npx usebecky deploy');
|
|
123
|
+
return 0;
|
|
124
|
+
}
|
|
125
|
+
const max = limitOf(o, 'sites');
|
|
126
|
+
ui.write(c.bold(`Sites in project ${ref}${max ? ` (${o.sites.length} of ${max})` : ''}:`));
|
|
127
|
+
const w = Math.max(...o.sites.map((s) => s.name.length));
|
|
128
|
+
for (const s of o.sites) {
|
|
129
|
+
ui.write(` ${c.bold(s.name.padEnd(w))} ${KIND_LABEL[s.kind].padEnd(11)} ${siteStatus(s)}`);
|
|
130
|
+
ui.write(` ${' '.repeat(w)} ${c.cyan(s.url)}${s.domains?.length ? c.dim(` + ${s.domains.map((d) => d.host).join(', ')}`) : ''}`);
|
|
131
|
+
}
|
|
132
|
+
const u = o.usage ?? {};
|
|
133
|
+
const bw = u.bandwidth_bytes;
|
|
134
|
+
const bwMax = limitOf(o, 'bandwidth_bytes');
|
|
135
|
+
const dep = u.deploys_today;
|
|
136
|
+
const depMax = limitOf(o, 'deploys_per_day');
|
|
137
|
+
const parts = [typeof bw === 'number' ? `Bandwidth this month: ${MB(bw)}${bwMax ? ` of ${MB(bwMax)}` : ''}` : null, typeof dep === 'number' ? `Deploys today: ${dep}${depMax ? ` of ${depMax}` : ''}` : null].filter(Boolean);
|
|
138
|
+
if (parts.length)
|
|
139
|
+
ui.write(c.dim(parts.join(' · ')));
|
|
140
|
+
return 0;
|
|
141
|
+
}
|
|
142
|
+
/** One page of a site's deploys, newest first: { deploys, next_cursor } (an older server answers with a plain array, the
|
|
143
|
+
* whole list). `query`: limit, cursor, q (a deploy's short id or the start of its id). Null after explaining an error. */
|
|
144
|
+
async function deployPage(ctx, ref, name, auth, query = {}) {
|
|
145
|
+
const qs = new URLSearchParams(query).toString();
|
|
146
|
+
const r = await ctx.api('GET', sitePath(ref, name, `/deploys${qs ? `?${qs}` : ''}`), undefined, auth);
|
|
147
|
+
if (r.error) {
|
|
148
|
+
await explain(ctx, r.error, ref, name);
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
if (Array.isArray(r.data))
|
|
152
|
+
return { deploys: r.data, next: null };
|
|
153
|
+
return { deploys: r.data?.deploys ?? [], next: r.data?.next_cursor ?? null };
|
|
154
|
+
}
|
|
155
|
+
/** Newest first, page after page, until `enough` says so or MAX_DEPLOY_PAGES pages were read. */
|
|
156
|
+
const MAX_DEPLOY_PAGES = 10;
|
|
157
|
+
async function loadDeploys(ctx, ref, name, auth, enough = () => true) {
|
|
158
|
+
const list = [];
|
|
159
|
+
let cursor = null;
|
|
160
|
+
for (let i = 0; i < MAX_DEPLOY_PAGES; i++) {
|
|
161
|
+
const page = await deployPage(ctx, ref, name, auth, { limit: '50', ...(cursor ? { cursor } : {}) });
|
|
162
|
+
if (!page)
|
|
163
|
+
return null;
|
|
164
|
+
list.push(...page.deploys);
|
|
165
|
+
cursor = page.next;
|
|
166
|
+
if (!cursor || enough(list))
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
return list;
|
|
170
|
+
}
|
|
171
|
+
const SHOWN = 30;
|
|
172
|
+
async function deploys(ctx, ref, name, auth) {
|
|
173
|
+
const { ui } = ctx;
|
|
174
|
+
const page = await deployPage(ctx, ref, name, auth, { limit: String(SHOWN) });
|
|
175
|
+
if (!page)
|
|
176
|
+
return 1;
|
|
177
|
+
const list = page.deploys;
|
|
178
|
+
if (!list.length) {
|
|
179
|
+
ui.info(`${name} has no deploys yet. Deploy: npx usebecky deploy --site ${name}`);
|
|
180
|
+
return 0;
|
|
181
|
+
}
|
|
182
|
+
ui.write(c.bold(`Deploys of ${name}, newest first:`));
|
|
183
|
+
for (const d of list.slice(0, SHOWN)) {
|
|
184
|
+
const status = d.production ? c.green('live ') : d.status === 'ready' ? 'preview ' : d.status === 'failed' ? c.red('failed ') : c.yellow('uploading ');
|
|
185
|
+
ui.write(` ${c.bold(d.short_id ?? d.id.slice(0, 7))} ${status} ${c.dim(`${when(d.created_at)}${d.created_by ? `, ${d.created_by}` : ''}, ${d.file_count} file${d.file_count === 1 ? '' : 's'}, ${MB(d.bytes)}`)}`);
|
|
186
|
+
if (d.preview_url)
|
|
187
|
+
ui.write(` ${c.cyan(d.preview_url)}`);
|
|
188
|
+
}
|
|
189
|
+
if (list.length > SHOWN)
|
|
190
|
+
ui.write(c.dim(` … and ${list.length - SHOWN} older`));
|
|
191
|
+
else if (page.next)
|
|
192
|
+
ui.write(c.dim(` … and older ones (all of them: Dashboard → Hosting → ${name} → Deploys)`));
|
|
193
|
+
return 0;
|
|
194
|
+
}
|
|
195
|
+
/** The deploy `wanted` names (short id, id, or the start of the id): among the newest, else the server searches. */
|
|
196
|
+
async function findDeploy(ctx, ref, name, auth, list, wanted) {
|
|
197
|
+
const match = (d) => d.id === wanted || d.short_id === wanted || (wanted.length >= 4 && d.id.startsWith(wanted));
|
|
198
|
+
const found = list.find(match);
|
|
199
|
+
if (found)
|
|
200
|
+
return found;
|
|
201
|
+
const page = await deployPage(ctx, ref, name, auth, { q: wanted, limit: '20' });
|
|
202
|
+
if (!page)
|
|
203
|
+
return null;
|
|
204
|
+
return page.deploys.find(match);
|
|
205
|
+
}
|
|
206
|
+
async function promote(ctx, ref, name, auth, wanted, sub) {
|
|
207
|
+
const { ui } = ctx;
|
|
208
|
+
if (sub === 'promote' && !wanted) {
|
|
209
|
+
ui.error('Which deploy? Example: npx usebecky hosting promote k3x9q2m');
|
|
210
|
+
ui.write(` ${c.dim('See them with npx usebecky hosting deploys')}`);
|
|
211
|
+
return 1;
|
|
212
|
+
}
|
|
213
|
+
// Rollback: read on until the live deploy and a ready one older than it are both in the list.
|
|
214
|
+
const list = await loadDeploys(ctx, ref, name, auth, (l) => {
|
|
215
|
+
const at = l.findIndex((d) => d.production);
|
|
216
|
+
return !!wanted || (at >= 0 && l.slice(at + 1).some((d) => d.status === 'ready'));
|
|
217
|
+
});
|
|
218
|
+
if (!list)
|
|
219
|
+
return 1;
|
|
220
|
+
const current = list.find((d) => d.production);
|
|
221
|
+
let target;
|
|
222
|
+
if (wanted) {
|
|
223
|
+
const found = await findDeploy(ctx, ref, name, auth, list, wanted);
|
|
224
|
+
if (found === null)
|
|
225
|
+
return 1;
|
|
226
|
+
target = found;
|
|
227
|
+
if (!target) {
|
|
228
|
+
ui.error(`${name} has no deploy “${wanted}”.`);
|
|
229
|
+
ui.write(` ${c.dim('See them with npx usebecky hosting deploys')}`);
|
|
230
|
+
return 1;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
// Rollback without an id: the newest ready deploy older than the live one.
|
|
235
|
+
const at = current ? list.indexOf(current) : -1;
|
|
236
|
+
target = list.slice(at + 1).find((d) => d.status === 'ready');
|
|
237
|
+
if (!target) {
|
|
238
|
+
ui.error(current ? 'There is no earlier deploy to go back to.' : `${name} has nothing live yet, so there's nothing to roll back.`);
|
|
239
|
+
return 1;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (target.production) {
|
|
243
|
+
ui.info(`${target.short_id} is already the live deploy of ${name}.`);
|
|
244
|
+
return 0;
|
|
245
|
+
}
|
|
246
|
+
if (target.status !== 'ready') {
|
|
247
|
+
ui.error(`${target.short_id} can't go live: its upload ${target.status === 'failed' ? 'failed' : 'never finished'}.`);
|
|
248
|
+
return 1;
|
|
249
|
+
}
|
|
250
|
+
const question = `Put ${c.bold(target.short_id)} (from ${when(target.created_at)}) live on ${name}?`;
|
|
251
|
+
if (!ctx.yes && !(await ui.confirm(question, true))) {
|
|
252
|
+
ui.info('Nothing changed.');
|
|
253
|
+
return 0;
|
|
254
|
+
}
|
|
255
|
+
const r = await ctx.api('POST', sitePath(ref, name, `/deploys/${encodeURIComponent(target.id)}/promote`), {}, auth);
|
|
256
|
+
if (r.error)
|
|
257
|
+
return explain(ctx, r.error, ref, name);
|
|
258
|
+
const url = productionUrlFrom(target.preview_url, name);
|
|
259
|
+
ui.success(`${target.short_id} is live${url ? ` on ${c.cyan(url)}` : ''}.${current ? c.dim(` Undo: npx usebecky hosting promote ${current.short_id}`) : ''}`);
|
|
260
|
+
return 0;
|
|
261
|
+
}
|
|
262
|
+
// ---- env ------------------------------------------------------------------------------------------------------
|
|
263
|
+
async function env(ctx, ref, name, auth, args) {
|
|
264
|
+
const { ui } = ctx;
|
|
265
|
+
const action = args[0] ?? 'list';
|
|
266
|
+
if (action === 'list' || action === 'ls') {
|
|
267
|
+
const r = await ctx.api('GET', sitePath(ref, name), undefined, auth);
|
|
268
|
+
if (r.error || !r.data)
|
|
269
|
+
return explain(ctx, r.error ?? { code: 'unknown', message: 'Could not load the site.', status: 500 }, ref, name);
|
|
270
|
+
const s = r.data;
|
|
271
|
+
if (s.kind === 'static')
|
|
272
|
+
staticNote(ctx);
|
|
273
|
+
ui.write(c.bold(`Environment variables of ${name}:`));
|
|
274
|
+
if (!s.env_names.length)
|
|
275
|
+
ui.write(` ${c.dim('None yet. Add one: npx usebecky hosting env set STRIPE_SECRET_KEY=sk_live_...')}`);
|
|
276
|
+
for (const n of s.env_names)
|
|
277
|
+
ui.write(` ${n} ${c.dim('(value hidden)')}`);
|
|
278
|
+
ui.write(c.dim(` Added by usebecky: USEBECKY_URL, USEBECKY_PUBLISHABLE_KEY, PORT${s.expose_secret_key ? ', USEBECKY_SECRET_KEY' : ''}`));
|
|
279
|
+
if (!s.expose_secret_key && s.kind === 'node')
|
|
280
|
+
ui.write(c.dim(' The secret key is off. Turn it on in Dashboard → Hosting → your site → Environment if your server needs it.'));
|
|
281
|
+
return 0;
|
|
282
|
+
}
|
|
283
|
+
if (action === 'set') {
|
|
284
|
+
const pairs = args.slice(1);
|
|
285
|
+
if (!pairs.length) {
|
|
286
|
+
ui.error('Pass NAME=value pairs. Example: npx usebecky hosting env set STRIPE_SECRET_KEY=sk_live_...');
|
|
287
|
+
return 1;
|
|
288
|
+
}
|
|
289
|
+
const values = {};
|
|
290
|
+
for (const p of pairs) {
|
|
291
|
+
const i = p.indexOf('=');
|
|
292
|
+
const key = i > 0 ? p.slice(0, i) : p;
|
|
293
|
+
const why = envNameProblem(key);
|
|
294
|
+
if (i <= 0 || why) {
|
|
295
|
+
ui.error(i <= 0 ? `“${key.slice(0, 64)}” isn't a NAME=value pair.` : why);
|
|
296
|
+
return 1;
|
|
297
|
+
}
|
|
298
|
+
values[key] = p.slice(i + 1);
|
|
299
|
+
}
|
|
300
|
+
const r = await ctx.api('PATCH', sitePath(ref, name), { env: values }, auth);
|
|
301
|
+
if (r.error)
|
|
302
|
+
return explain(ctx, r.error, ref, name);
|
|
303
|
+
const names = Object.keys(values);
|
|
304
|
+
ui.success(`Saved ${names.map((n) => c.bold(n)).join(', ')} for ${name}. Your app reads ${names.length === 1 ? 'it' : 'them'} as process.env.${names[0]}${names.length > 1 ? ', …' : ''}`);
|
|
305
|
+
if (r.data?.kind === 'static')
|
|
306
|
+
staticNote(ctx);
|
|
307
|
+
else
|
|
308
|
+
ui.write(` ${c.dim('Your app restarts with them on its next visit (a few seconds). No new deploy needed.')}`);
|
|
309
|
+
return 0;
|
|
310
|
+
}
|
|
311
|
+
if (action === 'unset' || action === 'delete' || action === 'rm') {
|
|
312
|
+
const names = args.slice(1);
|
|
313
|
+
if (!names.length) {
|
|
314
|
+
ui.error('Which variable? Example: npx usebecky hosting env unset STRIPE_SECRET_KEY');
|
|
315
|
+
return 1;
|
|
316
|
+
}
|
|
317
|
+
const r = await ctx.api('PATCH', sitePath(ref, name), { env: Object.fromEntries(names.map((n) => [n, null])) }, auth);
|
|
318
|
+
if (r.error)
|
|
319
|
+
return explain(ctx, r.error, ref, name);
|
|
320
|
+
ui.success(`Removed ${names.map((n) => c.bold(n)).join(', ')} from ${name}.`);
|
|
321
|
+
return 0;
|
|
322
|
+
}
|
|
323
|
+
ui.error(`Unknown command "hosting env ${action}".`);
|
|
324
|
+
ui.write(HOSTING_HELP);
|
|
325
|
+
return 1;
|
|
326
|
+
}
|
|
327
|
+
export function envNameProblem(n) {
|
|
328
|
+
if (!ENV_NAME_RE.test(n))
|
|
329
|
+
return `“${n.slice(0, 64)}”: use CAPITAL letters, numbers and underscores, starting with a letter. Like STRIPE_SECRET_KEY.`;
|
|
330
|
+
if (n === 'USEBECKY_SECRET_KEY')
|
|
331
|
+
return 'Don’t set USEBECKY_SECRET_KEY yourself: switch on “Give this site the secret key” in Dashboard → Hosting → your site → Environment.';
|
|
332
|
+
if (BUILT_IN_ENV.includes(n))
|
|
333
|
+
return `usebecky sets ${n} for you.`;
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
function staticNote(ctx) {
|
|
337
|
+
ctx.ui.warn('This is a static site: it has no server that reads these. Values a website needs go in when it is built (like VITE_… in .env).');
|
|
338
|
+
}
|
|
339
|
+
// ---- domains --------------------------------------------------------------------------------------------------
|
|
340
|
+
function cleanHost(input) {
|
|
341
|
+
return input.trim().toLowerCase().replace(/^[a-z]+:\/\//, '').replace(/[/?#].*$/, '').replace(/\.$/, '');
|
|
342
|
+
}
|
|
343
|
+
function printDomain(ctx, d) {
|
|
344
|
+
const { ui } = ctx;
|
|
345
|
+
const tone = d.status === 'active' ? c.green : d.status === 'error' ? c.red : c.yellow;
|
|
346
|
+
ui.write(` ${c.bold(d.host)} ${tone(DOMAIN_STATUS[d.status] ?? d.status)}`);
|
|
347
|
+
if (d.status === 'error' && d.error)
|
|
348
|
+
ui.write(` ${c.red(d.error)}`);
|
|
349
|
+
if (d.status !== 'active' && d.records?.length) {
|
|
350
|
+
ui.write(` ${c.dim('Add these records where you manage your domain (your registrar or DNS provider):')}`);
|
|
351
|
+
const tw = Math.max(4, ...d.records.map((r) => r.type.length));
|
|
352
|
+
const nw = Math.max(4, ...d.records.map((r) => r.name.length));
|
|
353
|
+
ui.write(` ${c.dim('Type'.padEnd(tw))} ${c.dim('Name'.padEnd(nw))} ${c.dim('Value')}`);
|
|
354
|
+
for (const r of d.records)
|
|
355
|
+
ui.write(` ${r.type.padEnd(tw)} ${r.name.padEnd(nw)} ${r.value}`);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
async function domains(ctx, ref, name, auth, args, dns) {
|
|
359
|
+
const { ui } = ctx;
|
|
360
|
+
const action = args[0] ?? 'list';
|
|
361
|
+
if (action === 'list' || action === 'ls') {
|
|
362
|
+
const r = await ctx.api('GET', sitePath(ref, name, '/domains'), undefined, auth);
|
|
363
|
+
if (r.error)
|
|
364
|
+
return explain(ctx, r.error, ref, name);
|
|
365
|
+
const list = Array.isArray(r.data) ? r.data : (r.data?.domains ?? []);
|
|
366
|
+
if (!list.length) {
|
|
367
|
+
ui.info(`${name} has no domains of its own yet. Add one: npx usebecky hosting domains add www.yourapp.com`);
|
|
368
|
+
return 0;
|
|
369
|
+
}
|
|
370
|
+
ui.write(c.bold(`Domains of ${name}:`));
|
|
371
|
+
for (const d of list)
|
|
372
|
+
printDomain(ctx, d);
|
|
373
|
+
return 0;
|
|
374
|
+
}
|
|
375
|
+
const host = cleanHost(args[1] ?? '');
|
|
376
|
+
if (!['add', 'check', 'remove', 'rm', 'delete'].includes(action)) {
|
|
377
|
+
ui.error(`Unknown command "hosting domains ${action}".`);
|
|
378
|
+
ui.write(HOSTING_HELP);
|
|
379
|
+
return 1;
|
|
380
|
+
}
|
|
381
|
+
if (!host || !/^[a-z0-9.-]+$/.test(host) || !host.includes('.') || host.startsWith('.') || host.includes('..')) {
|
|
382
|
+
ui.error(host ? `“${host}” doesn't look like a domain. Type it like www.yourapp.com.` : `Which domain? Example: npx usebecky hosting domains ${action} www.yourapp.com`);
|
|
383
|
+
return 1;
|
|
384
|
+
}
|
|
385
|
+
if (action === 'add') {
|
|
386
|
+
if (/(^|\.)usebecky\.(site|app|com|dev)$/.test(host)) {
|
|
387
|
+
ui.error('Your site already has its usebecky address. Add a domain you own.');
|
|
388
|
+
return 1;
|
|
389
|
+
}
|
|
390
|
+
const provider = dnsToken(dns, ctx.env);
|
|
391
|
+
if (provider.problem) {
|
|
392
|
+
ui.error(provider.problem);
|
|
393
|
+
return 1;
|
|
394
|
+
}
|
|
395
|
+
const r = await ctx.api('POST', sitePath(ref, name, '/domains'), { host }, auth);
|
|
396
|
+
if (r.error || !r.data)
|
|
397
|
+
return explain(ctx, r.error ?? { code: 'unknown', message: 'Could not add the domain.', status: 500 }, ref, name);
|
|
398
|
+
const d = 'domain' in r.data ? r.data.domain : r.data;
|
|
399
|
+
ui.success(`Added ${c.bold(d.host)} to ${name}.`);
|
|
400
|
+
if (provider.token) {
|
|
401
|
+
// usebecky writes the records at Cloudflare (the token is used for this request only) and checks them right away.
|
|
402
|
+
const w = await ctx.api('POST', sitePath(ref, name, `/domains/${encodeURIComponent(d.host)}/cloudflare`), { token: provider.token }, auth);
|
|
403
|
+
if (w.error || !w.data)
|
|
404
|
+
return explain(ctx, w.error ?? { code: 'unknown', message: 'Could not write the DNS records at Cloudflare.', status: 500 }, ref, name);
|
|
405
|
+
const ok = reportDns(ui, w.data);
|
|
406
|
+
printDomain(ctx, w.data.domain ?? d);
|
|
407
|
+
return ok ? 0 : 1;
|
|
408
|
+
}
|
|
409
|
+
printDomain(ctx, d);
|
|
410
|
+
ui.write(` ${c.dim(`Add the records before or after the domain: usebecky sees new ones within about a minute, then gets the certificate by itself. Check now: npx usebecky hosting domains check ${d.host}`)}`);
|
|
411
|
+
return 0;
|
|
412
|
+
}
|
|
413
|
+
if (action === 'check') {
|
|
414
|
+
const r = await ctx.api('POST', sitePath(ref, name, `/domains/${encodeURIComponent(host)}/check`), {}, auth);
|
|
415
|
+
if (r.error || !r.data)
|
|
416
|
+
return explain(ctx, r.error ?? { code: 'unknown', message: 'Could not check the domain.', status: 500 }, ref, name);
|
|
417
|
+
printDomain(ctx, 'domain' in r.data ? r.data.domain : r.data);
|
|
418
|
+
return 0;
|
|
419
|
+
}
|
|
420
|
+
if (!ctx.yes && !(await ui.confirm(`Disconnect ${c.bold(host)} from ${name}? It stops showing your site right away.`, false))) {
|
|
421
|
+
ui.info('Nothing changed.');
|
|
422
|
+
return 0;
|
|
423
|
+
}
|
|
424
|
+
const r = await ctx.api('DELETE', sitePath(ref, name, `/domains/${encodeURIComponent(host)}`), undefined, auth);
|
|
425
|
+
if (r.error)
|
|
426
|
+
return explain(ctx, r.error, ref, name);
|
|
427
|
+
ui.success(`Removed ${c.bold(host)}. You can delete its DNS records now.`);
|
|
428
|
+
return 0;
|
|
429
|
+
}
|
|
430
|
+
async function logs(ctx, ref, name, auth, follow) {
|
|
431
|
+
const { ui } = ctx;
|
|
432
|
+
const sleep = ctx.sleep ?? defaultSleep;
|
|
433
|
+
const site = await ctx.api('GET', sitePath(ref, name), undefined, auth);
|
|
434
|
+
if (site.error)
|
|
435
|
+
return explain(ctx, site.error, ref, name);
|
|
436
|
+
if (site.data?.kind === 'static') {
|
|
437
|
+
ui.info(`${name} is a static site: there is no server, so no logs. Visits show in Dashboard → Analytics.`);
|
|
438
|
+
return 0;
|
|
439
|
+
}
|
|
440
|
+
let since;
|
|
441
|
+
const seenAtSince = new Set();
|
|
442
|
+
const fetchNew = async () => {
|
|
443
|
+
const r = await ctx.api('GET', sitePath(ref, name, `/logs${since ? `?since=${encodeURIComponent(since)}` : ''}`), undefined, auth);
|
|
444
|
+
if (r.error) {
|
|
445
|
+
await explain(ctx, r.error, ref, name);
|
|
446
|
+
return null;
|
|
447
|
+
}
|
|
448
|
+
// Only what's new: nothing older than the last line shown, nor a line at that same moment shown already.
|
|
449
|
+
const lines = (r.data?.lines ?? []).filter((l) => !since || l.at > since || (l.at === since && !seenAtSince.has(`${l.stream}\u0000${l.text}`)));
|
|
450
|
+
for (const l of lines) {
|
|
451
|
+
if (l.at !== since) {
|
|
452
|
+
since = l.at;
|
|
453
|
+
seenAtSince.clear();
|
|
454
|
+
}
|
|
455
|
+
seenAtSince.add(`${l.stream}\u0000${l.text}`);
|
|
456
|
+
}
|
|
457
|
+
return lines;
|
|
458
|
+
};
|
|
459
|
+
const print = (lines) => {
|
|
460
|
+
for (const l of lines)
|
|
461
|
+
ui.write(`${c.dim((l.at ?? '').replace('T', ' ').slice(11, 19))} ${l.stream === 'stderr' ? c.red(l.text) : l.text}`);
|
|
462
|
+
};
|
|
463
|
+
const first = await fetchNew();
|
|
464
|
+
if (!first)
|
|
465
|
+
return 1;
|
|
466
|
+
if (!first.length && !follow)
|
|
467
|
+
ui.info(`${name} hasn't printed anything lately. It sleeps after 15 minutes without visitors; open its address to wake it.`);
|
|
468
|
+
print(first);
|
|
469
|
+
if (!follow)
|
|
470
|
+
return 0;
|
|
471
|
+
ui.write(c.dim(`Showing new lines as they come (Ctrl+C to stop)…`));
|
|
472
|
+
for (let i = 0; ctx.maxPolls === undefined || i < ctx.maxPolls; i++) {
|
|
473
|
+
await sleep(2000);
|
|
474
|
+
const next = await fetchNew();
|
|
475
|
+
if (!next)
|
|
476
|
+
return 1;
|
|
477
|
+
print(next);
|
|
478
|
+
}
|
|
479
|
+
return 0;
|
|
480
|
+
}
|
|
481
|
+
// ---- github ---------------------------------------------------------------------------------------------------
|
|
482
|
+
/** Write .github/workflows/usebecky-deploy.yml for this folder's site. No network: it only needs the project and the site. */
|
|
483
|
+
async function github(ctx, a) {
|
|
484
|
+
const { ui } = ctx;
|
|
485
|
+
const link = readLink(ctx.cwd);
|
|
486
|
+
const ref = projectRef(ctx, link);
|
|
487
|
+
const site = a.site ?? link?.site;
|
|
488
|
+
if (!ref || !site) {
|
|
489
|
+
ui.error('Deploy once from your computer first, so this folder knows its site.');
|
|
490
|
+
ui.write(` ${c.dim('Run npx usebecky deploy, then npx usebecky hosting github. (Or pass --project <ref> --site <name>.)')}`);
|
|
491
|
+
return 1;
|
|
492
|
+
}
|
|
493
|
+
const hasPkg = fs.existsSync(path.join(ctx.cwd, 'package.json'));
|
|
494
|
+
const text = githubWorkflow({ project: ref, site, packageManager: hasPkg ? detectPackageManager(ctx.cwd) : null });
|
|
495
|
+
const file = path.join(ctx.cwd, WORKFLOW_PATH);
|
|
496
|
+
const existing = fs.existsSync(file) ? fs.readFileSync(file, 'utf8') : null;
|
|
497
|
+
if (existing === text)
|
|
498
|
+
ui.info(`${WORKFLOW_PATH} is already up to date.`);
|
|
499
|
+
else if (existing !== null && !ctx.force) {
|
|
500
|
+
ui.error(`${WORKFLOW_PATH} already exists and is different.`);
|
|
501
|
+
ui.write(` ${c.dim('Add --force to replace it.')}`);
|
|
502
|
+
return 1;
|
|
503
|
+
}
|
|
504
|
+
else {
|
|
505
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
506
|
+
fs.writeFileSync(file, text);
|
|
507
|
+
ui.success(`${existing === null ? 'Created' : 'Replaced'} ${c.bold(WORKFLOW_PATH)}: pushes to main go live, pull requests get a preview address.`);
|
|
508
|
+
}
|
|
509
|
+
const dash = (await ctx.dashboardUrl())?.replace(/\/+$/, '');
|
|
510
|
+
ui.write(`\n${c.bold('Two more steps:')}`);
|
|
511
|
+
ui.write(` ${c.cyan('1.')} Create a deploy token: ${c.cyan(dash ? `${dash}/project/${ref}/hosting` : 'Dashboard → Hosting')} → Deploy tokens → Create token. It's shown once.`);
|
|
512
|
+
ui.write(` ${c.cyan('2.')} On GitHub, open your repository → Settings → Secrets and variables → Actions → New repository secret.`);
|
|
513
|
+
ui.write(` Name: ${c.bold(DEPLOY_TOKEN_SECRET)}, value: the token. ${c.yellow('Never commit the token itself.')}`);
|
|
514
|
+
ui.write(` Then commit ${WORKFLOW_PATH} and push.`);
|
|
515
|
+
if (needsBuildValues(ctx.cwd)) {
|
|
516
|
+
ui.write(`\n ${c.yellow('!')} Your build reads values from an .env file, which isn't in git, so GitHub doesn't have them.`);
|
|
517
|
+
ui.write(` ${c.dim('Add them in the same GitHub page under Variables, and pass them to the deploy step (see "Settings your build needs" in the hosting guide).')}`);
|
|
518
|
+
}
|
|
519
|
+
ui.write('');
|
|
520
|
+
return 0;
|
|
521
|
+
}
|
|
522
|
+
/** A web app whose build bakes in values from .env files (VITE_…, NEXT_PUBLIC_…, PUBLIC_…). */
|
|
523
|
+
function needsBuildValues(dir) {
|
|
524
|
+
for (const f of ['.env', '.env.local', '.env.production', '.env.production.local']) {
|
|
525
|
+
try {
|
|
526
|
+
if (/^\s*(VITE_|NEXT_PUBLIC_|PUBLIC_|EXPO_PUBLIC_|REACT_APP_|NUXT_PUBLIC_)\w+\s*=/m.test(fs.readFileSync(path.join(dir, f), 'utf8')))
|
|
527
|
+
return true;
|
|
528
|
+
}
|
|
529
|
+
catch { }
|
|
530
|
+
}
|
|
531
|
+
return false;
|
|
532
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Entry point for `npx usebecky`.
|
|
3
|
+
import { run } from "./run.js";
|
|
4
|
+
run(process.argv.slice(2)).then((code) => {
|
|
5
|
+
process.exitCode = code;
|
|
6
|
+
}, (err) => {
|
|
7
|
+
console.error(`\n✖ Something went wrong: ${err?.message ?? err}`);
|
|
8
|
+
process.exitCode = 1;
|
|
9
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type Ui } from './ui.ts';
|
|
2
|
+
type ApiResult<T> = {
|
|
3
|
+
data: T | null;
|
|
4
|
+
error: {
|
|
5
|
+
code: string;
|
|
6
|
+
message: string;
|
|
7
|
+
hint?: string;
|
|
8
|
+
status: number;
|
|
9
|
+
details?: any;
|
|
10
|
+
} | null;
|
|
11
|
+
};
|
|
12
|
+
export interface PaymentsContext {
|
|
13
|
+
ui: Ui;
|
|
14
|
+
args: string[];
|
|
15
|
+
ref: string | null;
|
|
16
|
+
yes: boolean;
|
|
17
|
+
api: <T = any>(method: string, path: string, body?: unknown) => Promise<ApiResult<T>>;
|
|
18
|
+
signIn: () => Promise<boolean>;
|
|
19
|
+
printError: (e: {
|
|
20
|
+
message: string;
|
|
21
|
+
hint?: string;
|
|
22
|
+
status?: number;
|
|
23
|
+
}) => number;
|
|
24
|
+
noProject: () => number;
|
|
25
|
+
/** The dashboard address (for "connect Stripe there"). */
|
|
26
|
+
dashboardUrl: () => Promise<string | null>;
|
|
27
|
+
/** Opens a link in the browser (`connect`); null: only print it (no terminal of its own, --no-browser, tests). */
|
|
28
|
+
openUrl: ((url: string) => void) | null;
|
|
29
|
+
/** Waits between checks while `connect` waits for Stripe (tests pass one that returns at once). */
|
|
30
|
+
sleep?: (ms: number) => Promise<void>;
|
|
31
|
+
/** How many checks `connect` makes before it gives up (default: 10 minutes' worth). */
|
|
32
|
+
maxPolls?: number;
|
|
33
|
+
}
|
|
34
|
+
export declare function paymentsCommand(ctx: PaymentsContext): Promise<number>;
|
|
35
|
+
export {};
|