@milesroxas/design-hub 0.11.2 → 0.13.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.
Files changed (87) hide show
  1. package/README.md +8 -6
  2. package/core/prototype.test.ts +52 -0
  3. package/core/prototype.ts +59 -0
  4. package/core/review/model.ts +20 -0
  5. package/core/review/service.test.ts +262 -37
  6. package/core/review/service.ts +276 -52
  7. package/core/review/store.ts +12 -6
  8. package/core/review/suite-kv.ts +17 -0
  9. package/dist/core/prototype.d.ts +50 -0
  10. package/dist/core/prototype.js +38 -0
  11. package/dist/core/review/model.d.ts +22 -0
  12. package/dist/core/review/model.js +8 -0
  13. package/dist/core/review/service.d.ts +98 -18
  14. package/dist/core/review/service.js +248 -46
  15. package/dist/core/review/store.d.ts +10 -4
  16. package/dist/core/review/store.js +2 -2
  17. package/dist/core/review/suite-kv.d.ts +7 -0
  18. package/dist/core/review/suite-kv.js +12 -0
  19. package/dist/scripts/client.d.ts +3 -3
  20. package/dist/scripts/client.js +55 -69
  21. package/dist/scripts/lib/cli.js +5 -0
  22. package/dist/scripts/lib/dns.d.ts +32 -0
  23. package/dist/scripts/lib/dns.js +69 -1
  24. package/dist/scripts/lib/fake-host.d.ts +35 -11
  25. package/dist/scripts/lib/fake-host.js +27 -13
  26. package/dist/scripts/lib/host.d.ts +13 -25
  27. package/dist/scripts/lib/host.js +15 -324
  28. package/dist/scripts/lib/studio-deps.d.ts +16 -0
  29. package/dist/scripts/lib/studio-deps.js +41 -16
  30. package/dist/scripts/lib/studios.d.ts +17 -5
  31. package/dist/scripts/lib/studios.js +25 -5
  32. package/dist/scripts/lib/vercel-host.d.ts +17 -0
  33. package/dist/scripts/lib/vercel-host.js +410 -0
  34. package/dist/scripts/people.d.ts +7 -4
  35. package/dist/scripts/people.js +147 -39
  36. package/dist/scripts/share.js +3 -2
  37. package/dist/scripts/studio.d.ts +10 -4
  38. package/dist/scripts/studio.js +136 -22
  39. package/dist/server/api.d.ts +12 -13
  40. package/dist/server/api.js +141 -52
  41. package/dist/server/gate.d.ts +97 -0
  42. package/dist/server/gate.js +537 -0
  43. package/dist/server/hosts.d.ts +3 -3
  44. package/dist/server/hosts.js +10 -10
  45. package/dist/server/mark.d.ts +17 -0
  46. package/dist/server/mark.js +46 -1
  47. package/dist/server/session.d.ts +9 -1
  48. package/dist/server/session.js +10 -3
  49. package/dist/server/stores/backends.d.ts +22 -0
  50. package/dist/server/stores/backends.js +96 -0
  51. package/dist/server/{fake-upstash.js → stores/fake-upstash.js} +1 -1
  52. package/dist/server/stores/index.d.ts +41 -0
  53. package/dist/server/stores/index.js +63 -0
  54. package/dist/server/stores/pglite.d.ts +16 -0
  55. package/dist/server/stores/pglite.js +98 -0
  56. package/dist/server/stores/postgres.d.ts +63 -0
  57. package/dist/server/stores/postgres.js +153 -0
  58. package/dist/server/{upstash.d.ts → stores/upstash.d.ts} +1 -1
  59. package/dist/vercel/files.js +1 -1
  60. package/dist/vercel/proxy.d.ts +6 -78
  61. package/dist/vercel/proxy.js +18 -359
  62. package/dist/vite/plugin.d.ts +6 -2
  63. package/dist/vite/plugin.js +22 -4
  64. package/package.json +3 -1
  65. package/shell/Index.tsx +29 -0
  66. package/shell/review/People.tsx +79 -49
  67. package/shell/review/Review.tsx +49 -1
  68. package/shell/review/ThreadCard.tsx +13 -1
  69. package/shell/review/client.ts +54 -21
  70. package/shell/review/review.css +1 -0
  71. package/shell/review/unsent.test.ts +63 -0
  72. package/shell/review/unsent.ts +64 -0
  73. package/shell/review/view.ts +22 -6
  74. package/shell/studio/ClientHub.tsx +9 -36
  75. package/shell/studio/StudioHub.tsx +293 -0
  76. package/shell/studio/hub.css +80 -5
  77. package/shell/studio/page.tsx +57 -0
  78. package/shell/studio/studio.html +14 -0
  79. package/shell/studio/studio.tsx +19 -0
  80. package/skills/miles-deploy/SKILL.md +17 -6
  81. package/skills/miles-design-hub/SKILL.md +3 -3
  82. package/skills/miles-review/SKILL.md +1 -1
  83. package/templates/studio/.env.example +3 -0
  84. package/templates/studio/README.md +45 -13
  85. package/templates/studio/package.json +1 -1
  86. /package/dist/server/{fake-upstash.d.ts → stores/fake-upstash.d.ts} +0 -0
  87. /package/dist/server/{upstash.js → stores/upstash.js} +0 -0
@@ -2,21 +2,29 @@
2
2
  /// <reference types="node" />
3
3
  // The people who comment on a project's prototypes in its studio: its
4
4
  // client's people, each signing in with a one-time link on the client's own
