create-heresay 0.2.46 → 0.2.47

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-heresay",
3
- "version": "0.2.46",
3
+ "version": "0.2.47",
4
4
  "description": "Set up your own Heresay: in-app feedback that every report gets a hearing. Creates a Firebase project in your Google account and deploys the dashboard, API and SDK to it.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/release/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.46
1
+ 0.2.47
@@ -1,3 +1,3 @@
1
1
  /** The Heresay release. Bumped by the release script; the CLI and /v1/health report it. */
2
- export const VERSION = '0.2.46';
2
+ export const VERSION = '0.2.47';
3
3
  //# sourceMappingURL=version.js.map
package/src/cli.mjs CHANGED
@@ -61,7 +61,8 @@ const HELP = `
61
61
 
62
62
  Setup flags, to skip questions:
63
63
  --name <name> --project-id <id> --region us-central1|asia-south1|europe-west1
64
- --billing <account> --owner <email> --yes (take every default, never ask)
64
+ --billing <account> --owner <email> --site <name> (your address: <name>.web.app)
65
+ --yes (take every default, never ask)
65
66
 
66
67
  Free plan, no billing (Spark): the API runs on a host you choose instead of Cloud Functions.
67
68
  --lite --origin https://your-host.example see docs/lite.md
@@ -92,9 +93,12 @@ function openInBrowser(target) {
92
93
  }
93
94
 
94
95
  /** The named Hosting site a saved Heresay lives on, from its saved address. */
95
- const savedSite = (rec) => /^https:\/\/([a-z0-9-]+)\.web\.app$/.exec(rec?.url ?? '')?.[1];
96
+ const savedSite = (id, rec) => {
97
+ const site = /^https:\/\/([a-z0-9-]+)\.web\.app$/.exec(rec?.url ?? '')?.[1];
98
+ return site && site !== id ? site : undefined; // the project's default site isn't a named one
99
+ };
96
100
  const savedCtx = (id, rec) => ({ id, name: rec.name, region: rec.region, billing: rec.billing, owner: rec.owner,
97
- lite: rec.lite, origin: rec.origin, site: savedSite(rec), onlySite: rec.only_site });
101
+ lite: rec.lite, origin: rec.origin, site: savedSite(id, rec), onlySite: rec.only_site });
98
102
 
99
103
  const slug = (s) => s.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');
100
104
  function suggestId(name) {
@@ -187,7 +191,7 @@ async function setup(args) {
187
191
  'This sets up your own Heresay in your Google account:',
188
192
  '',
189
193
  ` ${peacock('•')} A new Firebase project, owned by you`,
190
- ` ${peacock('•')} Its database, sign-in and hosting`,
194
+ ` ${peacock('•')} Its database, sign-in, and an address like heresay-<name>.web.app`,
191
195
  ` ${peacock('•')} The Heresay dashboard, API and SDK, deployed to it`,
192
196
  '',
193
197
  'Heresay’s API runs on Cloud Functions, which needs Firebase’s',
@@ -242,7 +246,9 @@ async function setup(args) {
242
246
  validate: (v) => (!v || /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v) ? undefined : 'That isn’t an email address'),
243
247
  })));
244
248
 
245
- ctx = { id, name, region, billing, owner, version, account: email, lite, origin };
249
+ const askedSite = args.site ? String(args.site).toLowerCase() : undefined;
250
+ if (askedSite && !steps.validSite(askedSite)) { p.log.error(`${askedSite} can’t be an address: 6 to 30 lowercase letters, digits and dashes, starting with a letter.`); process.exit(1); }
251
+ ctx = { id, name, region, billing, owner, version, account: email, lite, origin, askedSite };
246
252
  state.update(id, { name, region, billing, owner, lite, origin, url: lite ? origin : url(id), created_at: new Date().toISOString(), done: {} });
247
253
  if (!args.yes) p.log.message(pc.dim('You can add teammates in the dashboard later.'));
248
254
  }
@@ -326,6 +332,7 @@ const BUILD = [
326
332
  ['project', 'Creating the project', 'Project created', steps.project],
327
333
  ['billing', 'Linking billing', 'Billing linked', steps.billing],
328
334
  ['services', 'Switching on services', 'Services switched on', steps.services],
335
+ ['site', 'Choosing your address', 'Address reserved', steps.site],
329
336
  ['database', 'Creating the database', 'Database created', steps.database],
330
337
  ['signin', 'Setting up sign-in', 'Sign-in set up', steps.signIn],
331
338
  ['owner', 'Adding you as owner', 'Owner added', steps.owner],
@@ -361,6 +368,7 @@ async function build(ctx, only) {
361
368
  const secs = Math.round((Date.now() - started) / 1000);
362
369
  s.stop(`${title.padEnd(22)} ${pc.dim(String(detail ?? ''))}${secs > 20 ? pc.dim(` (${secs}s)`) : ''}`);
363
370
  state.markDone(ctx.id, key);
371
+ if (key === 'site' && ctx.site) state.update(ctx.id, { url: `https://${ctx.site}.web.app` });
364
372
  if (key === 'signin' && ctx.googleError) {
365
373
  p.log.warn(`Google sign-in couldn’t be switched on (${ctx.googleError}). Email links work. To add Google later: https://console.firebase.google.com/project/${ctx.id}/authentication/providers`);
366
374
  }
