@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,401 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
// A studio: one hosting team, one review store, one file store, one secret
|
|
4
|
+
// and one apex domain, serving every client of the studio from one site.
|
|
5
|
+
//
|
|
6
|
+
// studio create scaffolds the studio folder from templates/studio/,
|
|
7
|
+
// installs and builds it here, sets it up on its team,
|
|
8
|
+
// deploys it, and lists it on this machine
|
|
9
|
+
// studio deploy installs and builds the folder here and deploys it again (a
|
|
10
|
+
// package upgrade reaches the site this way), then puts every
|
|
11
|
+
// older deployment out of reach
|
|
12
|
+
// studio link lists a studio folder that already exists on this machine
|
|
13
|
+
// and links it to the studio's project on its team
|
|
14
|
+
//
|
|
15
|
+
// Every step checks before it acts, so a command stopped part way is safe to
|
|
16
|
+
// run again. Secrets go only to the folder's gitignored .env and the site's
|
|
17
|
+
// env; nothing printed here holds one.
|
|
18
|
+
import { randomBytes } from 'node:crypto';
|
|
19
|
+
import { copyFileSync, cpSync, existsSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync, } from 'node:fs';
|
|
20
|
+
import { basename, join, resolve } from 'node:path';
|
|
21
|
+
import { readEnv, removeEnv, writeEnv } from '../server/env.js';
|
|
22
|
+
import { localStudio } from '../server/local.js';
|
|
23
|
+
import { claimDatabase, MarkError } from '../server/mark.js';
|
|
24
|
+
import { PACKAGE, TEMPLATES } from '../server/paths.js';
|
|
25
|
+
import { CliError } from './lib/cli.js';
|
|
26
|
+
import { keepingGitignore } from './lib/deploy.js';
|
|
27
|
+
import { deployAlone } from './lib/host.js';
|
|
28
|
+
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';
|
|
30
|
+
export const spec = {
|
|
31
|
+
name: 'studio',
|
|
32
|
+
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.',
|
|
33
|
+
positionals: [
|
|
34
|
+
{
|
|
35
|
+
name: 'action',
|
|
36
|
+
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.',
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
flags: {
|
|
41
|
+
dir: { type: 'string', description: 'For create and link: the studio folder.' },
|
|
42
|
+
slug: { type: 'string', description: 'For create: the studio’s slug, kebab-case.' },
|
|
43
|
+
apex: {
|
|
44
|
+
type: 'string',
|
|
45
|
+
description: 'For create: the apex domain clients are hosted under, such as example.com.',
|
|
46
|
+
},
|
|
47
|
+
name: {
|
|
48
|
+
type: 'string',
|
|
49
|
+
description: 'For create: the studio’s name, shown beside its people on comments.',
|
|
50
|
+
},
|
|
51
|
+
owner: {
|
|
52
|
+
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.',
|
|
54
|
+
},
|
|
55
|
+
team: {
|
|
56
|
+
type: 'string',
|
|
57
|
+
description: 'For create: the Vercel team (its slug) the studio lives in. Required: the CLI’s current team is never used.',
|
|
58
|
+
},
|
|
59
|
+
project: { type: 'string', description: 'For create: the Vercel project name. Absent, the slug.' },
|
|
60
|
+
dns: {
|
|
61
|
+
type: 'string',
|
|
62
|
+
choices: DNS_PROVIDERS,
|
|
63
|
+
description: 'For create: who serves DNS for the apex. vercel: one wildcard for every client (only *.<apex> reaches certificate logs). cloudflare: a record per client by its API (each host is public in certificate logs). manual: a record per client, printed for you to add.',
|
|
64
|
+
},
|
|
65
|
+
zone: {
|
|
66
|
+
type: 'string',
|
|
67
|
+
description: 'For create with --dns cloudflare: the zone id. Absent, it is found from the apex.',
|
|
68
|
+
},
|
|
69
|
+
'cloudflare-token-file': {
|
|
70
|
+
type: 'string',
|
|
71
|
+
description: 'For create and link with Cloudflare DNS: a file holding an API token with DNS edit on the apex’s zone. It is copied into the studio’s .env; never pass a token itself.',
|
|
72
|
+
},
|
|
73
|
+
region: {
|
|
74
|
+
type: 'string',
|
|
75
|
+
default: 'iad1',
|
|
76
|
+
description: 'For create: the file store’s region, the site’s function region.',
|
|
77
|
+
},
|
|
78
|
+
database: {
|
|
79
|
+
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.',
|
|
81
|
+
},
|
|
82
|
+
package: {
|
|
83
|
+
type: 'string',
|
|
84
|
+
description: 'For create and deploy: a .tgz of @milesroxas/design-hub (pnpm pack in a checkout), copied into the studio folder and installed in place of the package it named, even one packed again at the same version. Without it, deploy installs what the folder’s package.json names: to move to a newer published version, pnpm add @milesroxas/design-hub@<version> in the folder first.',
|
|
85
|
+
},
|
|
86
|
+
studio: {
|
|
87
|
+
type: 'string',
|
|
88
|
+
description: 'For deploy: the studio’s slug. Absent, the studio folder this runs in.',
|
|
89
|
+
},
|
|
90
|
+
},
|
|
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 }',
|
|
92
|
+
};
|
|
93
|
+
const need = (value, flag, action) => {
|
|
94
|
+
if (!value)
|
|
95
|
+
throw new CliError(`--${flag} is required for studio ${action}.`, { fix: 'design-hub studio --help' });
|
|
96
|
+
return value;
|
|
97
|
+
};
|
|
98
|
+
/** The settings `studio create` is asked for, every problem named at once. */
|
|
99
|
+
function configFrom(flags) {
|
|
100
|
+
const provider = need(flags.dns, 'dns', 'create');
|
|
101
|
+
const config = {
|
|
102
|
+
id: randomBytes(16).toString('hex'),
|
|
103
|
+
slug: need(flags.slug, 'slug', 'create'),
|
|
104
|
+
apex: need(flags.apex, 'apex', 'create').toLowerCase(),
|
|
105
|
+
name: need(flags.name, 'name', 'create').trim(),
|
|
106
|
+
vercel: { team: need(flags.team, 'team', 'create'), project: flags.project ?? flags.slug ?? '' },
|
|
107
|
+
dns: { provider, mode: MODE_OF[provider], zone: flags.zone ?? null },
|
|
108
|
+
owner: null,
|
|
109
|
+
providers: { kv: 'upstash', files: 'vercel-blob', host: 'vercel' },
|
|
110
|
+
};
|
|
111
|
+
need(flags.owner, 'owner', 'create');
|
|
112
|
+
const problems = studioProblems(config);
|
|
113
|
+
if (problems.length)
|
|
114
|
+
throw new CliError('Some flags are not valid.', { problems });
|
|
115
|
+
if (provider === 'cloudflare')
|
|
116
|
+
need(flags['cloudflare-token-file'], 'cloudflare-token-file', 'create');
|
|
117
|
+
if (provider !== 'cloudflare' && (flags.zone || flags['cloudflare-token-file']))
|
|
118
|
+
throw new CliError('--zone and --cloudflare-token-file are for --dns cloudflare.');
|
|
119
|
+
return config;
|
|
120
|
+
}
|
|
121
|
+
/** The settings a rerun must agree with: the same studio on the same team, apex and DNS. */
|
|
122
|
+
function sameStudio(kept, asked) {
|
|
123
|
+
const pairs = [
|
|
124
|
+
['slug', kept.slug, asked.slug],
|
|
125
|
+
['apex', kept.apex, asked.apex],
|
|
126
|
+
['team', kept.vercel.team, asked.vercel.team],
|
|
127
|
+
['project', kept.vercel.project, asked.vercel.project],
|
|
128
|
+
['dns', kept.dns.provider, asked.dns.provider],
|
|
129
|
+
];
|
|
130
|
+
const differs = pairs.filter(([, a, b]) => a !== b).map(([flag]) => flag);
|
|
131
|
+
if (differs.length)
|
|
132
|
+
throw new CliError(`${STUDIO_FILE} in this folder is another studio's.`, {
|
|
133
|
+
differs,
|
|
134
|
+
fix: 'Pick an empty --dir for a new studio; run it again with the same flags to finish this one.',
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
const HUB = '@milesroxas/design-hub';
|
|
138
|
+
const LOCKFILE = 'pnpm-lock.yaml';
|
|
139
|
+
const manifestOf = (dir) => JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'));
|
|
140
|
+
/** The --package file, checked before anything is written. */
|
|
141
|
+
function packFile(tgz) {
|
|
142
|
+
if (tgz === undefined)
|
|
143
|
+
return undefined;
|
|
144
|
+
if (!tgz.endsWith('.tgz') || !existsSync(tgz))
|
|
145
|
+
throw new CliError('--package takes a .tgz file that exists.', { got: tgz });
|
|
146
|
+
return resolve(tgz);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Names the package the studio site installs in its package.json: this one's
|
|
150
|
+
* published version, or a packed .tgz copied beside it. A pack the folder
|
|
151
|
+
* named before under another file name is removed, so the deploy uploads one.
|
|
152
|
+
*/
|
|
153
|
+
function namePackage(dir, tgz) {
|
|
154
|
+
const pkg = manifestOf(dir);
|
|
155
|
+
const before = pkg.dependencies[HUB] ?? '';
|
|
156
|
+
let spec = JSON.parse(readFileSync(join(PACKAGE, 'package.json'), 'utf8')).version;
|
|
157
|
+
if (tgz) {
|
|
158
|
+
const copy = join(dir, basename(tgz));
|
|
159
|
+
if (copy !== tgz)
|
|
160
|
+
copyFileSync(tgz, copy);
|
|
161
|
+
spec = `file:${basename(tgz)}`;
|
|
162
|
+
}
|
|
163
|
+
pkg.dependencies[HUB] = spec;
|
|
164
|
+
writeFileSync(join(dir, 'package.json'), `${JSON.stringify(pkg, null, 2)}\n`);
|
|
165
|
+
const old = before.match(/^file:([\w.-]+\.tgz)$/)?.[1];
|
|
166
|
+
if (old && `file:${old}` !== spec)
|
|
167
|
+
rmSync(join(dir, old), { force: true });
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Installs and builds the studio folder here, before any deploy, and returns
|
|
171
|
+
* the package it installed. The install writes pnpm-lock.yaml, which Vercel's
|
|
172
|
+
* install needs: without one, Vercel runs pnpm 6, which fails on the Node it
|
|
173
|
+
* builds with (ERR_INVALID_THIS); with one, it takes pnpm from the lockfile's
|
|
174
|
+
* version. It also brings the lockfile in step with package.json, so the
|
|
175
|
+
* site installs what the folder names. A packed package is updated as well:
|
|
176
|
+
* packed again at the same version it keeps its file name, and the lockfile
|
|
177
|
+
* would keep the old pack's hash. The build is the one Vercel runs next.
|
|
178
|
+
*/
|
|
179
|
+
function installAndBuild(dir, deps) {
|
|
180
|
+
const spec = manifestOf(dir).dependencies[HUB] ?? '';
|
|
181
|
+
const steps = [
|
|
182
|
+
['install', '--no-frozen-lockfile'],
|
|
183
|
+
...(spec.startsWith('file:') ? [['update', HUB]] : []),
|
|
184
|
+
['run', 'build'],
|
|
185
|
+
];
|
|
186
|
+
for (const args of steps) {
|
|
187
|
+
const step = `pnpm ${args.join(' ')}`;
|
|
188
|
+
const out = deps.pnpm(dir, args);
|
|
189
|
+
if (!out)
|
|
190
|
+
throw new CliError('pnpm is not installed; nothing was deployed.', {
|
|
191
|
+
fix: 'npm i -g pnpm, then run this again.',
|
|
192
|
+
});
|
|
193
|
+
if (out.status !== 0)
|
|
194
|
+
throw new CliError(`${step} failed in the studio folder; nothing was deployed.`, {
|
|
195
|
+
dir,
|
|
196
|
+
output: `${out.stdout}\n${out.stderr}`.trim().slice(-600),
|
|
197
|
+
fix: `Run ${step} in ${dir} and fix what it names (most often the @milesroxas/design-hub version in package.json, or the network), then run this again.`,
|
|
198
|
+
});
|
|
199
|
+
if (args[0] === 'install' && !existsSync(join(dir, LOCKFILE)))
|
|
200
|
+
throw new CliError(`pnpm install wrote no ${LOCKFILE}; nothing was deployed.`, {
|
|
201
|
+
dir,
|
|
202
|
+
fix: `Vercel needs ${LOCKFILE} to pick its pnpm. Remove lockfile=false from your pnpm or npm config, then run this again.`,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
return spec;
|
|
206
|
+
}
|
|
207
|
+
/** The studio folder from the template, or the one already there when it is this studio's. */
|
|
208
|
+
function scaffold(dir, config, tgz) {
|
|
209
|
+
if (existsSync(join(dir, STUDIO_FILE))) {
|
|
210
|
+
const kept = readStudio(dir);
|
|
211
|
+
sameStudio(kept, config);
|
|
212
|
+
// A folder made before studios had ids gets one now; one already there never changes.
|
|
213
|
+
if (!kept.id)
|
|
214
|
+
writeStudio(dir, Object.assign(kept, { id: config.id }));
|
|
215
|
+
return { folder: 'kept', config: kept };
|
|
216
|
+
}
|
|
217
|
+
if (existsSync(dir) && readdirSync(dir).length)
|
|
218
|
+
throw new CliError(`${dir} is not empty and holds no ${STUDIO_FILE}.`, {
|
|
219
|
+
fix: 'Pick an empty or new --dir.',
|
|
220
|
+
});
|
|
221
|
+
cpSync(join(TEMPLATES, 'studio'), dir, { recursive: true });
|
|
222
|
+
renameSync(join(dir, '_gitignore'), join(dir, '.gitignore'));
|
|
223
|
+
namePackage(dir, tgz);
|
|
224
|
+
writeStudio(dir, config);
|
|
225
|
+
return { folder: 'scaffolded', config };
|
|
226
|
+
}
|
|
227
|
+
/** A token read from a file, so it never shows in a process list or a shell's history. */
|
|
228
|
+
function tokenFrom(file) {
|
|
229
|
+
if (!file)
|
|
230
|
+
return null;
|
|
231
|
+
let token;
|
|
232
|
+
try {
|
|
233
|
+
token = readFileSync(resolve(file), 'utf8').trim();
|
|
234
|
+
}
|
|
235
|
+
catch {
|
|
236
|
+
throw new CliError('--cloudflare-token-file could not be read.', { got: file });
|
|
237
|
+
}
|
|
238
|
+
if (!/^[\w-]{20,200}$/.test(token))
|
|
239
|
+
throw new CliError('--cloudflare-token-file does not hold one API token on one line.', { got: file });
|
|
240
|
+
return token;
|
|
241
|
+
}
|
|
242
|
+
/** The store and file credentials the CLI needs, read back from Production into .env where missing. */
|
|
243
|
+
async function pullMissing(dir, host) {
|
|
244
|
+
const env = readEnv(dir);
|
|
245
|
+
const missing = [...STORE_NAMES, BLOB_NAME].filter((n) => !env[n]);
|
|
246
|
+
if (!missing.length)
|
|
247
|
+
return [];
|
|
248
|
+
const values = await host.pullEnv(missing);
|
|
249
|
+
writeEnv(dir, values);
|
|
250
|
+
const still = missing.filter((n) => !values[n]);
|
|
251
|
+
if (still.length)
|
|
252
|
+
throw new CliError(`Could not read ${still.join(', ')} back from the studio's Production env.`, {
|
|
253
|
+
fix: 'Copy them from the store’s page on Vercel into the studio’s .env, then run this again.',
|
|
254
|
+
});
|
|
255
|
+
return Object.keys(values);
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* The gate's signing secret: the one in .env, else a new one, set on the
|
|
259
|
+
* site's Production env once. A site that has one while the folder does not
|
|
260
|
+
* is refused: a sensitive value cannot be read back, and a new one in the
|
|
261
|
+
* folder alone would sign links the site never accepts.
|
|
262
|
+
*/
|
|
263
|
+
async function gateSecret(dir, host) {
|
|
264
|
+
const current = readEnv(dir).GATE_SECRET ?? '';
|
|
265
|
+
if (current.length >= 32)
|
|
266
|
+
return host.setSecret('GATE_SECRET', current);
|
|
267
|
+
if (await host.hasEnv('GATE_SECRET'))
|
|
268
|
+
throw new CliError("The studio site has a GATE_SECRET and this folder's .env has none; nothing was changed.", {
|
|
269
|
+
fix: "Put the site's GATE_SECRET in the studio's .env, from wherever it was kept, then run this again. If it is lost: remove GATE_SECRET from the studio project's Production env on Vercel and run this again; the new one signs every person out.",
|
|
270
|
+
});
|
|
271
|
+
const secret = randomBytes(32).toString('base64url');
|
|
272
|
+
writeEnv(dir, { GATE_SECRET: secret });
|
|
273
|
+
return host.setSecret('GATE_SECRET', secret);
|
|
274
|
+
}
|
|
275
|
+
/** The wildcard every client's host falls under, on Vercel DNS; refused when Vercel cannot issue it. */
|
|
276
|
+
async function wildcard(host, apex) {
|
|
277
|
+
const name = `*.${apex}`;
|
|
278
|
+
const state = await host.addDomain(name);
|
|
279
|
+
if (!state.verified)
|
|
280
|
+
throw new CliError(`Vercel cannot serve ${name} yet.`, {
|
|
281
|
+
records: state.verification.map((v) => `${v.type} ${v.domain} ${v.value}`),
|
|
282
|
+
fix: `A wildcard needs ${apex} on Vercel's nameservers (ns1.vercel-dns.com, ns2.vercel-dns.com). Move it there, or use --dns cloudflare or manual, then run this again.`,
|
|
283
|
+
});
|
|
284
|
+
return 'added';
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Marks the connected database as this studio's (server/mark.ts). One that
|
|
288
|
+
* is not this studio's is let go before the refusal: its credentials leave
|
|
289
|
+
* the folder's .env, and, when this run connected it, it leaves the studio's
|
|
290
|
+
* project, so nothing of the other studio's is kept here and a rerun with a
|
|
291
|
+
* database of its own starts clean.
|
|
292
|
+
*/
|
|
293
|
+
async function claimOrLetGo(deps, host, studio, connected) {
|
|
294
|
+
try {
|
|
295
|
+
return await claimDatabase(deps.store(studio), studio);
|
|
296
|
+
}
|
|
297
|
+
catch (error) {
|
|
298
|
+
if (!(error instanceof MarkError))
|
|
299
|
+
throw error;
|
|
300
|
+
removeEnv(studio.dir, STORE_NAMES);
|
|
301
|
+
const gone = connected !== null && (await host.disconnectDatabase(connected));
|
|
302
|
+
const { dir, config } = studio;
|
|
303
|
+
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
|
+
throw new CliError(error.message, {
|
|
305
|
+
...error.detail,
|
|
306
|
+
fix: gone
|
|
307
|
+
? `It was disconnected from project ${config.vercel.project} and its credentials taken out of ${join(dir, '.env')}. A database serves one studio only: ${rerun}`
|
|
308
|
+
: `Its credentials were taken out of ${join(dir, '.env')}. A database serves one studio only; never connect one another studio uses. On team ${config.vercel.team}'s Storage page on Vercel, disconnect it from project ${config.vercel.project}, ${rerun}`,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
async function create(flags, deps) {
|
|
313
|
+
const asked = configFrom(flags);
|
|
314
|
+
const owner = need(flags.owner, 'owner', 'create').trim();
|
|
315
|
+
const dir = resolve(need(flags.dir, 'dir', 'create'));
|
|
316
|
+
const token = tokenFrom(flags['cloudflare-token-file']);
|
|
317
|
+
const { folder, config } = scaffold(dir, asked, packFile(flags.package));
|
|
318
|
+
const list = addToList(deps.home, config.slug, dir);
|
|
319
|
+
if (token)
|
|
320
|
+
writeEnv(dir, { [CLOUDFLARE_NAME]: token });
|
|
321
|
+
// Here first, before anything on the team: a folder that cannot install or build is never set up or deployed.
|
|
322
|
+
const pkg = installAndBuild(dir, deps);
|
|
323
|
+
const host = deps.host(dir, config);
|
|
324
|
+
const link = await keepingGitignore(dir, () => host.link(true));
|
|
325
|
+
const env = {
|
|
326
|
+
GATE_SECRET: await gateSecret(dir, host),
|
|
327
|
+
database: await host.provisionDatabase(flags.database),
|
|
328
|
+
files: await host.provisionFiles(flags.region),
|
|
329
|
+
pulled: await pullMissing(dir, host),
|
|
330
|
+
};
|
|
331
|
+
const studio = { dir, config };
|
|
332
|
+
// Before anything is written to the database: it serves this studio only, or nothing goes on.
|
|
333
|
+
const connected = env.database === 'connected' ? (flags.database ?? null) : null;
|
|
334
|
+
const mark = await claimOrLetGo(deps, host, studio, connected);
|
|
335
|
+
const dns = { provider: config.dns.provider, mode: config.dns.mode };
|
|
336
|
+
if (config.dns.mode === 'wildcard')
|
|
337
|
+
dns.wildcard = await wildcard(host, config.apex);
|
|
338
|
+
if (!config.owner) {
|
|
339
|
+
const review = await openReview(deps, studio);
|
|
340
|
+
const person = await review.addStudioPerson(localStudio('cli', 'CLI'), {
|
|
341
|
+
name: owner,
|
|
342
|
+
team: config.name,
|
|
343
|
+
});
|
|
344
|
+
config.owner = person.id;
|
|
345
|
+
writeStudio(dir, config);
|
|
346
|
+
}
|
|
347
|
+
const deployed = await deployAlone(host);
|
|
348
|
+
return {
|
|
349
|
+
studio: config.slug,
|
|
350
|
+
dir,
|
|
351
|
+
folder,
|
|
352
|
+
list,
|
|
353
|
+
package: pkg,
|
|
354
|
+
vercel: { team: config.vercel.team, project: config.vercel.project, link },
|
|
355
|
+
env,
|
|
356
|
+
mark,
|
|
357
|
+
dns,
|
|
358
|
+
owner: config.owner,
|
|
359
|
+
...deployed,
|
|
360
|
+
next: `design-hub client add <client> --studio ${config.slug} --name "<client name>" --person "<their name>" --team "<their company>"`,
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
async function deploy(flags, deps) {
|
|
364
|
+
const { dir, config } = findStudio(deps.home, flags.studio, deps.cwd);
|
|
365
|
+
const tgz = packFile(flags.package);
|
|
366
|
+
if (tgz)
|
|
367
|
+
namePackage(dir, tgz);
|
|
368
|
+
const pkg = installAndBuild(dir, deps);
|
|
369
|
+
return { studio: config.slug, package: pkg, ...(await deployAlone(deps.host(dir, config))) };
|
|
370
|
+
}
|
|
371
|
+
async function link(flags, deps) {
|
|
372
|
+
const dir = resolve(need(flags.dir, 'dir', 'link'));
|
|
373
|
+
const config = readStudio(dir);
|
|
374
|
+
const list = addToList(deps.home, config.slug, dir);
|
|
375
|
+
const token = tokenFrom(flags['cloudflare-token-file']);
|
|
376
|
+
if (token)
|
|
377
|
+
writeEnv(dir, { [CLOUDFLARE_NAME]: token });
|
|
378
|
+
// Linked every time, credentials or not: `.vercel/` is gitignored, so a copied
|
|
379
|
+
// folder can hold a full .env and no link, and a deploy follows the link.
|
|
380
|
+
const host = deps.host(dir, config);
|
|
381
|
+
await keepingGitignore(dir, () => host.link(false));
|
|
382
|
+
const pulled = await pullMissing(dir, host);
|
|
383
|
+
const warnings = config.dns.provider === 'cloudflare' && !readEnv(dir)[CLOUDFLARE_NAME]
|
|
384
|
+
? [
|
|
385
|
+
`No ${CLOUDFLARE_NAME} in .env: client add needs it. Run this again with --cloudflare-token-file <file>.`,
|
|
386
|
+
]
|
|
387
|
+
: [];
|
|
388
|
+
return { studio: config.slug, dir, list, env: { pulled }, warnings };
|
|
389
|
+
}
|
|
390
|
+
const ACTIONS = {
|
|
391
|
+
create,
|
|
392
|
+
deploy,
|
|
393
|
+
link,
|
|
394
|
+
};
|
|
395
|
+
export async function run({ positionals: [action], flags }, deps) {
|
|
396
|
+
const act = Object.hasOwn(ACTIONS, action) ? ACTIONS[action] : undefined;
|
|
397
|
+
if (!act)
|
|
398
|
+
throw new CliError(`Unknown action ${action}.`, { fix: 'create, deploy or link' });
|
|
399
|
+
return act(flags, deps);
|
|
400
|
+
}
|
|
401
|
+
export const main = (args) => run(args, realDeps());
|
|
@@ -10,10 +10,16 @@ export declare const spec: {
|
|
|
10
10
|
readonly description: 'Only report whether a newer version exists.';
|
|
11
11
|
};
|
|
12
12
|
};
|
|
13
|
-
readonly output: '{ ok, installed, latest, decision, outcome? }';
|
|
13
|
+
readonly output: '{ ok, installed, latest, decision, outcome?, error?, fix? }; ok is false, with error and fix, while the project still has its own deployment files. In a studio folder: { ok: false, code: studio_folder, error, fix }, with nothing changed.';
|
|
14
14
|
};
|
|
15
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* SessionStart: says nothing unless something changed or is waiting, or the
|
|
17
|
+
* project still has its own deployment files, which it says every session
|
|
18
|
+
* until they are gone. In a studio folder it does nothing and says nothing:
|
|
19
|
+
* a studio upgrades by `studio deploy`, never by this hook.
|
|
20
|
+
*/
|
|
16
21
|
export declare function upgradeHook(): Promise<void>;
|
|
22
|
+
/** Says what `skills` and the session hook say while the project still has its own deployment files, and fails the same way. */
|
|
17
23
|
export declare function main({ flags }: Args<typeof spec>): Promise<{
|
|
18
24
|
systemMessage?: string | undefined;
|
|
19
25
|
additionalContext?: string | undefined;
|
|
@@ -22,4 +28,6 @@ export declare function main({ flags }: Args<typeof spec>): Promise<{
|
|
|
22
28
|
latest: string | null;
|
|
23
29
|
decision: import("./lib/update.ts").Decision;
|
|
24
30
|
outcome?: Outcome | undefined;
|
|
31
|
+
error?: string | undefined;
|
|
32
|
+
fix?: string | undefined;
|
|
25
33
|
}>;
|
package/dist/scripts/upgrade.js
CHANGED
|
@@ -14,14 +14,16 @@ import { homedir } from 'node:os';
|
|
|
14
14
|
import { join } from 'node:path';
|
|
15
15
|
import { readEnv } from '../server/env.js';
|
|
16
16
|
import { ROOT } from '../server/paths.js';
|
|
17
|
-
import {
|
|
17
|
+
import { isStudioFolder } from './lib/studios.js';
|
|
18
|
+
import { cachedLatest, decide, ownDeployment, PACKAGE_NAME, report, } from './lib/update.js';
|
|
19
|
+
import { notInStudioFolder, ownDeploymentIn } from './skills.js';
|
|
18
20
|
export const spec = {
|
|
19
21
|
name: 'upgrade',
|
|
20
22
|
summary: 'Upgrade this project to the newest Design Hub and refresh its skills, scripts and hooks. The SessionStart hook runs it on every new session.',
|
|
21
23
|
flags: {
|
|
22
24
|
check: { type: 'boolean', description: 'Only report whether a newer version exists.' },
|
|
23
25
|
},
|
|
24
|
-
output: '{ ok, installed, latest, decision, outcome? }',
|
|
26
|
+
output: '{ ok, installed, latest, decision, outcome?, error?, fix? }; ok is false, with error and fix, while the project still has its own deployment files. In a studio folder: { ok: false, code: studio_folder, error, fix }, with nothing changed.',
|
|
25
27
|
};
|
|
26
28
|
const LOCK_STALE_MS = 10 * 60_000;
|
|
27
29
|
const INSTALL_TIMEOUT_MS = 150_000;
|
|
@@ -108,13 +110,20 @@ function run(cmd, argv) {
|
|
|
108
110
|
return `${cmd} ${argv[0]} exited ${out.status}: ${(out.stderr || out.stdout).trim().slice(-400)}`;
|
|
109
111
|
return null;
|
|
110
112
|
}
|
|
111
|
-
/**
|
|
113
|
+
/**
|
|
114
|
+
* Installs `to` and runs that version's own `skills`, so the newest skills,
|
|
115
|
+
* scripts and hooks land. A `skills` that installed everything and stopped
|
|
116
|
+
* only on the project's own deployment files is no failed upgrade: the
|
|
117
|
+
* caller says what is left (ownDeployment).
|
|
118
|
+
*/
|
|
112
119
|
function install(from, to) {
|
|
113
120
|
const before = dirtyPaths();
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
121
|
+
const added = run('pnpm', ['add', `${PACKAGE_NAME}@^${to}`]);
|
|
122
|
+
if (added)
|
|
123
|
+
return { kind: 'failed', from, to, error: added };
|
|
124
|
+
const skills = run(join(ROOT, 'node_modules', '.bin', 'design-hub'), ['skills']);
|
|
125
|
+
if (skills && !ownDeploymentIn().length)
|
|
126
|
+
return { kind: 'failed', from, to, error: skills };
|
|
118
127
|
const now = facts(to).installed;
|
|
119
128
|
if (now !== to)
|
|
120
129
|
return { kind: 'failed', from, to, error: `node_modules has ${now ?? 'nothing'} after the install` };
|
|
@@ -143,24 +152,45 @@ async function attempt(fresh, checkOnly) {
|
|
|
143
152
|
release();
|
|
144
153
|
}
|
|
145
154
|
}
|
|
146
|
-
/**
|
|
155
|
+
/** The version the project is on after an attempt. */
|
|
156
|
+
const nowOn = (f, outcome) => (outcome?.kind === 'upgraded' ? outcome.to : f.installed) ?? undefined;
|
|
157
|
+
/**
|
|
158
|
+
* SessionStart: says nothing unless something changed or is waiting, or the
|
|
159
|
+
* project still has its own deployment files, which it says every session
|
|
160
|
+
* until they are gone. In a studio folder it does nothing and says nothing:
|
|
161
|
+
* a studio upgrades by `studio deploy`, never by this hook.
|
|
162
|
+
*/
|
|
147
163
|
export async function upgradeHook() {
|
|
148
|
-
|
|
149
|
-
|
|
164
|
+
if (isStudioFolder(ROOT))
|
|
165
|
+
return;
|
|
166
|
+
const { f, outcome } = await attempt(false, false);
|
|
167
|
+
const left = ownDeploymentIn();
|
|
168
|
+
const said = [
|
|
169
|
+
outcome ? report(outcome) : null,
|
|
170
|
+
left.length ? ownDeployment(left, nowOn(f, outcome)) : null,
|
|
171
|
+
].filter((r) => r !== null);
|
|
172
|
+
if (!said.length)
|
|
150
173
|
return;
|
|
151
|
-
const { systemMessage, additionalContext } = report(outcome);
|
|
152
174
|
console.log(JSON.stringify({
|
|
153
|
-
systemMessage,
|
|
154
|
-
hookSpecificOutput: {
|
|
175
|
+
systemMessage: said.map((r) => r.systemMessage).join(' '),
|
|
176
|
+
hookSpecificOutput: {
|
|
177
|
+
hookEventName: 'SessionStart',
|
|
178
|
+
additionalContext: said.map((r) => r.additionalContext).join('\n\n'),
|
|
179
|
+
},
|
|
155
180
|
}));
|
|
156
181
|
}
|
|
182
|
+
/** Says what `skills` and the session hook say while the project still has its own deployment files, and fails the same way. */
|
|
157
183
|
export async function main({ flags }) {
|
|
184
|
+
notInStudioFolder();
|
|
158
185
|
const { f, decision, outcome } = await attempt(true, flags.check);
|
|
186
|
+
const left = ownDeploymentIn();
|
|
187
|
+
const own = left.length ? ownDeployment(left, nowOn(f, outcome)) : null;
|
|
159
188
|
return {
|
|
160
|
-
ok: outcome?.kind !== 'failed',
|
|
189
|
+
ok: outcome?.kind !== 'failed' && !own,
|
|
161
190
|
installed: f.installed,
|
|
162
191
|
latest: f.latest,
|
|
163
192
|
decision,
|
|
164
193
|
...(outcome ? { outcome, ...report(outcome) } : {}),
|
|
194
|
+
...(own ? { error: own.error, fix: own.fix } : {}),
|
|
165
195
|
};
|
|
166
196
|
}
|
package/dist/server/api.d.ts
CHANGED
|
@@ -1,26 +1,37 @@
|
|
|
1
1
|
import type { Checks } from '../core/checks.ts';
|
|
2
|
-
import type { Person } from '../core/review/model.ts';
|
|
2
|
+
import type { Person, Scope } from '../core/review/model.ts';
|
|
3
3
|
import { type Review } from '../core/review/service.ts';
|
|
4
|
-
import { type
|
|
4
|
+
import { type HostLookup } from './hosts.ts';
|
|
5
|
+
import { type Gate, type GateKeys } from './session.ts';
|
|
5
6
|
export declare const API_PATH = "/api/review";
|
|
7
|
+
/** One request: the service, who is signed in, and the scope every op runs in. */
|
|
6
8
|
export type ApiContext = {
|
|
7
9
|
review: Review;
|
|
8
10
|
actor: Person | null;
|
|
11
|
+
scope: Scope;
|
|
9
12
|
};
|
|
10
13
|
export declare function handleReview(request: Request, ctx: ApiContext): Promise<Response>;
|
|
11
|
-
/** The person a request's signed cookie names, while they may still sign in
|
|
12
|
-
export declare function actorFrom(request: Request, review: Review, gate: Gate): Promise<Person | null>;
|
|
14
|
+
/** The person a request's signed cookie names, while they may still sign in, in `scope`. */
|
|
15
|
+
export declare function actorFrom(request: Request, review: Review, gate: Gate, scope: Scope): Promise<Person | null>;
|
|
16
|
+
/** A studio site's review service and gate keys, or why it has none. */
|
|
13
17
|
export type Deployed = {
|
|
14
18
|
review: Review;
|
|
15
|
-
|
|
19
|
+
keys: GateKeys;
|
|
16
20
|
} | {
|
|
17
21
|
error: string;
|
|
18
22
|
};
|
|
19
23
|
/**
|
|
20
|
-
*
|
|
21
|
-
* its key is set and `checks` is given. `now` is the clock, so the gate
|
|
22
|
-
* the service it asks run on one (the gate's tests move it).
|
|
24
|
+
* A studio site's review service: Upstash for the store, Jev for triage
|
|
25
|
+
* when its key is set and `checks` is given. `now` is the clock, so the gate
|
|
26
|
+
* and the service it asks run on one (the gate's tests move it). Without the
|
|
27
|
+
* gate's secret or the store, the cause goes to the site's log and a visitor
|
|
28
|
+
* gets SITE_NOT_SET_UP.
|
|
23
29
|
*/
|
|
24
|
-
export declare function
|
|
25
|
-
/**
|
|
26
|
-
|
|
30
|
+
export declare function studioReview(env: Record<string, string | undefined>, checks?: Checks | null, now?: () => Date): Deployed;
|
|
31
|
+
/**
|
|
32
|
+
* A studio site's review function: the host's client is the scope, and the
|
|
33
|
+
* gate's client, of every op. The session is read before any project is
|
|
34
|
+
* looked up (S5), so a caller signed out, or signed in for another client,
|
|
35
|
+
* gets one answer whatever project the request names.
|
|
36
|
+
*/
|
|
37
|
+
export declare function studioHandler(request: Request, checks: Checks | null, hosts: HostLookup): Promise<Response>;
|