5
- // host. A link signs in one device, then stops working, so a second device
6
- // needs a new link. Studio also adds people from the People tab. Without a
7
- // link to a studio, and with --local, they are this machine's dev store's,
8
- // whose links open nothing anywhere.
9
- import { ACCESS, PERSON_KINDS } from '../core/review/model.js';
5
+ // host, and the studio's own people. A link signs in one device, then stops
6
+ // working, so a second device needs a new link. A Studio person's link is on
7
+ // the studio's own host (studio.<apex>) once it answers, where they see every
8
+ // client; before then, on the linked project's client's host. In the studio
9
+ // folder, or with --studio, people are the studio's own: that is how a Studio
10
+ // person, the owner too, gets a new link without a session. Studio also adds
11
+ // people from the People tab and the studio hub. Without a link to a studio,
12
+ // and with --local, they are this machine's dev store's, whose links open
13
+ // nothing anywhere.
14
+ import { ACCESS, PERSON_KINDS, STUDIO_SITE } from '../core/review/model.js';
15
+ import { localStudio } from '../server/local.js';
10
16
  import { CliError } from './lib/cli.js';
11
- import { realProjectDeps } from './lib/linked.js';
17
+ import { readLink, realProjectDeps } from './lib/linked.js';
12
18
  import { cliReview } from './lib/review.js';
19
+ import { openReview } from './lib/studio-deps.js';
20
+ import { findStudio, isStudioFolder, studioHost } from './lib/studios.js';
13
21
  export const spec = {
14
22
  name: 'people',
15
23
  summary: 'List or add the people who comment on the deployed prototypes, give them sign-in links, sign them out, revoke them, and set how long they keep access.',
16
24
  positionals: [
17
25
  {
18
26
  name: 'action',
19
- description: 'list (default): everyone. add: a new person and their link. link <id>: another one-time link, for another device. sign-out <id>: ends every session and unused link, and gives one new link. revoke <id>: ends their access everywhere. access [id]: with an id and --access, sets that person’s access from now; without an id, reads the default a new Client gets (the client’s own, else the studio’s), or sets the client’s with --access.',
27
+ description: 'list (default): everyone. add: a new person and their link. link <id>: another one-time link, for another device. sign-out <id>: ends every session and unused link, and gives one new link. revoke <id>: ends their access everywhere. access [id]: with an id and --access, sets that person’s access from now; without an id, reads the default a new Client gets (in a project the client’s own, else the studio’s; in the studio folder or with --studio the studio’s), or sets it with --access.',
20
28
  },
21
29
  { name: 'id', description: 'The person, for link, sign-out, revoke and access' },
22
30
  ],
@@ -28,23 +36,26 @@ export const spec = {
28
36
  },
29
37
  kind: {
30
38
  type: 'string',
31
- default: 'client',
32
39
  choices: PERSON_KINDS,
33
- description: 'client comments, replies and resolves any thread; studio also deletes any comment, sorts and manages people.',
40
+ description: 'client comments, replies and resolves any thread; studio also deletes any comment, sorts, manages people and sees every client at studio.<apex>. Absent: client in a project, studio in the studio folder or with --studio, which take studio only.',
34
41
  },
35
42
  access: {
36
43
  type: 'string',
37
44
  choices: ACCESS,
38
45
  description: 'How long they keep access: 3m, 6m or open. For add, absent means the client’s default for a client (people access reads it) and open for studio.',
39
46
  },
47
+ studio: {
48
+ type: 'string',
49
+ description: 'The studio whose own people these are, by slug, from anywhere on this machine; in the studio folder it is that studio. Their links are on studio.<apex>. Client people are a project’s: run people in the project linked to their client.',
50
+ },
40
51
  local: {
41
52
  type: 'boolean',
42
53
  description: 'Use this machine’s dev store, even in a project linked to a studio.',
43
54
  },
44
55
  },
45
- output: '{ ok, store, studio?, people } or { ok, store, studio?, person, link, next?, notice? } or { ok, store, studio?, person } or { ok, store, studio?, access }; store is studio in a linked project, with studio: { studio, client, host, project }, and notice when a Studio person is added there; local otherwise',
56
+ output: '{ ok, store, studio?, people } or { ok, store, studio?, person, link, next?, notice? } or { ok, store, studio?, person } or { ok, store, studio?, access }; store is studio in a linked project, with studio: { studio, client, host, project }, and notice when a Studio person is added there; store is studio with studio: { studio, host } for the studio’s own people (the studio folder or --studio); local otherwise. A Studio person’s link is on studio.<apex> once it answers.',
46
57
  };