package/src/steps.mjs CHANGED
@@ -112,6 +112,65 @@ export async function services(ctx, say) {
112
112
  return 'Firestore, Functions, Auth, Hosting';
113
113
  }
114
114
 
115
+ // ---- 3b. address ------------------------------------------------------------------------
116
+ //
117
+ // The project id carries a random tail (heresay-acme-7k2q) because project ids are global and
118
+ // permanent. The address people type and paste into their apps shouldn't: a named Hosting site
119
+ // gives heresay-acme.web.app, and it can later move to another project without changing.
120
+
121
+ const HOSTING = 'https://firebasehosting.googleapis.com/v1beta1';
122
+
123
+ /** Site names to try, best first: the one asked for, else heresay-<name>, then with the project's tail. */
124
+ export function siteCandidates(name, id, asked) {
125
+ if (asked) return [asked];
126
+ const slug = String(name ?? '').toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');
127
+ // Shorten the name, never the suffix, and never leave a dangling or doubled dash.
128
+ const join = (...parts) => {
129
+ const suffix = parts.slice(1).join('-');
130
+ const room = 30 - (suffix ? suffix.length + 1 : 0);
131
+ const head = parts[0].slice(0, room).replace(/-+$/, '');
132
+ return (suffix ? `${head}-${suffix}` : head).replace(/-{2,}/g, '-');
133
+ };
134
+ const base = join(slug ? `heresay-${slug}` : 'heresay');
135
+ const out = [base, join(base, 'team'), join(base, id.split('-').pop())].filter((c) => validSite(c));
136
+ return [...new Set(out)];
137
+ }
138
+ export const validSite = (s) => /^[a-z][a-z0-9-]{4,28}[a-z0-9]$/.test(s);
139
+
140
+ /** True if created, false if the name is taken (by anyone). Retries only while Hosting is still switching on. */
141
+ async function createSite(project, siteId) {
142
+ const until = Date.now() + 90_000;
143
+ for (;;) {
144
+ try {
145
+ await g('POST', `${HOSTING}/projects/${project}/sites?siteId=${siteId}`, { project, body: {} });
146
+ return true;
147
+ } catch (e) {
148
+ if (e.status === 409 || /already exists|unavailable|not available|taken|reserved/i.test(e.message)) return false;
149
+ if ((e.status === 403 || e.status === 404) && Date.now() < until) { await new Promise((r) => setTimeout(r, 5000)); continue; }
150
+ throw e;
151
+ }
152
+ }
153
+ }
154
+
155
+ export async function site(ctx, say) {
156
+ if (ctx.site) {
157
+ const mine = await g('GET', `${HOSTING}/projects/${ctx.id}/sites/${ctx.site}`, { project: ctx.id, missingOk: true });
158
+ if (mine) return `${ctx.site}.web.app`;
159
+ }
160
+ const tried = [];
161
+ for (const c of siteCandidates(ctx.name, ctx.id, ctx.askedSite)) {
162
+ say(`Reserving ${c}.web.app`);
163
+ // Ours already (a run that stopped right after reserving it): use it, don't count it as taken.
164
+ const ours = await g('GET', `${HOSTING}/projects/${ctx.id}/sites/${c}`, { project: ctx.id, missingOk: true }).catch(() => null);
165
+ if (ours || await createSite(ctx.id, c)) { ctx.site = c; return `${c}.web.app`; }
166
+ tried.push(c);
167
+ }
168
+ if (ctx.askedSite) throw new StepError(`${ctx.askedSite}.web.app is taken.`, 'Run this again with another --site name.');
169
+ // Every nice name is taken: the project's default address still works.
170
+ ctx.site = null;
171
+ return `${ctx.id}.web.app (${tried.join(', ')} taken)`;
172
+ }
173
+
115
174
  // ---- 4. database ------------------------------------------------------------------------
116
175
 
117
176
  export async function database(ctx, say) {