@alori/cli 0.1.3 → 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 +3 -1
- package/lib/folder.mjs +26 -2
- package/lib/git.mjs +21 -0
- package/lib/sync.mjs +64 -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
|
@@ -63,6 +63,27 @@ export function hadChanges(dir) {
|
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
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
|
+
|
|
66
87
|
/** After a pull: starts the repository when there is none, and commits what the pull brought. */
|
|
67
88
|
export function commitPull(dir, version, { hadChanges }) {
|
|
68
89
|
if (!hasGit()) return;
|
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,8 @@ function copyInto(from, to, path) {
|
|
|
72
72
|
copyFileSync(join(from, path), join(to, path));
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
/** Kept out of git: installed packages, builds, secrets, the pull snapshot and
|
|
76
|
-
const ignoreLines = ["node_modules/", `${stateDir}/`, "dist/", ".output/", ".tanstack/", ".vercel/", ".env*", "*.alori-theirs"];
|
|
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"];
|
|
77
77
|
|
|
78
78
|
/** pnpm 11 stops `pnpm install` until each package's build script is allowed or refused. */
|
|
79
79
|
const workspaceFile = `allowBuilds:
|
|
@@ -88,36 +88,41 @@ async function pullFresh(dir, me) {
|
|
|
88
88
|
const { folder, temp, version } = await download();
|
|
89
89
|
mkdirSync(dir, { recursive: true });
|
|
90
90
|
for (const path of walk(folder).filter((path) => !templateNotes.has(path))) copyInto(folder, dir, path);
|
|
91
|
+
const template = templateHashes(folder);
|
|
91
92
|
rmSync(temp, { recursive: true, force: true });
|
|
92
93
|
writeFileSync(join(dir, "CLAUDE.md"), claudeGuide(me.app));
|
|
93
94
|
ensureIgnored(dir, ignoreLines);
|
|
94
95
|
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) });
|
|
96
|
+
writeState(dir, { api: apiBase(), appId: me.appId, app: me.app, version, hashes: codeHashes(dir), templateHashes: template });
|
|
96
97
|
console.log(`Pulled ${me.app}, version ${version}, into ${dir}`);
|
|
97
98
|
commitPull(dir, version, { hadChanges: false });
|
|
98
99
|
install(dir);
|
|
99
100
|
}
|
|
100
101
|
|
|
101
102
|
/**
|
|
102
|
-
*
|
|
103
|
-
* changed
|
|
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.
|
|
104
108
|
*/
|
|
105
|
-
|
|
106
|
-
const { folder, temp, version } = await download();
|
|
107
|
-
const uncommitted = hadChanges(dir);
|
|
108
|
-
const remote = codeHashes(folder);
|
|
109
|
-
const local = codeHashes(dir);
|
|
109
|
+
function mergeInto(dir, folder, paths, base) {
|
|
110
110
|
const overlaps = [];
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const
|
|
114
|
-
const
|
|
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);
|
|
115
116
|
if (mine === theirs) continue;
|
|
116
|
-
if (mine ===
|
|
117
|
+
if (mine === before) {
|
|
117
118
|
// Unchanged here: take Alori's.
|
|
118
119
|
if (theirs === undefined) rmSync(join(dir, path), { force: true });
|
|
119
120
|
else copyInto(folder, dir, path);
|
|
120
|
-
} else if (theirs !==
|
|
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) {
|
|
121
126
|
// Changed on both sides.
|
|
122
127
|
if (theirs === undefined) overlaps.push(`${path} (removed in Alori)`);
|
|
123
128
|
else {
|
|
@@ -126,21 +131,54 @@ async function pullOver(dir, state, me) {
|
|
|
126
131
|
}
|
|
127
132
|
}
|
|
128
133
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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 });
|
|
133
164
|
rmSync(temp, { recursive: true, force: true });
|
|
134
165
|
ensureIgnored(dir, ignoreLines);
|
|
135
|
-
writeState(dir, { api: apiBase(), appId: me.appId, app: me.app, version, hashes: remote });
|
|
136
166
|
console.log(`Pulled ${me.app}, version ${version}.`);
|
|
137
|
-
|
|
138
|
-
|
|
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];
|
|
139
172
|
if (overlaps.length > 0) {
|
|
140
173
|
console.log("\nThese files changed both here and in Alori. Yours are kept; Alori's are beside them as .alori-theirs:");
|
|
141
174
|
for (const path of overlaps) console.log(` ${path}`);
|
|
142
175
|
console.log("Merge each one, delete the .alori-theirs files, then push.");
|
|
143
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
|
+
}
|
|
144
182
|
}
|
|
145
183
|
|
|
146
184
|
async function whoAmI() {
|