47
- /** A link on the client's own host; a local one is a path the dev server never asks for. */
58
+ /** A link on the host it signs in on; a local one is a path the dev server never asks for. */
48
59
  const linkFor = (token, host) => {
49
60
  const path = `/invite/${token}`;
50
61
  if (!host)
@@ -60,55 +71,152 @@ function needId(action, id) {
60
71
  throw new CliError(`Name the person: pnpm people ${action} <id>.`, { fix: 'pnpm people lists ids.' });
61
72
  return id;
62
73
  }
63
- const withLink = (r, host) => ({
64
- person: strip(r.person),
65
- ...linkFor(r.token, host),
66
- });
74
+ async function withLink(r, c) {
75
+ return { person: strip(r.person), ...linkFor(r.token, await c.hostFor(r.person)) };
76
+ }
67
77
  /** One person's access with an id, else the default for a new Client: read it, or set it with --access. */
68
- async function access({ review, scope, actor, flags, id }) {
78
+ async function access({ review, at, actor, flags, id }) {
69
79
  if (id) {
70
80
  if (!flags.access)
71
81
  throw new CliError('Say how long: pnpm people access <id> --access 3m|6m|open.');
72
- return { person: strip(await review.setAccess(scope, actor, id, flags.access)) };
82
+ return { person: strip(await review.setAccess(at, actor, id, flags.access)) };
73
83
  }
84
+ if (at === STUDIO_SITE)
85
+ return {
86
+ access: flags.access
87
+ ? await review.setStudioAccess(actor, flags.access)
88
+ : await review.studioAccess(actor),
89
+ };
74
90
  return {
75
91
  access: flags.access
76
- ? await review.setDefaultAccess(scope, actor, flags.access)
77
- : await review.defaultAccess(scope, actor),
92
+ ? await review.setDefaultAccess(at, actor, flags.access)
93
+ : await review.defaultAccess(at, actor),
78
94
  };
79
95
  }
80
- /** What adding a Studio person in a linked project means, in plain words. */
81
- const studioWide = ({ studio, client, host }) => `A Studio person sees every client of studio ${studio}, not only ${client}: every project, comment and person. This link signs them in on ${host}. For someone who should see ${client} alone, add them with --kind client.`;
96
+ /** What adding a Studio person from a linked project means, in plain words, with --kind studio or --studio alike. */
97
+ const studioWide = ({ studio, client }, link) => `A Studio person sees every client of studio ${studio}, not only ${client}: every project, comment and person. This link signs them in on ${new URL(link).host}. For someone who should see ${client} alone, add them in this project with --kind client and no --studio.`;
98
+ /** The actions that print a link: a run that cannot print one is refused before any of them writes. */
99
+ const LINKING = new Set(['add', 'link', 'sign-out']);
82
100
  const ACTIONS = {
83
- list: async ({ review, scope, actor }) => ({ people: (await review.listPeople(scope, actor)).map(strip) }),
84
- add: async ({ review, scope, actor, flags, host }) => withLink(await review.addPerson(scope, actor, {
85
- name: flags.name,
86
- team: flags.team,
87
- kind: flags.kind,
88
- access: flags.access,
89
- }), host),
90
- link: async ({ review, scope, actor, id, host }) => withLink(await review.newLink(scope, actor, needId('link', id)), host),
91
- 'sign-out': async ({ review, scope, actor, id, host }) => withLink(await review.signOutEverywhere(scope, actor, needId('sign-out', id)), host),
92
- revoke: async ({ review, scope, actor, id }) => ({
93
- person: strip(await review.revoke(scope, actor, needId('revoke', id))),
101
+ list: async ({ review, at, actor }) => ({ people: (await review.listPeople(at, actor)).map(strip) }),
102
+ add: async (c) => withLink(await c.review.addPerson(c.at, c.actor, {
103
+ name: c.flags.name,
104
+ team: c.flags.team,
105
+ kind: c.flags.kind ?? (c.at === STUDIO_SITE ? 'studio' : 'client'),
106
+ access: c.flags.access,
107
+ }), c),
108
+ link: async (c) => withLink(await c.review.newLink(c.at, c.actor, needId('link', c.id)), c),
109
+ 'sign-out': async (c) => withLink(await c.review.signOutEverywhere(c.at, c.actor, needId('sign-out', c.id)), c),
110
+ revoke: async ({ review, at, actor, id }) => ({
111
+ person: strip(await review.revoke(at, actor, needId('revoke', id))),
94
112
  }),
95
113
  access,
96
114
  };
115
+ /**
116
+ * The studio's own host while it answers (its proof passed in `studio
117
+ * create` or `studio deploy`), else null: no link may be printed for a host
118
+ * that is not yet the studio site.
119
+ */
120
+ async function answeringStudioHost(review, actor, apex) {
121
+ return (await review.studioHostProven(actor)) ? studioHost(apex) : null;
122
+ }
123
+ /**
124
+ * The studio's own people (`--studio`, or the studio folder): its store, once
125
+ * its mark says it is its own, at the studio's own host. Client people are a
126
+ * project's, so --kind client is refused here before anything is opened.
127
+ */
128
+ async function studioPeople(flags, deps) {
129
+ if (flags.local)
130
+ throw new CliError('--studio and --local name two different stores.', {
131
+ fix: 'Drop one: --studio for the studio’s own people, --local for this machine’s dev store.',
132
+ });
133
+ if (flags.kind === 'client')
134
+ throw new CliError('Client people belong to a client: none is named here.', {
135
+ code: 'studio_people',
136
+ fix: 'Run people add in a project linked to the client (design-hub project link), or give a new client its first person with design-hub client add.',
137
+ });
138
+ // Without --studio, the studio folder this runs in (the project root a command is given).
139
+ const studio = findStudio(deps.home, flags.studio, deps.root);
140
+ const review = await openReview(deps, studio);
141
+ const actor = localStudio('cli', studio.config.name);
142
+ const { slug, apex } = studio.config;
143
+ return {
144
+ review,
145
+ actor,
146
+ at: STUDIO_SITE,
147
+ store: 'studio',
148
+ studio: { studio: slug, host: studioHost(apex) },
149
+ // Checked before the action runs (canLink), so a person, a link or a sign out is never made for no link.
150
+ hostFor: async () => studioHost(apex),
151
+ async canLink() {
152
+ if (await answeringStudioHost(review, actor, apex))
153
+ return;
154
+ throw new CliError(`${studioHost(apex)} does not answer yet; nothing was changed.`, {
155
+ code: 'studio_host_waiting',
156
+ fix: `Run design-hub studio deploy --studio ${slug}. It prints the owner's link once the host answers; then this works.`,
157
+ });
158
+ },
159
+ };
160
+ }
161
+ /**
162
+ * A project's people: a linked project's client in its studio, else this
163
+ * machine's dev store. A Studio person's link is on the studio's own host
164
+ * once it answers, a Client's on their client's host.
165
+ */
166
+ async function projectPeople(flags, deps) {
167
+ const found = await cliReview(flags.local, deps);
168
+ const { review, store, scope, actor } = found;
169
+ const where = found.studio;
170
+ if (!where)
171
+ return { review, store, at: scope, actor, hostFor: async () => undefined };
172
+ return {
173
+ review,
174
+ store,
175
+ at: scope,
176
+ actor,
177
+ studio: where,
178
+ async hostFor(person) {
179
+ if (person.client !== null)
180
+ return where.host;
181
+ const { apex } = findStudio(deps.home, where.studio, deps.cwd).config;
182
+ return (await answeringStudioHost(review, actor, apex)) ?? where.host;
183
+ },
184
+ };
185
+ }
97
186
  export async function run({ positionals: [action = 'list', id], flags }, deps) {
98
187
  const act = Object.hasOwn(ACTIONS, action) ? ACTIONS[action] : undefined;
99
188
  if (!act)
100
189
  throw new CliError(`Unknown action ${action}.`, { fix: 'list, add, link, sign-out, revoke or access' });
101
- const found = await cliReview(flags.local, deps);
102
- const { review, store, scope } = found;
103
- // A linked project's links sign in on its client's own host, and nowhere else.
104
- const done = await act({ review, scope, actor: found.actor, flags, id, host: found.studio?.host });
190
+ // The studio's own people: named by --studio, or the studio folder this runs in.
191
+ const own = flags.studio !== undefined || (!flags.local && !readLink(deps.root) && isStudioFolder(deps.root));
192
+ const found = own ? await studioPeople(flags, deps) : await projectPeople(flags, deps);
193
+ const { store, studio, canLink, ...context } = found;
194
+ if (canLink && LINKING.has(action))
195
+ await canLink();
196
+ const done = await act({ ...context, flags, id });
197
+ const project = own ? linkedHere(deps.root, studio?.studio) : studio && 'client' in studio ? studio : null;
198
+ const studioAdded = project && action === 'add' && done.person.client === null;
105
199
  return {
106
200
  store,
107
- ...(found.studio ? { studio: found.studio } : {}),
201
+ ...(studio ? { studio } : {}),
108
202
  ...done,
109
- ...(found.studio && action === 'add' && flags.kind === 'studio'
110
- ? { notice: studioWide(found.studio) }
203
+ ...(studioAdded
204
+ ? { notice: studioWide(project, String(done.link)) }
111
205
  : {}),
112
206
  };
113
207
  }
208
+ /**
209
+ * The client of the project this runs in, when it is linked to `studio`: a
210
+ * Studio person added there with --studio sees more than it. A link that
211
+ * cannot be read, or to another studio, is none here.
212
+ */
213
+ function linkedHere(root, studio) {
214
+ try {
215
+ const link = readLink(root);
216
+ return link && link.studio === studio ? link : null;
217
+ }
218
+ catch {
219
+ return null;
220
+ }
221
+ }
114
222
  export const main = (args) => run(args, realProjectDeps());
@@ -17,6 +17,7 @@ import { prototypes } from './lib/catalog.js';
17
17
  import { CliError } from './lib/cli.js';
18
18
  import { addressOf, LINK_FILE, openLinked, readLink, realProjectDeps, } from './lib/linked.js';
19
19
  import { cliActor } from './lib/review.js';
20
+ import { openFiles } from './lib/studio-deps.js';
20
21
  import * as preflight from './preflight.js';
21
22
  export const spec = {
22
23
  name: 'share',
@@ -63,9 +64,9 @@ export async function run({ flags }, deps) {
63
64
  code: 'not_linked',
64
65
  fix: 'design-hub project link --studio <slug> --client <slug> --project <slug>, then share again.',
65
66
  });
66
- // The studio must agree with the link before anything is built, and its file store's token is the studio folder's.
67
+ // The studio must agree with the link before anything is built, and its file store must be its own (its mark).
67
68
  const { review, scope, client, studio } = await openLinked(deps, link);
68
- const files = deps.files(studio);
69
+ const files = await openFiles(deps, studio);
69
70
  const checked = await deps.preflight(flags.skip);
70
71
  if (!checked.ok)
71
72
  return {
@@ -7,7 +7,7 @@ export declare const spec: {
7
7
  readonly positionals: [{
8
8
  readonly name: 'action';
9
9
  readonly required: true;
10
- readonly description: 'create: scaffold --dir, install and build it here, set it up on --team, deploy, and list it. deploy: install and build the studio folder here, so the site installs the package its package.json names, deploy it again and remove every older deployment. link: list an existing studio folder (--dir) on this machine and link it to the studio’s project.';
10
+ readonly description: 'create: scaffold --dir, install and build it here, set it up on --team, deploy, set up studio.<apex> (its DNS record and domain) and list it. deploy: install and build the studio folder here, so the site installs the package its package.json names, deploy it again, remove every older deployment, and set up studio.<apex> if it is not yet. link: list an existing studio folder (--dir) on this machine and link it to the studio’s project.';
11
11
  }];
12
12
  readonly flags: {
13
13
  readonly dir: {
@@ -28,7 +28,7 @@ export declare const spec: {
28
28
  };
29
29
  readonly owner: {
30
30
  readonly type: 'string';
31
- readonly description: 'For create: your name. You are the studio’s first Studio person and get a link on each new client’s host.';
31
+ readonly description: 'For create: your name. You are the studio’s first Studio person and get the first sign-in link, on studio.<apex>, where every client opens.';
32
32
  };
33
33
  readonly team: {
34
34
  readonly type: 'string';
@@ -56,9 +56,15 @@ export declare const spec: {
56
56
  readonly default: 'iad1';
57
57
  readonly description: 'For create: the file store’s region, the site’s function region.';
58
58
  };
59
+ readonly kv: {
60
+ readonly type: 'string';
61
+ readonly choices: readonly ["upstash", "postgres"];
62
+ readonly default: 'upstash';
63
+ readonly description: 'For create: the studio’s database. upstash: Upstash for Redis. postgres: Postgres, a new Neon database on its free plan, or with --database any Postgres the team has from the Vercel Marketplace (Neon, Supabase). Kept in studio.json; a rerun must give the same.';
64
+ };
59
65
  readonly database: {
60
66
  readonly type: 'string';
61
- readonly description: 'For create: the name of a database (Upstash for Redis) the team already has and no studio uses, connected to the studio in place of a new one. A database serves one studio only: create marks it as this studio’s, and refuses one that is another studio’s or already holds a studio’s data, before writing to it, disconnecting it again and keeping none of its credentials. Absent, a new one is made, only ever on the free plan; a team refused the free plan stops here with nothing bought.';
67
+ readonly description: 'For create: the name of a database (of the --kv kind) the team already has and no studio uses, connected to the studio in place of a new one. A database serves one studio only: create marks it as this studio’s, and refuses one that is another studio’s or already holds a studio’s data, before writing to it, disconnecting it again and keeping none of its credentials. Absent, a new one is made, only ever on the free plan; a team refused the free plan stops here with nothing bought.';
62
68
  };
63
69
  readonly package: {
64
70
  readonly type: 'string';
@@ -69,7 +75,7 @@ export declare const spec: {
69
75
  readonly description: 'For deploy: the studio’s slug. Absent, the studio folder this runs in.';
70
76
  };
71
77
  };
72
- readonly output: 'create: { ok, studio, dir, folder, list, package, vercel, env: { GATE_SECRET: set | kept, database: created | connected | kept, files: created | kept, pulled }, mark: claimed | kept (the database marked as this studio’s), dns, owner, deployment, removed, next }. deploy: { ok, studio, package, deployment, removed }. package is what the folder’s package.json names for @milesroxas/design-hub. link: { ok, studio, dir, list, env, warnings }';
78
+ readonly output: 'create: { ok, studio, dir, folder, list, package, vercel, env: { GATE_SECRET: set | kept, database: created | connected | kept, files: created | kept, pulled }, mark: claimed | kept (the database marked as this studio’s), filesMark: claimed | kept (the file store, the same), dns, owner, deployment, removed, studioHost: { host, state: ready | waiting, error?, fix?, record? }, link? (the owner’s, on the studio’s host, the run it first answers), next }. deploy: { ok, studio, package, deployment, removed, studioHost, link? }. The deploy is done whatever studioHost says; while it is waiting, its fix says what to do (a record to add, a certificate still coming) and the next deploy finishes it. package is what the folder’s package.json names for @milesroxas/design-hub. link: { ok, studio, dir, list, env, warnings }';
73
79
  };
74
80
  export declare function run({ positionals: [action], flags }: Args<typeof spec>, deps: StudioDeps): Promise<Record<string, unknown>>;
75
81
  export declare const main: (args: Args<typeof spec>) => Promise<Record<string, unknown>>;
@@ -5,10 +5,12 @@
5
5
  //
6
6
  // studio create scaffolds the studio folder from templates/studio/,
7
7
  // installs and builds it here, sets it up on its team,
8
- // deploys it, and lists it on this machine
8
+ // deploys it, sets up the studio's own host
9
+ // (`studio.<apex>`) and lists it on this machine
9
10
  // studio deploy installs and builds the folder here and deploys it again (a
10
11
  // package upgrade reaches the site this way), then puts every
11
- // older deployment out of reach
12
+ // older deployment out of reach, and sets up the studio's
13
+ // own host if it is not yet
12
14
  // studio link lists a studio folder that already exists on this machine
13
15
  // and links it to the studio's project on its team
14
16
  //
@@ -20,13 +22,14 @@ import { copyFileSync, cpSync, existsSync, readdirSync, readFileSync, renameSync
20
22
  import { basename, join, resolve } from 'node:path';
21
23
  import { readEnv, removeEnv, writeEnv } from '../server/env.js';
22
24
  import { localStudio } from '../server/local.js';
23
- import { claimDatabase, MarkError } from '../server/mark.js';
25
+ import { claimDatabase, claimFiles, MarkError } from '../server/mark.js';
24
26
  import { PACKAGE, TEMPLATES } from '../server/paths.js';
25
27
  import { CliError } from './lib/cli.js';
26
28
  import { keepingGitignore } from './lib/deploy.js';
29
+ import { pointHost, waitServed } from './lib/dns.js';
27
30
  import { deployAlone } from './lib/host.js';
28
31
  import { openReview, realDeps } from './lib/studio-deps.js';
29
- import { addToList, BLOB_NAME, CLOUDFLARE_NAME, DNS_PROVIDERS, findStudio, MODE_OF, readStudio, STORE_NAMES, STUDIO_FILE, studioProblems, writeStudio, } from './lib/studios.js';
32
+ import { addToList, BLOB_NAME, CLOUDFLARE_NAME, DNS_PROVIDERS, findStudio, hasStore, KV_PROVIDERS, MODE_OF, readStudio, STORE_NAMES, STUDIO_FILE, studioHost, studioProblems, writeStudio, } from './lib/studios.js';
30
33
  export const spec = {
31
34
  name: 'studio',
32
35
  summary: 'Create a studio (its folder, its site on a Vercel team, its stores), deploy it again, or list an existing studio folder on this machine.',
@@ -34,7 +37,7 @@ export const spec = {
34
37
  {
35
38
  name: 'action',
36
39
  required: true,
37
- description: 'create: scaffold --dir, install and build it here, set it up on --team, deploy, and list it. deploy: install and build the studio folder here, so the site installs the package its package.json names, deploy it again and remove every older deployment. link: list an existing studio folder (--dir) on this machine and link it to the studio’s project.',
40
+ description: 'create: scaffold --dir, install and build it here, set it up on --team, deploy, set up studio.<apex> (its DNS record and domain) and list it. deploy: install and build the studio folder here, so the site installs the package its package.json names, deploy it again, remove every older deployment, and set up studio.<apex> if it is not yet. link: list an existing studio folder (--dir) on this machine and link it to the studio’s project.',
38
41
  },
39
42
  ],
40
43
  flags: {
@@ -50,7 +53,7 @@ export const spec = {
50
53
  },
51
54
  owner: {
52
55
  type: 'string',
53
- description: 'For create: your name. You are the studio’s first Studio person and get a link on each new client’s host.',
56
+ description: 'For create: your name. You are the studio’s first Studio person and get the first sign-in link, on studio.<apex>, where every client opens.',
54
57
  },
55
58
  team: {
56
59
  type: 'string',
@@ -75,9 +78,15 @@ export const spec = {
75
78
  default: 'iad1',
76
79
  description: 'For create: the file store’s region, the site’s function region.',
77
80
  },
81
+ kv: {
82
+ type: 'string',
83
+ choices: KV_PROVIDERS,
84
+ default: 'upstash',
85
+ description: 'For create: the studio’s database. upstash: Upstash for Redis. postgres: Postgres, a new Neon database on its free plan, or with --database any Postgres the team has from the Vercel Marketplace (Neon, Supabase). Kept in studio.json; a rerun must give the same.',
86
+ },
78
87
  database: {
79
88
  type: 'string',
80
- description: 'For create: the name of a database (Upstash for Redis) the team already has and no studio uses, connected to the studio in place of a new one. A database serves one studio only: create marks it as this studio’s, and refuses one that is another studio’s or already holds a studio’s data, before writing to it, disconnecting it again and keeping none of its credentials. Absent, a new one is made, only ever on the free plan; a team refused the free plan stops here with nothing bought.',
89
+ description: 'For create: the name of a database (of the --kv kind) the team already has and no studio uses, connected to the studio in place of a new one. A database serves one studio only: create marks it as this studio’s, and refuses one that is another studio’s or already holds a studio’s data, before writing to it, disconnecting it again and keeping none of its credentials. Absent, a new one is made, only ever on the free plan; a team refused the free plan stops here with nothing bought.',
81
90
  },
82
91
  package: {
83
92
  type: 'string',
@@ -88,7 +97,7 @@ export const spec = {
88
97
  description: 'For deploy: the studio’s slug. Absent, the studio folder this runs in.',
89
98
  },
90
99
  },
91
- output: 'create: { ok, studio, dir, folder, list, package, vercel, env: { GATE_SECRET: set | kept, database: created | connected | kept, files: created | kept, pulled }, mark: claimed | kept (the database marked as this studio’s), dns, owner, deployment, removed, next }. deploy: { ok, studio, package, deployment, removed }. package is what the folder’s package.json names for @milesroxas/design-hub. link: { ok, studio, dir, list, env, warnings }',
100
+ output: 'create: { ok, studio, dir, folder, list, package, vercel, env: { GATE_SECRET: set | kept, database: created | connected | kept, files: created | kept, pulled }, mark: claimed | kept (the database marked as this studio’s), filesMark: claimed | kept (the file store, the same), dns, owner, deployment, removed, studioHost: { host, state: ready | waiting, error?, fix?, record? }, link? (the owner’s, on the studio’s host, the run it first answers), next }. deploy: { ok, studio, package, deployment, removed, studioHost, link? }. The deploy is done whatever studioHost says; while it is waiting, its fix says what to do (a record to add, a certificate still coming) and the next deploy finishes it. package is what the folder’s package.json names for @milesroxas/design-hub. link: { ok, studio, dir, list, env, warnings }',
92
101
  };
93
102
  const need = (value, flag, action) => {
94
103
  if (!value)
@@ -106,7 +115,7 @@ function configFrom(flags) {
106
115
  vercel: { team: need(flags.team, 'team', 'create'), project: flags.project ?? flags.slug ?? '' },
107
116
  dns: { provider, mode: MODE_OF[provider], zone: flags.zone ?? null },
108
117
  owner: null,
109
- providers: { kv: 'upstash', files: 'vercel-blob', host: 'vercel' },
118
+ providers: { kv: (flags.kv ?? 'upstash'), files: 'vercel-blob', host: 'vercel' },
110
119
  };
111
120
  need(flags.owner, 'owner', 'create');
112
121
  const problems = studioProblems(config);
@@ -126,6 +135,7 @@ function sameStudio(kept, asked) {
126
135
  ['team', kept.vercel.team, asked.vercel.team],
127
136
  ['project', kept.vercel.project, asked.vercel.project],
128
137
  ['dns', kept.dns.provider, asked.dns.provider],
138
+ ['kv', kept.providers.kv, asked.providers.kv],
129
139
  ];
130
140
  const differs = pairs.filter(([, a, b]) => a !== b).map(([flag]) => flag);
131
141
  if (differs.length)
@@ -239,15 +249,22 @@ function tokenFrom(file) {
239
249
  throw new CliError('--cloudflare-token-file does not hold one API token on one line.', { got: file });
240
250
  return token;
241
251
  }
242
- /** The store and file credentials the CLI needs, read back from Production into .env where missing. */
243
- async function pullMissing(dir, host) {
252
+ /**
253
+ * The store and file credentials the CLI needs, read back from Production
254
+ * into .env where missing: the database's names of the studio's kind (for
255
+ * Postgres, whichever of DATABASE_URL and POSTGRES_URL the site has) and the
256
+ * file store's token.
257
+ */
258
+ async function pullMissing(dir, host, kind) {
244
259
  const env = readEnv(dir);
245
- const missing = [...STORE_NAMES, BLOB_NAME].filter((n) => !env[n]);
260
+ const store = hasStore(kind, (n) => !!env[n]) ? [] : STORE_NAMES[kind].filter((n) => !env[n]);
261
+ const missing = [...store, ...(env[BLOB_NAME] ? [] : [BLOB_NAME])];
246
262
  if (!missing.length)
247
263
  return [];
248
264
  const values = await host.pullEnv(missing);
249
265
  writeEnv(dir, values);
250
- const still = missing.filter((n) => !values[n]);
266
+ const now = { ...env, ...values };
267
+ const still = [...(hasStore(kind, (n) => !!now[n]) ? [] : store), ...(now[BLOB_NAME] ? [] : [BLOB_NAME])];
251
268
  if (still.length)
252
269
  throw new CliError(`Could not read ${still.join(', ')} back from the studio's Production env.`, {
253
270
  fix: 'Copy them from the store’s page on Vercel into the studio’s .env, then run this again.',
@@ -291,14 +308,15 @@ async function wildcard(host, apex) {
291
308
  * database of its own starts clean.
292
309
  */
293
310
  async function claimOrLetGo(deps, host, studio, connected) {
311
+ const kind = studio.config.providers.kv;
294
312
  try {
295
313
  return await claimDatabase(deps.store(studio), studio);
296
314
  }
297
315
  catch (error) {
298
316
  if (!(error instanceof MarkError))
299
317
  throw error;
300
- removeEnv(studio.dir, STORE_NAMES);
301
- const gone = connected !== null && (await host.disconnectDatabase(connected));
318
+ removeEnv(studio.dir, STORE_NAMES[kind]);
319
+ const gone = connected !== null && (await host.disconnectDatabase(kind, connected));
302
320
  const { dir, config } = studio;
303
321
  const rerun = `then run design-hub studio create again in ${dir} with the same flags, without --database (a new free one) or with --database <a database no studio uses>.`;
304
322
  throw new CliError(error.message, {
@@ -309,6 +327,79 @@ async function claimOrLetGo(deps, host, studio, connected) {
309
327
  });
310
328
  }
311
329
  }
330
+ /**
331
+ * The studio's own host, `studio.<apex>`, where Studio signs in and opens
332
+ * every client, set up as `client add` sets up a client's: its DNS record and
333
+ * its domain on the site, its entry in the store (which the gate reads), then
334
+ * a wait until its DNS serves it and a proof that the host answers from this
335
+ * studio's site. Only then is the owner's link made, on the run that first
336
+ * proves it; once proven, later runs leave the host alone. Each step is safe
337
+ * to repeat, so a run that stopped part way is finished by the next.
338
+ */
339
+ async function studioHostUp(deps, studio, host) {
340
+ const { config } = studio;
341
+ const name = studioHost(config.apex);
342
+ const cli = localStudio('cli', 'CLI');
343
+ const review = await openReview(deps, studio);
344
+ if (await review.studioHostProven(cli))
345
+ return { host: name, state: 'ready' };
346
+ const again = {
347
+ run: `run design-hub studio deploy --studio ${config.slug} again`,
348
+ stop: 'It prints your sign-in link once the host answers.',
349
+ };
350
+ const ensured = await pointHost(host, deps.dns(studio, host), name, again);
351
+ await review.setStudioHost(cli, name);
352
+ const proof = await review.startStudioProof(cli);
353
+ await waitServed(deps.names, config.apex, name, ensured, again);
354
+ if (!(await host.reaches(name, proof)))
355
+ throw new CliError(`${name} does not answer from the studio site yet; no sign-in link was made.`, {
356
+ ...(typeof ensured === 'object' ? { record: ensured.record } : {}),
357
+ fix: typeof ensured === 'object'
358
+ ? `Add the record at the DNS provider, then ${again.run}. ${again.stop}`
359
+ : `A new certificate can take a minute: ${again.run}. If ${name} has a DNS record of its own, another server answers it: remove that record, then ${again.run}.`,
360
+ });
361
+ await review.endStudioProof(cli);
362
+ return { host: name, state: 'ready', ...(config.owner ? { link: await ownerLink(review, config) } : {}) };
363
+ }
364
+ /**
365
+ * The studio-host step after a deploy, which is done whatever happens here:
366
+ * a step that fails (a record to add, a certificate still coming, the DNS
367
+ * token or the store missing from .env) is reported as the studio host's,
368
+ * with what to do, never as a failed deploy.
369
+ */
370
+ async function setUpStudioHost(deps, studio, host) {
371
+ try {
372
+ return await studioHostUp(deps, studio, host);
373
+ }
374
+ catch (error) {
375
+ const detail = error instanceof CliError || error instanceof MarkError ? error.detail : {};
376
+ return {
377
+ fix: `Fix what error names, then run design-hub studio deploy --studio ${studio.config.slug} again.`,
378
+ ...detail,
379
+ host: studioHost(studio.config.apex),
380
+ state: 'waiting',
381
+ error: error.message,
382
+ };
383
+ }
384
+ }
385
+ /** A new one-time link for the studio's owner, on the studio's own host. */
386
+ async function ownerLink(review, config) {
387
+ const { token } = await review.newStudioLink(localStudio('cli', 'CLI'), String(config.owner));
388
+ return `https://${studioHost(config.apex)}/invite/${token}`;
389
+ }
390
+ /**
391
+ * What `studio create` says to do next: sign in with the link; or, when the
392
+ * studio host answered on an earlier run, how to get a new link; or what the
393
+ * studio host still needs first.
394
+ */
395
+ function createNext(config, own) {
396
+ const add = `design-hub client add <client> --studio ${config.slug} --name "<client name>" --person "<their name>" --team "<their company>"`;
397
+ if (own.link)
398
+ return `Open the link to sign in at ${own.host} (it signs in one device, then stops working), then add a client: ${add}`;
399
+ if (own.state === 'ready')
400
+ return `${own.host} already answers, so no new sign-in link was made. For a new one: design-hub people link ${config.owner} --studio ${config.slug}. Add a client with: ${add}`;
401
+ return `${own.host} does not answer yet: do what studioHost.fix says, then design-hub studio deploy --studio ${config.slug} prints your sign-in link. Clients can be added meanwhile: ${add}`;
402
+ }
312
403
  async function create(flags, deps) {
313
404
  const asked = configFrom(flags);
314
405
  const owner = need(flags.owner, 'owner', 'create').trim();
@@ -322,21 +413,26 @@ async function create(flags, deps) {
322
413
  const pkg = installAndBuild(dir, deps);
323
414
  const host = deps.host(dir, config);
324
415
  const link = await keepingGitignore(dir, () => host.link(true));
416
+ const kind = config.providers.kv;
325
417
  const env = {
326
418
  GATE_SECRET: await gateSecret(dir, host),
327
- database: await host.provisionDatabase(flags.database),
419
+ database: await host.provisionDatabase(kind, flags.database),
328
420
  files: await host.provisionFiles(flags.region),
329
- pulled: await pullMissing(dir, host),
421
+ pulled: await pullMissing(dir, host, kind),
330
422
  };
331
423
  const studio = { dir, config };
424
+ // Postgres's table, made here and only here, never by a visitor's request.
425
+ await deps.prepareStore(studio);
332
426
  // Before anything is written to the database: it serves this studio only, or nothing goes on.
333
427
  const connected = env.database === 'connected' ? (flags.database ?? null) : null;
334
428
  const mark = await claimOrLetGo(deps, host, studio, connected);
429
+ // The file store too: one marked as another studio's is refused before a file is written.
430
+ const filesMark = await claimFiles(deps.files(studio), studio);
335
431
  const dns = { provider: config.dns.provider, mode: config.dns.mode };
336
432
  if (config.dns.mode === 'wildcard')
337
433
  dns.wildcard = await wildcard(host, config.apex);
434
+ const review = await openReview(deps, studio);
338
435
  if (!config.owner) {
339
- const review = await openReview(deps, studio);
340
436
  const person = await review.addStudioPerson(localStudio('cli', 'CLI'), {
341
437
  name: owner,
342
438
  team: config.name,
@@ -345,6 +441,7 @@ async function create(flags, deps) {
345
441
  writeStudio(dir, config);
346
442
  }
347
443
  const deployed = await deployAlone(host);
444
+ const { link: first, ...own } = await setUpStudioHost(deps, studio, host);
348
445
  return {
349
446
  studio: config.slug,
350
447
  dir,
@@ -354,10 +451,13 @@ async function create(flags, deps) {
354
451
  vercel: { team: config.vercel.team, project: config.vercel.project, link },
355
452
  env,
356
453
  mark,
454
+ filesMark,
357
455
  dns,
358
456
  owner: config.owner,
359
457
  ...deployed,
360
- next: `design-hub client add <client> --studio ${config.slug} --name "<client name>" --person "<their name>" --team "<their company>"`,
458
+ studioHost: own,
459
+ ...(first ? { link: first } : {}),
460
+ next: createNext(config, { ...own, link: first }),
361
461
  };
362
462
  }
363
463
  async function deploy(flags, deps) {
@@ -366,7 +466,21 @@ async function deploy(flags, deps) {
366
466
  if (tgz)
367
467
  namePackage(dir, tgz);
368
468
  const pkg = installAndBuild(dir, deps);
369
- return { studio: config.slug, package: pkg, ...(await deployAlone(deps.host(dir, config))) };
469
+ const studio = { dir, config };
470
+ // A Postgres studio's table, made or closed to other roles again, so an upgrade applies what a newer package needs.
471
+ if (config.providers.kv === 'postgres')
472
+ await deps.prepareStore(studio);
473
+ const host = deps.host(dir, config);
474
+ const deployed = await deployAlone(host);
475
+ // For a studio made before it had one, the owner's first link on the studio's host comes the run it answers.
476
+ const { link: first, ...own } = await setUpStudioHost(deps, studio, host);
477
+ return {
478
+ studio: config.slug,
479
+ package: pkg,
480
+ ...deployed,
481
+ studioHost: own,
482
+ ...(first ? { link: first } : {}),
483
+ };
370
484
  }
371
485
  async function link(flags, deps) {
372
486
  const dir = resolve(need(flags.dir, 'dir', 'link'));
@@ -379,10 +493,10 @@ async function link(flags, deps) {
379
493
  // folder can hold a full .env and no link, and a deploy follows the link.
380
494
  const host = deps.host(dir, config);
381
495
  await keepingGitignore(dir, () => host.link(false));
382
- const pulled = await pullMissing(dir, host);
496
+ const pulled = await pullMissing(dir, host, config.providers.kv);
383
497
  const warnings = config.dns.provider === 'cloudflare' && !readEnv(dir)[CLOUDFLARE_NAME]
384
498
  ? [
385
- `No ${CLOUDFLARE_NAME} in .env: client add needs it. Run this again with --cloudflare-token-file <file>.`,
499
+ `No ${CLOUDFLARE_NAME} in .env: client add needs it, and studio deploy until studio.${config.apex} answers. Run this again with --cloudflare-token-file <file>.`,
386
500
  ]
387
501
  : [];
388
502
  return { studio: config.slug, dir, list, env: { pulled }, warnings };