designsource 0.14.0 → 0.15.1

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 (2) hide show
  1. package/dist/index.js +469 -26
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1008,6 +1008,7 @@ var INTEGRATE_DEFAULT_FOLDER = ".";
1008
1008
  var USAGE = `designsource <command> [options] (also installed as: dss)
1009
1009
 
1010
1010
  Commands
1011
+ setup set up a design system in this folder: sign in, pick or make one, add the kit, set up your coding apps
1011
1012
  add [names\u2026] add your design system's UI kit to this React app (theme, components, libraries)
1012
1013
  connect connect your coding apps (Claude Code, Cursor, VS Code, Codex, Gemini CLI) to this design system
1013
1014
  login sign in for coding agents, so your agent can create design systems from this folder
@@ -1016,7 +1017,7 @@ Commands
1016
1017
  create [folder] new Next.js project with the tokens, every component and your mockup page (default folder: ${CREATE_DEFAULT_FOLDER})
1017
1018
  mcp serve the design system to a coding agent over MCP (stdio) \u2014 for .mcp.json, not for a terminal
1018
1019
 
1019
- Run a command with --help for its options. In a terminal, no command shows a menu.
1020
+ Run a command with --help for its options. In a terminal, no command runs setup; --menu shows the list of commands.
1020
1021
  `;
1021
1022
  var TOKEN_LINE = " --token the project token (workbench \u2192 Project settings); or DS_SYNC_TOKEN";
1022
1023
  var API_LINE = ` --api API origin (default ${DEFAULT_API_ORIGIN})`;
@@ -1095,6 +1096,17 @@ settings here. The settings hold no token, so they are safe to commit. Run it on
1095
1096
  --yes no questions: the apps found on this computer (also outside a terminal)
1096
1097
  ${API_LINE}; outside a terminal only the default or a server on this computer
1097
1098
  `;
1099
+ var SETUP_USAGE = `designsource setup [--api <origin>]
1100
+ designsource --menu
1101
+
1102
+ Sets up a design system in this folder, asking only what it cannot tell: signs you in (a code to enter at
1103
+ ${DEFAULT_API_ORIGIN}/device, once per computer), picks or makes the design system, adds the UI kit and the
1104
+ /design-system section to a React app (a design-system/ folder for other stacks, a new Next.js app in an empty
1105
+ folder) and connects your coding apps. Needs a terminal; scripts use add, connect or login. Typing npx designsource
1106
+ with no command runs it.
1107
+ --menu the list of commands instead
1108
+ ${API_LINE}
1109
+ `;
1098
1110
  var LOGOUT_USAGE = `designsource logout [--api <origin>]
1099
1111
 
1100
1112
  Revokes this computer's sign-in on the server and removes it, with its trusted folders, from this computer.
@@ -1104,6 +1116,7 @@ ${API_LINE}
1104
1116
  function usageFor(verb) {
1105
1117
  if (verb === "login") return LOGIN_USAGE;
1106
1118
  if (verb === "logout") return LOGOUT_USAGE;
1119
+ if (verb === "setup") return SETUP_USAGE;
1107
1120
  return verb === "add" ? ADD_USAGE : verb === "connect" ? CONNECT_USAGE : verb === "create" ? CREATE_USAGE : verb === "integrate" ? INTEGRATE_USAGE : verb === "mcp" ? MCP_USAGE : USAGE;
1108
1121
  }
