@alori/cli 0.1.2 → 0.1.4

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
@@ -15,4 +15,9 @@ looks right. How it works: `docs/CLAUDE_CODE.md` in Alori's repository.
15
15
  `alori dev` runs the app from your files against its real backend and data: screens and text change as you save,
16
16
  backend changes (`convex/`) reach it with `alori push`.
17
17
 
18
+ The folder is a git repository: each pull is a commit by Alori, so it opens in GitHub Desktop and can go to your own
19
+ GitHub. A push to GitHub deploys nothing; only `alori push` does. A pull keeps your edits, also to files outside `src/`
20
+ and `convex/` (`package.json`, `public/`); when Alori changed the same file, its version waits beside yours as
21
+ `<file>.alori-theirs`.
22
+
18
23
  No dependencies; Node 20 or newer, and `tar`. pnpm is used when installed, through npx otherwise.
package/lib/folder.mjs CHANGED
@@ -11,10 +11,21 @@ const stateFile = join(stateDir, "state.json");
11
11
 
12
12
  /**
13
13
  * The app's own code lives under `src/` and `convex/`. Generated files never travel: Alori writes them on each build.
14
- * Everything else (package.json, vite.config.ts, public/) is Alori's template and comes fresh with each pull.
14
+ * Everything else (package.json, vite.config.ts, public/) is Alori's template: see `isTemplateFile`.
15
15
  */
16
16
  export function isAppCode(path) {
17
- return (path.startsWith("src/") || path.startsWith("convex/")) && !path.startsWith("convex/_generated/") && path !== "src/routeTree.gen.ts" && !path.endsWith(".alori-theirs");
17
+ return (path.startsWith("src/") || path.startsWith("convex/")) && !path.startsWith("convex/_generated/") && path !== "src/routeTree.gen.ts" && !path.endsWith(".alori-theirs") && !path.endsWith(".alori-mine");
18
+ }
19
+
20
+ /** Files the CLI writes or Alori generates on each build, which always come fresh with a pull. */
21
+ const fresh = new Set(["CLAUDE.md", "AGENTS.md", ".gitignore", "src/routeTree.gen.ts"]);
22
+
23
+ /**
24
+ * The template's files a person may still change here (package.json, vite.config.ts, public/…): a pull keeps their
25
+ * edits the same way it keeps their app code, though `alori push` sends only `src/` and `convex/`.
26
+ */
27
+ export function isTemplateFile(path) {
28
+ return !isAppCode(path) && !fresh.has(path) && !path.startsWith("convex/_generated/") && !path.endsWith(".alori-theirs") && !path.endsWith(".alori-mine");
18
29
  }
19
30
 
20
31
  const skipped = new Set(["node_modules", ".git", stateDir, "dist", ".output", ".tanstack", ".vercel", ".DS_Store"]);
@@ -40,6 +51,19 @@ export function codeHashes(dir) {
40
51
  return hashes;
41
52
  }
42
53
 
