@alori/cli 0.1.7 → 0.1.9

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 CHANGED
@@ -14,6 +14,9 @@ looks right. If someone else saved changes since your pull, Alori merges them wi
14
14
  folder; only the same lines changed on both sides stop the push, with their copy beside yours as
15
15
  `<file>.alori-theirs` to merge. How it works: `docs/CLAUDE_CODE.md` in Alori's repository.
16
16
 
17
+ One sign-in works on every app you can change: `alori apps` lists them (yours, and your agency's clients), and
18
+ `alori dev --app "Client name"` downloads one into a new folder. Each folder keeps working on its own app.
19
+
17
20
  `alori dev` runs the app from your files against its real backend and data: screens and text change as you save,
18
21
  backend changes (`convex/`) reach it with `alori push`.
19
22
 
package/bin/alori.mjs CHANGED
@@ -4,14 +4,17 @@
4
4
  * Alori builds it, deploys it to the preview and saves it as a version, with no AI from Alori.
5
5
  */
6
6
  import { resolve } from "node:path";
7
- import { apiBase, call, forget, login, SignedOut } from "../lib/auth.mjs";
8
- import { dev, pull, push, status } from "../lib/sync.mjs";
7
+ import { apiBase, call, forget, login, SignedOut, useApp } from "../lib/auth.mjs";
8
+ import { readState } from "../lib/folder.mjs";
9
+ import { appsList, dev, pull, push, status } from "../lib/sync.mjs";
9
10
 
10
11
  const help = `alori: build your Alori app with Claude Code
11
12
 
12
13
  alori login Connect this computer to your Alori account
13
14
  alori dev [folder] Download your app, install it and run it on this computer (opens your browser)
14
15
  alori pull [folder] Download your app's code (or bring the latest into a pulled folder)
16
+ --app <name or id> With dev or pull, for a new folder: which app (a client's). A pulled folder keeps its own
17
+ alori apps The apps you can change: yours, your agency's clients, the ones support opened
15
18
  alori status What changed since the last pull or push
16
19
  alori push -m "message" Send your changes: Alori builds them and saves a new version
17
20
  alori whoami Which app you're connected to
@@ -19,11 +22,20 @@ const help = `alori: build your Alori app with Claude Code
19
22
 
20
23
  Set ALORI_API to use another Alori than ${apiBase()}.`;
21
24
 
22
- function messageOf(args) {
23
- const index = args.findIndex((arg) => arg === "-m" || arg === "--message");
25
+ /** The value after one of these flags, or undefined. */
26
+ function flagOf(args, names) {
27
+ const index = args.findIndex((arg) => names.includes(arg));
24
28
  return index >= 0 ? args[index + 1] : undefined;
25
29
  }
26
30
 
31
+ const messageOf = (args) => flagOf(args, ["-m", "--message"]);
32
+ const appArgOf = (args) => flagOf(args, ["--app", "-a"]);
33
+
34
+ /** The folder named on the line: the first word that is neither a flag nor a flag's value. */
35
+ function folderArgOf(args) {
36
+ return args.find((arg, index) => !arg.startsWith("-") && !["--app", "-a", "-m", "--message"].includes(args[index - 1] ?? ""));
37
+ }
38
+
27
39
  /** Runs a command that needs a sign-in, signing in first when this computer isn't connected yet. */
28
40
  async function signedIn(run) {
29
41
  try {
@@ -53,16 +65,28 @@ async function main() {
53
65
  console.log("Signed out on this computer.");
54
66
  return;
55
67
  case "whoami": {
68
+ // In a pulled folder, the folder's app.
69
+ useApp(readState(resolve("."))?.appId ?? null);
56
70
  const me = await call("/code/me");
57
71
  if (!me.ok) throw Object.assign(new Error(me.body?.error === "cantBuild" ? "Your role doesn't build this app." : "Run: alori login"), { plain: true });
58
72
  console.log(`${me.body.app} (version ${me.body.version ?? "none"}), signed in as ${me.body.email ?? "you"}.`);
59
73
  return;
60
74
  }
61
75
  case "dev":
62
- await signedIn(() => dev(args.find((arg) => !arg.startsWith("-"))));
76
+ await signedIn(() => dev(folderArgOf(args), appArgOf(args)));
63
77
  return;
64
78
  case "pull":
65
- await signedIn(() => pull(args.find((arg) => !arg.startsWith("-"))));
79
+ await signedIn(() => pull(folderArgOf(args), appArgOf(args)));
80
+ return;
81
+ case "apps":
82
+ await signedIn(async () => {
83
+ const apps = await appsList();
84
+ if (apps.length === 0) console.log("No app you can change with this sign-in.");
85
+ for (const app of apps) {
86
+ const how = app.access === "agency" ? "your client" : app.access === "staff" ? `Alori support, until ${new Date(app.until).toLocaleString()}` : "yours";
87
+ console.log(` ${app.name} (${how}) --app ${app.appId}`);
88
+ }
89
+ });
66
90
  return;
67
91
  case "status":
68
92
  status(resolve("."));
package/lib/auth.mjs CHANGED
@@ -175,6 +175,20 @@ export class SignedOut extends Error {
175
175
  }
176
176
  }
177
177
 
178
+ /**
179
+ * The app every call is for (the folder's, or `--app`), sent as `Alori-App`. Unset, Alori answers for the app the
180
+ * sign-in was made from. Alori refuses an app this sign-in can't reach; it never answers for another one instead.
181
+ */
182
+ let currentApp = null;
183
+
184
+ export function useApp(appId) {
185
+ currentApp = appId ?? null;
186
+ }
187
+
188
+ export function askedApp() {
189
+ return currentApp;
190
+ }
191
+
178
192
  /** Calls Alori's code routes as the signed-in person. Renews the token once on a 401. */
179
193
  export async function call(path, { method = "GET", body } = {}) {
180
194
  for (const renew of [false, true]) {
@@ -182,7 +196,11 @@ export async function call(path, { method = "GET", body } = {}) {
182
196
  if (!token) throw new SignedOut();
183
197
  const response = await fetch(`${apiBase()}${path}`, {
184
198
  method,
185
- headers: { Authorization: `Bearer ${token}`, ...(body === undefined ? {} : { "Content-Type": "application/json" }) },
199
+ headers: {
200
+ Authorization: `Bearer ${token}`,
201
+ ...(currentApp ? { "Alori-App": currentApp } : {}),
202
+ ...(body === undefined ? {} : { "Content-Type": "application/json" }),
203
+ },
186
204
  ...(body === undefined ? {} : { body: JSON.stringify(body) }),
187
205
  });
188
206
  if (response.status === 401 && !renew) continue;
package/lib/guide.mjs CHANGED
@@ -1,5 +1,8 @@
1
- /** The CLAUDE.md a pull writes at the folder's root, so Claude Code knows how this folder works. */
2
- export function claudeGuide(app) {
1
+ /**
2
+ * The CLAUDE.md a pull writes at the folder's root, so Claude Code knows how this folder works. `website`: the public
3
+ * website's rules Alori sends with the pull (the SEO checklist, and the old website's addresses to keep), or "".
4
+ */
5
+ export function claudeGuide(app, website = "") {
3
6
  return `# ${app}: an Alori app
4
7
 
5
8
  This folder is the code of ${app}, pulled from Alori with the \`alori\` command. Change it here, then send the changes back
@@ -42,5 +45,5 @@ alori pull # brings changes saved since (the builder's, a teammate's)
42
45
  A push that someone else's change overlaps (the same file, saved since your pull) is refused: run \`alori pull\`, which
43
46
  keeps your changes and writes their version beside each overlapping file as \`<file>.alori-theirs\`. Merge them, delete the
44
47
  \`.alori-theirs\` files, then push again.
45
- `;
48
+ ${website ? `\n${website}\n` : ""}`;
46
49
  }
package/lib/sync.mjs CHANGED
@@ -7,7 +7,7 @@ import { execFileSync } from "node:child_process";
7
7
  import { copyFileSync, cpSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, unlinkSync, writeFileSync } from "node:fs";
8
8
  import { tmpdir } from "node:os";
9
9
  import { basename, dirname, join, resolve } from "node:path";
10
- import { apiBase, call } from "./auth.mjs";
10
+ import { apiBase, askedApp, call, useApp } from "./auth.mjs";
11
11
  import { changesOf, codeHashes, fileHash, isAppCode, isTemplateFile, readState, stateDir, templateHashes, walk, writeState } from "./folder.mjs";
12
12
  import { install, installed, runLocally } from "./dev.mjs";
13
13
  import { commitPull, ensureIgnored, hadChanges, startRepoFrom } from "./git.mjs";
@@ -20,6 +20,7 @@ const errors = {
20
20
  notCodeClient: "This sign-in can't change code. Run: alori login",
21
21
  cantBuild: "Your role in this app doesn't build it. Ask an admin to change your role.",
22
22
  noApp: "Your Alori account has no app yet.",
23
+ notYours: "You can't change this app with this sign-in. Run alori apps to see the ones you can. Alori support: click Work on its code on the account's page in /admin first.",
23
24
  rateLimited: "Too many calls in a minute. Wait a moment and try again.",
24
25
  noCode: "This app has no saved code yet.",
25
26
  };
@@ -61,7 +62,7 @@ async function download() {
61
62
  mkdirSync(folder);
62
63
  execFileSync("tar", ["-xzf", tarball, "-C", folder]);
63
64
  unlinkSync(tarball);
64
- return { folder, temp, version: job.version };
65
+ return { folder, temp, version: job.version, website: typeof job.website === "string" ? job.website : "" };
65
66
  }
66
67
 
67
68
  /** The template's own notes for Alori's developers: the folder gets the owner's guide (`guide.mjs`) instead. */
@@ -102,12 +103,12 @@ const workspaceFile = `allowBuilds:
102
103
 
103
104
  /** A first pull into an empty folder. */
104
105
  async function pullFresh(dir, me) {
105
- const { folder, temp, version } = await download();
106
+ const { folder, temp, version, website } = await download();
106
107
  mkdirSync(dir, { recursive: true });
107
108
  for (const path of walk(folder).filter((path) => !templateNotes.has(path))) copyInto(folder, dir, path);
108
109
  const template = templateHashes(folder);
109
110
  rmSync(temp, { recursive: true, force: true });
110
- writeFileSync(join(dir, "CLAUDE.md"), claudeGuide(me.app));
111
+ writeFileSync(join(dir, "CLAUDE.md"), claudeGuide(me.app, website));
111
112
  ensureIgnored(dir, ignoreLines);
112
113
  if (!existsSync(join(dir, "pnpm-workspace.yaml"))) writeFileSync(join(dir, "pnpm-workspace.yaml"), workspaceFile);
113
114
  writeState(dir, { api: apiBase(), appId: me.appId, app: me.app, version, hashes: codeHashes(dir), templateHashes: template });
@@ -168,7 +169,7 @@ function snapshotOf(dir, folder, temp) {
168
169
 
169
170
  /** A pull into a folder already pulled: Alori's latest code comes in, and the person's changes stay (`mergeInto`). */
170
171
  async function pullOver(dir, state, me) {
171
- const { folder, temp, version } = await download();
172
+ const { folder, temp, version, website } = await download();
172
173
  const repo = startRepoFrom(dir, () => snapshotOf(dir, folder, temp), version);
173
174
  // The ignore list first, so what this computer generated (the lockfile, .DS_Store) doesn't count as the person's changes.
174
175
  ensureIgnored(dir, ignoreLines);
@@ -180,6 +181,8 @@ async function pullOver(dir, state, me) {
180
181
  // What Alori writes for every app (the .gitignore, generated files) always comes fresh.
181
182
  for (const path of walk(folder).filter((path) => !isAppCode(path) && !isTemplateFile(path) && !templateNotes.has(path))) copyInto(folder, dir, path);
182
183
  writeState(dir, { api: apiBase(), appId: me.appId, app: me.app, version, hashes: codeHashes(folder), templateHashes: remoteTemplate });
184
+ // Alori's guide comes fresh each pull, with the website's rules of today.
185
+ writeFileSync(join(dir, "CLAUDE.md"), claudeGuide(me.app, website));
183
186
  rmSync(temp, { recursive: true, force: true });
184
187
  ensureIgnored(dir, ignoreLines);
185
188
  console.log(`Pulled ${me.app}, version ${version}.`);
@@ -203,11 +206,50 @@ async function pullOver(dir, state, me) {
203
206
  async function whoAmI() {
204
207
  const me = await call("/code/me");
205
208
  if (!me.ok) failOn(me);
209
+ // Alori answers for the app asked; an older Alori ignores the ask and answers for the sign-in's own app.
210
+ if (askedApp() && me.body.appId !== askedApp()) fail(`Alori answered for ${me.body.app}, not the app this folder holds. Update Alori's command (npm install -g @alori/cli) or try again later.`);
206
211
  return me.body;
207
212
  }
208
213
 
209
- function checkSameApp(state, me) {
210
- if (state.appId && state.appId !== me.appId) fail(`This folder holds ${state.app}, but you're signed in to ${me.app}. Run alori login with the right account.`);
214
+ /** The apps this sign-in may change (`GET /code/apps`). */
215
+ export async function appsList() {
216
+ const answer = await call("/code/apps");
217
+ if (!answer.ok) failOn(answer);
218
+ return answer.body.apps ?? [];
219
+ }
220
+
221
+ const plainName = (text) =>
222
+ text
223
+ .normalize("NFD")
224
+ .replace(/[\u0300-\u036f]/g, "")
225
+ .toLowerCase()
226
+ .trim();
227
+
228
+ /** `--app`: an app id, or a name matched exactly (case and accents ignored) against the apps this sign-in may change. */
229
+ async function appOf(wanted) {
230
+ const apps = await appsList();
231
+ const byId = apps.find((app) => app.appId === wanted);
232
+ if (byId) return byId.appId;
233
+ const named = apps.filter((app) => plainName(app.name) === plainName(wanted));
234
+ if (named.length === 1) return named[0].appId;
235
+ const list = apps.map((app) => ` ${app.name} (--app ${app.appId})`).join("\n");
236
+ // An id Alori didn't list may still be one support just opened: let Alori decide, it refuses what isn't reachable.
237
+ if (named.length === 0 && /^[a-z0-9]{20,}$/.test(wanted)) return wanted;
238
+ fail(named.length === 0 ? `No app named "${wanted}" for this sign-in.${list ? ` Yours:\n${list}` : ""}` : `Several apps are named "${wanted}". Pick one by id:\n${named.map((app) => ` ${app.name} (--app ${app.appId})`).join("\n")}`);
239
+ }
240
+
241
+ /**
242
+ * Which app this command works on: a pulled folder's own (always, so a folder never sends its files to another app),
243
+ * else `--app`, else the app the sign-in was made from.
244
+ */
245
+ async function chooseApp(dir, wanted) {
246
+ const state = readState(dir);
247
+ if (state?.appId) {
248
+ if (wanted && wanted !== state.appId && plainName(wanted) !== plainName(state.app ?? "")) fail(`This folder holds ${state.app}. Pull ${wanted} into another folder: alori pull <folder> --app ${wanted}`);
249
+ useApp(state.appId);
250
+ return;
251
+ }
252
+ useApp(wanted ? await appOf(wanted) : null);
211
253
  }
212
254
 
213
255
  /** A folder name from the app's name: "Pro-Quai Inc" → pro-quai-inc. */
@@ -226,12 +268,21 @@ function folderFor(named, me) {
226
268
  return resolve(folderNameOf(me.app));
227
269
  }
228
270
 
229
- export async function pull(named) {
271
+ /** The folder a command works in and the app it holds; a folder named after the app may be one pulled before. */
272
+ async function placeOf(named, wanted) {
273
+ const first = named ? resolve(named) : resolve(".");
274
+ await chooseApp(first, wanted);
230
275
  const me = await whoAmI();
231
276
  const dir = folderFor(named, me);
277
+ if (dir === first) return { me, dir };
278
+ await chooseApp(dir, wanted);
279
+ return { me: await whoAmI(), dir };
280
+ }
281
+
282
+ export async function pull(named, wanted) {
283
+ const { me, dir } = await placeOf(named, wanted);
232
284
  const state = readState(dir);
233
285
  if (state) {
234
- checkSameApp(state, me);
235
286
  await pullOver(dir, state, me);
236
287
  return;
237
288
  }
@@ -242,12 +293,10 @@ export async function pull(named) {
242
293
  }
243
294
 
244
295
  /** `alori dev`: pulls the app first when there is no pulled folder, installs what's missing, and runs it here. */
245
- export async function dev(named) {
246
- const me = await whoAmI();
247
- const dir = folderFor(named, me);
296
+ export async function dev(named, wanted) {
297
+ const { me, dir } = await placeOf(named, wanted);
248
298
  const state = readState(dir);
249
- if (state) checkSameApp(state, me);
250
- else {
299
+ if (!state) {
251
300
  if (existsSync(dir) && readdirSync(dir).some((name) => name !== ".DS_Store")) fail(`${dir} isn't empty and wasn't pulled from Alori. Run alori dev <new folder>.`);
252
301
  await pullFresh(dir, me);
253
302
  }
@@ -296,8 +345,8 @@ export async function push(dir, message) {
296
345
  const leftovers = walk(dir).filter((path) => path.endsWith(".alori-theirs"));
297
346
  if (leftovers.length > 0) fail(`Merge and delete these first:\n${leftovers.map((path) => ` ${path}`).join("\n")}`);
298
347
  if (state.needsPull) fail("Someone else saved changes while your last push built. Run alori pull first, then push.");
348
+ useApp(state.appId ?? null);
299
349
  const me = await whoAmI();
300
- checkSameApp(state, me);
301
350
  const { changed, deleted, now } = changesOf(dir, state);
302
351
  if (changed.length + deleted.length === 0) {
303
352
  console.log("Nothing changed since the last pull or push.");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alori/cli",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "Build your Alori app with your own Claude Code: pull its code, change it, push it back.",
5
5
  "type": "module",
6
6
  "bin": {