@alori/cli 0.1.3 → 0.1.5
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 +3 -1
- package/lib/folder.mjs +26 -2
- package/lib/git.mjs +37 -1
- package/lib/sync.mjs +83 -26
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -16,6 +16,8 @@ looks right. How it works: `docs/CLAUDE_CODE.md` in Alori's repository.
|
|
|
16
16
|
backend changes (`convex/`) reach it with `alori push`.
|
|
17
17
|
|
|
18
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.
|
|
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`.
|
|
20
22
|
|
|
21
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
|
|
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
CHANGED
|
@@ -57,12 +57,46 @@ export function ensureIgnored(dir, lines) {
|
|
|
57
57
|
export function hadChanges(dir) {
|
|
58
58
|
if (repoOf(dir) !== "own") return false;
|
|
59
59
|
try {
|
|
60
|
-
|
|
60
|
+
// The .gitignore comes fresh with every pull, and a file it now leaves out stops being tracked in the pull's commit.
|
|
61
|
+
const alorisOwn = new Set([".gitignore", ...trackedIgnored(dir)]);
|
|
62
|
+
return git(dir, ["status", "--porcelain", "-z"])
|
|
63
|
+
.split("\0")
|
|
64
|
+
.filter(Boolean)
|
|
65
|
+
.some((entry) => !alorisOwn.has(entry.slice(3)));
|
|
61
66
|
} catch {
|
|
62
67
|
return false;
|
|
63
68
|
}
|
|
64
69
|
}
|
|
65
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Tracked files the .gitignore now leaves out, such as the route tree an older pull committed. The pull's commit stops
|
|
73
|
+
* tracking them; they stay on disk.
|
|
74
|
+
*/
|
|
75
|
+
function trackedIgnored(dir) {
|
|
76
|
+
return git(dir, ["ls-files", "-ci", "--exclude-standard", "-z"]).split("\0").filter(Boolean);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* A pull into a folder pulled before it had git: starts the repository with Alori's version alone as its first commit,
|
|
81
|
+
* 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
|
|
82
|
+
* inside Alori's commit. "started", "failed" (it said why; the folder must not get a pull commit either), or null
|
|
83
|
+
* when the folder already has git or git isn't installed.
|
|
84
|
+
*/
|
|
85
|
+
export function startRepoFrom(dir, snapshot, version) {
|
|
86
|
+
if (!hasGit() || repoOf(dir) !== "none") return null;
|
|
87
|
+
try {
|
|
88
|
+
const tree = snapshot();
|
|
89
|
+
git(dir, ["init", "-q", "-b", "main"]);
|
|
90
|
+
const onSnapshot = ["--git-dir", join(dir, ".git"), "--work-tree", tree];
|
|
91
|
+
git(dir, [...onSnapshot, "add", "-A"]);
|
|
92
|
+
git(dir, [...onSnapshot, "commit", "-q", "-m", `Pulled version ${version} from Alori`]);
|
|
93
|
+
return "started";
|
|
94
|
+
} catch (error) {
|
|
95
|
+
console.log(`Git couldn't start in this folder (${String(error.stderr ?? error.message).trim().split("\n")[0]}). Your files are pulled.`);
|
|
96
|
+
return "failed";
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
66
100
|
/** After a pull: starts the repository when there is none, and commits what the pull brought. */
|
|
67
101
|
export function commitPull(dir, version, { hadChanges }) {
|
|
68
102
|
if (!hasGit()) return;
|
|
@@ -80,6 +114,8 @@ export function commitPull(dir, version, { hadChanges }) {
|
|
|
80
114
|
console.log("Your folder had changes not committed, so this pull isn't a commit of its own. Commit when ready.");
|
|
81
115
|
return;
|
|
82
116
|
}
|
|
117
|
+
const stale = trackedIgnored(dir);
|
|
118
|
+
if (stale.length > 0) git(dir, ["rm", "--cached", "-q", "--", ...stale]);
|
|
83
119
|
git(dir, ["add", "-A"]);
|
|
84
120
|
if (git(dir, ["status", "--porcelain"]).trim() === "") return;
|
|
85
121
|
git(dir, ["commit", "-q", "-m", `Pulled version ${version} from Alori`]);
|
package/lib/sync.mjs
CHANGED
|
@@ -4,13 +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 } from "./git.mjs";
|
|
13
|
+
import { commitPull, ensureIgnored, hadChanges, startRepoFrom } from "./git.mjs";
|
|
14
14
|
import { claudeGuide } from "./guide.mjs";
|
|
15
15
|
|
|
16
16
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
@@ -72,8 +72,25 @@ function copyInto(from, to, path) {
|
|
|
72
72
|
copyFileSync(join(from, path), join(to, path));
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
/**
|
|
76
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Kept out of git: installed packages, builds, secrets, the pull snapshot and the copies a pull puts beside files, plus
|
|
77
|
+
* what this computer makes on its own: the lockfile the install writes, the route tree the dev server regenerates for
|
|
78
|
+
* the app's own pages, and macOS's folder files. Left tracked, a fresh pull shows changes nobody made.
|
|
79
|
+
*/
|
|
80
|
+
const ignoreLines = [
|
|
81
|
+
"node_modules/",
|
|
82
|
+
`${stateDir}/`,
|
|
83
|
+
"dist/",
|
|
84
|
+
".output/",
|
|
85
|
+
".tanstack/",
|
|
86
|
+
".vercel/",
|
|
87
|
+
".env*",
|
|
88
|
+
"*.alori-theirs",
|
|
89
|
+
"*.alori-mine",
|
|
90
|
+
"pnpm-lock.yaml",
|
|
91
|
+
"src/routeTree.gen.ts",
|
|
92
|
+
".DS_Store",
|
|
93
|
+
];
|
|
77
94
|
|
|
78
95
|
/** pnpm 11 stops `pnpm install` until each package's build script is allowed or refused. */
|
|
79
96
|
const workspaceFile = `allowBuilds:
|
|
@@ -88,36 +105,41 @@ async function pullFresh(dir, me) {
|
|
|
88
105
|
const { folder, temp, version } = await download();
|
|
89
106
|
mkdirSync(dir, { recursive: true });
|
|
90
107
|
for (const path of walk(folder).filter((path) => !templateNotes.has(path))) copyInto(folder, dir, path);
|
|
108
|
+
const template = templateHashes(folder);
|
|
91
109
|
rmSync(temp, { recursive: true, force: true });
|
|
92
110
|
writeFileSync(join(dir, "CLAUDE.md"), claudeGuide(me.app));
|
|
93
111
|
ensureIgnored(dir, ignoreLines);
|
|
94
112
|
if (!existsSync(join(dir, "pnpm-workspace.yaml"))) writeFileSync(join(dir, "pnpm-workspace.yaml"), workspaceFile);
|
|
95
|
-
writeState(dir, { api: apiBase(), appId: me.appId, app: me.app, version, hashes: codeHashes(dir) });
|
|
113
|
+
writeState(dir, { api: apiBase(), appId: me.appId, app: me.app, version, hashes: codeHashes(dir), templateHashes: template });
|
|
96
114
|
console.log(`Pulled ${me.app}, version ${version}, into ${dir}`);
|
|
97
115
|
commitPull(dir, version, { hadChanges: false });
|
|
98
116
|
install(dir);
|
|
99
117
|
}
|
|
100
118
|
|
|
101
119
|
/**
|
|
102
|
-
*
|
|
103
|
-
* changed
|
|
120
|
+
* Three-way merge of `paths` into `dir` from Alori's `folder`, against `base` (their hashes at the last pull). A file
|
|
121
|
+
* changed only in Alori comes in; one changed only here stays. One changed on both sides keeps the person's version
|
|
122
|
+
* and gets Alori's beside it as `<file>.alori-theirs`. Without a base (a folder pulled by an older CLI, which kept no
|
|
123
|
+
* hashes of the template's files) nothing tells who changed a file, so Alori's comes in and the person's copy stays
|
|
124
|
+
* beside it as `<file>.alori-mine`: the app keeps building and no edit is lost.
|
|
104
125
|
*/
|
|
105
|
-
|
|
106
|
-
const { folder, temp, version } = await download();
|
|
107
|
-
const uncommitted = hadChanges(dir);
|
|
108
|
-
const remote = codeHashes(folder);
|
|
109
|
-
const local = codeHashes(dir);
|
|
126
|
+
function mergeInto(dir, folder, paths, base) {
|
|
110
127
|
const overlaps = [];
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const
|
|
114
|
-
const
|
|
128
|
+
const kept = [];
|
|
129
|
+
for (const path of paths) {
|
|
130
|
+
const before = base?.[path];
|
|
131
|
+
const mine = fileHash(dir, path);
|
|
132
|
+
const theirs = fileHash(folder, path);
|
|
115
133
|
if (mine === theirs) continue;
|
|
116
|
-
if (mine ===
|
|
134
|
+
if (mine === before) {
|
|
117
135
|
// Unchanged here: take Alori's.
|
|
118
136
|
if (theirs === undefined) rmSync(join(dir, path), { force: true });
|
|
119
137
|
else copyInto(folder, dir, path);
|
|
120
|
-
} else if (theirs !==
|
|
138
|
+
} else if (!base && theirs !== undefined) {
|
|
139
|
+
copyFileSync(join(dir, path), join(dir, `${path}.alori-mine`));
|
|
140
|
+
copyInto(folder, dir, path);
|
|
141
|
+
kept.push(path);
|
|
142
|
+
} else if (theirs !== before) {
|
|
121
143
|
// Changed on both sides.
|
|
122
144
|
if (theirs === undefined) overlaps.push(`${path} (removed in Alori)`);
|
|
123
145
|
else {
|
|
@@ -126,21 +148,56 @@ async function pullOver(dir, state, me) {
|
|
|
126
148
|
}
|
|
127
149
|
}
|
|
128
150
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
151
|
+
return { overlaps, kept };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Alori's version as the folder would hold it with no edits of the person's: the downloaded code plus the files the
|
|
156
|
+
* CLI writes. It becomes the first commit of a folder pulled before it had git.
|
|
157
|
+
*/
|
|
158
|
+
function snapshotOf(dir, folder, temp) {
|
|
159
|
+
const snapshot = join(temp, "snapshot");
|
|
160
|
+
cpSync(folder, snapshot, { recursive: true });
|
|
161
|
+
for (const name of templateNotes) rmSync(join(snapshot, name), { force: true });
|
|
162
|
+
for (const name of ["CLAUDE.md", "pnpm-workspace.yaml"]) {
|
|
163
|
+
if (existsSync(join(dir, name)) && !existsSync(join(snapshot, name))) copyFileSync(join(dir, name), join(snapshot, name));
|
|
164
|
+
}
|
|
165
|
+
ensureIgnored(snapshot, ignoreLines);
|
|
166
|
+
return snapshot;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** A pull into a folder already pulled: Alori's latest code comes in, and the person's changes stay (`mergeInto`). */
|
|
170
|
+
async function pullOver(dir, state, me) {
|
|
171
|
+
const { folder, temp, version } = await download();
|
|
172
|
+
const repo = startRepoFrom(dir, () => snapshotOf(dir, folder, temp), version);
|
|
173
|
+
// The ignore list first, so what this computer generated (the lockfile, .DS_Store) doesn't count as the person's changes.
|
|
174
|
+
ensureIgnored(dir, ignoreLines);
|
|
175
|
+
const uncommitted = hadChanges(dir);
|
|
176
|
+
const lockBefore = fileHash(dir, "pnpm-lock.yaml");
|
|
177
|
+
const code = mergeInto(dir, folder, new Set([...Object.keys(codeHashes(folder)), ...Object.keys(codeHashes(dir)), ...Object.keys(state.hashes)]), state.hashes);
|
|
178
|
+
const remoteTemplate = templateHashes(folder);
|
|
179
|
+
const template = mergeInto(dir, folder, new Set([...Object.keys(remoteTemplate), ...Object.keys(state.templateHashes ?? {})]), state.templateHashes);
|
|
180
|
+
// What Alori writes for every app (the .gitignore, generated files) always comes fresh.
|
|
181
|
+
for (const path of walk(folder).filter((path) => !isAppCode(path) && !isTemplateFile(path) && !templateNotes.has(path))) copyInto(folder, dir, path);
|
|
182
|
+
writeState(dir, { api: apiBase(), appId: me.appId, app: me.app, version, hashes: codeHashes(folder), templateHashes: remoteTemplate });
|
|
133
183
|
rmSync(temp, { recursive: true, force: true });
|
|
134
184
|
ensureIgnored(dir, ignoreLines);
|
|
135
|
-
writeState(dir, { api: apiBase(), appId: me.appId, app: me.app, version, hashes: remote });
|
|
136
185
|
console.log(`Pulled ${me.app}, version ${version}.`);
|
|
137
|
-
|
|
138
|
-
|
|
186
|
+
if (repo === "started") {
|
|
187
|
+
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.`);
|
|
188
|
+
} else if (repo === null) commitPull(dir, version, { hadChanges: uncommitted });
|
|
189
|
+
if (fileHash(dir, "pnpm-lock.yaml") !== lockBefore || !installed(dir)) install(dir);
|
|
190
|
+
const overlaps = [...code.overlaps, ...template.overlaps];
|
|
139
191
|
if (overlaps.length > 0) {
|
|
140
192
|
console.log("\nThese files changed both here and in Alori. Yours are kept; Alori's are beside them as .alori-theirs:");
|
|
141
193
|
for (const path of overlaps) console.log(` ${path}`);
|
|
142
194
|
console.log("Merge each one, delete the .alori-theirs files, then push.");
|
|
143
195
|
}
|
|
196
|
+
if (template.kept.length > 0) {
|
|
197
|
+
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:");
|
|
198
|
+
for (const path of template.kept) console.log(` ${path}`);
|
|
199
|
+
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.");
|
|
200
|
+
}
|
|
144
201
|
}
|
|
145
202
|
|
|
146
203
|
async function whoAmI() {
|