54
+ /** The template's files in `dir` a person may change, by path, with each file's hash. */
55
+ export function templateHashes(dir) {
56
+ const hashes = {};
57
+ for (const path of walk(dir).filter(isTemplateFile)) hashes[path] = hashOf(readFileSync(join(dir, path)));
58
+ return hashes;
59
+ }
60
+
61
+ /** One file's hash, or undefined when it isn't there. */
62
+ export function fileHash(dir, path) {
63
+ const file = join(dir, path);
64
+ return existsSync(file) ? hashOf(readFileSync(file)) : undefined;
65
+ }
66
+
43
67
  export function readState(dir) {
44
68
  const file = join(dir, stateFile);
45
69
  if (!existsSync(file)) return null;
package/lib/git.mjs ADDED
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Git in the pulled folder, so GitHub Desktop, GitHub and Claude Code work with it. Each pull is a commit by Alori
3
+ * ("Pulled version 12"), apart from the person's own commits. Git stays on this computer: Alori never pushes to a
4
+ * remote, and a push to GitHub deploys nothing (only `alori push` does). A folder inside another repository is left
5
+ * alone: that repository is the person's to manage.
6
+ */
7
+ import { execFileSync } from "node:child_process";
8
+ import { existsSync, readFileSync, realpathSync, writeFileSync } from "node:fs";
9
+ import { join } from "node:path";
10
+
11
+ /** Pull commits are Alori's, which also works on a computer where git has no name or email set. */
12
+ const asAlori = {
13
+ ...process.env,
14
+ GIT_AUTHOR_NAME: "Alori",
15
+ GIT_AUTHOR_EMAIL: "noreply@alori.dev",
16
+ GIT_COMMITTER_NAME: "Alori",
17
+ GIT_COMMITTER_EMAIL: "noreply@alori.dev",
18
+ };
19
+
20
+ function git(dir, args) {
21
+ return execFileSync("git", ["-C", dir, ...args], { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], env: asAlori });
22
+ }
23
+
24
+ function hasGit() {
25
+ try {
26
+ execFileSync("git", ["--version"], { stdio: "ignore" });
27
+ return true;
28
+ } catch {
29
+ return false;
30
+ }
31
+ }
32
+
33
+ /** "own": `dir` is the root of its own repository; "none": no repository; "parent": inside someone else's. */
34
+ function repoOf(dir) {
35
+ try {
36
+ const top = git(dir, ["rev-parse", "--show-toplevel"]).trim();
37
+ return realpathSync(top) === realpathSync(dir) ? "own" : "parent";
38
+ } catch {
39
+ return "none";
40
+ }
41
+ }
42
+
43
+ /** Lines every pulled folder's .gitignore needs: the template's own file comes fresh with each pull and lacks them. */
44
+ export function ensureIgnored(dir, lines) {
45
+ const file = join(dir, ".gitignore");
46
+ const current = existsSync(file) ? readFileSync(file, "utf8") : "";
47
+ const have = new Set(current.split("\n").map((line) => line.trim()));
48
+ const missing = lines.filter((line) => !have.has(line));
49
+ if (missing.length === 0) return;
50
+ writeFileSync(file, `${current}${current === "" || current.endsWith("\n") ? "" : "\n"}${missing.join("\n")}\n`);
51
+ }
52
+
53
+ /**
54
+ * Before a pull lays Alori's files down: whether the person has changes not committed yet. A pull commits only into a
55
+ * clean folder, so their work never lands inside Alori's commit.
56
+ */
57
+ export function hadChanges(dir) {
58
+ if (repoOf(dir) !== "own") return false;
59
+ try {
60
+ return git(dir, ["status", "--porcelain"]).trim() !== "";
61
+ } catch {
62
+ return false;
63
+ }
64
+ }
65
+
66
+ /**
67
+ * A pull into a folder pulled before it had git: starts the repository with Alori's version alone as its first commit,
68
+ * built from the folder `snapshot()` makes (the downloaded code), so the person's own edits in `dir` show as changes on top of it, never
69
+ * inside Alori's commit. "started", "failed" (it said why; the folder must not get a pull commit either), or null
70
+ * when the folder already has git or git isn't installed.
71
+ */
72
+ export function startRepoFrom(dir, snapshot, version) {
73
+ if (!hasGit() || repoOf(dir) !== "none") return null;
74
+ try {
75
+ const tree = snapshot();
76
+ git(dir, ["init", "-q", "-b", "main"]);
77
+ const onSnapshot = ["--git-dir", join(dir, ".git"), "--work-tree", tree];
78
+ git(dir, [...onSnapshot, "add", "-A"]);
79
+ git(dir, [...onSnapshot, "commit", "-q", "-m", `Pulled version ${version} from Alori`]);
80
+ return "started";
81
+ } catch (error) {
82
+ console.log(`Git couldn't start in this folder (${String(error.stderr ?? error.message).trim().split("\n")[0]}). Your files are pulled.`);
83
+ return "failed";
84
+ }
85
+ }
86
+
87
+ /** After a pull: starts the repository when there is none, and commits what the pull brought. */
88
+ export function commitPull(dir, version, { hadChanges }) {
89
+ if (!hasGit()) return;
90
+ const repo = repoOf(dir);
91
+ if (repo === "parent") return;
92
+ try {
93
+ if (repo === "none") {
94
+ git(dir, ["init", "-b", "main"]);
95
+ git(dir, ["add", "-A"]);
96
+ git(dir, ["commit", "-q", "-m", `Pulled version ${version} from Alori`]);
97
+ console.log("Started git in this folder: open it in GitHub Desktop, or add your own GitHub remote.");
98
+ return;
99
+ }
100
+ if (hadChanges) {
101
+ console.log("Your folder had changes not committed, so this pull isn't a commit of its own. Commit when ready.");
102
+ return;
103
+ }
104
+ git(dir, ["add", "-A"]);
105
+ if (git(dir, ["status", "--porcelain"]).trim() === "") return;
106
+ git(dir, ["commit", "-q", "-m", `Pulled version ${version} from Alori`]);
107
+ } catch (error) {
108
+ console.log(`Git couldn't commit this pull (${String(error.stderr ?? error.message).trim().split("\n")[0]}). Your files are pulled.`);
109
+ }
110
+ }
package/lib/guide.mjs CHANGED
@@ -16,6 +16,12 @@ with \`alori push\`. Alori builds them, deploys them, and shows them in the work
16
16
  even before Publish. Never drop a table or a field that holds data.
17
17
  - \`convex/_generated/\` and \`src/routeTree.gen.ts\` are generated: never edit them.
18
18
 
19
+ ## Git and GitHub
20
+
21
+ This folder is a git repository, and each \`alori pull\` is a commit by Alori ("Pulled version 12 from Alori"), so
22
+ \`git log\` shows Alori's changes apart from yours. Commit your own work as usual, and push it to your own GitHub if you
23
+ like. **GitHub is only a copy:** a push to GitHub deploys nothing. Only \`alori push\` sends changes to the app.
24
+
19
25
  ## Seeing changes
20
26
 
21
27
  \`alori dev\` runs the app at http://localhost:5300 (or the next free port) from this folder's files, against the app's
package/lib/sync.mjs CHANGED
@@ -4,12 +4,13 @@
4
4
  * the preview and saves as a new version.
5
5
  */
6
6
  import { execFileSync } from "node:child_process";
7
- import { copyFileSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, unlinkSync, writeFileSync } from "node:fs";
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
10
  import { apiBase, call } from "./auth.mjs";
11
- import { changesOf, codeHashes, isAppCode, readState, stateDir, walk, writeState } from "./folder.mjs";
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
+ import { commitPull, ensureIgnored, hadChanges, startRepoFrom } from "./git.mjs";
13
14
  import { claudeGuide } from "./guide.mjs";
14
15
 
15
16
  const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
@@ -71,14 +72,8 @@ function copyInto(from, to, path) {
71
72
  copyFileSync(join(from, path), join(to, path));
72
73
  }
73
74
 
74
- const ignoreFile = `node_modules/
75
- ${stateDir}/
76
- dist/
77
- .output/
78
- .tanstack/
79
- .env*
80
- *.alori-theirs
81
- `;
75
+ /** Kept out of git: installed packages, builds, secrets, the pull snapshot and the copies a pull puts beside files. */
76
+ const ignoreLines = ["node_modules/", `${stateDir}/`, "dist/", ".output/", ".tanstack/", ".vercel/", ".env*", "*.alori-theirs", "*.alori-mine"];
82
77
 
83
78
  /** pnpm 11 stops `pnpm install` until each package's build script is allowed or refused. */
84
79
  const workspaceFile = `allowBuilds:
