@alori/cli 0.1.7 → 0.1.10
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 +6 -0
- package/bin/alori.mjs +51 -8
- package/lib/auth.mjs +19 -1
- package/lib/guide.mjs +6 -3
- package/lib/prospect.mjs +43 -0
- package/lib/sync.mjs +70 -15
- package/lib/upload.mjs +65 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,6 +14,12 @@ 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
|
+
|
|
20
|
+
`alori upload docs/*.pdf` puts files (PDFs, photos) in your app's own storage and prints each one's address, to link
|
|
21
|
+
from your site's code.
|
|
22
|
+
|
|
17
23
|
`alori dev` runs the app from your files against its real backend and data: screens and text change as you save,
|
|
18
24
|
backend changes (`convex/`) reach it with `alori push`.
|
|
19
25
|
|
package/bin/alori.mjs
CHANGED
|
@@ -4,26 +4,44 @@
|
|
|
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 {
|
|
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";
|
|
10
|
+
import { upload } from "../lib/upload.mjs";
|
|
11
|
+
import { prospect } from "../lib/prospect.mjs";
|
|
9
12
|
|
|
10
13
|
const help = `alori: build your Alori app with Claude Code
|
|
11
14
|
|
|
12
15
|
alori login Connect this computer to your Alori account
|
|
13
16
|
alori dev [folder] Download your app, install it and run it on this computer (opens your browser)
|
|
14
17
|
alori pull [folder] Download your app's code (or bring the latest into a pulled folder)
|
|
18
|
+
--app <name or id> With dev or pull, for a new folder: which app (a client's). A pulled folder keeps its own
|
|
19
|
+
alori apps The apps you can change: yours, your agency's clients, the ones support opened
|
|
15
20
|
alori status What changed since the last pull or push
|
|
16
21
|
alori push -m "message" Send your changes: Alori builds them and saves a new version
|
|
22
|
+
alori upload <files…> Put files (PDFs, photos) in your app's storage and print each one's address
|
|
23
|
+
--json Print the addresses as JSON
|
|
24
|
+
alori prospect <website> Alori team: a demo of a prospect's site at <name>.prospects.aloriapp.com
|
|
25
|
+
--name <company> The name on its "Made for" bar (else taken from the address)
|
|
17
26
|
alori whoami Which app you're connected to
|
|
18
27
|
alori logout Forget this computer's sign-in
|
|
19
28
|
|
|
20
29
|
Set ALORI_API to use another Alori than ${apiBase()}.`;
|
|
21
30
|
|
|
22
|
-
|
|
23
|
-
|
|
31
|
+
/** The value after one of these flags, or undefined. */
|
|
32
|
+
function flagOf(args, names) {
|
|
33
|
+
const index = args.findIndex((arg) => names.includes(arg));
|
|
24
34
|
return index >= 0 ? args[index + 1] : undefined;
|
|
25
35
|
}
|
|
26
36
|
|
|
37
|
+
const messageOf = (args) => flagOf(args, ["-m", "--message"]);
|
|
38
|
+
const appArgOf = (args) => flagOf(args, ["--app", "-a"]);
|
|
39
|
+
|
|
40
|
+
/** The folder named on the line: the first word that is neither a flag nor a flag's value. */
|
|
41
|
+
function folderArgOf(args) {
|
|
42
|
+
return args.find((arg, index) => !arg.startsWith("-") && !["--app", "-a", "-m", "--message"].includes(args[index - 1] ?? ""));
|
|
43
|
+
}
|
|
44
|
+
|
|
27
45
|
/** Runs a command that needs a sign-in, signing in first when this computer isn't connected yet. */
|
|
28
46
|
async function signedIn(run) {
|
|
29
47
|
try {
|
|
@@ -42,7 +60,7 @@ async function main() {
|
|
|
42
60
|
await login();
|
|
43
61
|
const me = await call("/code/me");
|
|
44
62
|
if (!me.ok) {
|
|
45
|
-
console.log(me.body?.error === "cantBuild" ? "Connected, but your role doesn't build this app." : "Connected.");
|
|
63
|
+
console.log(me.body?.error === "superAdminsOnly" ? "The alori command is only for the Alori team for now." : me.body?.error === "cantBuild" ? "Connected, but your role doesn't build this app." : "Connected.");
|
|
46
64
|
return;
|
|
47
65
|
}
|
|
48
66
|
console.log(`Connected to ${me.body.app} as ${me.body.email ?? "you"}. Next: alori dev`);
|
|
@@ -53,16 +71,38 @@ async function main() {
|
|
|
53
71
|
console.log("Signed out on this computer.");
|
|
54
72
|
return;
|
|
55
73
|
case "whoami": {
|
|
74
|
+
// In a pulled folder, the folder's app.
|
|
75
|
+
useApp(readState(resolve("."))?.appId ?? null);
|
|
56
76
|
const me = await call("/code/me");
|
|
57
|
-
if (!me.ok) throw Object.assign(new Error(me.body?.error === "cantBuild" ? "Your role doesn't build this app." : "Run: alori login"), { plain: true });
|
|
77
|
+
if (!me.ok) throw Object.assign(new Error(me.body?.error === "superAdminsOnly" ? "The alori command is only for the Alori team for now." : me.body?.error === "cantBuild" ? "Your role doesn't build this app." : "Run: alori login"), { plain: true });
|
|
58
78
|
console.log(`${me.body.app} (version ${me.body.version ?? "none"}), signed in as ${me.body.email ?? "you"}.`);
|
|
59
79
|
return;
|
|
60
80
|
}
|
|
61
81
|
case "dev":
|
|
62
|
-
await signedIn(() => dev(args
|
|
82
|
+
await signedIn(() => dev(folderArgOf(args), appArgOf(args)));
|
|
63
83
|
return;
|
|
64
84
|
case "pull":
|
|
65
|
-
await signedIn(() => pull(args
|
|
85
|
+
await signedIn(() => pull(folderArgOf(args), appArgOf(args)));
|
|
86
|
+
return;
|
|
87
|
+
case "apps":
|
|
88
|
+
await signedIn(async () => {
|
|
89
|
+
const apps = await appsList();
|
|
90
|
+
if (apps.length === 0) console.log("No app you can change with this sign-in.");
|
|
91
|
+
for (const app of apps) {
|
|
92
|
+
const how = app.access === "agency" ? "your client" : app.access === "staff" ? `Alori support, until ${new Date(app.until).toLocaleString()}` : "yours";
|
|
93
|
+
console.log(` ${app.name} (${how}) --app ${app.appId}`);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
return;
|
|
97
|
+
case "upload":
|
|
98
|
+
// In a pulled folder, the folder's app; elsewhere, --app or the sign-in's own app.
|
|
99
|
+
useApp(readState(resolve("."))?.appId ?? appArgOf(args) ?? null);
|
|
100
|
+
await signedIn(async () => {
|
|
101
|
+
const files = args.filter((arg, index) => !arg.startsWith("-") && !["--app", "-a"].includes(args[index - 1] ?? ""));
|
|
102
|
+
const saved = await upload(files);
|
|
103
|
+
if (args.includes("--json")) console.log(JSON.stringify(saved, null, 2));
|
|
104
|
+
else for (const file of saved) console.log(`${file.url} ${file.name}`);
|
|
105
|
+
});
|
|
66
106
|
return;
|
|
67
107
|
case "status":
|
|
68
108
|
status(resolve("."));
|
|
@@ -70,6 +110,9 @@ async function main() {
|
|
|
70
110
|
case "push":
|
|
71
111
|
await push(resolve("."), messageOf(args));
|
|
72
112
|
return;
|
|
113
|
+
case "prospect":
|
|
114
|
+
await signedIn(() => prospect(args.find((arg, index) => !arg.startsWith("-") && args[index - 1] !== "--name"), flagOf(args, ["--name"])));
|
|
115
|
+
return;
|
|
73
116
|
default:
|
|
74
117
|
console.log(help);
|
|
75
118
|
}
|
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: {
|
|
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
|
-
/**
|
|
2
|
-
|
|
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/prospect.mjs
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { call } from "./auth.mjs";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `alori prospect <url>` (Alori team only): turns a prospect's website into a demo site on Alori, at
|
|
5
|
+
* `<name>.prospects.aloriapp.com`, then waits until it is live. The platform reads the site, copies its photos and
|
|
6
|
+
* builds it (`convex/prospects/demos.ts`); the demo hides from search, shows a Make it mine bar and ends after 30 days.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const errors = {
|
|
10
|
+
superAdminsOnly: "The alori command is only for the Alori team for now.",
|
|
11
|
+
noProspectsApp: "The Prospects app isn't set up on this Alori (ALORI_PROSPECTS_APP_ID).",
|
|
12
|
+
badUrl: "That isn't a website address. Try: alori prospect aspavocats.ca",
|
|
13
|
+
noAddress: "No address is free for this demo. Try again with --name.",
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const pollMs = 10_000;
|
|
17
|
+
const waitMs = 20 * 60 * 1000;
|
|
18
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
19
|
+
|
|
20
|
+
function fail(message) {
|
|
21
|
+
const error = new Error(message);
|
|
22
|
+
error.plain = true;
|
|
23
|
+
return error;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function prospect(url, company) {
|
|
27
|
+
if (!url) throw fail("Which website? Try: alori prospect aspavocats.ca");
|
|
28
|
+
const started = await call("/code/prospect", { method: "POST", body: { url, ...(company ? { company } : {}) } });
|
|
29
|
+
if (!started.ok) throw fail(errors[started.body?.error] ?? `The demo couldn't start (${started.body?.error ?? started.status}). Try again.`);
|
|
30
|
+
const address = `https://${started.body.host}`;
|
|
31
|
+
if (started.body.again) console.log(`This site already has a demo: ${address}`);
|
|
32
|
+
else console.log(`Building the demo at ${address} (a few minutes)…`);
|
|
33
|
+
const until = Date.now() + waitMs;
|
|
34
|
+
while (Date.now() < until) {
|
|
35
|
+
const demo = await call(`/code/prospect?id=${encodeURIComponent(started.body.projectId)}`);
|
|
36
|
+
if (demo.ok && demo.body.live) {
|
|
37
|
+
console.log(`Live: ${address} (for ${demo.body.company}, ends ${new Date(demo.body.expiresAt).toLocaleDateString()})`);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
await sleep(pollMs);
|
|
41
|
+
}
|
|
42
|
+
console.log(`Still building. It will be live at ${address}`);
|
|
43
|
+
}
|
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,8 @@ 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
|
+
superAdminsOnly: "The alori command is only for the Alori team for now.",
|
|
24
|
+
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
25
|
rateLimited: "Too many calls in a minute. Wait a moment and try again.",
|
|
24
26
|
noCode: "This app has no saved code yet.",
|
|
25
27
|
};
|
|
@@ -61,7 +63,7 @@ async function download() {
|
|
|
61
63
|
mkdirSync(folder);
|
|
62
64
|
execFileSync("tar", ["-xzf", tarball, "-C", folder]);
|
|
63
65
|
unlinkSync(tarball);
|
|
64
|
-
return { folder, temp, version: job.version };
|
|
66
|
+
return { folder, temp, version: job.version, website: typeof job.website === "string" ? job.website : "" };
|
|
65
67
|
}
|
|
66
68
|
|
|
67
69
|
/** The template's own notes for Alori's developers: the folder gets the owner's guide (`guide.mjs`) instead. */
|
|
@@ -102,12 +104,12 @@ const workspaceFile = `allowBuilds:
|
|
|
102
104
|
|
|
103
105
|
/** A first pull into an empty folder. */
|
|
104
106
|
async function pullFresh(dir, me) {
|
|
105
|
-
const { folder, temp, version } = await download();
|
|
107
|
+
const { folder, temp, version, website } = await download();
|
|
106
108
|
mkdirSync(dir, { recursive: true });
|
|
107
109
|
for (const path of walk(folder).filter((path) => !templateNotes.has(path))) copyInto(folder, dir, path);
|
|
108
110
|
const template = templateHashes(folder);
|
|
109
111
|
rmSync(temp, { recursive: true, force: true });
|
|
110
|
-
writeFileSync(join(dir, "CLAUDE.md"), claudeGuide(me.app));
|
|
112
|
+
writeFileSync(join(dir, "CLAUDE.md"), claudeGuide(me.app, website));
|
|
111
113
|
ensureIgnored(dir, ignoreLines);
|
|
112
114
|
if (!existsSync(join(dir, "pnpm-workspace.yaml"))) writeFileSync(join(dir, "pnpm-workspace.yaml"), workspaceFile);
|
|
113
115
|
writeState(dir, { api: apiBase(), appId: me.appId, app: me.app, version, hashes: codeHashes(dir), templateHashes: template });
|
|
@@ -168,7 +170,7 @@ function snapshotOf(dir, folder, temp) {
|
|
|
168
170
|
|
|
169
171
|
/** A pull into a folder already pulled: Alori's latest code comes in, and the person's changes stay (`mergeInto`). */
|
|
170
172
|
async function pullOver(dir, state, me) {
|
|
171
|
-
const { folder, temp, version } = await download();
|
|
173
|
+
const { folder, temp, version, website } = await download();
|
|
172
174
|
const repo = startRepoFrom(dir, () => snapshotOf(dir, folder, temp), version);
|
|
173
175
|
// The ignore list first, so what this computer generated (the lockfile, .DS_Store) doesn't count as the person's changes.
|
|
174
176
|
ensureIgnored(dir, ignoreLines);
|
|
@@ -180,6 +182,8 @@ async function pullOver(dir, state, me) {
|
|
|
180
182
|
// What Alori writes for every app (the .gitignore, generated files) always comes fresh.
|
|
181
183
|
for (const path of walk(folder).filter((path) => !isAppCode(path) && !isTemplateFile(path) && !templateNotes.has(path))) copyInto(folder, dir, path);
|
|
182
184
|
writeState(dir, { api: apiBase(), appId: me.appId, app: me.app, version, hashes: codeHashes(folder), templateHashes: remoteTemplate });
|
|
185
|
+
// Alori's guide comes fresh each pull, with the website's rules of today.
|
|
186
|
+
writeFileSync(join(dir, "CLAUDE.md"), claudeGuide(me.app, website));
|
|
183
187
|
rmSync(temp, { recursive: true, force: true });
|
|
184
188
|
ensureIgnored(dir, ignoreLines);
|
|
185
189
|
console.log(`Pulled ${me.app}, version ${version}.`);
|
|
@@ -203,11 +207,50 @@ async function pullOver(dir, state, me) {
|
|
|
203
207
|
async function whoAmI() {
|
|
204
208
|
const me = await call("/code/me");
|
|
205
209
|
if (!me.ok) failOn(me);
|
|
210
|
+
// Alori answers for the app asked; an older Alori ignores the ask and answers for the sign-in's own app.
|
|
211
|
+
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
212
|
return me.body;
|
|
207
213
|
}
|
|
208
214
|
|
|
209
|
-
|
|
210
|
-
|
|
215
|
+
/** The apps this sign-in may change (`GET /code/apps`). */
|
|
216
|
+
export async function appsList() {
|
|
217
|
+
const answer = await call("/code/apps");
|
|
218
|
+
if (!answer.ok) failOn(answer);
|
|
219
|
+
return answer.body.apps ?? [];
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const plainName = (text) =>
|
|
223
|
+
text
|
|
224
|
+
.normalize("NFD")
|
|
225
|
+
.replace(/[\u0300-\u036f]/g, "")
|
|
226
|
+
.toLowerCase()
|
|
227
|
+
.trim();
|
|
228
|
+
|
|
229
|
+
/** `--app`: an app id, or a name matched exactly (case and accents ignored) against the apps this sign-in may change. */
|
|
230
|
+
async function appOf(wanted) {
|
|
231
|
+
const apps = await appsList();
|
|
232
|
+
const byId = apps.find((app) => app.appId === wanted);
|
|
233
|
+
if (byId) return byId.appId;
|
|
234
|
+
const named = apps.filter((app) => plainName(app.name) === plainName(wanted));
|
|
235
|
+
if (named.length === 1) return named[0].appId;
|
|
236
|
+
const list = apps.map((app) => ` ${app.name} (--app ${app.appId})`).join("\n");
|
|
237
|
+
// An id Alori didn't list may still be one support just opened: let Alori decide, it refuses what isn't reachable.
|
|
238
|
+
if (named.length === 0 && /^[a-z0-9]{20,}$/.test(wanted)) return wanted;
|
|
239
|
+
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")}`);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Which app this command works on: a pulled folder's own (always, so a folder never sends its files to another app),
|
|
244
|
+
* else `--app`, else the app the sign-in was made from.
|
|
245
|
+
*/
|
|
246
|
+
async function chooseApp(dir, wanted) {
|
|
247
|
+
const state = readState(dir);
|
|
248
|
+
if (state?.appId) {
|
|
249
|
+
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}`);
|
|
250
|
+
useApp(state.appId);
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
useApp(wanted ? await appOf(wanted) : null);
|
|
211
254
|
}
|
|
212
255
|
|
|
213
256
|
/** A folder name from the app's name: "Pro-Quai Inc" → pro-quai-inc. */
|
|
@@ -226,12 +269,21 @@ function folderFor(named, me) {
|
|
|
226
269
|
return resolve(folderNameOf(me.app));
|
|
227
270
|
}
|
|
228
271
|
|
|
229
|
-
|
|
272
|
+
/** The folder a command works in and the app it holds; a folder named after the app may be one pulled before. */
|
|
273
|
+
async function placeOf(named, wanted) {
|
|
274
|
+
const first = named ? resolve(named) : resolve(".");
|
|
275
|
+
await chooseApp(first, wanted);
|
|
230
276
|
const me = await whoAmI();
|
|
231
277
|
const dir = folderFor(named, me);
|
|
278
|
+
if (dir === first) return { me, dir };
|
|
279
|
+
await chooseApp(dir, wanted);
|
|
280
|
+
return { me: await whoAmI(), dir };
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export async function pull(named, wanted) {
|
|
284
|
+
const { me, dir } = await placeOf(named, wanted);
|
|
232
285
|
const state = readState(dir);
|
|
233
286
|
if (state) {
|
|
234
|
-
checkSameApp(state, me);
|
|
235
287
|
await pullOver(dir, state, me);
|
|
236
288
|
return;
|
|
237
289
|
}
|
|
@@ -242,12 +294,10 @@ export async function pull(named) {
|
|
|
242
294
|
}
|
|
243
295
|
|
|
244
296
|
/** `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
|
|
247
|
-
const dir = folderFor(named, me);
|
|
297
|
+
export async function dev(named, wanted) {
|
|
298
|
+
const { me, dir } = await placeOf(named, wanted);
|
|
248
299
|
const state = readState(dir);
|
|
249
|
-
if (state)
|
|
250
|
-
else {
|
|
300
|
+
if (!state) {
|
|
251
301
|
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
302
|
await pullFresh(dir, me);
|
|
253
303
|
}
|
|
@@ -296,8 +346,8 @@ export async function push(dir, message) {
|
|
|
296
346
|
const leftovers = walk(dir).filter((path) => path.endsWith(".alori-theirs"));
|
|
297
347
|
if (leftovers.length > 0) fail(`Merge and delete these first:\n${leftovers.map((path) => ` ${path}`).join("\n")}`);
|
|
298
348
|
if (state.needsPull) fail("Someone else saved changes while your last push built. Run alori pull first, then push.");
|
|
349
|
+
useApp(state.appId ?? null);
|
|
299
350
|
const me = await whoAmI();
|
|
300
|
-
checkSameApp(state, me);
|
|
301
351
|
const { changed, deleted, now } = changesOf(dir, state);
|
|
302
352
|
if (changed.length + deleted.length === 0) {
|
|
303
353
|
console.log("Nothing changed since the last pull or push.");
|
|
@@ -361,6 +411,11 @@ export async function push(dir, message) {
|
|
|
361
411
|
console.log("Someone else changed these files too. Their changes and yours were merged:");
|
|
362
412
|
for (const path of merged) console.log(` ${path}`);
|
|
363
413
|
}
|
|
414
|
+
if (pushed.copied > 0 && !pushed.behind) {
|
|
415
|
+
// The saved code points at the app's own copies of files it linked on other websites: bring those links here.
|
|
416
|
+
console.log(`\nCopied ${pushed.copied} file(s) linked from other websites into your app's storage. Bringing the new links here…`);
|
|
417
|
+
await pullOver(dir, readState(dir), me);
|
|
418
|
+
}
|
|
364
419
|
if (pushed.behind) {
|
|
365
420
|
// Bring the merged files and everyone else's changes here, so this folder holds what was just saved.
|
|
366
421
|
console.log("\nSomeone else saved changes since your pull. Bringing them here…");
|
package/lib/upload.mjs
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { openAsBlob, statSync } from "node:fs";
|
|
2
|
+
import { basename, extname } from "node:path";
|
|
3
|
+
import { call } from "./auth.mjs";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* `alori upload <files…>`: puts files into the app's own storage (a product's PDFs, photos) and prints the lasting
|
|
7
|
+
* address of each, for the site's code to link. The bytes go straight to the app's backend through one-time links
|
|
8
|
+
* (`POST /code/upload`), never through Alori; each file is then recorded by the app (`POST /code/files`).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const perCall = 50;
|
|
12
|
+
|
|
13
|
+
const types = {
|
|
14
|
+
".pdf": "application/pdf",
|
|
15
|
+
".png": "image/png",
|
|
16
|
+
".jpg": "image/jpeg",
|
|
17
|
+
".jpeg": "image/jpeg",
|
|
18
|
+
".webp": "image/webp",
|
|
19
|
+
".gif": "image/gif",
|
|
20
|
+
".svg": "image/svg+xml",
|
|
21
|
+
".mp4": "video/mp4",
|
|
22
|
+
".webm": "video/webm",
|
|
23
|
+
".zip": "application/zip",
|
|
24
|
+
".dwg": "application/acad",
|
|
25
|
+
".ies": "text/plain",
|
|
26
|
+
".ldt": "text/plain",
|
|
27
|
+
".csv": "text/csv",
|
|
28
|
+
".txt": "text/plain",
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const typeOf = (path) => types[extname(path).toLowerCase()] ?? "application/octet-stream";
|
|
32
|
+
|
|
33
|
+
function fail(message) {
|
|
34
|
+
return Object.assign(new Error(message), { plain: true });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Sends one file's bytes to a one-time link; the app's storage id. */
|
|
38
|
+
async function send(url, path) {
|
|
39
|
+
const blob = await openAsBlob(path, { type: typeOf(path) });
|
|
40
|
+
const response = await fetch(url, { method: "POST", headers: { "Content-Type": typeOf(path) }, body: blob });
|
|
41
|
+
const answer = await response.json().catch(() => null);
|
|
42
|
+
if (!response.ok || typeof answer?.storageId !== "string") throw fail(`${basename(path)} wasn't uploaded (${response.status}). Try again.`);
|
|
43
|
+
return answer.storageId;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Uploads these files; each one's name, address and size, in the order given. */
|
|
47
|
+
export async function upload(paths) {
|
|
48
|
+
if (paths.length === 0) throw fail("Name the files to upload: alori upload docs/*.pdf");
|
|
49
|
+
for (const path of paths) if (!statSync(path, { throwIfNoEntry: false })?.isFile()) throw fail(`No file at ${path}.`);
|
|
50
|
+
const saved = [];
|
|
51
|
+
for (let start = 0; start < paths.length; start += perCall) {
|
|
52
|
+
const batch = paths.slice(start, start + perCall);
|
|
53
|
+
const links = await call("/code/upload", { method: "POST", body: { count: batch.length } });
|
|
54
|
+
if (!links.ok) throw fail(links.body?.error === "notYours" ? "This sign-in can't reach this app." : `The app couldn't take files (${links.body?.error ?? links.status}). Try again.`);
|
|
55
|
+
const urls = links.body.uploadUrls;
|
|
56
|
+
for (const [index, path] of batch.entries()) {
|
|
57
|
+
const storageId = await send(urls[index], path);
|
|
58
|
+
const kept = await call("/code/files", { method: "POST", body: { storageId, name: basename(path) } });
|
|
59
|
+
if (!kept.ok) throw fail(`${basename(path)} wasn't kept by the app (${kept.body?.error ?? kept.status}).`);
|
|
60
|
+
saved.push({ file: path, name: kept.body.name, url: kept.body.url, size: kept.body.size, mime: kept.body.mime });
|
|
61
|
+
process.stderr.write(` ${start + index + 1}/${paths.length} ${basename(path)}\n`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return saved;
|
|
65
|
+
}
|