create-efforge 0.0.0-stage → 0.2.0
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/LICENSE +21 -0
- package/README.md +88 -2
- package/dist/addon.js +115 -0
- package/dist/bin.js +13 -0
- package/dist/cli.js +28 -0
- package/dist/commands/add.js +640 -0
- package/dist/commands/create.js +319 -0
- package/dist/commands/list.js +80 -0
- package/dist/diff.js +72 -0
- package/dist/exec.js +33 -0
- package/dist/generate.js +181 -0
- package/dist/manifest.js +14 -0
- package/dist/projectName.js +41 -0
- package/dist/prompts/flow.js +122 -0
- package/dist/prompts/navigable.js +193 -0
- package/dist/routeTree.js +7 -0
- package/dist/rules.js +27 -0
- package/dist/slots.js +116 -0
- package/dist/stack.js +68 -0
- package/dist/transforms/addAlchemyResource.js +71 -0
- package/dist/transforms/addArrayItem.js +31 -0
- package/dist/transforms/addAuthPlugin.js +77 -0
- package/dist/transforms/addDevVar.js +18 -0
- package/dist/transforms/addExport.js +46 -0
- package/dist/transforms/addImport.js +11 -0
- package/dist/transforms/addLayer.js +31 -0
- package/dist/transforms/addServerFnMiddleware.js +62 -0
- package/dist/transforms/addWorkflowSecret.js +43 -0
- package/dist/transforms/addWranglerBinding.js +61 -0
- package/dist/transforms/appendAgentsSection.js +26 -0
- package/dist/transforms/errors.js +7 -0
- package/dist/transforms/index.js +40 -0
- package/dist/transforms/setWranglerValue.js +40 -0
- package/dist/transforms/util.js +219 -0
- package/dist/tree.js +1 -0
- package/dist/versions.js +73 -0
- package/dist/write.js +28 -0
- package/package.json +63 -4
- package/templates/ai/effect-ai-byok/addon.json +619 -0
- package/templates/ai/effect-ai-byok/files/core/llm-crypto.test.ts +52 -0
- package/templates/ai/effect-ai-byok/files/core/llm-crypto.ts +76 -0
- package/templates/ai/effect-ai-byok/files/core/llm-store.ts +21 -0
- package/templates/ai/effect-ai-byok/files/core/llm.ts +32 -0
- package/templates/ai/effect-ai-byok/files/drizzle/d1/20261018161056_llm_credential/migration.sql +9 -0
- package/templates/ai/effect-ai-byok/files/drizzle/d1/20261018161056_llm_credential/snapshot.json +944 -0
- package/templates/ai/effect-ai-byok/files/drizzle/pg/20261018161056_llm_credential/migration.sql +10 -0
- package/templates/ai/effect-ai-byok/files/drizzle/pg/20261018161056_llm_credential/snapshot.json +1232 -0
- package/templates/ai/effect-ai-byok/files/routes/ai.tsx +193 -0
- package/templates/ai/effect-ai-byok/files/schema/d1.ts +16 -0
- package/templates/ai/effect-ai-byok/files/schema/pg.ts +14 -0
- package/templates/ai/effect-ai-byok/files/server/ai-settings.ts +129 -0
- package/templates/ai/effect-ai-byok/files/server/ai.ts +25 -0
- package/templates/ai/effect-ai-byok/files/server/llm-env.ts +48 -0
- package/templates/ai/effect-ai-byok/files/server/llm-orgs.ts +146 -0
- package/templates/ai/effect-ai-byok/files/server/llm-provider.ts +56 -0
- package/templates/ai/effect-ai-byok/files/server/llm-store-drizzle.ts +48 -0
- package/templates/ai/effect-ai-byok/files/server/llm-store-sql-d1.ts +41 -0
- package/templates/ai/effect-ai-byok/files/server/llm-store-sql-pg.ts +41 -0
- package/templates/ai/effect-ai-byok/files/sql/d1.sql +8 -0
- package/templates/ai/effect-ai-byok/files/sql/pg.sql +8 -0
- package/templates/auth/better-auth/addon.json +424 -0
- package/templates/auth/better-auth/files/core/Auth.ts +18 -0
- package/templates/auth/better-auth/files/drizzle/d1/20261018161054_auth/migration.sql +51 -0
- package/templates/auth/better-auth/files/drizzle/d1/20261018161054_auth/snapshot.json +522 -0
- package/templates/auth/better-auth/files/drizzle/pg/20261018161053_auth/migration.sql +51 -0
- package/templates/auth/better-auth/files/drizzle/pg/20261018161053_auth/snapshot.json +680 -0
- package/templates/auth/better-auth/files/lib/auth-client.ts +4 -0
- package/templates/auth/better-auth/files/routes/app.tsx +54 -0
- package/templates/auth/better-auth/files/schema/d1/auth-schema.ts +86 -0
- package/templates/auth/better-auth/files/schema/pg/auth-schema.ts +73 -0
- package/templates/auth/better-auth/files/server/auth-live.ts +47 -0
- package/templates/auth/better-auth/files/server/session.ts +14 -0
- package/templates/auth/better-auth/files/shared/components/auth-card.tsx +32 -0
- package/templates/auth/better-auth/files/shared/components/user-menu.tsx +44 -0
- package/templates/auth/better-auth/files/shared/drizzle/d1/drizzle.config.ts +7 -0
- package/templates/auth/better-auth/files/shared/drizzle/pg/drizzle.config.ts +10 -0
- package/templates/auth/better-auth/files/shared/lib/form.ts +5 -0
- package/templates/auth/better-auth/files/shared/lib/redirect.ts +6 -0
- package/templates/auth/better-auth/files/shared/routes/app/index.tsx +54 -0
- package/templates/auth/better-auth/files/shared/routes/sign-in.tsx +85 -0
- package/templates/auth/better-auth/files/shared/routes/sign-up.tsx +91 -0
- package/templates/auth/better-auth/files/shared/server/auth-route.ts +15 -0
- package/templates/auth/better-auth/files/shared/server/auth.ts +22 -0
- package/templates/auth/better-auth/files/shared/server/db/d1-drizzle.ts +25 -0
- package/templates/auth/better-auth/files/shared/server/db/d1-effect-sql.ts +17 -0
- package/templates/auth/better-auth/files/shared/server/db/pg-drizzle.ts +32 -0
- package/templates/auth/better-auth/files/shared/server/db/pg-effect-sql.ts +29 -0
- package/templates/auth/better-auth/files/shared/server/require-session.ts +20 -0
- package/templates/auth/better-auth/files/sql/d1/0002_auth.sql +13 -0
- package/templates/auth/better-auth/files/sql/pg/0002_auth.sql +13 -0
- package/templates/auth/better-auth-orgs/addon.json +463 -0
- package/templates/auth/better-auth-orgs/files/components/org-switcher.tsx +65 -0
- package/templates/auth/better-auth-orgs/files/core/Auth.ts +20 -0
- package/templates/auth/better-auth-orgs/files/drizzle/d1/20261018161055_auth/migration.sql +88 -0
- package/templates/auth/better-auth-orgs/files/drizzle/d1/20261018161055_auth/snapshot.json +862 -0
- package/templates/auth/better-auth-orgs/files/drizzle/pg/20261018161055_auth/migration.sql +88 -0
- package/templates/auth/better-auth-orgs/files/drizzle/pg/20261018161055_auth/snapshot.json +1127 -0
- package/templates/auth/better-auth-orgs/files/lib/auth-client.ts +5 -0
- package/templates/auth/better-auth-orgs/files/routes/accept-invite.$id.tsx +68 -0
- package/templates/auth/better-auth-orgs/files/routes/app/settings/index.tsx +27 -0
- package/templates/auth/better-auth-orgs/files/routes/app/settings/members.tsx +202 -0
- package/templates/auth/better-auth-orgs/files/routes/app/settings.tsx +30 -0
- package/templates/auth/better-auth-orgs/files/routes/app.tsx +58 -0
- package/templates/auth/better-auth-orgs/files/routes/onboarding.tsx +66 -0
- package/templates/auth/better-auth-orgs/files/schema/d1/auth-schema.ts +139 -0
- package/templates/auth/better-auth-orgs/files/schema/pg/auth-schema.ts +124 -0
- package/templates/auth/better-auth-orgs/files/server/auth-live.ts +52 -0
- package/templates/auth/better-auth-orgs/files/server/session.ts +44 -0
- package/templates/auth/better-auth-orgs/files/sql/d1/0002_auth.sql +27 -0
- package/templates/auth/better-auth-orgs/files/sql/pg/0002_auth.sql +27 -0
- package/templates/base/addon.json +216 -0
- package/templates/base/files/AGENTS.md +82 -0
- package/templates/base/files/CLAUDE.md +1 -0
- package/templates/base/files/README.md +32 -0
- package/templates/base/files/core/package.json +13 -0
- package/templates/base/files/core/src/index.ts +2 -0
- package/templates/base/files/core/test/core.test.ts +4 -0
- package/templates/base/files/core/tsconfig.json +6 -0
- package/templates/base/files/gitignore +11 -0
- package/templates/base/files/pnpm-workspace.yaml +11 -0
- package/templates/base/files/root/lint/effect-tests.js +45 -0
- package/templates/base/files/root/package.json +16 -0
- package/templates/base/files/root/tsconfig.base.json +19 -0
- package/templates/base/files/root/turbo.json +9 -0
- package/templates/base/files/root/vite.config.ts +86 -0
- package/templates/base/files/web/components.json +21 -0
- package/templates/base/files/web/package.json +17 -0
- package/templates/base/files/web/src/components/theme-provider.tsx +57 -0
- package/templates/base/files/web/src/components/theme-toggle.tsx +34 -0
- package/templates/base/files/web/src/components/ui/avatar.tsx +93 -0
- package/templates/base/files/web/src/components/ui/button.tsx +61 -0
- package/templates/base/files/web/src/components/ui/card.tsx +74 -0
- package/templates/base/files/web/src/components/ui/dialog.tsx +141 -0
- package/templates/base/files/web/src/components/ui/dropdown-menu.tsx +225 -0
- package/templates/base/files/web/src/components/ui/input.tsx +20 -0
- package/templates/base/files/web/src/components/ui/label.tsx +18 -0
- package/templates/base/files/web/src/components/ui/sonner.tsx +45 -0
- package/templates/base/files/web/src/components/widgets.tsx +4 -0
- package/templates/base/files/web/src/lib/utils.ts +6 -0
- package/templates/base/files/web/src/router.tsx +12 -0
- package/templates/base/files/web/src/routes/__root.tsx +42 -0
- package/templates/base/files/web/src/routes/app/index.tsx +27 -0
- package/templates/base/files/web/src/routes/app/settings.tsx +29 -0
- package/templates/base/files/web/src/routes/app.tsx +47 -0
- package/templates/base/files/web/src/routes/index.tsx +51 -0
- package/templates/base/files/web/src/server/greeting.ts +12 -0
- package/templates/base/files/web/src/server/layers.ts +10 -0
- package/templates/base/files/web/src/server/runtime.ts +5 -0
- package/templates/base/files/web/src/styles.css +81 -0
- package/templates/base/files/web/tsconfig.json +11 -0
- package/templates/base/files/web/vite.config.ts +9 -0
- package/templates/database/d1/addon.json +118 -0
- package/templates/database/d1/files/package.json +12 -0
- package/templates/database/d1/files/tsconfig.json +5 -0
- package/templates/database/postgres/addon.json +142 -0
- package/templates/database/postgres/files/docker-compose.yml +21 -0
- package/templates/database/postgres/files/package.json +12 -0
- package/templates/database/postgres/files/runtime.ts +12 -0
- package/templates/database/postgres/files/tsconfig.json +5 -0
- package/templates/extras/browser/addon.json +173 -0
- package/templates/extras/browser/files/BrowserContainer.ts +14 -0
- package/templates/extras/browser/files/app/Dockerfile +15 -0
- package/templates/extras/browser/files/app/dockerignore +1 -0
- package/templates/extras/browser/files/app/package-lock +605 -0
- package/templates/extras/browser/files/app/package.json +9 -0
- package/templates/extras/browser/files/app/src/server.ts +78 -0
- package/templates/extras/browser/files/app/src/url-policy.ts +115 -0
- package/templates/extras/browser/files/app/tsconfig.json +6 -0
- package/templates/extras/browser/files/browser.ts +34 -0
- package/templates/extras/browser/files/core-browser.ts +21 -0
- package/templates/extras/browser/files/run-browser.ts +25 -0
- package/templates/extras/email/addon.json +182 -0
- package/templates/extras/email/files/core-mailer.ts +28 -0
- package/templates/extras/email/files/invitation-email.ts +38 -0
- package/templates/extras/email/files/mailer.ts +58 -0
- package/templates/extras/realtime/addon.json +135 -0
- package/templates/extras/realtime/files/Room.ts +46 -0
- package/templates/extras/realtime/files/core-realtime.ts +19 -0
- package/templates/extras/realtime/files/realtime-demo.tsx +40 -0
- package/templates/extras/realtime/files/realtime-limits.test.ts +32 -0
- package/templates/extras/realtime/files/realtime-limits.ts +35 -0
- package/templates/extras/realtime/files/realtime-route.ts +39 -0
- package/templates/extras/realtime/files/realtime.ts +39 -0
- package/templates/extras/realtime/files/use-room.ts +67 -0
- package/templates/extras/storage/addon.json +132 -0
- package/templates/extras/storage/files/core-storage.ts +64 -0
- package/templates/extras/storage/files/storage-route.ts +50 -0
- package/templates/extras/storage/files/storage-security.test.ts +71 -0
- package/templates/extras/storage/files/storage-security.ts +92 -0
- package/templates/extras/storage/files/storage.ts +84 -0
- package/templates/extras/workflows/addon.json +95 -0
- package/templates/extras/workflows/files/Example.ts +42 -0
- package/templates/extras/workflows/files/core-jobs.ts +45 -0
- package/templates/extras/workflows/files/example-job.ts +36 -0
- package/templates/extras/workflows/files/jobs.ts +34 -0
- package/templates/infra/alchemy/addon.json +37 -0
- package/templates/infra/alchemy/files/README.md +27 -0
- package/templates/infra/alchemy/files/alchemy.run.ts +36 -0
- package/templates/infra/alchemy/files/github/deploy.yml +63 -0
- package/templates/infra/alchemy/files/tsconfig.json +5 -0
- package/templates/infra/alchemy/files/vite.config.ts +30 -0
- package/templates/orm/drizzle/addon.json +188 -0
- package/templates/orm/drizzle/files/d1/Database.ts +7 -0
- package/templates/orm/drizzle/files/d1/drizzle/20261018154417_init/migration.sql +10 -0
- package/templates/orm/drizzle/files/d1/drizzle/20261018154417_init/snapshot.json +60 -0
- package/templates/orm/drizzle/files/d1/drizzle.config.ts +7 -0
- package/templates/orm/drizzle/files/d1/layer.ts +13 -0
- package/templates/orm/drizzle/files/d1/migrate.ts +37 -0
- package/templates/orm/drizzle/files/d1/schema.ts +15 -0
- package/templates/orm/drizzle/files/pg/Database.ts +7 -0
- package/templates/orm/drizzle/files/pg/drizzle/20261018154416_init/migration.sql +10 -0
- package/templates/orm/drizzle/files/pg/drizzle/20261018154416_init/snapshot.json +75 -0
- package/templates/orm/drizzle/files/pg/drizzle.config.ts +10 -0
- package/templates/orm/drizzle/files/pg/layer.ts +16 -0
- package/templates/orm/drizzle/files/pg/migrate.ts +24 -0
- package/templates/orm/drizzle/files/pg/schema.ts +11 -0
- package/templates/orm/drizzle/files/shared/index.ts +2 -0
- package/templates/orm/drizzle/files/shared/notes.ts +15 -0
- package/templates/orm/drizzle/files/shared/page.tsx +50 -0
- package/templates/orm/effect-sql/addon.json +124 -0
- package/templates/orm/effect-sql/files/d1/layer.ts +13 -0
- package/templates/orm/effect-sql/files/d1/migrate.ts +20 -0
- package/templates/orm/effect-sql/files/d1/schema.ts +7 -0
- package/templates/orm/effect-sql/files/d1-sql/0001_init.sql +10 -0
- package/templates/orm/effect-sql/files/pg/layer.ts +16 -0
- package/templates/orm/effect-sql/files/pg/migrate.ts +66 -0
- package/templates/orm/effect-sql/files/pg/schema.ts +7 -0
- package/templates/orm/effect-sql/files/pg-sql/0001_init.sql +10 -0
- package/templates/orm/effect-sql/files/shared/Database.ts +7 -0
- package/templates/orm/effect-sql/files/shared/index.ts +2 -0
- package/templates/orm/effect-sql/files/shared/notes.ts +18 -0
- package/templates/orm/effect-sql/files/shared/page.tsx +50 -0
- package/templates/runtime/cloudflare/addon.json +52 -0
- package/templates/runtime/cloudflare/files/cloudflare.ts +10 -0
- package/templates/runtime/cloudflare/files/routes.ts +5 -0
- package/templates/runtime/cloudflare/files/server.ts +22 -0
- package/templates/runtime/cloudflare/files/tsconfig.json +12 -0
- package/templates/runtime/cloudflare/files/vite.config.ts +20 -0
- package/templates/runtime/cloudflare/files/wrangler.jsonc +8 -0
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
import { Console, Effect, FileSystem, Path, Schema } from "effect";
|
|
2
|
+
import { Argument, Command, Flag } from "effect/cli";
|
|
3
|
+
import { parse } from "jsonc-parser";
|
|
4
|
+
import pc from "picocolors";
|
|
5
|
+
import { parseDocument } from "yaml";
|
|
6
|
+
import { addonIdsFor, loadAddons } from "../addon.js";
|
|
7
|
+
import { unifiedDiff } from "../diff.js";
|
|
8
|
+
import { exec, has } from "../exec.js";
|
|
9
|
+
import { GenerateError, generate, isComputed, matches } from "../generate.js";
|
|
10
|
+
import { EfforgeManifest, hash, renderManifest } from "../manifest.js";
|
|
11
|
+
import { applyProjectName } from "../projectName.js";
|
|
12
|
+
import { generateRouteTree } from "../routeTree.js";
|
|
13
|
+
import { IncompatibleStack, validate } from "../rules.js";
|
|
14
|
+
import { slots } from "../slots.js";
|
|
15
|
+
import { toCommand } from "../stack.js";
|
|
16
|
+
import { WRANGLER_FILE } from "../transforms/addWranglerBinding.js";
|
|
17
|
+
import { runTransform, transformTargets, } from "../transforms/index.js";
|
|
18
|
+
import { PromptIO } from "../prompts/navigable.js";
|
|
19
|
+
import { efforgeVersion, versions } from "../versions.js";
|
|
20
|
+
import { PnpmMissing, TemplatesDir } from "./create.js";
|
|
21
|
+
export class NotAnEfforgeProject extends Schema.TaggedError()("NotAnEfforgeProject", { dir: Schema.String, reason: Schema.String }) {
|
|
22
|
+
}
|
|
23
|
+
/** An id that names no add-on (typo, removal via `none`, or ambiguous). */
|
|
24
|
+
export class UnknownAddon extends Schema.TaggedError()("UnknownAddon", {
|
|
25
|
+
id: Schema.String,
|
|
26
|
+
message: Schema.String,
|
|
27
|
+
}) {
|
|
28
|
+
}
|
|
29
|
+
/** A target file that exists, differs from what efforge generated, and `--overwrite` wasn't given. */
|
|
30
|
+
export class OverlayConflict extends Schema.TaggedError()("OverlayConflict", {
|
|
31
|
+
file: Schema.String,
|
|
32
|
+
diff: Schema.String,
|
|
33
|
+
}) {
|
|
34
|
+
}
|
|
35
|
+
const incompatible = (slot, value, allowed, reason) => new IncompatibleStack({ slot, value, allowed, reason });
|
|
36
|
+
const allIds = slots.flatMap((s) => s.options.filter((o) => o.id !== "none").map((o) => `${s.id}/${o.id}`));
|
|
37
|
+
/** Edit distance, for "did you mean". */
|
|
38
|
+
const distance = (a, b) => {
|
|
39
|
+
const row = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
40
|
+
for (let i = 1; i <= a.length; i++) {
|
|
41
|
+
let prev = row[0];
|
|
42
|
+
row[0] = i;
|
|
43
|
+
for (let j = 1; j <= b.length; j++) {
|
|
44
|
+
const tmp = row[j];
|
|
45
|
+
row[j] = Math.min(row[j] + 1, row[j - 1] + 1, prev + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
46
|
+
prev = tmp;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return row[b.length];
|
|
50
|
+
};
|
|
51
|
+
const suggest = (id) => {
|
|
52
|
+
// a slot name or a fragment ("ai", "auth", "better") before edit distance, which favours short ids
|
|
53
|
+
const partial = allIds.filter((full) => full.split("/").some((part) => part.startsWith(id)));
|
|
54
|
+
if (partial.length > 0)
|
|
55
|
+
return partial.join(", ");
|
|
56
|
+
const best = allIds
|
|
57
|
+
.flatMap((full) => [full, full.split("/")[1]])
|
|
58
|
+
.map((c) => [c, distance(id, c)])
|
|
59
|
+
.toSorted((a, b) => a[1] - b[1])[0];
|
|
60
|
+
return best !== undefined && best[1] <= 3 ? best[0] : undefined;
|
|
61
|
+
};
|
|
62
|
+
/** Same JSONC data and comments, ignoring formatting, key order and array order (earlier `add`s reorder bindings). */
|
|
63
|
+
const sameJsonc = (a, b) => {
|
|
64
|
+
if (b === undefined)
|
|
65
|
+
return false;
|
|
66
|
+
const canon = (v) => Array.isArray(v)
|
|
67
|
+
? v.map(canon).toSorted((x, y) => byName(JSON.stringify(x), JSON.stringify(y)))
|
|
68
|
+
: v !== null && typeof v === "object"
|
|
69
|
+
? Object.fromEntries(Object.entries(v)
|
|
70
|
+
.toSorted(([x], [y]) => byName(x, y))
|
|
71
|
+
.map(([k, x]) => [k, canon(x)]))
|
|
72
|
+
: v;
|
|
73
|
+
const read = (t) => {
|
|
74
|
+
const errors = [];
|
|
75
|
+
const v = parse(t, errors, { allowTrailingComma: true });
|
|
76
|
+
return errors.length > 0 ? undefined : JSON.stringify(canon(v));
|
|
77
|
+
};
|
|
78
|
+
// comments are user content too, and the parser drops them
|
|
79
|
+
const comments = (t) => (t.match(/\/\/.*$|\/\*[\s\S]*?\*\//gm) ?? [])
|
|
80
|
+
.map((c) => c.trim())
|
|
81
|
+
.toSorted()
|
|
82
|
+
.join("\n");
|
|
83
|
+
const x = read(a);
|
|
84
|
+
return x !== undefined && x === read(b) && comments(a) === comments(b);
|
|
85
|
+
};
|
|
86
|
+
const isNewPackageJson = (target) => target.endsWith("/package.json");
|
|
87
|
+
const byName = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
88
|
+
/** `email`, `extras/email`, `ai/effect-ai-byok`, `ai=effect-ai-byok` -> slot + option. */
|
|
89
|
+
const parseId = (raw) => {
|
|
90
|
+
const id = raw.trim().toLowerCase();
|
|
91
|
+
const [slotPart, option = ""] = id.includes("=")
|
|
92
|
+
? id.split("=", 2)
|
|
93
|
+
: id.includes("/")
|
|
94
|
+
? id.split("/", 2)
|
|
95
|
+
: [undefined, id];
|
|
96
|
+
const unknown = (message) => Effect.fail(new UnknownAddon({ id: raw, message }));
|
|
97
|
+
if (option === "none")
|
|
98
|
+
return unknown(`Removing an add-on isn't supported in v0.1 ("${raw}").`);
|
|
99
|
+
const candidates = slots.filter((s) => (slotPart === undefined || s.id === slotPart) && s.options.some((o) => o.id === option));
|
|
100
|
+
const [first] = candidates;
|
|
101
|
+
if (first === undefined) {
|
|
102
|
+
const guess = suggest(id);
|
|
103
|
+
return unknown(`"${raw}" is not an add-on.${guess === undefined ? "" : ` Did you mean ${guess}?`} Run efforge list to see them all.`);
|
|
104
|
+
}
|
|
105
|
+
if (candidates.length > 1)
|
|
106
|
+
return unknown(`"${id}" is ambiguous; write it as ${candidates.map((s) => `${s.id}/${id}`).join(" or ")}.`);
|
|
107
|
+
return Effect.succeed({ slot: first, option });
|
|
108
|
+
};
|
|
109
|
+
/** Applies `ids` to `stack`. Single-select slots may only go from `none` to a value. */
|
|
110
|
+
const applyIds = (stack, ids) => Effect.gen(function* () {
|
|
111
|
+
let next = stack;
|
|
112
|
+
const skipped = [];
|
|
113
|
+
for (const id of ids) {
|
|
114
|
+
const { slot, option } = yield* parseId(id);
|
|
115
|
+
if (slot.id === "extras") {
|
|
116
|
+
if (next.extras.includes(option))
|
|
117
|
+
skipped.push(`extras/${option}`);
|
|
118
|
+
else {
|
|
119
|
+
const order = slot.options.map((o) => o.id);
|
|
120
|
+
const extras = [...next.extras, option].toSorted((a, b) => order.indexOf(a) - order.indexOf(b));
|
|
121
|
+
next = { ...next, extras };
|
|
122
|
+
}
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
const current = next[slot.id];
|
|
126
|
+
if (current === option)
|
|
127
|
+
skipped.push(`${slot.id}/${option}`);
|
|
128
|
+
else if (current === "none")
|
|
129
|
+
next = { ...next, [slot.id]: option };
|
|
130
|
+
else
|
|
131
|
+
return yield* incompatible(slot.id, option, [current], `${slot.id} is already "${current}"; swapping an existing choice isn't supported in v0.1. Create a new project to use a different ${slot.id}.`);
|
|
132
|
+
}
|
|
133
|
+
const valid = yield* validate(next).pipe(Effect.mapError((e) => {
|
|
134
|
+
// the stack rules hold across slots: say which ids to add together
|
|
135
|
+
const typed = ids.map((x) => x.trim().toLowerCase());
|
|
136
|
+
const hasOrm = typed.some((x) => x.startsWith("orm/") || x === "drizzle" || x === "effect-sql");
|
|
137
|
+
if (next.database === "none" && (e.slot === "orm" || e.slot === "auth"))
|
|
138
|
+
return incompatible(e.slot, e.value, [...e.allowed], `${e.slot === "auth" ? "auth" : "an ORM"} needs a database, and this project has none. Add one in the same command: efforge add database/d1 ${[...(hasOrm ? [] : ["orm/effect-sql"]), ...typed].join(" ")}`);
|
|
139
|
+
if (e.slot === "orm" && next.orm === "none")
|
|
140
|
+
return incompatible("database", next.database, [...e.allowed], `a database needs an ORM. Add one with it: efforge add ${typed.join(" ")} orm/drizzle (or orm/effect-sql)`);
|
|
141
|
+
return e;
|
|
142
|
+
}));
|
|
143
|
+
return { stack: valid, skipped };
|
|
144
|
+
});
|
|
145
|
+
export const readManifest = (dir) => Effect.gen(function* () {
|
|
146
|
+
const fs = yield* FileSystem.FileSystem;
|
|
147
|
+
const path = yield* Path.Path;
|
|
148
|
+
const fail = (reason) => new NotAnEfforgeProject({ dir, reason });
|
|
149
|
+
if (!(yield* fs.exists(dir).pipe(Effect.orElseSucceed(() => false))))
|
|
150
|
+
return yield* fail("the directory does not exist");
|
|
151
|
+
const text = yield* fs
|
|
152
|
+
.readFileString(path.join(dir, "efforge.jsonc"))
|
|
153
|
+
.pipe(Effect.mapError(() => fail("efforge.jsonc not found")));
|
|
154
|
+
const errors = [];
|
|
155
|
+
const json = parse(text, errors, { allowTrailingComma: true });
|
|
156
|
+
if (errors.length > 0)
|
|
157
|
+
return yield* fail("efforge.jsonc is not valid JSONC");
|
|
158
|
+
return yield* Schema.decodeUnknownEffect(EfforgeManifest)(json).pipe(Effect.mapError((e) => fail(`efforge.jsonc is not a valid manifest: ${e.message}`)));
|
|
159
|
+
});
|
|
160
|
+
/**
|
|
161
|
+
* Adds add-ons to an existing efforge project. Everything is computed in memory first
|
|
162
|
+
* (overlays, transforms against the files on disk, dependency/env/manifest merges); nothing is
|
|
163
|
+
* written unless all of it succeeds.
|
|
164
|
+
*/
|
|
165
|
+
export const addAddons = (dir, ids, opts) => Effect.gen(function* () {
|
|
166
|
+
const fs = yield* FileSystem.FileSystem;
|
|
167
|
+
const path = yield* Path.Path;
|
|
168
|
+
const templatesDir = yield* TemplatesDir;
|
|
169
|
+
const manifest = yield* readManifest(dir);
|
|
170
|
+
const io = (target) => Effect.mapError((e) => new GenerateError({ reason: "io", path: target, message: e.message }));
|
|
171
|
+
const disk = new Map();
|
|
172
|
+
const read = (rel) => Effect.gen(function* () {
|
|
173
|
+
if (disk.has(rel))
|
|
174
|
+
return disk.get(rel);
|
|
175
|
+
const abs = path.join(dir, rel);
|
|
176
|
+
const text = (yield* fs.exists(abs)) ? yield* fs.readFileString(abs) : undefined;
|
|
177
|
+
disk.set(rel, text);
|
|
178
|
+
return text;
|
|
179
|
+
}).pipe(io(rel));
|
|
180
|
+
// 1. resolve ids into the new stack
|
|
181
|
+
const { stack, skipped: alreadyThere } = yield* applyIds(manifest.stack, ids);
|
|
182
|
+
const before = new Set(addonIdsFor(manifest.stack));
|
|
183
|
+
const newIds = addonIdsFor(stack).filter((id) => !before.has(id));
|
|
184
|
+
const addons = yield* loadAddons(templatesDir);
|
|
185
|
+
const fresh = [];
|
|
186
|
+
// every add-on of the new stack, in generate order: new ones apply fully, existing ones only
|
|
187
|
+
// what the new choice switches on (e.g. infra=alchemy turns on their workflow secrets)
|
|
188
|
+
const scope = [];
|
|
189
|
+
for (const id of addonIdsFor(stack)) {
|
|
190
|
+
const a = addons.get(id);
|
|
191
|
+
if (a === undefined)
|
|
192
|
+
return yield* new GenerateError({
|
|
193
|
+
reason: "unknown-addon",
|
|
194
|
+
message: `add-on "${id}" is not loaded`,
|
|
195
|
+
});
|
|
196
|
+
scope.push(a);
|
|
197
|
+
if (!before.has(id))
|
|
198
|
+
fresh.push(a);
|
|
199
|
+
}
|
|
200
|
+
const newSet = new Set(newIds);
|
|
201
|
+
const switchedOn = (id, when) => matches(when, stack) && (newSet.has(id) || !matches(when, manifest.stack));
|
|
202
|
+
const skipped = [...alreadyThere];
|
|
203
|
+
const out = new Map(); // path -> new content, everything to write
|
|
204
|
+
const written = new Set();
|
|
205
|
+
const hashes = {};
|
|
206
|
+
// 2. overlays: write if absent, replace if still what efforge generated, else conflict
|
|
207
|
+
const overlays = new Map();
|
|
208
|
+
for (const { manifest: m, dir: addonDir } of scope)
|
|
209
|
+
for (const f of m.files) {
|
|
210
|
+
if (!switchedOn(m.id, f.when))
|
|
211
|
+
continue;
|
|
212
|
+
// package.json files are merged, not replaced: only a new workspace package's is created
|
|
213
|
+
if (isComputed(f.target) && !(isNewPackageJson(f.target) && !(yield* read(f.target))))
|
|
214
|
+
continue;
|
|
215
|
+
overlays.set(f.target, yield* fs.readFileString(path.join(addonDir, f.path)).pipe(io(f.path)));
|
|
216
|
+
}
|
|
217
|
+
const safe = (rel) => {
|
|
218
|
+
const r = path.relative(dir, path.resolve(dir, rel));
|
|
219
|
+
return (rel !== "" &&
|
|
220
|
+
!path.isAbsolute(rel) &&
|
|
221
|
+
r !== "" &&
|
|
222
|
+
!r.startsWith("..") &&
|
|
223
|
+
!path.isAbsolute(r));
|
|
224
|
+
};
|
|
225
|
+
for (const target of overlays.keys())
|
|
226
|
+
if (!safe(target))
|
|
227
|
+
return yield* new GenerateError({
|
|
228
|
+
reason: "unsafe-path",
|
|
229
|
+
path: target,
|
|
230
|
+
message: `add-on target "${target}" is outside the project directory`,
|
|
231
|
+
});
|
|
232
|
+
const tree = new Map();
|
|
233
|
+
const conflicts = [];
|
|
234
|
+
for (const [target, content] of applyProjectName(overlays, stack.name)) {
|
|
235
|
+
const existing = yield* read(target);
|
|
236
|
+
if (existing === content) {
|
|
237
|
+
skipped.push(target);
|
|
238
|
+
}
|
|
239
|
+
else if (existing === undefined ||
|
|
240
|
+
opts.overwrite ||
|
|
241
|
+
manifest.files[target] === hash(existing)) {
|
|
242
|
+
out.set(target, content);
|
|
243
|
+
written.add(target);
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
conflicts.push({ file: target, diff: unifiedDiff(target, existing, content) });
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
if (!isComputed(target))
|
|
250
|
+
hashes[target] = hash(content);
|
|
251
|
+
tree.set(target, content);
|
|
252
|
+
}
|
|
253
|
+
if (conflicts.length > 0)
|
|
254
|
+
return yield* new OverlayConflict({
|
|
255
|
+
file: conflicts.map((c) => c.file).join(", "),
|
|
256
|
+
diff: conflicts.map((c) => c.diff).join("\n"),
|
|
257
|
+
});
|
|
258
|
+
// 2b. files the new stack no longer generates (wrangler.jsonc once alchemy declares the Worker)
|
|
259
|
+
const removed = new Set();
|
|
260
|
+
if (newIds.includes("infra/alchemy") &&
|
|
261
|
+
manifest.stack.infra === "none" &&
|
|
262
|
+
(yield* read(WRANGLER_FILE)) !== undefined) {
|
|
263
|
+
const existing = (yield* read(WRANGLER_FILE));
|
|
264
|
+
const generated = (yield* generate(manifest.stack, addons)).get(WRANGLER_FILE);
|
|
265
|
+
if (opts.overwrite ||
|
|
266
|
+
sameJsonc(existing, generated) ||
|
|
267
|
+
manifest.files[WRANGLER_FILE] === hash(existing))
|
|
268
|
+
removed.add(WRANGLER_FILE);
|
|
269
|
+
else
|
|
270
|
+
return yield* new OverlayConflict({
|
|
271
|
+
file: WRANGLER_FILE,
|
|
272
|
+
diff: unifiedDiff(WRANGLER_FILE, existing, generated ?? ""),
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
// 3. transforms, against the current disk content of the seam files
|
|
276
|
+
const active = scope.flatMap(({ manifest: m }) => m.transforms.filter((t) => switchedOn(m.id, t.when)));
|
|
277
|
+
const wanted = new Set();
|
|
278
|
+
for (const t of active)
|
|
279
|
+
for (const file of transformTargets(t)) {
|
|
280
|
+
if (!safe(file))
|
|
281
|
+
return yield* new GenerateError({
|
|
282
|
+
reason: "unsafe-path",
|
|
283
|
+
path: file,
|
|
284
|
+
message: `transform file "${file}" is outside the project directory`,
|
|
285
|
+
});
|
|
286
|
+
wanted.add(file);
|
|
287
|
+
}
|
|
288
|
+
for (const rel of wanted) {
|
|
289
|
+
const text = yield* read(rel);
|
|
290
|
+
if (text !== undefined && !tree.has(rel))
|
|
291
|
+
tree.set(rel, text);
|
|
292
|
+
}
|
|
293
|
+
let current = tree;
|
|
294
|
+
const seams = new Set();
|
|
295
|
+
for (const t of active) {
|
|
296
|
+
const prev = current;
|
|
297
|
+
current = yield* runTransform(prev, t);
|
|
298
|
+
for (const [file, content] of current)
|
|
299
|
+
if (prev.get(file) !== content) {
|
|
300
|
+
seams.add(file);
|
|
301
|
+
if (content !== disk.get(file))
|
|
302
|
+
out.set(file, content);
|
|
303
|
+
else
|
|
304
|
+
out.delete(file);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
const transformed = new Set();
|
|
308
|
+
for (const file of seams) {
|
|
309
|
+
// seam files are re-edited by transforms on every add, never hash-checked
|
|
310
|
+
delete hashes[file];
|
|
311
|
+
if (out.has(file))
|
|
312
|
+
(written.has(file) ? written : transformed).add(file);
|
|
313
|
+
}
|
|
314
|
+
// 4. dependencies merged into the existing package.json files
|
|
315
|
+
const used = new Set();
|
|
316
|
+
const packages = new Map();
|
|
317
|
+
for (const { manifest: m } of fresh)
|
|
318
|
+
for (const key of ["dependencies", "devDependencies"])
|
|
319
|
+
for (const [pkgDir, names] of Object.entries(m[key])) {
|
|
320
|
+
const target = pkgDir === "." ? "package.json" : `${pkgDir}/package.json`;
|
|
321
|
+
const text = packages.get(target) ?? tree.get(target) ?? (yield* read(target));
|
|
322
|
+
if (text === undefined)
|
|
323
|
+
return yield* new GenerateError({
|
|
324
|
+
reason: "missing-package-json",
|
|
325
|
+
path: target,
|
|
326
|
+
message: `${m.id} adds dependencies to ${target}, which does not exist`,
|
|
327
|
+
});
|
|
328
|
+
const pkg = yield* Effect.try({
|
|
329
|
+
try: () => JSON.parse(text),
|
|
330
|
+
catch: (e) => new GenerateError({
|
|
331
|
+
reason: "invalid-json",
|
|
332
|
+
path: target,
|
|
333
|
+
message: `${target} is not valid JSON: ${String(e)}`,
|
|
334
|
+
}),
|
|
335
|
+
});
|
|
336
|
+
const existing = pkg[key] ?? {};
|
|
337
|
+
const deps = { ...existing };
|
|
338
|
+
// a Docker build context can't see the root catalog, so such a package pins exact versions
|
|
339
|
+
const pkgPrefix = pkgDir === "." ? "" : `${pkgDir}/`;
|
|
340
|
+
const standalone = tree.has(`${pkgPrefix}Dockerfile`) ||
|
|
341
|
+
(yield* read(`${pkgPrefix}Dockerfile`)) !== undefined;
|
|
342
|
+
for (const name of names) {
|
|
343
|
+
if (name.startsWith("@repo/")) {
|
|
344
|
+
if (!(name in deps))
|
|
345
|
+
deps[name] = "workspace:*";
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
if (versions[name] === undefined)
|
|
349
|
+
return yield* new GenerateError({
|
|
350
|
+
reason: "unpinned",
|
|
351
|
+
package: name,
|
|
352
|
+
message: `${m.id} uses "${name}", which has no entry in versions.ts`,
|
|
353
|
+
});
|
|
354
|
+
if (!standalone)
|
|
355
|
+
used.add(name);
|
|
356
|
+
if (!(name in deps))
|
|
357
|
+
deps[name] = standalone ? versions[name] : "catalog:";
|
|
358
|
+
}
|
|
359
|
+
if (Object.keys(deps).length === Object.keys(existing).length)
|
|
360
|
+
continue;
|
|
361
|
+
// keep the user's order unless they were already sorted
|
|
362
|
+
const keys = Object.keys(existing);
|
|
363
|
+
const ordered = keys.every((k, i) => i === 0 || byName(keys[i - 1], k) <= 0)
|
|
364
|
+
? Object.fromEntries(Object.entries(deps).toSorted(([a], [b]) => byName(a, b)))
|
|
365
|
+
: deps;
|
|
366
|
+
const indent = /^([ \t]+)"/m.exec(text)?.[1] ?? " ";
|
|
367
|
+
packages.set(target, `${JSON.stringify({ ...pkg, [key]: ordered }, null, indent)}${text.endsWith("\n") ? "\n" : ""}`);
|
|
368
|
+
}
|
|
369
|
+
// scripts of the new add-ons (they replace same-named ones, like generate does)
|
|
370
|
+
const replacedScripts = [];
|
|
371
|
+
for (const { manifest: m } of fresh)
|
|
372
|
+
for (const [pkgDir, scripts] of Object.entries(m.scripts ?? {})) {
|
|
373
|
+
const target = pkgDir === "." ? "package.json" : `${pkgDir}/package.json`;
|
|
374
|
+
const text = packages.get(target) ?? tree.get(target) ?? (yield* read(target));
|
|
375
|
+
if (text === undefined)
|
|
376
|
+
return yield* new GenerateError({
|
|
377
|
+
reason: "missing-package-json",
|
|
378
|
+
path: target,
|
|
379
|
+
message: `${m.id} adds scripts to ${target}, which does not exist`,
|
|
380
|
+
});
|
|
381
|
+
const pkg = JSON.parse(text);
|
|
382
|
+
const existingScripts = pkg.scripts ?? {};
|
|
383
|
+
for (const [name, cmd] of Object.entries(scripts))
|
|
384
|
+
if (existingScripts[name] !== undefined && existingScripts[name] !== cmd)
|
|
385
|
+
replacedScripts.push(`${target}: ${name}`);
|
|
386
|
+
const merged = { ...existingScripts, ...scripts };
|
|
387
|
+
const indent = /^([ \t]+)"/m.exec(text)?.[1] ?? " ";
|
|
388
|
+
const next = `${JSON.stringify({ ...pkg, scripts: merged }, null, indent)}${text.endsWith("\n") ? "\n" : ""}`;
|
|
389
|
+
if (next !== text)
|
|
390
|
+
packages.set(target, next);
|
|
391
|
+
}
|
|
392
|
+
for (const [target, text] of packages) {
|
|
393
|
+
out.set(target, text);
|
|
394
|
+
written.add(target);
|
|
395
|
+
}
|
|
396
|
+
// 5. pnpm catalog
|
|
397
|
+
if (used.size > 0) {
|
|
398
|
+
const text = (yield* read("pnpm-workspace.yaml")) ?? "packages:\n - apps/*\n - packages/*\n";
|
|
399
|
+
const doc = parseDocument(text);
|
|
400
|
+
const catalog = {
|
|
401
|
+
...doc.getIn(["catalog"])?.toJSON(),
|
|
402
|
+
};
|
|
403
|
+
for (const name of used)
|
|
404
|
+
catalog[name] ??= versions[name];
|
|
405
|
+
doc.setIn(["catalog"], Object.fromEntries(Object.entries(catalog).toSorted(([a], [b]) => byName(a, b))));
|
|
406
|
+
const next = doc.toString();
|
|
407
|
+
if (next !== text) {
|
|
408
|
+
out.set("pnpm-workspace.yaml", next);
|
|
409
|
+
written.add("pnpm-workspace.yaml");
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
// 6. env vars
|
|
413
|
+
const envText = (yield* read(".env.example")) ?? "";
|
|
414
|
+
const have = new Set(envText.split("\n").map((l) => l.split("=")[0]?.trim()));
|
|
415
|
+
const groups = [];
|
|
416
|
+
const envVars = [];
|
|
417
|
+
for (const { manifest: m } of scope) {
|
|
418
|
+
const vars = m.envVars.filter((v) => switchedOn(m.id, v.when) && !have.has(v.name));
|
|
419
|
+
for (const v of vars) {
|
|
420
|
+
have.add(v.name);
|
|
421
|
+
envVars.push({ name: v.name, description: v.description });
|
|
422
|
+
}
|
|
423
|
+
if (vars.length > 0)
|
|
424
|
+
groups.push(`# ${m.title}\n${vars.map((v) => `${v.name}=${v.example}`).join("\n")}`);
|
|
425
|
+
}
|
|
426
|
+
if (groups.length > 0) {
|
|
427
|
+
const base = envText === "" || envText.endsWith("\n") ? envText : `${envText}\n`;
|
|
428
|
+
out.set(".env.example", `${base}${base === "" ? "" : "\n"}${groups.join("\n\n")}\n`);
|
|
429
|
+
written.add(".env.example");
|
|
430
|
+
}
|
|
431
|
+
// 7. efforge.jsonc
|
|
432
|
+
if (newIds.length > 0) {
|
|
433
|
+
out.set("efforge.jsonc", renderManifest({
|
|
434
|
+
efforgeVersion: manifest.efforgeVersion,
|
|
435
|
+
stack,
|
|
436
|
+
command: toCommand(stack),
|
|
437
|
+
files: Object.fromEntries(Object.entries({ ...manifest.files, ...hashes })
|
|
438
|
+
.filter(([file]) => !seams.has(file) && !removed.has(file))
|
|
439
|
+
.toSorted(([a], [b]) => byName(a, b))),
|
|
440
|
+
}));
|
|
441
|
+
written.add("efforge.jsonc");
|
|
442
|
+
}
|
|
443
|
+
const result = {
|
|
444
|
+
written: [...written].toSorted(),
|
|
445
|
+
created: [...written].filter((f) => disk.has(f) && disk.get(f) === undefined).toSorted(),
|
|
446
|
+
transformed: [...transformed].toSorted(),
|
|
447
|
+
skipped: skipped.toSorted(),
|
|
448
|
+
removed: [...removed].toSorted(),
|
|
449
|
+
added: fresh.map(({ manifest: m }) => m.docs === undefined
|
|
450
|
+
? { id: m.id, title: m.title }
|
|
451
|
+
: { id: m.id, title: m.title, docs: m.docs }),
|
|
452
|
+
envVars,
|
|
453
|
+
replacedScripts,
|
|
454
|
+
};
|
|
455
|
+
if (opts.dryRun)
|
|
456
|
+
return result;
|
|
457
|
+
// all-or-nothing: only now touch the disk
|
|
458
|
+
for (const rel of removed)
|
|
459
|
+
yield* fs.remove(path.join(dir, rel), { force: true }).pipe(io(rel));
|
|
460
|
+
for (const [rel, content] of out) {
|
|
461
|
+
const abs = path.join(dir, rel);
|
|
462
|
+
yield* fs.makeDirectory(path.dirname(abs), { recursive: true }).pipe(io(rel));
|
|
463
|
+
yield* fs.writeFileString(abs, content).pipe(io(rel));
|
|
464
|
+
}
|
|
465
|
+
if ((out.size > 0 || removed.size > 0) && opts.install !== false) {
|
|
466
|
+
yield* exec("pnpm", ["install"], dir);
|
|
467
|
+
// add-ons may add or replace routes
|
|
468
|
+
yield* generateRouteTree(dir);
|
|
469
|
+
}
|
|
470
|
+
return result;
|
|
471
|
+
});
|
|
472
|
+
// --- CLI -----------------------------------------------------------------------
|
|
473
|
+
const describe = (e) => {
|
|
474
|
+
switch (e._tag) {
|
|
475
|
+
case "NotAnEfforgeProject":
|
|
476
|
+
return `${String(e.dir)} is not an efforge project (${String(e.reason)}). Run this from the project root or pass --cwd.`;
|
|
477
|
+
case "PnpmMissing":
|
|
478
|
+
return "efforge projects use pnpm, and it isn't on your PATH. Install it (corepack enable pnpm, or https://pnpm.io/installation) and re-run, or pass --no-install.";
|
|
479
|
+
case "UnknownAddon":
|
|
480
|
+
return String(e.message);
|
|
481
|
+
case "IncompatibleStack":
|
|
482
|
+
return `Can't add ${String(e.slot)}/${String(e.value)}: ${typeof e.reason === "string" ? e.reason : "not compatible with the current stack."}`;
|
|
483
|
+
case "OverlayConflict":
|
|
484
|
+
return `You've edited ${String(e.file)} since it was generated. Nothing was written. Re-run with --overwrite to replace it.\n\n${String(e.diff)}`;
|
|
485
|
+
case "TransformAnchorMissing":
|
|
486
|
+
return `Couldn't find ${String(e.anchor)} in ${String(e.file)}. Nothing was written. Add this by hand, then re-run:\n${String(e.snippet)}`;
|
|
487
|
+
case "CommandFailed":
|
|
488
|
+
return `\`${String(e.command)}\` failed. The files were written; run it yourself.\n${String(e.output).trim()}`;
|
|
489
|
+
default:
|
|
490
|
+
return e.message ?? e._tag;
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
export const addConfig = {
|
|
494
|
+
ids: Argument.String("ids").pipe(Argument.withDescription("Add-ons to add: email, extras/email, ai/effect-ai-byok, ..."), Argument.atLeast(1)),
|
|
495
|
+
overwrite: Flag.Boolean("overwrite").pipe(Flag.withDescription("Replace generated files you have edited"), Flag.withDefault(false)),
|
|
496
|
+
dryRun: Flag.Boolean("dry-run").pipe(Flag.withDescription("Print what would change; write nothing"), Flag.withDefault(false)),
|
|
497
|
+
json: Flag.Boolean("json").pipe(Flag.withDescription("Print the result as JSON on stdout"), Flag.withDefault(false)),
|
|
498
|
+
cwd: Flag.String("cwd").pipe(Flag.withDescription("Project directory (default: current directory)"), Flag.withDefault(".")),
|
|
499
|
+
install: Flag.Boolean("install").pipe(Flag.withDescription("Run pnpm install afterwards (--no-install to skip)"), Flag.withDefault(true)),
|
|
500
|
+
};
|
|
501
|
+
/** `a.b.c` -> numbers, for the "project is newer than this CLI" check. */
|
|
502
|
+
const newer = (a, b) => {
|
|
503
|
+
const pa = a.split(/[.-]/).map(Number);
|
|
504
|
+
const pb = b.split(/[.-]/).map(Number);
|
|
505
|
+
for (let i = 0; i < 3; i++) {
|
|
506
|
+
const x = pa[i] ?? 0;
|
|
507
|
+
const y = pb[i] ?? 0;
|
|
508
|
+
if (x !== y)
|
|
509
|
+
return x > y;
|
|
510
|
+
}
|
|
511
|
+
return false;
|
|
512
|
+
};
|
|
513
|
+
/** Word-wraps `text` to `width` columns. */
|
|
514
|
+
const wrap = (text, width) => {
|
|
515
|
+
const lines = [];
|
|
516
|
+
let line = "";
|
|
517
|
+
for (const word of text.split(/\s+/)) {
|
|
518
|
+
if (line !== "" && line.length + 1 + word.length > width) {
|
|
519
|
+
lines.push(line);
|
|
520
|
+
line = word;
|
|
521
|
+
}
|
|
522
|
+
else
|
|
523
|
+
line = line === "" ? word : `${line} ${word}`;
|
|
524
|
+
}
|
|
525
|
+
if (line !== "")
|
|
526
|
+
lines.push(line);
|
|
527
|
+
return lines;
|
|
528
|
+
};
|
|
529
|
+
const list = (files, mark) => files.map((f) => `${mark} ${f}`);
|
|
530
|
+
export const addHandler = (input) => Effect.gen(function* () {
|
|
531
|
+
const path = yield* Path.Path;
|
|
532
|
+
const live = yield* PromptIO;
|
|
533
|
+
const dir = path.resolve(input.cwd);
|
|
534
|
+
// --json keeps stdout to the one JSON document: steps still run, silently
|
|
535
|
+
const io = input.json
|
|
536
|
+
? {
|
|
537
|
+
...live,
|
|
538
|
+
step: (_label, eff) => eff,
|
|
539
|
+
task: (_label, eff) => eff(() => { }),
|
|
540
|
+
}
|
|
541
|
+
: live;
|
|
542
|
+
if (!input.json)
|
|
543
|
+
yield* io.intro(`${pc.bgCyan(pc.black(" efforge add "))}${pc.dim(` ${input.ids.join(" ")}`)}`);
|
|
544
|
+
const manifest = yield* readManifest(dir);
|
|
545
|
+
if (!input.json && newer(manifest.efforgeVersion, efforgeVersion))
|
|
546
|
+
yield* io.warn(`This project was made with efforge ${manifest.efforgeVersion}, newer than this CLI (${efforgeVersion}). Use npx create-efforge@latest add.`);
|
|
547
|
+
const opts = { overwrite: input.overwrite, install: false };
|
|
548
|
+
// checked before anything is written: install and the route tree both need pnpm
|
|
549
|
+
if (input.install && !input.dryRun && !(yield* has("pnpm", dir)))
|
|
550
|
+
return yield* new PnpmMissing();
|
|
551
|
+
// plan first (in memory): the dirty-tree warning only matters when something will change
|
|
552
|
+
const plan = yield* addAddons(dir, input.ids, { ...opts, dryRun: true });
|
|
553
|
+
const changes = plan.written.length + plan.transformed.length + plan.removed.length > 0;
|
|
554
|
+
if (changes && !input.dryRun && !input.json) {
|
|
555
|
+
const dirty = yield* exec("git", ["status", "--porcelain"], dir).pipe(Effect.map((out) => out.trim() !== ""), Effect.catchTag("CommandFailed", () => Effect.succeed(false)));
|
|
556
|
+
if (dirty)
|
|
557
|
+
yield* io.warn("You have uncommitted changes, so git diff will mix them with efforge's edits. Commit or stash first to review them on their own.");
|
|
558
|
+
}
|
|
559
|
+
const result = input.dryRun || !changes ? plan : yield* addAddons(dir, input.ids, { ...opts, dryRun: false });
|
|
560
|
+
// only when dependencies changed: package.json files or the catalog
|
|
561
|
+
const depsChanged = result.written.some((f) => f.endsWith("package.json") || f === "pnpm-workspace.yaml");
|
|
562
|
+
const finish = Effect.gen(function* () {
|
|
563
|
+
if (!input.install || input.dryRun || !changes)
|
|
564
|
+
return;
|
|
565
|
+
if (depsChanged)
|
|
566
|
+
yield* io.task("Installing dependencies (pnpm install)", (log) => exec("pnpm", ["install"], dir, log));
|
|
567
|
+
// add-ons may add or replace routes
|
|
568
|
+
yield* io.step("Generating the route tree", generateRouteTree(dir));
|
|
569
|
+
});
|
|
570
|
+
if (input.json) {
|
|
571
|
+
yield* finish;
|
|
572
|
+
return yield* Console.log(JSON.stringify({ dryRun: input.dryRun, ...result }, null, 2));
|
|
573
|
+
}
|
|
574
|
+
const changed = result.written.length + result.transformed.length + result.removed.length > 0;
|
|
575
|
+
if (!changed) {
|
|
576
|
+
yield* io.info(`Already there: ${result.skipped.join(", ")}`);
|
|
577
|
+
return yield* io.outro("Nothing to do.");
|
|
578
|
+
}
|
|
579
|
+
yield* io.note([
|
|
580
|
+
...list(result.created, pc.green("+")),
|
|
581
|
+
...list([
|
|
582
|
+
...result.written.filter((f) => !result.created.includes(f)),
|
|
583
|
+
...result.transformed,
|
|
584
|
+
].toSorted(), pc.yellow("~")),
|
|
585
|
+
...list(result.removed, pc.red("-")),
|
|
586
|
+
].join("\n"), `${input.dryRun ? "Would change" : "Changed"}: ${result.added.map((a) => a.title).join(", ") || "stack"}`);
|
|
587
|
+
if (result.removed.length > 0)
|
|
588
|
+
yield* io.warn(`${input.dryRun ? "Would remove" : "Removed"} ${result.removed.join(", ")}: the new add-ons replace it.`);
|
|
589
|
+
if (result.replacedScripts.length > 0)
|
|
590
|
+
yield* io.warn(`${input.dryRun ? "Would replace" : "Replaced"} existing package scripts:\n${result.replacedScripts.map((x) => ` ${x}`).join("\n")}`);
|
|
591
|
+
if (input.dryRun)
|
|
592
|
+
return yield* io.outro(pc.dim("dry run: nothing written"));
|
|
593
|
+
yield* finish;
|
|
594
|
+
// a new database needs its tables before the app (or sign-up) can use it
|
|
595
|
+
const database = result.added.find((a) => a.id.startsWith("database/"))?.id.split("/")[1];
|
|
596
|
+
const alchemy = manifest.stack.infra === "alchemy" || result.added.some((a) => a.id === "infra/alchemy");
|
|
597
|
+
const commands = [
|
|
598
|
+
...(input.install || !depsChanged ? [] : ["pnpm install"]),
|
|
599
|
+
...(database === "postgres" ? ["docker compose up -d --wait", "pnpm db:migrate"] : []),
|
|
600
|
+
...(database === "d1" && !alchemy ? ["pnpm db:migrate"] : []),
|
|
601
|
+
];
|
|
602
|
+
const width = Math.max(0, ...result.envVars.map((x) => x.name.length));
|
|
603
|
+
const room = Math.max(30, (process.stdout.columns || 80) - 14);
|
|
604
|
+
const steps = [
|
|
605
|
+
...commands.map((c) => ` ${pc.cyan(c)}`),
|
|
606
|
+
...(result.envVars.length > 0
|
|
607
|
+
? [
|
|
608
|
+
`${commands.length > 0 ? "\n" : ""}${pc.bold("New env vars")} ${pc.dim("(in .env.example; local values in .dev.vars)")}`,
|
|
609
|
+
// full descriptions, wrapped under the name: the hint at the end is often the useful part
|
|
610
|
+
...result.envVars.map((v) => ` ${v.name.padEnd(width)}\n${wrap(v.description, room)
|
|
611
|
+
.map((l) => ` ${pc.dim(l)}`)
|
|
612
|
+
.join("\n")}`),
|
|
613
|
+
]
|
|
614
|
+
: []),
|
|
615
|
+
...result.added
|
|
616
|
+
.filter((a) => a.docs !== undefined)
|
|
617
|
+
.map((a) => `${a.title}: ${pc.dim(a.docs)}`),
|
|
618
|
+
];
|
|
619
|
+
if (steps.length > 0)
|
|
620
|
+
yield* io.note(steps.join("\n"), "Next steps");
|
|
621
|
+
yield* io.outro(`${pc.green("Done.")} ${pc.dim("Review the changes with")} git diff`);
|
|
622
|
+
}).pipe(Effect.catch((e) => Effect.gen(function* () {
|
|
623
|
+
yield* Effect.sync(() => {
|
|
624
|
+
process.exitCode = 1;
|
|
625
|
+
});
|
|
626
|
+
if (input.json)
|
|
627
|
+
return yield* Console.log(JSON.stringify({ error: { code: e._tag, message: describe(e) } }, null, 2));
|
|
628
|
+
yield* Console.error(pc.red(describe(e)));
|
|
629
|
+
})));
|
|
630
|
+
export const addCommand = Command.make("add", addConfig, addHandler).pipe(Command.withDescription("Add an add-on to an existing efforge project"), Command.withExamples([
|
|
631
|
+
{ command: "npx create-efforge add email", description: "Add the email add-on" },
|
|
632
|
+
{
|
|
633
|
+
command: "npx create-efforge add ai/effect-ai-byok extras/storage --dry-run",
|
|
634
|
+
description: "Show what would change; write nothing",
|
|
635
|
+
},
|
|
636
|
+
{
|
|
637
|
+
command: "npx create-efforge add database/d1 orm/effect-sql auth/better-auth",
|
|
638
|
+
description: "Add a database, an ORM and auth together",
|
|
639
|
+
},
|
|
640
|
+
]));
|