@@ -93,34 +88,41 @@ async function pullFresh(dir, me) {
93
88
  const { folder, temp, version } = await download();
94
89
  mkdirSync(dir, { recursive: true });
95
90
  for (const path of walk(folder).filter((path) => !templateNotes.has(path))) copyInto(folder, dir, path);
91
+ const template = templateHashes(folder);
96
92
  rmSync(temp, { recursive: true, force: true });
97
93
  writeFileSync(join(dir, "CLAUDE.md"), claudeGuide(me.app));
98
- if (!existsSync(join(dir, ".gitignore"))) writeFileSync(join(dir, ".gitignore"), ignoreFile);
94
+ ensureIgnored(dir, ignoreLines);
99
95
  if (!existsSync(join(dir, "pnpm-workspace.yaml"))) writeFileSync(join(dir, "pnpm-workspace.yaml"), workspaceFile);
100
- writeState(dir, { api: apiBase(), appId: me.appId, app: me.app, version, hashes: codeHashes(dir) });
96
+ writeState(dir, { api: apiBase(), appId: me.appId, app: me.app, version, hashes: codeHashes(dir), templateHashes: template });
101
97
  console.log(`Pulled ${me.app}, version ${version}, into ${dir}`);
98
+ commitPull(dir, version, { hadChanges: false });
102
99
  install(dir);
103
100
  }
