@milesroxas/design-hub 0.10.0 → 0.11.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 +13 -11
- package/core/files.test.ts +76 -0
- package/core/files.ts +131 -0
- package/core/prototype.test.ts +124 -7
- package/core/prototype.ts +133 -24
- package/core/review/model.ts +119 -2
- package/core/review/policy.test.ts +79 -22
- package/core/review/policy.ts +36 -8
- package/core/review/service.test.ts +1464 -340
- package/core/review/service.ts +777 -191
- package/core/review/store.test.ts +91 -0
- package/core/review/store.ts +143 -33
- package/dist/core/files.d.ts +39 -0
- package/dist/core/files.js +107 -0
- package/dist/core/prototype.d.ts +61 -12
- package/dist/core/prototype.js +105 -26
- package/dist/core/review/model.d.ts +76 -2
- package/dist/core/review/model.js +76 -1
- package/dist/core/review/policy.d.ts +11 -4
- package/dist/core/review/policy.js +14 -2
- package/dist/core/review/service.d.ts +197 -45
- package/dist/core/review/service.js +702 -192
- package/dist/core/review/store.d.ts +84 -31
- package/dist/core/review/store.js +66 -33
- package/dist/scripts/bootstrap.d.ts +4 -34
- package/dist/scripts/bootstrap.js +9 -244
- package/dist/scripts/cli.js +6 -0
- package/dist/scripts/client.d.ts +45 -0
- package/dist/scripts/client.js +320 -0
- package/dist/scripts/commands.js +6 -0
- package/dist/scripts/comments.d.ts +8 -26
- package/dist/scripts/comments.js +27 -14
- package/dist/scripts/lib/builds.d.ts +40 -0
- package/dist/scripts/lib/builds.js +198 -0
- package/dist/scripts/lib/cli.js +3 -1
- package/dist/scripts/lib/deploy.d.ts +6 -90
- package/dist/scripts/lib/deploy.js +24 -137
- package/dist/scripts/lib/dns.d.ts +49 -0
- package/dist/scripts/lib/dns.js +141 -0
- package/dist/scripts/lib/fake-host.d.ts +168 -0
- package/dist/scripts/lib/fake-host.js +244 -0
- package/dist/scripts/lib/host.d.ts +87 -0
- package/dist/scripts/lib/host.js +362 -0
- package/dist/scripts/lib/linked.d.ts +51 -0
- package/dist/scripts/lib/linked.js +124 -0
- package/dist/scripts/lib/names.d.ts +35 -0
- package/dist/scripts/lib/names.js +77 -0
- package/dist/scripts/lib/review.d.ts +23 -2
- package/dist/scripts/lib/review.js +32 -15
- package/dist/scripts/lib/studio-deps.d.ts +35 -0
- package/dist/scripts/lib/studio-deps.js +79 -0
- package/dist/scripts/lib/studios.d.ts +70 -0
- package/dist/scripts/lib/studios.js +187 -0
- package/dist/scripts/lib/update.d.ts +33 -0
- package/dist/scripts/lib/update.js +40 -0
- package/dist/scripts/lib/vercel.d.ts +1 -23
- package/dist/scripts/lib/vercel.js +6 -81
- package/dist/scripts/people.d.ts +7 -11
- package/dist/scripts/people.js +51 -46
- package/dist/scripts/preflight.d.ts +8 -1
- package/dist/scripts/preflight.js +36 -19
- package/dist/scripts/project.d.ts +86 -0
- package/dist/scripts/project.js +141 -0
- package/dist/scripts/share.d.ts +105 -30
- package/dist/scripts/share.js +82 -154
- package/dist/scripts/skills.d.ts +18 -2
- package/dist/scripts/skills.js +22 -3
- package/dist/scripts/studio.d.ts +75 -0
- package/dist/scripts/studio.js +401 -0
- package/dist/scripts/upgrade.d.ts +10 -2
- package/dist/scripts/upgrade.js +44 -14
- package/dist/server/api.d.ts +22 -11
- package/dist/server/api.js +114 -46
- package/dist/server/blob.d.ts +10 -0
- package/dist/server/blob.js +53 -0
- package/dist/server/checks.js +2 -2
- package/dist/server/dev.js +4 -4
- package/dist/server/env.d.ts +2 -0
- package/dist/server/env.js +9 -0
- package/dist/server/fake-upstash.js +52 -3
- package/dist/server/hosts.d.ts +11 -0
- package/dist/server/hosts.js +31 -0
- package/dist/server/local.d.ts +7 -1
- package/dist/server/local.js +10 -1
- package/dist/server/mark.d.ts +41 -0
- package/dist/server/mark.js +111 -0
- package/dist/server/session.d.ts +17 -9
- package/dist/server/session.js +22 -45
- package/dist/server/upstash.js +20 -0
- package/dist/vercel/files.d.ts +11 -0
- package/dist/vercel/files.js +68 -0
- package/dist/vercel/index.d.ts +4 -3
- package/dist/vercel/index.js +12 -8
- package/dist/vercel/proxy.d.ts +69 -4
- package/dist/vercel/proxy.js +171 -107
- package/dist/vite/plugin.d.ts +24 -1
- package/dist/vite/plugin.js +46 -2
- package/kit/dials.tsx +2 -2
- package/package.json +2 -1
- package/shell/Guide.tsx +11 -5
- package/shell/Index.tsx +32 -10
- package/shell/Notice.tsx +1 -1
- package/shell/controls.ts +1 -1
- package/shell/hub.ts +5 -1
- package/shell/main.tsx +18 -3
- package/shell/review/Review.tsx +2 -2
- package/shell/review/ThreadCard.tsx +5 -5
- package/shell/review/client.ts +28 -11
- package/shell/review/view.test.ts +19 -0
- package/shell/review/view.ts +25 -0
- package/shell/shell.css +4 -0
- package/shell/studio/ClientHub.tsx +98 -0
- package/shell/studio/hub.css +48 -0
- package/shell/studio/index.html +14 -0
- package/shell/studio/main.tsx +17 -0
- package/skills/miles-deploy/SKILL.md +120 -65
- package/skills/miles-design-hub/SKILL.md +11 -9
- package/skills/miles-review/SKILL.md +5 -7
- package/skills/miles-worktrees/SKILL.md +3 -3
- package/templates/conductor/settings.toml +1 -1
- package/templates/studio/.env.example +9 -0
- package/templates/studio/.vercelignore +5 -0
- package/templates/studio/README.md +84 -0
- package/templates/studio/_gitignore +7 -0
- package/templates/studio/api/files.ts +4 -0
- package/templates/studio/api/review.ts +4 -0
- package/templates/studio/build.mjs +4 -0
- package/templates/studio/package.json +16 -0
- package/templates/studio/proxy.ts +4 -0
- package/templates/studio/studio.json +9 -0
- package/templates/studio/vercel.json +27 -0
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
// The host port: what the studio commands ask of the account that runs the
|
|
3
|
+
// studio site. One adapter, Vercel, through its CLI. Every call names the
|
|
4
|
+
// studio's team (`--scope`), so no command ever acts in whichever team the CLI
|
|
5
|
+
// last switched to. A call that acts through the folder's link (deploy, env,
|
|
6
|
+
// the stores) runs only once the link is the studio's project in that team,
|
|
7
|
+
// and the CLI never sees the env vars that would pick another. No call prints
|
|
8
|
+
// a value: secrets go in on stdin and come back only into the folder's `.env`.
|
|
9
|
+
import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
|
|
10
|
+
import { tmpdir } from 'node:os';
|
|
11
|
+
import { join } from 'node:path';
|
|
12
|
+
import { hostName, proofPath } from '../../core/prototype.js';
|
|
13
|
+
import { readEnv } from '../../server/env.js';
|
|
14
|
+
import { CliError } from './cli.js';
|
|
15
|
+
import { upstashTerms } from './deploy.js';
|
|
16
|
+
import { BLOB_NAME, STORE_NAMES } from './studios.js';
|
|
17
|
+
import { vercel } from './vercel.js';
|
|
18
|
+
/**
|
|
19
|
+
* Deploys the studio site and then puts every older deployment out of reach,
|
|
20
|
+
* since a request can name a deployment by its id and be served by that
|
|
21
|
+
* deployment's gate and functions (studios-findings.md, section 1, pinning):
|
|
22
|
+
* a fix, or a rotated secret, is in force only once no older one answers.
|
|
23
|
+
* It deletes nothing unless the project's list holds the new deployment, since
|
|
24
|
+
* one made on another project would leave the list without a live one. It
|
|
25
|
+
* deletes them, then lists the project again and fails unless the new
|
|
26
|
+
* deployment is the only one left.
|
|
27
|
+
*/
|
|
28
|
+
export async function deployAlone(host) {
|
|
29
|
+
const live = await host.deploy();
|
|
30
|
+
if (live.state !== 'READY')
|
|
31
|
+
throw new CliError(`The new deployment is ${live.state}, not ready; nothing older was removed.`, {
|
|
32
|
+
deployment: live.id,
|
|
33
|
+
fix: 'Run design-hub studio deploy again once its build log is fixed.',
|
|
34
|
+
});
|
|
35
|
+
const listed = await host.deployments();
|
|
36
|
+
if (!listed.some((d) => d.id === live.id))
|
|
37
|
+
throw new CliError('The new deployment is not one of the studio project’s; nothing was removed.', {
|
|
38
|
+
deployment: live.id,
|
|
39
|
+
fix: 'Run design-hub studio link --dir <the studio folder>, then design-hub studio deploy again. The new deployment is on another project: delete it there on Vercel.',
|
|
40
|
+
});
|
|
41
|
+
const older = listed.filter((d) => d.id !== live.id).map((d) => d.id);
|
|
42
|
+
for (const id of older)
|
|
43
|
+
await host.removeDeployment(id);
|
|
44
|
+
const left = await host.deployments();
|
|
45
|
+
const still = left.filter((d) => d.id !== live.id).map((d) => d.id);
|
|
46
|
+
if (still.length || !left.some((d) => d.id === live.id))
|
|
47
|
+
throw new CliError('An older deployment of the studio site can still be reached.', {
|
|
48
|
+
deployment: live.id,
|
|
49
|
+
left: still,
|
|
50
|
+
fix: 'Delete each one listed in the project’s Deployments page on Vercel, then run design-hub studio deploy again.',
|
|
51
|
+
});
|
|
52
|
+
return { deployment: live.id, removed: older };
|
|
53
|
+
}
|
|
54
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
55
|
+
const HTTPS_TRIES = 20;
|
|
56
|
+
const HTTPS_GAP_MS = 3000;
|
|
57
|
+
/**
|
|
58
|
+
* A DELETE through `vercel api`. The CLI (59.x) asks before every DELETE and,
|
|
59
|
+
* with no terminal (as every design-hub command runs), refuses one unless it is
|
|
60
|
+
* told to skip that question. Only the two removals send it, each on a path
|
|
61
|
+
* built from a checked deployment id or host. `vercel remove` is not used: it
|
|
62
|
+
* also looks its argument up as a project and deletes any project it matches.
|
|
63
|
+
* No CLI command takes one host off one project (`vercel domains rm` takes the
|
|
64
|
+
* domain off the whole team).
|
|
65
|
+
*/
|
|
66
|
+
const DELETE = ['-X', 'DELETE', '--dangerously-skip-permissions'];
|
|
67
|
+
const DEPLOYMENT_ID = /^dpl_[A-Za-z0-9]+$/;
|
|
68
|
+
/** A Marketplace resource's name, as `vercel integration resource connect` takes it. */
|
|
69
|
+
const RESOURCE_NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
|
|
70
|
+
/** What the Marketplace answers a plan the team cannot have (studios-findings.md, section 5). */
|
|
71
|
+
const NO_FREE_PLAN = /billing plan not found|plan[^\n]*not (found|available)/i;
|
|
72
|
+
/** The env vars the Vercel CLI (59.x) reads to pick a project or team over the folder's link and `--scope`. */
|
|
73
|
+
export const PICKERS = [
|
|
74
|
+
'VERCEL_ORG_ID',
|
|
75
|
+
'VERCEL_PROJECT_ID',
|
|
76
|
+
'NOW_ORG_ID',
|
|
77
|
+
'NOW_PROJECT_ID',
|
|
78
|
+
'VERCEL_TEAM_ID',
|
|
79
|
+
];
|
|
80
|
+
/** The caller's env without the names that would send a studio command to another project or team. */
|
|
81
|
+
function studioCliEnv(env) {
|
|
82
|
+
const out = { ...env };
|
|
83
|
+
for (const name of PICKERS)
|
|
84
|
+
delete out[name];
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
const studioCli = (cwd, args, input) => vercel(cwd, args, input, studioCliEnv(process.env));
|
|
88
|
+
/** The ids `.vercel/project.json` links the folder to, or none when it is missing or unreadable. */
|
|
89
|
+
function linkOf(dir) {
|
|
90
|
+
try {
|
|
91
|
+
return JSON.parse(readFileSync(join(dir, '.vercel', 'project.json'), 'utf8'));
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
return {};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/** The Vercel adapter. Every call goes through `call`, which adds `--scope <team>`. */
|
|
98
|
+
export function vercelHost({ dir, team, project, run = studioCli, fetcher = fetch, wait = sleep, }) {
|
|
99
|
+
const call = (args, input) => {
|
|
100
|
+
const out = run(dir, [...args, '--scope', team], input);
|
|
101
|
+
if (!out)
|
|
102
|
+
throw new CliError('The Vercel CLI is not installed.', {
|
|
103
|
+
fix: 'npm i -g vercel, then design-hub signin.',
|
|
104
|
+
});
|
|
105
|
+
return out;
|
|
106
|
+
};
|
|
107
|
+
const must = (args, input) => {
|
|
108
|
+
const out = call(args, input);
|
|
109
|
+
if (out.status !== 0)
|
|
110
|
+
throw failed(args, out);
|
|
111
|
+
return out.stdout;
|
|
112
|
+
};
|
|
113
|
+
const api = async (path, method = 'GET', fields = []) => {
|
|
114
|
+
const verb = method === 'GET' ? [] : method === 'DELETE' ? DELETE : ['-X', method];
|
|
115
|
+
const args = ['api', path, '--raw', ...verb];
|
|
116
|
+
const out = call([...args, ...fields.flatMap((f) => ['-F', f])]);
|
|
117
|
+
if (out.status !== 0) {
|
|
118
|
+
if (/\b404\b|not[_ ]found/i.test(`${out.stdout}\n${out.stderr}`))
|
|
119
|
+
return null;
|
|
120
|
+
throw failed(['api', method, path], out);
|
|
121
|
+
}
|
|
122
|
+
return out.stdout.trim() ? JSON.parse(out.stdout) : {};
|
|
123
|
+
};
|
|
124
|
+
/** The studio's project on its team, as ids, or null when the team has none of that name. */
|
|
125
|
+
const studioProject = async () => {
|
|
126
|
+
const found = await api(`/v9/projects/${encodeURIComponent(project)}`);
|
|
127
|
+
return found ? { id: found.id, org: found.accountId } : null;
|
|
128
|
+
};
|
|
129
|
+
/** Whether `.vercel/project.json` is that project, in that team, by id: a name alone can be another team's. */
|
|
130
|
+
const linkedTo = (want) => {
|
|
131
|
+
const have = linkOf(dir);
|
|
132
|
+
return (!!want &&
|
|
133
|
+
typeof want.id === 'string' &&
|
|
134
|
+
typeof want.org === 'string' &&
|
|
135
|
+
have.projectId === want.id &&
|
|
136
|
+
have.orgId === want.org);
|
|
137
|
+
};
|
|
138
|
+
/** A call the CLI sends wherever the folder is linked: refused, before it runs, unless that is the studio. */
|
|
139
|
+
const linked = async (args, input) => {
|
|
140
|
+
if (!linkedTo(await studioProject()))
|
|
141
|
+
throw new CliError(`The studio folder is not linked to project ${project} in team ${team}; nothing was changed.`, {
|
|
142
|
+
dir,
|
|
143
|
+
fix: `design-hub studio link --dir ${dir}, then run this again.`,
|
|
144
|
+
});
|
|
145
|
+
return call(args, input);
|
|
146
|
+
};
|
|
147
|
+
const linkedMust = async (args, input) => {
|
|
148
|
+
const out = await linked(args, input);
|
|
149
|
+
if (out.status !== 0)
|
|
150
|
+
throw failed(args, out);
|
|
151
|
+
return out.stdout;
|
|
152
|
+
};
|
|
153
|
+
const names = async () => new Set([...(await linkedMust(['env', 'ls', 'production'])).matchAll(/^\s*([A-Z][A-Z0-9_]*)\s/gm)].map((m) => m[1]));
|
|
154
|
+
const domainPath = (host) => `/v9/projects/${encodeURIComponent(project)}/domains/${encodeURIComponent(host)}`;
|
|
155
|
+
return {
|
|
156
|
+
async link(create) {
|
|
157
|
+
const want = await studioProject();
|
|
158
|
+
if (linkedTo(want))
|
|
159
|
+
return 'kept';
|
|
160
|
+
if (create && want)
|
|
161
|
+
throw new CliError(`Team ${team} already has a project named ${project}.`, {
|
|
162
|
+
fix: 'Pick another --project, or run design-hub studio link on the folder that made it.',
|
|
163
|
+
});
|
|
164
|
+
if (!create && !want)
|
|
165
|
+
throw new CliError(`Team ${team} has no project named ${project}.`, {
|
|
166
|
+
fix: 'Check vercel.team and vercel.project in studio.json.',
|
|
167
|
+
});
|
|
168
|
+
if (create)
|
|
169
|
+
must(['project', 'add', project]);
|
|
170
|
+
must(['link', '--yes', '--project', project]);
|
|
171
|
+
if (!linkedTo(await studioProject()))
|
|
172
|
+
throw new CliError(`vercel link did not link the studio folder to project ${project} in team ${team}.`, {
|
|
173
|
+
dir,
|
|
174
|
+
fix: `Delete ${join(dir, '.vercel')}, then run this again.`,
|
|
175
|
+
});
|
|
176
|
+
return create ? 'created' : 'linked';
|
|
177
|
+
},
|
|
178
|
+
async hasEnv(name) {
|
|
179
|
+
return (await names()).has(name);
|
|
180
|
+
},
|
|
181
|
+
async setSecret(name, value) {
|
|
182
|
+
if ((await names()).has(name))
|
|
183
|
+
return 'kept';
|
|
184
|
+
await linkedMust(['env', 'add', name, 'production', '--sensitive', '--yes'], value);
|
|
185
|
+
return 'set';
|
|
186
|
+
},
|
|
187
|
+
async pullEnv(wanted) {
|
|
188
|
+
const tmp = mkdtempSync(join(tmpdir(), 'studio-env-'));
|
|
189
|
+
try {
|
|
190
|
+
await linkedMust(['env', 'pull', join(tmp, '.env'), '--environment=production', '--yes']);
|
|
191
|
+
const pulled = readEnv(tmp);
|
|
192
|
+
return Object.fromEntries(wanted.filter((n) => pulled[n]).map((n) => [n, pulled[n]]));
|
|
193
|
+
}
|
|
194
|
+
finally {
|
|
195
|
+
rmSync(tmp, { recursive: true, force: true });
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
async provisionDatabase(existing) {
|
|
199
|
+
const have = await names();
|
|
200
|
+
if (STORE_NAMES.every((n) => have.has(n)))
|
|
201
|
+
return 'kept';
|
|
202
|
+
if (existing !== undefined) {
|
|
203
|
+
if (!RESOURCE_NAME.test(existing))
|
|
204
|
+
throw new CliError('--database takes the name of a database the team has.', { got: existing });
|
|
205
|
+
const out = await linked(['integration', 'resource', 'connect', existing, project, '--yes']);
|
|
206
|
+
if (out.status !== 0)
|
|
207
|
+
throw new CliError(`Could not connect the database ${existing} to the studio project.`, {
|
|
208
|
+
stderr: out.stderr.trim().slice(-600),
|
|
209
|
+
fix: `Check the name on team ${team}'s Storage page on Vercel, then run this again.`,
|
|
210
|
+
});
|
|
211
|
+
return 'connected';
|
|
212
|
+
}
|
|
213
|
+
// Only ever the free plan: a team without one is refused by the Marketplace, and nothing is bought.
|
|
214
|
+
const args = [
|
|
215
|
+
'integration',
|
|
216
|
+
'add',
|
|
217
|
+
'upstash/upstash-kv',
|
|
218
|
+
'--name',
|
|
219
|
+
`${project}-review`,
|
|
220
|
+
'--plan',
|
|
221
|
+
'free',
|
|
222
|
+
];
|
|
223
|
+
const out = await linked([...args, '-e', 'production', '--no-env-pull']);
|
|
224
|
+
if (out.status !== 0) {
|
|
225
|
+
if (NO_FREE_PLAN.test(out.stderr))
|
|
226
|
+
throw new CliError(`Team ${team} cannot have a free review store (Upstash for Redis); nothing was bought.`, {
|
|
227
|
+
stderr: out.stderr.trim().slice(-600),
|
|
228
|
+
fix: `A team gets one free database from the Marketplace, and a database serves one studio only: never connect one another studio uses. If team ${team} has a database no studio uses (its Storage page on Vercel), run this again with --database <its name>; else give this studio a team or an Upstash account of its own.`,
|
|
229
|
+
});
|
|
230
|
+
const terms = upstashTerms(out.stderr, team);
|
|
231
|
+
throw new CliError('Could not add the review store (Upstash for Redis) to the studio project.', {
|
|
232
|
+
stderr: out.stderr.trim().slice(-600),
|
|
233
|
+
terms: terms.url,
|
|
234
|
+
fix: `Most often the team has not accepted the Upstash terms. Tell the user, word for word: "${terms.say}" Then run this again; or connect a database the team has that no studio uses with --database <its name>.`,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
return 'created';
|
|
238
|
+
},
|
|
239
|
+
async disconnectDatabase(name) {
|
|
240
|
+
if (!RESOURCE_NAME.test(name))
|
|
241
|
+
return false;
|
|
242
|
+
const out = await linked(['integration', 'resource', 'disconnect', name, project, '--yes']);
|
|
243
|
+
if (out.status !== 0)
|
|
244
|
+
return false;
|
|
245
|
+
const have = await names();
|
|
246
|
+
return !STORE_NAMES.some((n) => have.has(n));
|
|
247
|
+
},
|
|
248
|
+
async provisionFiles(region) {
|
|
249
|
+
if ((await names()).has(BLOB_NAME))
|
|
250
|
+
return 'kept';
|
|
251
|
+
await linkedMust([
|
|
252
|
+
'blob',
|
|
253
|
+
'create-store',
|
|
254
|
+
`${project}-files`,
|
|
255
|
+
'--access',
|
|
256
|
+
'private',
|
|
257
|
+
'--region',
|
|
258
|
+
region,
|
|
259
|
+
'--yes',
|
|
260
|
+
]);
|
|
261
|
+
return 'created';
|
|
262
|
+
},
|
|
263
|
+
async addDomain(host) {
|
|
264
|
+
const existing = await api(domainPath(host));
|
|
265
|
+
const state = existing === null
|
|
266
|
+
? await api(`/v10/projects/${encodeURIComponent(project)}/domains`, 'POST', [`name=${host}`])
|
|
267
|
+
: existing.verified
|
|
268
|
+
? existing
|
|
269
|
+
: await api(`${domainPath(host)}/verify`, 'POST');
|
|
270
|
+
return {
|
|
271
|
+
verified: state?.verified === true,
|
|
272
|
+
verification: state?.verification ?? existing?.verification ?? [],
|
|
273
|
+
};
|
|
274
|
+
},
|
|
275
|
+
async removeDomain(host) {
|
|
276
|
+
if (hostName(host) !== host)
|
|
277
|
+
throw new CliError(`${host} is not a host name; no domain was removed.`);
|
|
278
|
+
if ((await api(domainPath(host))) === null)
|
|
279
|
+
return 'absent';
|
|
280
|
+
await api(domainPath(host), 'DELETE');
|
|
281
|
+
return 'removed';
|
|
282
|
+
},
|
|
283
|
+
async recordFor(host) {
|
|
284
|
+
const config = await api(`/v6/domains/${encodeURIComponent(host)}/config?projectIdOrName=${encodeURIComponent(project)}`);
|
|
285
|
+
return cnameTarget(config);
|
|
286
|
+
},
|
|
287
|
+
async reaches(host, proof) {
|
|
288
|
+
for (let i = 0; i < HTTPS_TRIES; i++) {
|
|
289
|
+
if (await answers(fetcher, host, proof))
|
|
290
|
+
return true;
|
|
291
|
+
await wait(HTTPS_GAP_MS);
|
|
292
|
+
}
|
|
293
|
+
return false;
|
|
294
|
+
},
|
|
295
|
+
async deploy() {
|
|
296
|
+
const out = await linked(['deploy', '--prod', '--yes']);
|
|
297
|
+
const url = `${out.stdout}\n${out.stderr}`.match(/https:\/\/([\w.-]+\.vercel\.app)/)?.[1];
|
|
298
|
+
if (out.status !== 0 || !url)
|
|
299
|
+
throw failed(['deploy', '--prod'], out);
|
|
300
|
+
const found = await api(`/v13/deployments/${encodeURIComponent(url)}`);
|
|
301
|
+
if (!found?.id)
|
|
302
|
+
throw new CliError('The new deployment could not be read back.', {
|
|
303
|
+
fix: 'Run design-hub studio deploy again.',
|
|
304
|
+
});
|
|
305
|
+
return { id: String(found.id), state: String(found.readyState ?? found.status ?? '') };
|
|
306
|
+
},
|
|
307
|
+
async deployments() {
|
|
308
|
+
const all = [];
|
|
309
|
+
let until = '';
|
|
310
|
+
do {
|
|
311
|
+
const page = await api(`/v6/deployments?projectId=${encodeURIComponent(project)}&limit=100${until ? `&until=${until}` : ''}`);
|
|
312
|
+
for (const d of (page?.deployments ?? []))
|
|
313
|
+
all.push({ id: d.uid, state: String(d.state ?? d.readyState ?? '') });
|
|
314
|
+
until = page?.pagination?.next ? String(page.pagination.next) : '';
|
|
315
|
+
} while (until);
|
|
316
|
+
return all;
|
|
317
|
+
},
|
|
318
|
+
async removeDeployment(id) {
|
|
319
|
+
if (!DEPLOYMENT_ID.test(id))
|
|
320
|
+
throw new CliError(`${id} is not a deployment id; nothing was removed.`);
|
|
321
|
+
const out = call(['api', `/v13/deployments/${id}`, '--raw', ...DELETE]);
|
|
322
|
+
if (out.status !== 0)
|
|
323
|
+
throw new CliError(`Vercel would not delete the older deployment ${id}.`, {
|
|
324
|
+
stderr: out.stderr.trim().slice(-600),
|
|
325
|
+
fix: 'Delete it in the project’s Deployments page on Vercel, then run design-hub studio deploy again.',
|
|
326
|
+
});
|
|
327
|
+
},
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
function failed(args, out) {
|
|
331
|
+
return new CliError(`vercel ${args.slice(0, 2).join(' ')} failed.`, {
|
|
332
|
+
stderr: out.stderr.trim().slice(-600),
|
|
333
|
+
fix: /log ?in|credentials|token|not authorized/i.test(out.stderr)
|
|
334
|
+
? 'design-hub signin, send the user its url, then run this again.'
|
|
335
|
+
: undefined,
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
/** Vercel's first-ranked CNAME for a host, without its trailing dot. */
|
|
339
|
+
export function cnameTarget(config) {
|
|
340
|
+
const ranked = (config?.recommendedCNAME ?? [])
|
|
341
|
+
.slice()
|
|
342
|
+
.sort((a, b) => a.rank - b.rank);
|
|
343
|
+
const value = ranked[0]?.value?.replace(/\.$/, '').toLowerCase();
|
|
344
|
+
if (!value)
|
|
345
|
+
throw new CliError('Vercel named no record for this host.', { fix: 'Run this again in a minute.' });
|
|
346
|
+
return value;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* A host answers from this studio's site: asked the proof's path over HTTPS,
|
|
350
|
+
* it sends back the proof's answer, which only the gate reading this studio's
|
|
351
|
+
* store has. Any other answer, whatever its status or headers, is another
|
|
352
|
+
* server's, or a site that does not know the client yet.
|
|
353
|
+
*/
|
|
354
|
+
async function answers(fetcher, host, { ask, answer }) {
|
|
355
|
+
try {
|
|
356
|
+
const res = await fetcher(`https://${host}${proofPath(ask)}`, { redirect: 'manual' });
|
|
357
|
+
return res.status === 200 && (await res.text()) === answer;
|
|
358
|
+
}
|
|
359
|
+
catch {
|
|
360
|
+
return false;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type Client, type Project } from '../../core/review/model.ts';
|
|
2
|
+
import type { Review } from '../../core/review/service.ts';
|
|
3
|
+
import { type StudioDeps } from './studio-deps.ts';
|
|
4
|
+
import { type Studio } from './studios.ts';
|
|
5
|
+
/** The link's place in the project, relative to its root. */
|
|
6
|
+
export declare const LINK_FILE = ".design-hub/studio.json";
|
|
7
|
+
export type ProjectLink = {
|
|
8
|
+
studio: string;
|
|
9
|
+
id: string;
|
|
10
|
+
client: string;
|
|
11
|
+
project: string;
|
|
12
|
+
};
|
|
13
|
+
/** What a project command runs against: the studio commands' dependencies, and the project's folder. */
|
|
14
|
+
export type ProjectDeps = StudioDeps & {
|
|
15
|
+
root: string;
|
|
16
|
+
};
|
|
17
|
+
export declare const realProjectDeps: () => ProjectDeps;
|
|
18
|
+
/** The project's link, or null when it has none. A file that is not a link is an error, never "none". */
|
|
19
|
+
export declare function readLink(root: string): ProjectLink | null;
|
|
20
|
+
export declare function writeLink(root: string, link: ProjectLink): void;
|
|
21
|
+
type InStudio = {
|
|
22
|
+
studio: Studio;
|
|
23
|
+
review: Review;
|
|
24
|
+
client: Client;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* The studio this machine lists under `slug`, its store once its mark says
|
|
28
|
+
* it is the studio's own, and the client's live record there, once its host
|
|
29
|
+
* has proved it is the studio site (mustBeAdded). With an `id`, the listed
|
|
30
|
+
* studio must be that one: a slug can repeat across machines and teams, an
|
|
31
|
+
* id cannot. Nothing is written.
|
|
32
|
+
*/
|
|
33
|
+
export declare function inStudio(deps: StudioDeps, slug: string, id: string | null, client: string): Promise<InStudio>;
|
|
34
|
+
export type Linked = InStudio & {
|
|
35
|
+
link: ProjectLink;
|
|
36
|
+
project: Project;
|
|
37
|
+
scope: {
|
|
38
|
+
client: string;
|
|
39
|
+
project: string;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* The studio a linked project's command acts on, once the studio agrees with
|
|
44
|
+
* the link: the studio of the link's id, its own database, the client live
|
|
45
|
+
* and finished being added there, and the project one of the client's.
|
|
46
|
+
* Nothing is written.
|
|
47
|
+
*/
|
|
48
|
+
export declare function openLinked(deps: StudioDeps, link: ProjectLink): Promise<Linked>;
|
|
49
|
+
/** The address a project is sent at: its client's own host and its path, never a deployment's. */
|
|
50
|
+
export declare const addressOf: (client: Client, project: string) => string;
|
|
51
|
+
export {};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
// Where a project sits in a studio. `.design-hub/studio.json`, written by
|
|
3
|
+
// `design-hub project link` and committed, names the studio (its slug and the
|
|
4
|
+
// id its database is marked with), the client and the project. It holds no
|
|
5
|
+
// secret: the studio's credentials stay in the studio's folder, which this
|
|
6
|
+
// machine finds by slug in its studio list (`~/.design-hub/studios.json`).
|
|
7
|
+
// A command run in a linked project acts on that studio only, and only once
|
|
8
|
+
// the studio agrees with the link: the folder listed under the slug is the
|
|
9
|
+
// studio of that id, its database is marked as its own (openReview), the
|
|
10
|
+
// client is live there and the project is one of the client's. Otherwise it
|
|
11
|
+
// stops with a named error before it changes anything.
|
|
12
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
13
|
+
import { dirname, join } from 'node:path';
|
|
14
|
+
import { SLUG } from '../../core/feature.js';
|
|
15
|
+
import { isProjectSlug } from '../../core/prototype.js';
|
|
16
|
+
import { isAdded, isStudioClient } from '../../core/review/model.js';
|
|
17
|
+
import { ROOT } from '../../server/paths.js';
|
|
18
|
+
import { CliError } from './cli.js';
|
|
19
|
+
import { openReview, realDeps, recordOf } from './studio-deps.js';
|
|
20
|
+
import { findStudio } from './studios.js';
|
|
21
|
+
/** The link's place in the project, relative to its root. */
|
|
22
|
+
export const LINK_FILE = '.design-hub/studio.json';
|
|
23
|
+
export const realProjectDeps = () => ({ ...realDeps(), root: ROOT });
|
|
24
|
+
const RELINK = 'design-hub project link --studio <slug> --client <slug> --project <slug> writes it again.';
|
|
25
|
+
/** The problems with a link, by field; none for a well-formed one. */
|
|
26
|
+
function linkProblems(v) {
|
|
27
|
+
if (!v || typeof v !== 'object' || Array.isArray(v))
|
|
28
|
+
return ['studio.json: an object'];
|
|
29
|
+
const l = v;
|
|
30
|
+
const problems = [];
|
|
31
|
+
if (typeof l.studio !== 'string' || !SLUG.test(l.studio))
|
|
32
|
+
problems.push('studio: the studio’s slug');
|
|
33
|
+
if (typeof l.id !== 'string' || !/^[a-f0-9]{32}$/.test(l.id))
|
|
34
|
+
problems.push('id: the studio’s id, 32 hex');
|
|
35
|
+
if (!isStudioClient(l.client))
|
|
36
|
+
problems.push('client: the client’s slug');
|
|
37
|
+
if (!isProjectSlug(l.project))
|
|
38
|
+
problems.push('project: the project’s slug');
|
|
39
|
+
return problems;
|
|
40
|
+
}
|
|
41
|
+
/** The project's link, or null when it has none. A file that is not a link is an error, never "none". */
|
|
42
|
+
export function readLink(root) {
|
|
43
|
+
const file = join(root, LINK_FILE);
|
|
44
|
+
if (!existsSync(file))
|
|
45
|
+
return null;
|
|
46
|
+
let parsed = null;
|
|
47
|
+
try {
|
|
48
|
+
parsed = JSON.parse(readFileSync(file, 'utf8'));
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
// Reported below with the other problems.
|
|
52
|
+
}
|
|
53
|
+
const problems = linkProblems(parsed);
|
|
54
|
+
if (problems.length)
|
|
55
|
+
throw new CliError(`${LINK_FILE} is not a studio link.`, { problems, fix: RELINK });
|
|
56
|
+
const { studio, id, client, project } = parsed;
|
|
57
|
+
return { studio, id, client, project };
|
|
58
|
+
}
|
|
59
|
+
export function writeLink(root, link) {
|
|
60
|
+
const file = join(root, LINK_FILE);
|
|
61
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
62
|
+
writeFileSync(file, `${JSON.stringify(link, null, 2)}\n`);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Refuses a client still being added (isAdded): its `client add` stopped
|
|
66
|
+
* before its host was marked proven, whatever became of the proof. Until it
|
|
67
|
+
* is, the host may be another server's, so nothing of the project goes to it
|
|
68
|
+
* (no record, no build) and no sign-in link is made for it, a Studio
|
|
69
|
+
* person's included. The same `client add` run again finishes it.
|
|
70
|
+
*/
|
|
71
|
+
function mustBeAdded(studio, client) {
|
|
72
|
+
if (isAdded(client))
|
|
73
|
+
return;
|
|
74
|
+
const { slug, apex } = studio.config;
|
|
75
|
+
const host = client.host === `${client.slug}.${apex}` ? '' : ` --host ${client.host}`;
|
|
76
|
+
throw new CliError(`Client ${client.slug} is still being added: ${client.host} has not yet proved it is the studio site; nothing was changed.`, {
|
|
77
|
+
code: 'client_not_added',
|
|
78
|
+
fix: `Finish it: run design-hub client add ${client.slug} --studio ${slug} --name "${client.name}"${host} --person "<its first person>" again, with the flags it was first run with, then this. Or drop it: design-hub client archive ${client.slug} --studio ${slug}.`,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The studio this machine lists under `slug`, its store once its mark says
|
|
83
|
+
* it is the studio's own, and the client's live record there, once its host
|
|
84
|
+
* has proved it is the studio site (mustBeAdded). With an `id`, the listed
|
|
85
|
+
* studio must be that one: a slug can repeat across machines and teams, an
|
|
86
|
+
* id cannot. Nothing is written.
|
|
87
|
+
*/
|
|
88
|
+
export async function inStudio(deps, slug, id, client) {
|
|
89
|
+
const studio = findStudio(deps.home, slug, deps.cwd);
|
|
90
|
+
if (id !== null && studio.config.id !== id)
|
|
91
|
+
throw new CliError(`The studio this machine lists as ${slug} is not the studio this project is linked to; nothing was changed.`, {
|
|
92
|
+
code: 'studio_not_linked',
|
|
93
|
+
dir: studio.dir,
|
|
94
|
+
fix: `design-hub studio link --dir <the folder of studio ${slug} whose id is ${id}> lists the right one.`,
|
|
95
|
+
});
|
|
96
|
+
const review = await openReview(deps, studio);
|
|
97
|
+
const record = await recordOf(review, client);
|
|
98
|
+
if (!record || record.archivedAt !== null)
|
|
99
|
+
throw new CliError(`Studio ${slug} has no client ${client}; nothing was changed.`, {
|
|
100
|
+
code: 'client_unknown',
|
|
101
|
+
fix: `design-hub client add ${client} --studio ${slug} adds it, or link the project to a client the studio has.`,
|
|
102
|
+
});
|
|
103
|
+
mustBeAdded(studio, record);
|
|
104
|
+
return { studio, review, client: record };
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* The studio a linked project's command acts on, once the studio agrees with
|
|
108
|
+
* the link: the studio of the link's id, its own database, the client live
|
|
109
|
+
* and finished being added there, and the project one of the client's.
|
|
110
|
+
* Nothing is written.
|
|
111
|
+
*/
|
|
112
|
+
export async function openLinked(deps, link) {
|
|
113
|
+
const found = await inStudio(deps, link.studio, link.id, link.client);
|
|
114
|
+
const scope = { client: link.client, project: link.project };
|
|
115
|
+
const project = await found.review.projectOf(scope);
|
|
116
|
+
if (!project)
|
|
117
|
+
throw new CliError(`Studio ${link.studio} has no project ${link.project} for client ${link.client}; nothing was changed.`, {
|
|
118
|
+
code: 'project_unknown',
|
|
119
|
+
fix: `design-hub project link --studio ${link.studio} --client ${link.client} --project ${link.project} makes it.`,
|
|
120
|
+
});
|
|
121
|
+
return { ...found, link, project, scope };
|
|
122
|
+
}
|
|
123
|
+
/** The address a project is sent at: its client's own host and its path, never a deployment's. */
|
|
124
|
+
export const addressOf = (client, project) => `https://${client.host}/${project}/`;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* served: every nameserver that answered has the host. missing: one says it
|
|
3
|
+
* has no such name (or nothing at it). unreachable: none answered, or the
|
|
4
|
+
* zone's nameservers could not be found.
|
|
5
|
+
*/
|
|
6
|
+
export type Served = {
|
|
7
|
+
state: 'served' | 'missing' | 'unreachable';
|
|
8
|
+
/** The zone the host is in, as found above the apex. */
|
|
9
|
+
zone: string | null;
|
|
10
|
+
/** The zone's negative-cache seconds (its SOA minimum), or null when it gave none. */
|
|
11
|
+
negative: number | null;
|
|
12
|
+
};
|
|
13
|
+
export interface Names {
|
|
14
|
+
/** Waits, up to about a minute, until every nameserver of the apex's zone serves the host; never asks the system resolver for the host. */
|
|
15
|
+
served(host: string, apex: string): Promise<Served>;
|
|
16
|
+
/** Whether this machine's own resolver finds the host, as an HTTPS request would. Asked only once it is served. */
|
|
17
|
+
seen(host: string): Promise<boolean>;
|
|
18
|
+
}
|
|
19
|
+
/** The parts of node's DNS resolver used here: the system's with no servers, else only the servers named. */
|
|
20
|
+
export type Ask = {
|
|
21
|
+
resolveNs(name: string): Promise<string[]>;
|
|
22
|
+
resolve4(name: string): Promise<string[]>;
|
|
23
|
+
resolveCname(name: string): Promise<string[]>;
|
|
24
|
+
resolveSoa(name: string): Promise<{
|
|
25
|
+
minttl: number;
|
|
26
|
+
}>;
|
|
27
|
+
};
|
|
28
|
+
type Options = {
|
|
29
|
+
ask?: (servers?: string[]) => Ask;
|
|
30
|
+
lookup?: (host: string) => Promise<unknown>;
|
|
31
|
+
wait?: (ms: number) => Promise<unknown>;
|
|
32
|
+
};
|
|
33
|
+
/** The DNS adapter: the zone's nameservers asked directly, then the system resolver once. */
|
|
34
|
+
export declare function authoritativeNames({ ask, lookup, wait, }?: Options): Names;
|
|
35
|
+
export {};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
// Whether a client's new host is served by its DNS yet, found without
|
|
3
|
+
// poisoning any cache. A recursive resolver asked for a name in the seconds
|
|
4
|
+
// before its record is served keeps "no such name" for the zone's
|
|
5
|
+
// negative-cache time (its SOA minimum: 1800 seconds on the zone of the first
|
|
6
|
+
// real client add), and this machine, with everyone else behind that
|
|
7
|
+
// resolver, cannot open the host until it runs out. So the host is first
|
|
8
|
+
// asked only of the zone's own nameservers, which never cache, directly; the
|
|
9
|
+
// system resolver is asked for the apex's nameservers and their addresses,
|
|
10
|
+
// never for the host, until every one of them serves it. Only then does the
|
|
11
|
+
// command look the host up through the system, once, and ask it for its proof.
|
|
12
|
+
import { promises as dns } from 'node:dns';
|
|
13
|
+
const TRIES = 20;
|
|
14
|
+
const GAP_MS = 3000;
|
|
15
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
16
|
+
function nodeAsk(servers) {
|
|
17
|
+
const r = new dns.Resolver({ timeout: 2000, tries: 2 });
|
|
18
|
+
if (servers)
|
|
19
|
+
r.setServers(servers);
|
|
20
|
+
return r;
|
|
21
|
+
}
|
|
22
|
+
const code = (error) => error?.code;
|
|
23
|
+
/** No such name, or nothing at it: a nameserver's own answer, which it never caches. */
|
|
24
|
+
const NO_NAME = new Set(['ENOTFOUND', 'ENODATA']);
|
|
25
|
+
/** One nameserver's answer for the host: a CNAME (per-host, manual) or an address (a wildcard on Vercel DNS). */
|
|
26
|
+
async function answer(ask, host) {
|
|
27
|
+
const asked = await Promise.allSettled([ask.resolveCname(host), ask.resolve4(host)]);
|
|
28
|
+
if (asked.some((a) => a.status === 'fulfilled' && a.value.length > 0))
|
|
29
|
+
return 'served';
|
|
30
|
+
return asked.some((a) => a.status === 'rejected' && NO_NAME.has(code(a.reason) ?? ''))
|
|
31
|
+
? 'missing'
|
|
32
|
+
: 'silent';
|
|
33
|
+
}
|
|
34
|
+
/** The zone at or above the apex and its nameservers' addresses, from the system resolver, which is never asked for the host. */
|
|
35
|
+
async function zoneOf(system, apex) {
|
|
36
|
+
for (let name = apex; name.includes('.'); name = name.slice(name.indexOf('.') + 1)) {
|
|
37
|
+
const ns = await system.resolveNs(name).catch(() => []);
|
|
38
|
+
if (!ns.length)
|
|
39
|
+
continue;
|
|
40
|
+
const found = await Promise.all(ns.map((n) => system.resolve4(n).catch(() => [])));
|
|
41
|
+
const servers = [...new Set(found.flat())];
|
|
42
|
+
return servers.length ? { zone: name, servers } : null;
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
/** The DNS adapter: the zone's nameservers asked directly, then the system resolver once. */
|
|
47
|
+
export function authoritativeNames({ ask = nodeAsk, lookup = (host) => dns.lookup(host), wait = sleep, } = {}) {
|
|
48
|
+
return {
|
|
49
|
+
async served(host, apex) {
|
|
50
|
+
const found = await zoneOf(ask(), apex);
|
|
51
|
+
if (!found)
|
|
52
|
+
return { state: 'unreachable', zone: null, negative: null };
|
|
53
|
+
const each = found.servers.map((s) => ask([s]));
|
|
54
|
+
const negative = await each[0]
|
|
55
|
+
.resolveSoa(found.zone)
|
|
56
|
+
.then((soa) => soa.minttl)
|
|
57
|
+
.catch(() => null);
|
|
58
|
+
let state = 'unreachable';
|
|
59
|
+
for (let i = 0; i < TRIES; i++) {
|
|
60
|
+
if (i)
|
|
61
|
+
await wait(GAP_MS);
|
|
62
|
+
const answers = await Promise.all(each.map((a) => answer(a, host)));
|
|
63
|
+
state = answers.includes('missing')
|
|
64
|
+
? 'missing'
|
|
65
|
+
: answers.includes('served')
|
|
66
|
+
? 'served'
|
|
67
|
+
: 'unreachable';
|
|
68
|
+
if (state === 'served')
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
return { state, zone: found.zone, negative };
|
|
72
|
+
},
|
|
73
|
+
async seen(host) {
|
|
74
|
+
return lookup(host).then(() => true, () => false);
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|