1109
1122
  var ArgsError = class extends Error {
@@ -1119,11 +1132,16 @@ function baseArgs(env) {
1119
1132
  return args;
1120
1133
  }
1121
1134
  var HELP = /* @__PURE__ */ new Set(["--help", "-h", "help"]);
1122
- var USE_VERBS = "Use add, connect, login, logout, integrate, create or mcp.";
1135
+ var USE_VERBS = "Use setup, add, connect, login, logout, integrate, create or mcp.";
1123
1136
  function parseArgs(argv, env) {
1124
1137
  const [first, ...rest] = argv;
1125
- if (first === void 0) return { kind: "menu" };
1138
+ if (first === void 0) return { kind: "setup", args: { api: DEFAULT_API_ORIGIN, apiGiven: false }, bare: true };
1139
+ if (first === "--menu") {
1140
+ if (rest.length > 0) throw new ArgsError("--menu takes nothing after it", USAGE);
1141
+ return { kind: "menu" };
1142
+ }
1126
1143
  if (HELP.has(first)) return { kind: "help", verb: void 0 };
1144
+ if (first === "setup") return parseSetup(rest);
1127
1145
  if (first === "create" || first === "integrate") return parseVerb(first, rest, env);
1128
1146
  if (first === "mcp") return parseMcp(rest, env);
1129
1147
  if (first === "add") return parseAdd(rest, env);
@@ -1246,6 +1264,17 @@ function parseLogout(argv) {
1246
1264
  }
1247
1265
  return { kind: "logout", args };
1248
1266
  }
1267
+ function parseSetup(argv) {
1268
+ const args = { api: DEFAULT_API_ORIGIN, apiGiven: false };
1269
+ for (const arg of scan(argv, ["api"], [])) {
1270
+ if (arg.kind === "help") return { kind: "help", verb: "setup" };
1271
+ if (arg.kind === "positional") throw new ArgsError("setup takes no folder: run it in the app's folder", SETUP_USAGE);
1272
+ if (arg.known === null) throw unknownOption(arg, SETUP_USAGE);
1273
+ args.api = parseApiOrigin(valueOf(arg, SETUP_USAGE), SETUP_USAGE);
1274
+ args.apiGiven = true;
1275
+ }
1276
+ return { kind: "setup", args, bare: false };
1277
+ }
1249
1278
  function parseConnect(argv, env) {
1250
1279
  const args = { api: DEFAULT_API_ORIGIN, yes: false, forget: false };
1251
1280
  if (env.DS_SYNC_TOKEN) args.token = env.DS_SYNC_TOKEN;
@@ -5049,7 +5078,7 @@ function closingLines2(apps, changed, usesAccount) {
5049
5078
  COMMIT_SETTINGS_NOTE
5050
5079
  ];
5051
5080
  }
5052
- var plansFor = (root, apps) => apps.map((id) => ({ id, plan: planAppFile(root, id, null) }));
5081
+ var plansFor = (root, apps, projectId = null) => apps.map((id) => ({ id, plan: planAppFile(root, id, projectId) }));
5053
5082
  var changes = (plan) => plan.contents !== null && (plan.status === "create" || plan.status === "update");
5054
5083
  async function runLogin(args, io, deps = defaultLoginDeps(process.env)) {
5055
5084
  const { accounts, terminal } = deps;
@@ -5075,6 +5104,21 @@ async function runLogin(args, io, deps = defaultLoginDeps(process.env)) {
5075
5104
  }
5076
5105
  plansFor(root, apps);
5077
5106
  say([`Signing in for ${trust}`, ...worktree ? [WORKTREE_NOTE(trust)] : []]);
5107
+ const { email, head } = await signInAccount({ api, trust, io, deps });
5108
+ const { plans, changed, usesAccount, kept } = writeAgentFiles(root, apps, "login");
5109
+ say([
5110
+ `Signed in as ${email} on this computer.`,
5111
+ ...head,
5112
+ ...plans.flatMap(({ id, plan }) => appLines(id, plan)),
5113
+ ...kept,
5114
+ ...closingLines2(apps, changed, usesAccount)
5115
+ ]);
5116
+ }
5117
+ async function signInAccount(a) {
5118
+ const { api, trust, io, deps } = a;
5119
+ const { accounts, terminal } = deps;
5120
+ const say = (lines) => io.stdout(lines.map((l) => `${l}
5121
+ `).join(""));
5078
5122
  const head = [];
5079
5123
  let email = null;
5080
5124
  const saved = accounts.get(api);
@@ -5108,24 +5152,21 @@ async function runLogin(args, io, deps = defaultLoginDeps(process.env)) {
5108
5152
  if (entry.backup) head.push(`Your old account file could not be read, so it was kept as ${entry.backup}.`);
5109
5153
  head.push(savedLine2(entry.where, deps.accountPath));
5110
5154
  }
5111
- const plans = plansFor(root, apps);
5155
+ return { email, head };
5156
+ }
5157
+ function writeAgentFiles(root, apps, verb, projectId = null) {
5158
+ const plans = plansFor(root, apps, projectId);
5112
5159
  const files = plans.flatMap(({ plan }) => changes(plan) ? [{ path: plan.path, contents: plan.contents }] : []);
5113
5160
  try {
5114
5161
  if (files.length > 0) writeFiles(root, files, { force: true, created: false });
5115
5162
  } catch (e) {
5116
- if (e instanceof CliError) throw new CliError(`${e.message} You are signed in; run npx designsource login again to write the settings.`, e.exitCode);
5163
+ if (e instanceof CliError) throw new CliError(`${e.message} You are signed in; run npx designsource ${verb} again to write the settings.`, e.exitCode);
5117
5164
  throw e;
5118
5165
  }
5119
5166
  const changed = plans.filter(({ plan }) => changes(plan)).map(({ id }) => id);
5120
5167
  const usesAccount = plans.some(({ plan }) => changes(plan) || plan.status === "unchanged" && !plan.keepsProject);
5121
5168
  const kept = plans.flatMap(({ id, plan }) => plan.status === "unchanged" && plan.keepsProject ? [KEEPS_PROJECT_ENTRY(id, plan.path)] : []);
5122
- say([
5123
- `Signed in as ${email} on this computer.`,
5124
- ...head,
5125
- ...plans.flatMap(({ id, plan }) => appLines(id, plan)),
5126
- ...kept,
5127
- ...closingLines2(apps, changed, usesAccount)
5128
- ]);
5169
+ return { plans, changed, usesAccount, kept };
5129
5170
  }
5130
5171
  async function lacksScope(api, token, fetchImpl) {
5131
5172
  try {
@@ -5176,7 +5217,7 @@ async function runLogout(args, io, deps = defaultLoginDeps(process.env)) {
5176
5217
  import { homedir as homedir6 } from "node:os";
5177
5218
 
5178
5219
  // src/version.ts
5179
- var VERSION = true ? "0.14.0" : "0.0.0-dev";
5220
+ var VERSION = true ? "0.15.1" : "0.0.0-dev";
5180
5221
 
5181
5222
  // src/mcp/jsonrpc.ts
5182
5223
  import { StringDecoder } from "node:string_decoder";
@@ -6243,6 +6284,400 @@ async function runMcp(args, io, streams = { input: process.stdin, output: proces
6243
6284
  }
6244
6285
  }
6245
6286
 
6287
+ // src/setup/state.ts
6288
+ import { existsSync as existsSync15, readdirSync as readdirSync6, readFileSync as readFileSync13 } from "node:fs";
6289
+ import { join as join16 } from "node:path";
6290
+ function frameworkOf(deps) {
6291
+ if ("@angular/core" in deps) return "angular";
6292
+ if ("svelte" in deps || "@sveltejs/kit" in deps) return "svelte";
6293
+ if ("vue" in deps || "nuxt" in deps) return "vue";
6294
+ return "css";
6295
+ }
6296
+ var FRAMEWORK_LABELS = { vue: "Vue", svelte: "Svelte", angular: "Angular", css: "plain CSS" };
6297
+ var appLabel = (info) => info.framework === "next" ? "Next.js app" : info.framework === "vite" ? "Vite app" : "React app";
6298
+ function describeApp(info) {
6299
+ const parts = [
6300
+ ...info.reactMajor !== null && Number.isFinite(info.reactMajor) ? [`React ${info.reactMajor}`] : [],
6301
+ ...info.tailwindMajor !== null && Number.isFinite(info.tailwindMajor) ? [`Tailwind ${info.tailwindMajor}`] : [],
6302
+ ...info.shadcn ? ["shadcn"] : []
6303
+ ];
6304
+ return `${appLabel(info)}${parts.length > 0 ? ` (${parts.join(", ")})` : ""}.`;
6305
+ }
6306
+ function readDeps(root) {
6307
+ try {
6308
+ const pkg = JSON.parse(readFileSync13(join16(root, "package.json"), "utf8"));
6309
+ const pick2 = (v) => typeof v === "object" && v !== null ? Object.fromEntries(Object.entries(v).filter((e) => typeof e[1] === "string")) : {};
6310
+ return { ...pick2(pkg?.dependencies), ...pick2(pkg?.devDependencies) };
6311
+ } catch {
6312
+ return null;
6313
+ }
6314
+ }
6315
+ var WORKSPACE_ROOT = "This is a workspace root. Run npx designsource in the app's own folder (apps/web, say).";
6316
+ function hasWorkspaces(root) {
6317
+ try {
6318
+ return "workspaces" in JSON.parse(readFileSync13(join16(root, "package.json"), "utf8"));
6319
+ } catch {
6320
+ return false;
6321
+ }
6322
+ }
6323
+ function targetsOf(root, info) {
6324
+ try {
6325
+ return info.shadcn ? resolveTargets(root, info.shadcn) : defaultTargets(root, info);
6326
+ } catch {
6327
+ return null;
6328
+ }
6329
+ }
6330
+ function readSetupState(cwd, home) {
6331
+ const root = normaliseFolder(cwd);
6332
+ const homeDir = normaliseFolder(home);
6333
+ const { work, trust } = projectFolders(cwd);
6334
+ const refusal = homeFolderRefusal(root, homeDir) ?? homeFolderRefusal(work, homeDir) ?? homeFolderRefusal(trust, homeDir);
6335
+ if (refusal) return { kind: "refused", message: refusal };
6336
+ if (!existsSync15(join16(root, "package.json"))) {
6337
+ const names = readdirSync6(root);
6338
+ if (names.every((n) => n.startsWith("."))) {
6339
+ const onlyGit = names.length > 0 && names.every((n) => n === ".git" || n === ".DS_Store");
6340
+ return { kind: "empty", root, found: "Empty folder.", dotfiles: names.length > 0, onlyGit };
6341
+ }
6342
+ return { kind: "other-stack", root, found: "No package.json here, so this is treated as a plain CSS site.", framework: "css" };
6343
+ }
6344
+ const deps = readDeps(root);
6345
+ if (deps === null) throw new CliError("package.json is not valid JSON.");
6346
+ if (!("react" in deps) && (hasWorkspaces(root) || existsSync15(join16(root, "pnpm-workspace.yaml")))) return { kind: "refused", message: WORKSPACE_ROOT };
6347
+ if (!("react" in deps)) {
6348
+ const framework = frameworkOf(deps);
6349
+ return { kind: "other-stack", root, found: framework === "css" ? "No React here; the design system goes in as plain CSS." : `${FRAMEWORK_LABELS[framework]} app.`, framework };
6350
+ }
6351
+ const info = detectApp(root);
6352
+ let cannotAdd = null;
6353
+ try {
6354
+ assertCanAdd(info);
6355
+ } catch (e) {
6356
+ if (!(e instanceof CliError)) throw e;
6357
+ cannotAdd = e.message;
6358
+ }
6359
+ const targets = cannotAdd === null ? targetsOf(root, info) : null;
6360
+ if (targets) {
6361
+ const components = previouslyAdded(root, targets).filter((n) => n !== "utils" && n !== "preview-portal").length;
6362
+ const section = sectionPresent(root, targets).present;
6363
+ if (components > 0 || section) {
6364
+ const screens = screensOnDisk(root, targets).slugs.length;
6365
+ return { kind: "react-set-up", root, found: `${describeApp(info)} The design system is set up here.`, info, components, section, screens };
6366
+ }
6367
+ }
6368
+ return { kind: "react-fresh", root, found: `${describeApp(info)} No design system here yet.`, info, cannotAdd };
6369
+ }
6370
+
6371
+ // src/commands/setup.ts
6372
+ var SETUP_NEEDS_TERMINAL = "setup asks questions, so it needs a terminal. In a script, use add, connect or login.";
6373
+ var SETUP_NOT_A_PROJECT = "This folder is neither a repository nor an app. Run npx designsource in your app's folder.";
6374
+ var CANNOT_ADD_AGENT_ONLY = "The UI kit cannot go into this app, so setup connects your coding agent only; it can still create a design system and port it by hand.";
6375
+ var MAKE_NEW_WAIT = "Make your design system in the browser. This continues when it is saved.";
6376
+ var WAIT_TIMEOUT = "Still waiting? Run npx designsource again once your design system is saved.";
6377
+ var NO_APPS_HERE = "No coding apps found on this computer. Run npx designsource login --apps claude (or cursor, vscode, codex, gemini) to set one up.";
6378
+ var BLOCKS_HINT = "Want the ready-made blocks too? Run npx designsource add --blocks";
6379
+ var USING = (name) => `Using \u201C${name}\u201D.`;
6380
+ var upgradeUrl = (origin) => `${origin}/credits?upgrade=pro`;
6381
+ var FREE_PLAN_HEADING = (what) => `Free plan: ${what === "kit" ? "the kit was not added to this app" : what === "files" ? "the design system files were not added" : "the app was not created"}.`;
6382
+ var FREE_PLAN_LINES = (origin, what, color = false) => [
6383
+ "",
6384
+ ` ${color ? `\x1B[1;33m${FREE_PLAN_HEADING(what)}\x1B[0m` : FREE_PLAN_HEADING(what)}`,
6385
+ "",
6386
+ ` Upgrade ${upgradeUrl(origin)}`,
6387
+ " Then run npx designsource",
6388
+ ""
6389
+ ];
6390
+ var SET_UP_STATUS = (name, components, section, screens) => `\u201C${name}\u201D is set up here (${[
6391
+ `${components} component${components === 1 ? "" : "s"}`,
6392
+ ...section ? ["the /design-system section"] : [],
6393
+ ...screens > 0 ? [`${screens} screen${screens === 1 ? "" : "s"}`] : []
6394
+ ].join(", ")}).`;
6395
+ var NEXT_STEP = (sentence) => ["Next: ask your coding agent", ` \u201C${sentence}\u201D`];
6396
+ var RESTYLE_SENTENCE = "Restyle this app's existing screens with the design system.";
6397
+ var APPLY_SENTENCE = "Apply the design system in design-system/ to this app.";
6398
+ var WAIT_MS = 30 * 6e4;
6399
+ var POLL_MS = 5e3;
6400
+ var PICKER_MAX = 20;
6401
+ var TERMINAL2 = { pick: (q, o, d) => pick(q, o, d ?? 0), pickMany };
6402
+ function defaultSetupDeps(env, menu) {
6403
+ const { prompts: _loginPrompts, ...login } = defaultLoginDeps(env);
6404
+ return { ...login, store: defaultStore(env), exec: spawnExec, menu, color: process.stdout.isTTY === true && !env.NO_COLOR };
6405
+ }
6406
+ var PROJECT_ID5 = /^[A-Za-z0-9_-]{1,64}$/;
6407
+ async function listProjects2(api, token, fetchImpl) {
6408
+ const res = await requestJson({ api, path: "/api/agent/projects", method: "GET", token }, fetchImpl);
6409
+ if (res.status === 401) throw new CliError(ACCOUNT_TOKEN_MESSAGE);
6410
+ if (res.status !== 200) throw new CliError(messageForResponse(res.status, res.body, api));
6411
+ const rows = typeof res.body === "object" && res.body !== null ? res.body.projects : null;
6412
+ if (!Array.isArray(rows)) throw new CliError("Unexpected response from the API \u2014 not a list of design systems.");
6413
+ return rows.flatMap((r) => {
6414
+ if (typeof r !== "object" || r === null) return [];
6415
+ const { id, name } = r;
6416
+ return typeof id === "string" && PROJECT_ID5.test(id) && typeof name === "string" ? [{ id, name: printable(name) || id }] : [];
6417
+ });
6418
+ }
6419
+ async function exportsAllowed(api, token, fetchImpl) {
6420
+ try {
6421
+ const res = await requestJson({ api, path: "/api/agent/token", method: "GET", token }, fetchImpl);
6422
+ const value = res.status === 200 && typeof res.body === "object" && res.body !== null ? res.body.exports : void 0;
6423
+ return typeof value === "boolean" ? value : null;
6424
+ } catch {
6425
+ return null;
6426
+ }
6427
+ }
6428
+ async function chooseApps(root, deps, prompts) {
6429
+ const withEntry = APPS.map((a) => a.id).filter((id) => planAppFile(root, id, null).status === "unchanged");
6430
+ if (withEntry.length > 0) return withEntry;
6431
+ const detected = deps.detect();
6432
+ if (detected.length <= 1) return detected;
6433
+ return prompts.pickMany("Set up which coding apps?", APP_OPTIONS, detected);
6434
+ }
6435
+ async function runSetup(args, io, deps) {
6436
+ if (!deps.terminal) throw new CliError(SETUP_NEEDS_TERMINAL);
6437
+ const prompts = deps.prompts ?? TERMINAL2;
6438
+ const say = (lines) => io.stdout(lines.map((l) => `${l}
6439
+ `).join(""));
6440
+ const state = readSetupState(io.cwd, deps.home);
6441
+ if (state.kind === "refused") throw new CliError(state.message);
6442
+ say([state.found]);
6443
+ if (state.kind === "react-fresh" && state.cannotAdd) say([state.cannotAdd, CANNOT_ADD_AGENT_ONLY]);
6444
+ if (state.kind === "empty") {
6445
+ const choice = await prompts.pick("What do you want to do?", [{ value: "create", label: "Create a new Next.js app here" }, { value: "other", label: "Something else\u2026" }]);
6446
+ if (choice === "other") return deps.menu();
6447
+ } else if (state.kind === "other-stack") {
6448
+ const choice = await prompts.pick("What do you want to do?", [{ value: "files", label: `Add the design system files for ${FRAMEWORK_LABELS[state.framework]}` }, { value: "other", label: "Something else\u2026" }]);
6449
+ if (choice === "other") return deps.menu();
6450
+ }
6451
+ const { work, trust, worktree } = projectFolders(io.cwd);
6452
+ if (state.kind !== "empty" && !isProjectFolder(work) && !isProjectFolder(state.root)) throw new CliError(SETUP_NOT_A_PROJECT);
6453
+ const api = loginApi(args, true);
6454
+ const apps = await chooseApps(work, deps, prompts);
6455
+ const saved = await resolveToken({ cwd: io.cwd, store: deps.store, interactive: true, prompt: false });
6456
+ let access;
6457
+ if (saved?.entry) {
6458
+ access = { kind: "project", token: saved.token, api: apiFor(args, saved.entry), name: printable(saved.entry.name), projectId: saved.entry.projectId };
6459
+ } else {
6460
+ say([`Signing in for ${trust}`, ...worktree ? [WORKTREE_NOTE(trust)] : []]);
6461
+ const signed = await signInAccount({ api, trust, io, deps });
6462
+ say([`Signed in as ${signed.email} on this computer.`, ...signed.head]);
6463
+ const token = await deps.accounts.token(api);
6464
+ if (token === null) throw new CliError(ACCOUNT_TOKEN_MESSAGE);
6465
+ const projects = await listProjects2(api, token, io.fetchImpl);
6466
+ const entry = deps.accounts.get(api);
6467
+ const linkedId = entry ? trustedFolderOf(entry, { work, trust })?.projectId ?? null : null;
6468
+ access = { kind: "account", token, api, email: signed.email, projects, linked: projects.find((p) => p.id === linkedId) ?? null };
6469
+ }
6470
+ let agent = null;
6471
+ const writeAgents = () => {
6472
+ if (apps.length > 0) agent = writeAgentFiles(work, apps, "setup", access.kind === "project" ? access.projectId : null);
6473
+ };
6474
+ if (state.kind !== "empty") writeAgents();
6475
+ let name;
6476
+ let projectId = null;
6477
+ if (access.kind === "project") {
6478
+ name = access.name;
6479
+ } else if (access.linked) {
6480
+ name = access.linked.name;
6481
+ projectId = access.linked.id;
6482
+ } else {
6483
+ const chosen = await chooseDesignSystem(access, deps, prompts, io, say);
6484
+ if (chosen === null) return say(agentLines());
6485
+ deps.accounts.link(access.api, trust, chosen.id);
6486
+ name = chosen.name;
6487
+ projectId = chosen.id;
6488
+ }
6489
+ const kitPath = () => projectId === null ? void 0 : accountProjectRoute(projectId, "kit");
6490
+ const kitLines = [];
6491
+ let planStopped = false;
6492
+ const freeLines = (...lines) => {
6493
+ planStopped = true;
6494
+ kitLines.push(...lines);
6495
+ };
6496
+ const pushKit = (r) => {
6497
+ if (r.planStopped) freeLines(...r.lines);
6498
+ else kitLines.push(...r.lines);
6499
+ };
6500
+ const exports = access.kind === "account" ? await exportsAllowed(access.api, access.token, io.fetchImpl) : null;
6501
+ const color = deps.color === true;
6502
+ let kitStep = null;
6503
+ let stop = "kit";
6504
+ if (state.kind === "react-set-up") {
6505
+ say([SET_UP_STATUS(name, state.components, state.section, state.screens)]);
6506
+ const options = [
6507
+ { value: "update", label: `Update from \u201C${name}\u201D` },
6508
+ ...access.kind === "account" ? [{ value: "different", label: "Use a different design system" }] : [],
6509
+ { value: "other", label: "Something else\u2026" }
6510
+ ];
6511
+ const choice = await prompts.pick("What do you want to do?", options);
6512
+ if (choice === "other") return deps.menu();
6513
+ if (choice === "different" && access.kind === "account") {
6514
+ const chosen = await chooseDesignSystem({ ...access, linked: null }, deps, prompts, io, say);
6515
+ if (chosen === null) return say(agentLines());
6516
+ deps.accounts.link(access.api, trust, chosen.id);
6517
+ name = chosen.name;
6518
+ projectId = chosen.id;
6519
+ }
6520
+ kitStep = async () => pushKit(await addKit(state, access, kitPath(), false, deps, io));
6521
+ } else if (state.kind === "react-fresh") {
6522
+ if (state.cannotAdd === null) kitStep = async () => pushKit(await addKit(state, access, kitPath(), true, deps, io));
6523
+ } else if (state.kind === "other-stack") {
6524
+ stop = "files";
6525
+ kitStep = async () => {
6526
+ await (deps.runIntegrate ?? runIntegrate)({ folder: ".", framework: state.framework, api: args.api, apiGiven: args.apiGiven, force: false }, process.env, io, deps.store, deps.accounts);
6527
+ };
6528
+ } else if (state.kind === "empty") {
6529
+ stop = "app";
6530
+ kitStep = async () => {
6531
+ await (deps.runCreate ?? runCreate)({ folder: ".", api: args.api, apiGiven: args.apiGiven, force: state.onlyGit }, process.env, io, deps.store, deps.accounts);
6532
+ };
6533
+ }
6534
+ say([USING(name)]);
6535
+ if (kitStep) {
6536
+ if (exports === false && access.kind === "account") {
6537
+ say(FREE_PLAN_LINES(access.api, stop, color));
6538
+ if (await upgradeAndWait(access, deps, prompts, io, say)) await kitStep();
6539
+ else planStopped = true;
6540
+ } else await kitStep();
6541
+ }
6542
+ if (state.kind === "empty") writeAgents();
6543
+ const noKit = planStopped || state.kind === "react-fresh" && state.cannotAdd !== null;
6544
+ const sentence = noKit ? null : state.kind === "other-stack" ? APPLY_SENTENCE : state.kind === "empty" ? null : RESTYLE_SENTENCE;
6545
+ say([...kitLines, ...agentLines(), ...sentence ? NEXT_STEP(sentence) : []]);
6546
+ function agentLines() {
6547
+ const done = agent;
6548
+ return [
6549
+ ...apps.length === 0 ? [NO_APPS_HERE] : [],
6550
+ ...done ? agentSummary(done) : []
6551
+ ];
6552
+ }
6553
+ }
6554
+ var labelOf2 = (id) => APPS.find((a) => a.id === id).label;
6555
+ var listOf2 = (words) => words.length <= 1 ? words.join("") : `${words.slice(0, -1).join(", ")} and ${words.at(-1)}`;
6556
+ var APPS_SET_UP = (labels) => `Coding apps: ${listOf2(labels)} ${labels.length === 1 ? "is" : "are"} set up.`;
6557
+ var APPS_WRITTEN = (apps) => `Set up ${listOf2(apps.map((a) => `${a.label} (${a.path})`))}.`;
6558
+ var ALREADY_SET_UP = (labels) => `${listOf2(labels)} ${labels.length === 1 ? "was" : "were"} already set up.`;
6559
+ var COMMIT_THESE = "Commit these files: they hold no token.";
6560
+ function agentSummary(done) {
6561
+ const written = done.plans.filter(({ plan }) => plan.status === "create" || plan.status === "update");
6562
+ const unchanged = done.plans.filter(({ plan }) => plan.status === "unchanged" && !plan.keepsProject);
6563
+ const manual = done.plans.filter(({ plan }) => plan.status === "manual");
6564
+ const envWarnings = unchanged.flatMap(({ id, plan }) => appLines(id, plan).slice(1));
6565
+ if (written.length === 0) {
6566
+ return [
6567
+ ...unchanged.length > 0 ? [APPS_SET_UP(unchanged.map(({ id }) => labelOf2(id)))] : [],
6568
+ ...envWarnings,
6569
+ ...manual.flatMap(({ id, plan }) => appLines(id, plan)),
6570
+ ...done.kept
6571
+ ];
6572
+ }
6573
+ const ids = written.map(({ id }) => id);
6574
+ const notes = [
6575
+ ...ids.includes("claude") ? ["Claude Code asks once to allow it"] : [],
6576
+ ...ids.includes("codex") ? ["Codex needs this project trusted"] : [],
6577
+ ...ids.includes("gemini") ? ["Gemini CLI needs this folder trusted if folder trust is on"] : []
6578
+ ];
6579
+ return [
6580
+ APPS_WRITTEN(written.map(({ id, plan }) => ({ label: labelOf2(id), path: plan.path }))),
6581
+ ...unchanged.length > 0 ? [ALREADY_SET_UP(unchanged.map(({ id }) => labelOf2(id)))] : [],
6582
+ ...envWarnings,
6583
+ ...manual.flatMap(({ id, plan }) => appLines(id, plan)),
6584
+ ...done.kept,
6585
+ `Restart ${written.length === 1 ? "it" : "them"} to load designsource.${notes.length > 0 ? ` ${notes.join("; ")}.` : ""}`,
6586
+ COMMIT_THESE
6587
+ ];
6588
+ }
6589
+ var UPGRADE_QUESTION = "Upgrade to Pro now?";
6590
+ var UPGRADE_WAIT = "Subscribe in the browser. This continues when Pro is on. (Ctrl+C to stop)";
6591
+ var PRO_ON = "Pro is on.";
6592
+ var UPGRADE_TIMEOUT = "Still on the free plan. After upgrading, run npx designsource again.";
6593
+ async function upgradeAndWait(access, deps, prompts, io, say) {
6594
+ const choice = await prompts.pick(UPGRADE_QUESTION, [
6595
+ { value: "upgrade", label: "Upgrade to Pro now (opens the browser)" },
6596
+ { value: "later", label: "Not now" }
6597
+ ]);
6598
+ if (choice !== "upgrade") return false;
6599
+ const on = await openAndPoll(upgradeUrl(access.api), UPGRADE_WAIT, UPGRADE_TIMEOUT, deps, say, async () => {
6600
+ const res = await requestJson({ api: access.api, path: "/api/agent/token", method: "GET", token: access.token }, io.fetchImpl).catch(() => null);
6601
+ if (res?.status === 401) throw new CliError(ACCOUNT_TOKEN_MESSAGE);
6602
+ const exports = res?.status === 200 && typeof res.body === "object" && res.body !== null ? res.body.exports : void 0;
6603
+ return exports === true ? true : void 0;
6604
+ });
6605
+ if (on) say([PRO_ON]);
6606
+ return on === true;
6607
+ }
6608
+ async function chooseDesignSystem(access, deps, prompts, io, say) {
6609
+ const { projects } = access;
6610
+ if (projects.length === 0) return makeNew(access, deps, prompts, io, say);
6611
+ const options = [
6612
+ { value: "new", label: "Make a new one" },
6613
+ ...projects.slice(0, PICKER_MAX).map((p) => ({ value: `p:${p.id}`, label: p.name })),
6614
+ ...projects.length > PICKER_MAX ? [{ value: "more", label: "More\u2026" }] : []
6615
+ ];
6616
+ let choice = await prompts.pick("Which design system?", options);
6617
+ if (choice === "more") choice = await prompts.pick("Which design system?", projects.map((p) => ({ value: `p:${p.id}`, label: p.name })));
6618
+ if (choice === "new") return makeNew(access, deps, prompts, io, say);
6619
+ return projects.find((p) => `p:${p.id}` === choice) ?? null;
6620
+ }
6621
+ async function makeNew(access, deps, prompts, io, say) {
6622
+ const known = new Set(access.projects.map((p) => p.id));
6623
+ const fresh = await openAndPoll(`${access.api}/projects`, MAKE_NEW_WAIT, WAIT_TIMEOUT, deps, say, async () => {
6624
+ let list;
6625
+ try {
6626
+ list = await listProjects2(access.api, access.token, io.fetchImpl);
6627
+ } catch (e) {
6628
+ if (e instanceof CliError && e.message === ACCOUNT_TOKEN_MESSAGE) throw e;
6629
+ return void 0;
6630
+ }
6631
+ const added = list.filter((p) => !known.has(p.id));
6632
+ return added.length > 0 ? added : void 0;
6633
+ });
6634
+ if (fresh === null) return null;
6635
+ if (fresh.length === 1) return fresh[0];
6636
+ const choice = await prompts.pick("Which new design system?", fresh.map((p) => ({ value: `p:${p.id}`, label: p.name })));
6637
+ return fresh.find((p) => `p:${p.id}` === choice) ?? null;
6638
+ }
6639
+ async function openAndPoll(url, waiting2, timedOut, deps, say, check) {
6640
+ await (deps.open ?? ((u) => openBrowser(u, deps.platform)))(url);
6641
+ say([`Opening ${url}`, waiting2]);
6642
+ const now = deps.now ?? Date.now;
6643
+ const sleep = deps.sleep ?? ((ms) => new Promise((resolve10) => setTimeout(resolve10, ms)));
6644
+ const start = now();
6645
+ while (now() - start < (deps.waitMs ?? WAIT_MS)) {
6646
+ await sleep(deps.pollMs ?? POLL_MS);
6647
+ const found = await check();
6648
+ if (found !== void 0) return found;
6649
+ }
6650
+ say([timedOut]);
6651
+ return null;
6652
+ }
6653
+ async function addKit(state, access, kitPath, fresh, deps, io) {
6654
+ const addArgs = { components: [], api: access.api, yes: false, all: fresh, force: false, skipInstall: false, noBlocks: false, blocks: false };
6655
+ let result;
6656
+ try {
6657
+ result = await runAdd(
6658
+ {
6659
+ root: state.root,
6660
+ token: access.token,
6661
+ api: access.api,
6662
+ ...kitPath ? { kitPath, messages: cliAccountMessageForResponse } : {},
6663
+ components: [],
6664
+ skipInstall: false,
6665
+ all: fresh,
6666
+ blocks: !fresh,
6667
+ refreshScreens: true,
6668
+ force: false
6669
+ },
6670
+ { fetchImpl: io.fetchImpl, exec: deps.exec, questions: deps.addQuestions ?? ttyQuestions(addArgs, "all", io), progress: (line) => io.stdout(`${line}
6671
+ `) }
6672
+ );
6673
+ } catch (e) {
6674
+ if (e instanceof CliError && e.message === PLAN_MESSAGE) return { lines: FREE_PLAN_LINES(access.api, "kit", deps.color === true), planStopped: true };
6675
+ throw e;
6676
+ }
6677
+ const hint = result.section && result.section.blocks === 0 && !result.section.blocksLeftInPlace ? [BLOCKS_HINT] : [];
6678
+ return { lines: [...summaryLines(result), ...hint], planStopped: false };
6679
+ }
6680
+
6246
6681
  // src/index.ts
6247
6682
  var MENU = [
6248
6683
  { value: "add", label: "Add the UI kit to a React app", hint: "your components and theme, set up for you" },
@@ -6268,27 +6703,22 @@ ${e.usage}`);
6268
6703
  io.stdout(usageFor(command.verb));
6269
6704
  return 0;
6270
6705
  }
6706
+ const terminal = process.stdin.isTTY === true && process.stdout.isTTY === true;
6271
6707
  try {
6272
- if (command.kind === "menu") {
6273
- if (process.stdin.isTTY !== true || process.stdout.isTTY !== true) {
6708
+ if (command.kind === "setup" && command.bare || command.kind === "menu") {
6709
+ if (!terminal) {
6274
6710
  io.stderr(`Missing command. ${USE_VERBS}
6275
6711
 
6276
6712
  ${USAGE}`);
6277
6713
  return 1;
6278
6714
  }
6279
- const verb = await pick("What do you want to do?", MENU, 0);
6280
- command = parseArgs([verb], env);
6281
6715
  }
6282
- if (command.kind === "mcp") {
6283
- await runMcp(command.args, io);
6716
+ if (command.kind === "menu") command = await menuCommand(env);
6717
+ if (command.kind === "setup") {
6718
+ await runSetup(command.args, io, defaultSetupDeps(env, async () => runCommand(await menuCommand(env), env, io)));
6284
6719
  return 0;
6285
6720
  }
6286
- if (command.kind === "add") await runAddCommand(command.args, io);
6287
- else if (command.kind === "connect") await runConnect(command.args, io, defaultConnectDeps(env));
6288
- else if (command.kind === "create") await runCreate(command.args, env, io);
6289
- else if (command.kind === "integrate") await runIntegrate(command.args, env, io);
6290
- else if (command.kind === "login") await runLogin(command.args, io, defaultLoginDeps(env));
6291
- else if (command.kind === "logout") await runLogout(command.args, io, defaultLoginDeps(env));
6721
+ await runCommand(command, env, io);
6292
6722
  return 0;
6293
6723
  } catch (e) {
6294
6724
  if (e instanceof CliError) {
@@ -6299,6 +6729,19 @@ ${USAGE}`);
6299
6729
  throw e;
6300
6730
  }
6301
6731
  }
6732
+ async function menuCommand(env) {
6733
+ const verb = await pick("What do you want to do?", MENU, 0);
6734
+ return parseArgs([verb], env);
6735
+ }
6736
+ async function runCommand(command, env, io) {
6737
+ if (command.kind === "mcp") await runMcp(command.args, io);
6738
+ else if (command.kind === "add") await runAddCommand(command.args, io);
6739
+ else if (command.kind === "connect") await runConnect(command.args, io, defaultConnectDeps(env));
6740
+ else if (command.kind === "create") await runCreate(command.args, env, io);
6741
+ else if (command.kind === "integrate") await runIntegrate(command.args, env, io);
6742
+ else if (command.kind === "login") await runLogin(command.args, io, defaultLoginDeps(env));
6743
+ else if (command.kind === "logout") await runLogout(command.args, io, defaultLoginDeps(env));
6744
+ }
6302
6745
  var code = await main(process.argv.slice(2), process.env, {
6303
6746
  stdout: (s) => process.stdout.write(s),
6304
6747
  stderr: (s) => process.stderr.write(s),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "designsource",
3
- "version": "0.14.0",
3
+ "version": "0.15.1",
4
4
  "description": "The DS source CLI: add your design system's UI kit to a React app (add), add the design system to any other app (integrate), start a new Next.js project from it (create), or serve it to a coding agent over MCP (mcp) — tokens, every component and your mockup page.",
5
5
  "license": "MIT",
6
6
  "repository": {