104
101
 
105
102
  /**
106
- * A pull into a folder already pulled: Alori's latest code comes in, and the person's changes stay. A file both sides
107
- * changed keeps the person's version and gets Alori's beside it as `<file>.alori-theirs`.
103
+ * Three-way merge of `paths` into `dir` from Alori's `folder`, against `base` (their hashes at the last pull). A file
104
+ * changed only in Alori comes in; one changed only here stays. One changed on both sides keeps the person's version
105
+ * and gets Alori's beside it as `<file>.alori-theirs`. Without a base (a folder pulled by an older CLI, which kept no
106
+ * hashes of the template's files) nothing tells who changed a file, so Alori's comes in and the person's copy stays
107
+ * beside it as `<file>.alori-mine`: the app keeps building and no edit is lost.
108
108
  */
109
- async function pullOver(dir, state, me) {
110
- const { folder, temp, version } = await download();
111
- const remote = codeHashes(folder);
112
- const local = codeHashes(dir);
109
+ function mergeInto(dir, folder, paths, base) {
113
110
  const overlaps = [];
114
- for (const path of new Set([...Object.keys(remote), ...Object.keys(local), ...Object.keys(state.hashes)])) {
115
- const base = state.hashes[path];
116
- const mine = local[path];
117
- const theirs = remote[path];
111
+ const kept = [];
112
+ for (const path of paths) {
113
+ const before = base?.[path];
114
+ const mine = fileHash(dir, path);
115
+ const theirs = fileHash(folder, path);
118
116
  if (mine === theirs) continue;
119
- if (mine === base) {
117
+ if (mine === before) {
120
118
  // Unchanged here: take Alori's.
121
119
  if (theirs === undefined) rmSync(join(dir, path), { force: true });
122
120
  else copyInto(folder, dir, path);
123
- } else if (theirs !== base) {
121
+ } else if (!base && theirs !== undefined) {
122
+ copyFileSync(join(dir, path), join(dir, `${path}.alori-mine`));
123
+ copyInto(folder, dir, path);
124
+ kept.push(path);
125
+ } else if (theirs !== before) {
124
126
  // Changed on both sides.
125
127
  if (theirs === undefined) overlaps.push(`${path} (removed in Alori)`);
126
128
  else {
@@ -129,19 +131,54 @@ async function pullOver(dir, state, me) {
129
131
  }
130
132
  }
131
133
  }
132
- // Alori's template files (package.json, vite.config.ts, public/…) always come fresh.
133
- const lockfile = (root) => (existsSync(join(root, "pnpm-lock.yaml")) ? readFileSync(join(root, "pnpm-lock.yaml"), "utf8") : "");
134
- const packagesChanged = lockfile(folder) !== lockfile(dir);
135
- for (const path of walk(folder).filter((path) => !isAppCode(path) && !templateNotes.has(path))) copyInto(folder, dir, path);
134
+ return { overlaps, kept };
135
+ }
136
+
137
+ /**
138
+ * Alori's version as the folder would hold it with no edits of the person's: the downloaded code plus the files the
139
+ * CLI writes. It becomes the first commit of a folder pulled before it had git.
140
+ */
141
+ function snapshotOf(dir, folder, temp) {
142
+ const snapshot = join(temp, "snapshot");
143
+ cpSync(folder, snapshot, { recursive: true });
144
+ for (const name of templateNotes) rmSync(join(snapshot, name), { force: true });
145
+ for (const name of ["CLAUDE.md", "pnpm-workspace.yaml"]) {
146
+ if (existsSync(join(dir, name)) && !existsSync(join(snapshot, name))) copyFileSync(join(dir, name), join(snapshot, name));
147
+ }
148
+ ensureIgnored(snapshot, ignoreLines);
149
+ return snapshot;
150
+ }
151
+
152
+ /** A pull into a folder already pulled: Alori's latest code comes in, and the person's changes stay (`mergeInto`). */
153
+ async function pullOver(dir, state, me) {
154
+ const { folder, temp, version } = await download();
155
+ const repo = startRepoFrom(dir, () => snapshotOf(dir, folder, temp), version);
156
+ const uncommitted = hadChanges(dir);
157
+ const lockBefore = fileHash(dir, "pnpm-lock.yaml");
158
+ const code = mergeInto(dir, folder, new Set([...Object.keys(codeHashes(folder)), ...Object.keys(codeHashes(dir)), ...Object.keys(state.hashes)]), state.hashes);
159
+ const remoteTemplate = templateHashes(folder);
160
+ const template = mergeInto(dir, folder, new Set([...Object.keys(remoteTemplate), ...Object.keys(state.templateHashes ?? {})]), state.templateHashes);
161
+ // What Alori writes for every app (the .gitignore, generated files) always comes fresh.
162
+ for (const path of walk(folder).filter((path) => !isAppCode(path) && !isTemplateFile(path) && !templateNotes.has(path))) copyInto(folder, dir, path);
163
+ writeState(dir, { api: apiBase(), appId: me.appId, app: me.app, version, hashes: codeHashes(folder), templateHashes: remoteTemplate });
136
164
  rmSync(temp, { recursive: true, force: true });
137
- writeState(dir, { api: apiBase(), appId: me.appId, app: me.app, version, hashes: remote });
165
+ ensureIgnored(dir, ignoreLines);
138
166
  console.log(`Pulled ${me.app}, version ${version}.`);
139
- if (packagesChanged || !installed(dir)) install(dir);
167
+ if (repo === "started") {
168
+ console.log(`Started git in this folder: its first commit is Alori's version ${version}${hadChanges(dir) ? ", and your own edits show as changes to commit" : ""}. Open it in GitHub Desktop.`);
169
+ } else if (repo === null) commitPull(dir, version, { hadChanges: uncommitted });
170
+ if (fileHash(dir, "pnpm-lock.yaml") !== lockBefore || !installed(dir)) install(dir);
171
+ const overlaps = [...code.overlaps, ...template.overlaps];
140
172
  if (overlaps.length > 0) {
141
173
  console.log("\nThese files changed both here and in Alori. Yours are kept; Alori's are beside them as .alori-theirs:");
142
174
  for (const path of overlaps) console.log(` ${path}`);
143
175
  console.log("Merge each one, delete the .alori-theirs files, then push.");
144
176
  }
177
+ if (template.kept.length > 0) {
178
+ console.log("\nThese files differ from Alori's, and this folder can't tell who changed them. Alori's are in; yours are beside them as .alori-mine:");
179
+ for (const path of template.kept) console.log(` ${path}`);
180
+ console.log("Bring back any edit of yours you still want, then delete the .alori-mine files. Only src/ and convex/ go up with alori push.");
181
+ }
145
182
  }
146
183
 
147
184
  async function whoAmI() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alori/cli",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
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": {