Lines 1–11 at 63 (were 1–11 at 62)
11// abso init and abso join: start a project from a folder, or join one into a folder. 3−import { existsSync, mkdirSync, rmSync, statSync, writeFileSync } from "node:fs";
4−import { join, resolve } from "node:path";
3+import { existsSync, mkdirSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs"; 4+import { join, relative, resolve } from "node:path"; 55import { Api, ApiError } from "./api.ts"; 66import { bool, type Parsed, str, UsageError } from "./args.ts"; 77import { writeKnownBlobs } from "./blobs.ts"; 8−import { mintKey, readBox, signedInServers } from "./box.ts";
8+import { defaultAgentName, mintFreeName, mintKey, readBox, signedInServers } from "./box.ts"; 99import { STARTING_ABSOIGNORE } from "./check-ignore.ts"; 1010import { CLIENT_DIR } from "./client-version.ts"; 1111import { type Env, type Output, plural } from "./command-env.ts"; Lines 33–44 at 63 (were 33–42 at 62)
3333 return signedIn[0] ?? TOKEN_SERVERS[0]!; 36+const NAME = /^[A-Za-z0-9][A-Za-z0-9._-]*$/; 3638function agentFrom(args: Parsed): string { 3739 const agent = str(args, "agent"); 3840 if (!agent) throw new UsageError("give this folder's agent name with --agent <name>"); 39− if (!/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(agent))
41+ if (!NAME.test(agent)) 4042 throw new UsageError(`agent names are letters, digits, dot, dash and underscore (got ${JSON.stringify(agent)})`); Lines 105–153 at 63 (were 103–120 at 62)
109+ * Where abso join writes (abso issue 351): --dir, else here when this folder is empty, else ./<project>, which has 110+ * to be new or empty so nobody's files are written over. 112+function joinFolder(args: Parsed, env: Env, project: string): string { 113+ const dir = str(args, "dir"); 114+ if (dir !== undefined) return resolve(env.cwd, dir); 115+ const empty = (path: string) => readdirSync(path).length === 0; 116+ if (empty(env.cwd)) return resolve(env.cwd); 117+ const sub = resolve(env.cwd, project); 118+ if (existsSync(sub) && (!statSync(sub).isDirectory() || !empty(sub))) 119+ throw new UsageError(`${sub} already has files in it: say where to join with --dir <folder>`); 106123export async function runJoin(args: Parsed, env: Env): Promise<Output> { 107− if (args.positionals.length !== 1)
108− throw new UsageError("join takes the project name: abso join <project> --agent <name>");
124+ if (args.positionals.length !== 1) throw new UsageError("join takes the project name: abso join <project>"); 109125 const project = args.positionals[0]!; 110− const root = resolve(env.cwd, str(args, "dir") ?? ".");
126+ if (!NAME.test(project)) 127+ throw new UsageError( 128+ `project names are letters, digits, dot, dash and underscore (got ${JSON.stringify(project)})`, 130+ const root = joinFolder(args, env, project); 111131 mkdirSync(root, { recursive: true }); 112132 if (findRoot(root) === root) throw new UsageError(`${root} already has .abso/config.json`); 113133 const reader = bool(args, "reader"); 114− const config = { ...makeSettings(args, env, project), ...(reader ? { reader: true } : {}) };
115− const box = readBox(config.server);
116− const minted =
117− box && !existsSync(join(stateDir(root), "key")) ? await mintKey(box, project, config.agent) : undefined;
134+ const server = serverFrom(args, env); 135+ const box = readBox(server); 136+ const hasKey = existsSync(join(stateDir(root), "key")); 138+ let minted: { key: string; id: string } | undefined; 139+ if (str(args, "agent") !== undefined) { 140+ agent = agentFrom(args); 141+ minted = box && !hasKey ? await mintKey(box, project, agent) : undefined; 143+ // abso issue 351: no --agent takes the box's name, or the next free -2, -3. 144+ if (!box) throw new UsageError("abso login signs this box in first (or give --agent <name>)"); 145+ if (hasKey) throw new UsageError(`${root} already has a key: give its agent's name with --agent <name>`); 146+ const free = await mintFreeName(box, project, defaultAgentName()); 148+ minted = { key: free.key, id: free.id }; 150+ const config = { server, project, agent, ...(reader ? { reader: true } : {}) }; 118151 writeConfig(root, config); 120153 if (minted) installKey(root, JSON.stringify(minted)); Lines 175–181 at 63 (were 142–147 at 62)
143176 ...setupHint(root), 144177 hookHint(env.env), 178+ ...(root === resolve(env.cwd) ? [] : [`cd ${relative(env.cwd, root) || root}`]),