create-efforge 0.0.0-stage → 0.1.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 +140 -2
- package/dist/addon.js +115 -0
- package/dist/bin.js +6 -0
- package/dist/cli.js +9 -0
- package/dist/commands/add.js +440 -0
- package/dist/commands/create.js +186 -0
- package/dist/diff.js +72 -0
- package/dist/exec.js +20 -0
- package/dist/generate.js +181 -0
- package/dist/manifest.js +14 -0
- package/dist/projectName.js +41 -0
- package/dist/prompts/flow.js +111 -0
- package/dist/prompts/navigable.js +97 -0
- package/dist/routeTree.js +7 -0
- package/dist/rules.js +24 -0
- package/dist/slots.js +96 -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 +26 -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 +47 -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 +198 -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 +212 -0
- package/templates/base/files/AGENTS.md +70 -0
- package/templates/base/files/CLAUDE.md +1 -0
- package/templates/base/files/README.md +28 -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/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 +61 -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,440 @@
|
|
|
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 } 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 { versions } from "../versions.js";
|
|
19
|
+
import { TemplatesDir } from "./create.js";
|
|
20
|
+
export class NotAnEfforgeProject extends Schema.TaggedError()("NotAnEfforgeProject", { dir: Schema.String, reason: Schema.String }) {
|
|
21
|
+
}
|
|
22
|
+
/** A target file that exists, differs from what efforge generated, and `--overwrite` wasn't given. */
|
|
23
|
+
export class OverlayConflict extends Schema.TaggedError()("OverlayConflict", {
|
|
24
|
+
file: Schema.String,
|
|
25
|
+
diff: Schema.String,
|
|
26
|
+
}) {
|
|
27
|
+
}
|
|
28
|
+
const incompatible = (slot, value, allowed, reason) => new IncompatibleStack({ slot, value, allowed, reason });
|
|
29
|
+
const allIds = slots.flatMap((s) => s.options.filter((o) => o.id !== "none").map((o) => `${s.id}/${o.id}`));
|
|
30
|
+
const isNewPackageJson = (target) => target.endsWith("/package.json");
|
|
31
|
+
const byName = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
32
|
+
/** `email`, `extras/email`, `ai/effect-ai-byok`, `ai=effect-ai-byok` -> slot + option. */
|
|
33
|
+
const parseId = (id) => {
|
|
34
|
+
const [slotPart, option = ""] = id.includes("=")
|
|
35
|
+
? id.split("=", 2)
|
|
36
|
+
: id.includes("/")
|
|
37
|
+
? id.split("/", 2)
|
|
38
|
+
: [undefined, id];
|
|
39
|
+
const unknown = (why) => Effect.fail(incompatible("extras", id, allIds, why));
|
|
40
|
+
if (option === "none")
|
|
41
|
+
return unknown("Removing an add-on isn't supported in v0.1.");
|
|
42
|
+
const candidates = slots.filter((s) => (slotPart === undefined || s.id === slotPart) && s.options.some((o) => o.id === option));
|
|
43
|
+
const [first] = candidates;
|
|
44
|
+
if (first === undefined)
|
|
45
|
+
return unknown(`"${id}" is not an add-on.`);
|
|
46
|
+
if (candidates.length > 1)
|
|
47
|
+
return unknown(`"${id}" is ambiguous; write it as ${candidates.map((s) => `${s.id}/${id}`).join(" or ")}.`);
|
|
48
|
+
return Effect.succeed({ slot: first, option });
|
|
49
|
+
};
|
|
50
|
+
/** Applies `ids` to `stack`. Single-select slots may only go from `none` to a value. */
|
|
51
|
+
const applyIds = (stack, ids) => Effect.gen(function* () {
|
|
52
|
+
let next = stack;
|
|
53
|
+
const skipped = [];
|
|
54
|
+
for (const id of ids) {
|
|
55
|
+
const { slot, option } = yield* parseId(id);
|
|
56
|
+
if (slot.id === "extras") {
|
|
57
|
+
if (next.extras.includes(option))
|
|
58
|
+
skipped.push(`extras/${option}`);
|
|
59
|
+
else {
|
|
60
|
+
const order = slot.options.map((o) => o.id);
|
|
61
|
+
const extras = [...next.extras, option].toSorted((a, b) => order.indexOf(a) - order.indexOf(b));
|
|
62
|
+
next = { ...next, extras };
|
|
63
|
+
}
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
const current = next[slot.id];
|
|
67
|
+
if (current === option)
|
|
68
|
+
skipped.push(`${slot.id}/${option}`);
|
|
69
|
+
else if (current === "none")
|
|
70
|
+
next = { ...next, [slot.id]: option };
|
|
71
|
+
else
|
|
72
|
+
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}.`);
|
|
73
|
+
}
|
|
74
|
+
return { stack: yield* validate(next), skipped };
|
|
75
|
+
});
|
|
76
|
+
const readManifest = (dir) => Effect.gen(function* () {
|
|
77
|
+
const fs = yield* FileSystem.FileSystem;
|
|
78
|
+
const path = yield* Path.Path;
|
|
79
|
+
const fail = (reason) => new NotAnEfforgeProject({ dir, reason });
|
|
80
|
+
const text = yield* fs
|
|
81
|
+
.readFileString(path.join(dir, "efforge.jsonc"))
|
|
82
|
+
.pipe(Effect.mapError(() => fail("efforge.jsonc not found")));
|
|
83
|
+
const errors = [];
|
|
84
|
+
const json = parse(text, errors, { allowTrailingComma: true });
|
|
85
|
+
if (errors.length > 0)
|
|
86
|
+
return yield* fail("efforge.jsonc is not valid JSONC");
|
|
87
|
+
return yield* Schema.decodeUnknownEffect(EfforgeManifest)(json).pipe(Effect.mapError((e) => fail(`efforge.jsonc is not a valid manifest: ${e.message}`)));
|
|
88
|
+
});
|
|
89
|
+
/**
|
|
90
|
+
* Adds add-ons to an existing efforge project. Everything is computed in memory first
|
|
91
|
+
* (overlays, transforms against the files on disk, dependency/env/manifest merges); nothing is
|
|
92
|
+
* written unless all of it succeeds.
|
|
93
|
+
*/
|
|
94
|
+
export const addAddons = (dir, ids, opts) => Effect.gen(function* () {
|
|
95
|
+
const fs = yield* FileSystem.FileSystem;
|
|
96
|
+
const path = yield* Path.Path;
|
|
97
|
+
const templatesDir = yield* TemplatesDir;
|
|
98
|
+
const manifest = yield* readManifest(dir);
|
|
99
|
+
const io = (target) => Effect.mapError((e) => new GenerateError({ reason: "io", path: target, message: e.message }));
|
|
100
|
+
const disk = new Map();
|
|
101
|
+
const read = (rel) => Effect.gen(function* () {
|
|
102
|
+
if (disk.has(rel))
|
|
103
|
+
return disk.get(rel);
|
|
104
|
+
const abs = path.join(dir, rel);
|
|
105
|
+
const text = (yield* fs.exists(abs)) ? yield* fs.readFileString(abs) : undefined;
|
|
106
|
+
disk.set(rel, text);
|
|
107
|
+
return text;
|
|
108
|
+
}).pipe(io(rel));
|
|
109
|
+
// 1. resolve ids into the new stack
|
|
110
|
+
const { stack, skipped: alreadyThere } = yield* applyIds(manifest.stack, ids);
|
|
111
|
+
const before = new Set(addonIdsFor(manifest.stack));
|
|
112
|
+
const newIds = addonIdsFor(stack).filter((id) => !before.has(id));
|
|
113
|
+
const addons = yield* loadAddons(templatesDir);
|
|
114
|
+
const fresh = [];
|
|
115
|
+
// every add-on of the new stack, in generate order: new ones apply fully, existing ones only
|
|
116
|
+
// what the new choice switches on (e.g. infra=alchemy turns on their workflow secrets)
|
|
117
|
+
const scope = [];
|
|
118
|
+
for (const id of addonIdsFor(stack)) {
|
|
119
|
+
const a = addons.get(id);
|
|
120
|
+
if (a === undefined)
|
|
121
|
+
return yield* new GenerateError({
|
|
122
|
+
reason: "unknown-addon",
|
|
123
|
+
message: `add-on "${id}" is not loaded`,
|
|
124
|
+
});
|
|
125
|
+
scope.push(a);
|
|
126
|
+
if (!before.has(id))
|
|
127
|
+
fresh.push(a);
|
|
128
|
+
}
|
|
129
|
+
const newSet = new Set(newIds);
|
|
130
|
+
const switchedOn = (id, when) => matches(when, stack) && (newSet.has(id) || !matches(when, manifest.stack));
|
|
131
|
+
const skipped = [...alreadyThere];
|
|
132
|
+
const out = new Map(); // path -> new content, everything to write
|
|
133
|
+
const written = new Set();
|
|
134
|
+
const hashes = {};
|
|
135
|
+
// 2. overlays: write if absent, replace if still what efforge generated, else conflict
|
|
136
|
+
const overlays = new Map();
|
|
137
|
+
for (const { manifest: m, dir: addonDir } of scope)
|
|
138
|
+
for (const f of m.files) {
|
|
139
|
+
if (!switchedOn(m.id, f.when))
|
|
140
|
+
continue;
|
|
141
|
+
// package.json files are merged, not replaced: only a new workspace package's is created
|
|
142
|
+
if (isComputed(f.target) && !(isNewPackageJson(f.target) && !(yield* read(f.target))))
|
|
143
|
+
continue;
|
|
144
|
+
overlays.set(f.target, yield* fs.readFileString(path.join(addonDir, f.path)).pipe(io(f.path)));
|
|
145
|
+
}
|
|
146
|
+
const safe = (rel) => {
|
|
147
|
+
const r = path.relative(dir, path.resolve(dir, rel));
|
|
148
|
+
return (rel !== "" &&
|
|
149
|
+
!path.isAbsolute(rel) &&
|
|
150
|
+
r !== "" &&
|
|
151
|
+
!r.startsWith("..") &&
|
|
152
|
+
!path.isAbsolute(r));
|
|
153
|
+
};
|
|
154
|
+
for (const target of overlays.keys())
|
|
155
|
+
if (!safe(target))
|
|
156
|
+
return yield* new GenerateError({
|
|
157
|
+
reason: "unsafe-path",
|
|
158
|
+
path: target,
|
|
159
|
+
message: `add-on target "${target}" is outside the project directory`,
|
|
160
|
+
});
|
|
161
|
+
const tree = new Map();
|
|
162
|
+
const conflicts = [];
|
|
163
|
+
for (const [target, content] of applyProjectName(overlays, stack.name)) {
|
|
164
|
+
const existing = yield* read(target);
|
|
165
|
+
if (existing === content) {
|
|
166
|
+
skipped.push(target);
|
|
167
|
+
}
|
|
168
|
+
else if (existing === undefined ||
|
|
169
|
+
opts.overwrite ||
|
|
170
|
+
manifest.files[target] === hash(existing)) {
|
|
171
|
+
out.set(target, content);
|
|
172
|
+
written.add(target);
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
conflicts.push({ file: target, diff: unifiedDiff(target, existing, content) });
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
if (!isComputed(target))
|
|
179
|
+
hashes[target] = hash(content);
|
|
180
|
+
tree.set(target, content);
|
|
181
|
+
}
|
|
182
|
+
if (conflicts.length > 0)
|
|
183
|
+
return yield* new OverlayConflict({
|
|
184
|
+
file: conflicts.map((c) => c.file).join(", "),
|
|
185
|
+
diff: conflicts.map((c) => c.diff).join("\n"),
|
|
186
|
+
});
|
|
187
|
+
// 2b. files the new stack no longer generates (wrangler.jsonc once alchemy declares the Worker)
|
|
188
|
+
const removed = new Set();
|
|
189
|
+
if (newIds.includes("infra/alchemy") &&
|
|
190
|
+
manifest.stack.infra === "none" &&
|
|
191
|
+
(yield* read(WRANGLER_FILE)) !== undefined) {
|
|
192
|
+
const existing = (yield* read(WRANGLER_FILE));
|
|
193
|
+
const generated = (yield* generate(manifest.stack, addons)).get(WRANGLER_FILE);
|
|
194
|
+
if (opts.overwrite ||
|
|
195
|
+
existing === generated ||
|
|
196
|
+
manifest.files[WRANGLER_FILE] === hash(existing))
|
|
197
|
+
removed.add(WRANGLER_FILE);
|
|
198
|
+
else
|
|
199
|
+
return yield* new OverlayConflict({
|
|
200
|
+
file: WRANGLER_FILE,
|
|
201
|
+
diff: unifiedDiff(WRANGLER_FILE, existing, generated ?? ""),
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
// 3. transforms, against the current disk content of the seam files
|
|
205
|
+
const active = scope.flatMap(({ manifest: m }) => m.transforms.filter((t) => switchedOn(m.id, t.when)));
|
|
206
|
+
const wanted = new Set();
|
|
207
|
+
for (const t of active)
|
|
208
|
+
for (const file of transformTargets(t)) {
|
|
209
|
+
if (!safe(file))
|
|
210
|
+
return yield* new GenerateError({
|
|
211
|
+
reason: "unsafe-path",
|
|
212
|
+
path: file,
|
|
213
|
+
message: `transform file "${file}" is outside the project directory`,
|
|
214
|
+
});
|
|
215
|
+
wanted.add(file);
|
|
216
|
+
}
|
|
217
|
+
for (const rel of wanted) {
|
|
218
|
+
const text = yield* read(rel);
|
|
219
|
+
if (text !== undefined && !tree.has(rel))
|
|
220
|
+
tree.set(rel, text);
|
|
221
|
+
}
|
|
222
|
+
let current = tree;
|
|
223
|
+
const seams = new Set();
|
|
224
|
+
for (const t of active) {
|
|
225
|
+
const prev = current;
|
|
226
|
+
current = yield* runTransform(prev, t);
|
|
227
|
+
for (const [file, content] of current)
|
|
228
|
+
if (prev.get(file) !== content) {
|
|
229
|
+
seams.add(file);
|
|
230
|
+
if (content !== disk.get(file))
|
|
231
|
+
out.set(file, content);
|
|
232
|
+
else
|
|
233
|
+
out.delete(file);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
const transformed = new Set();
|
|
237
|
+
for (const file of seams) {
|
|
238
|
+
// seam files are re-edited by transforms on every add, never hash-checked
|
|
239
|
+
delete hashes[file];
|
|
240
|
+
if (out.has(file))
|
|
241
|
+
(written.has(file) ? written : transformed).add(file);
|
|
242
|
+
}
|
|
243
|
+
// 4. dependencies merged into the existing package.json files
|
|
244
|
+
const used = new Set();
|
|
245
|
+
const packages = new Map();
|
|
246
|
+
for (const { manifest: m } of fresh)
|
|
247
|
+
for (const key of ["dependencies", "devDependencies"])
|
|
248
|
+
for (const [pkgDir, names] of Object.entries(m[key])) {
|
|
249
|
+
const target = pkgDir === "." ? "package.json" : `${pkgDir}/package.json`;
|
|
250
|
+
const text = packages.get(target) ?? tree.get(target) ?? (yield* read(target));
|
|
251
|
+
if (text === undefined)
|
|
252
|
+
return yield* new GenerateError({
|
|
253
|
+
reason: "missing-package-json",
|
|
254
|
+
path: target,
|
|
255
|
+
message: `${m.id} adds dependencies to ${target}, which does not exist`,
|
|
256
|
+
});
|
|
257
|
+
const pkg = yield* Effect.try({
|
|
258
|
+
try: () => JSON.parse(text),
|
|
259
|
+
catch: (e) => new GenerateError({
|
|
260
|
+
reason: "invalid-json",
|
|
261
|
+
path: target,
|
|
262
|
+
message: `${target} is not valid JSON: ${String(e)}`,
|
|
263
|
+
}),
|
|
264
|
+
});
|
|
265
|
+
const existing = pkg[key] ?? {};
|
|
266
|
+
const deps = { ...existing };
|
|
267
|
+
// a Docker build context can't see the root catalog, so such a package pins exact versions
|
|
268
|
+
const pkgPrefix = pkgDir === "." ? "" : `${pkgDir}/`;
|
|
269
|
+
const standalone = tree.has(`${pkgPrefix}Dockerfile`) ||
|
|
270
|
+
(yield* read(`${pkgPrefix}Dockerfile`)) !== undefined;
|
|
271
|
+
for (const name of names) {
|
|
272
|
+
if (name.startsWith("@repo/")) {
|
|
273
|
+
if (!(name in deps))
|
|
274
|
+
deps[name] = "workspace:*";
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
if (versions[name] === undefined)
|
|
278
|
+
return yield* new GenerateError({
|
|
279
|
+
reason: "unpinned",
|
|
280
|
+
package: name,
|
|
281
|
+
message: `${m.id} uses "${name}", which has no entry in versions.ts`,
|
|
282
|
+
});
|
|
283
|
+
if (!standalone)
|
|
284
|
+
used.add(name);
|
|
285
|
+
if (!(name in deps))
|
|
286
|
+
deps[name] = standalone ? versions[name] : "catalog:";
|
|
287
|
+
}
|
|
288
|
+
if (Object.keys(deps).length === Object.keys(existing).length)
|
|
289
|
+
continue;
|
|
290
|
+
// keep the user's order unless they were already sorted
|
|
291
|
+
const keys = Object.keys(existing);
|
|
292
|
+
const ordered = keys.every((k, i) => i === 0 || byName(keys[i - 1], k) <= 0)
|
|
293
|
+
? Object.fromEntries(Object.entries(deps).toSorted(([a], [b]) => byName(a, b)))
|
|
294
|
+
: deps;
|
|
295
|
+
const indent = /^([ \t]+)"/m.exec(text)?.[1] ?? " ";
|
|
296
|
+
packages.set(target, `${JSON.stringify({ ...pkg, [key]: ordered }, null, indent)}${text.endsWith("\n") ? "\n" : ""}`);
|
|
297
|
+
}
|
|
298
|
+
// scripts of the new add-ons (they replace same-named ones, like generate does)
|
|
299
|
+
for (const { manifest: m } of fresh)
|
|
300
|
+
for (const [pkgDir, scripts] of Object.entries(m.scripts ?? {})) {
|
|
301
|
+
const target = pkgDir === "." ? "package.json" : `${pkgDir}/package.json`;
|
|
302
|
+
const text = packages.get(target) ?? tree.get(target) ?? (yield* read(target));
|
|
303
|
+
if (text === undefined)
|
|
304
|
+
return yield* new GenerateError({
|
|
305
|
+
reason: "missing-package-json",
|
|
306
|
+
path: target,
|
|
307
|
+
message: `${m.id} adds scripts to ${target}, which does not exist`,
|
|
308
|
+
});
|
|
309
|
+
const pkg = JSON.parse(text);
|
|
310
|
+
const merged = { ...pkg.scripts, ...scripts };
|
|
311
|
+
const indent = /^([ \t]+)"/m.exec(text)?.[1] ?? " ";
|
|
312
|
+
const next = `${JSON.stringify({ ...pkg, scripts: merged }, null, indent)}${text.endsWith("\n") ? "\n" : ""}`;
|
|
313
|
+
if (next !== text)
|
|
314
|
+
packages.set(target, next);
|
|
315
|
+
}
|
|
316
|
+
for (const [target, text] of packages) {
|
|
317
|
+
out.set(target, text);
|
|
318
|
+
written.add(target);
|
|
319
|
+
}
|
|
320
|
+
// 5. pnpm catalog
|
|
321
|
+
if (used.size > 0) {
|
|
322
|
+
const text = (yield* read("pnpm-workspace.yaml")) ?? "packages:\n - apps/*\n - packages/*\n";
|
|
323
|
+
const doc = parseDocument(text);
|
|
324
|
+
const catalog = {
|
|
325
|
+
...doc.getIn(["catalog"])?.toJSON(),
|
|
326
|
+
};
|
|
327
|
+
for (const name of used)
|
|
328
|
+
catalog[name] ??= versions[name];
|
|
329
|
+
doc.setIn(["catalog"], Object.fromEntries(Object.entries(catalog).toSorted(([a], [b]) => byName(a, b))));
|
|
330
|
+
const next = doc.toString();
|
|
331
|
+
if (next !== text) {
|
|
332
|
+
out.set("pnpm-workspace.yaml", next);
|
|
333
|
+
written.add("pnpm-workspace.yaml");
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
// 6. env vars
|
|
337
|
+
const envText = (yield* read(".env.example")) ?? "";
|
|
338
|
+
const have = new Set(envText.split("\n").map((l) => l.split("=")[0]?.trim()));
|
|
339
|
+
const groups = [];
|
|
340
|
+
for (const { manifest: m } of scope) {
|
|
341
|
+
const vars = m.envVars.filter((v) => switchedOn(m.id, v.when) && !have.has(v.name));
|
|
342
|
+
for (const v of vars)
|
|
343
|
+
have.add(v.name);
|
|
344
|
+
if (vars.length > 0)
|
|
345
|
+
groups.push(`# ${m.title}\n${vars.map((v) => `${v.name}=${v.example}`).join("\n")}`);
|
|
346
|
+
}
|
|
347
|
+
if (groups.length > 0) {
|
|
348
|
+
const base = envText === "" || envText.endsWith("\n") ? envText : `${envText}\n`;
|
|
349
|
+
out.set(".env.example", `${base}${base === "" ? "" : "\n"}${groups.join("\n\n")}\n`);
|
|
350
|
+
written.add(".env.example");
|
|
351
|
+
}
|
|
352
|
+
// 7. efforge.jsonc
|
|
353
|
+
if (newIds.length > 0) {
|
|
354
|
+
out.set("efforge.jsonc", renderManifest({
|
|
355
|
+
efforgeVersion: manifest.efforgeVersion,
|
|
356
|
+
stack,
|
|
357
|
+
command: toCommand(stack),
|
|
358
|
+
files: Object.fromEntries(Object.entries({ ...manifest.files, ...hashes })
|
|
359
|
+
.filter(([file]) => !seams.has(file) && !removed.has(file))
|
|
360
|
+
.toSorted(([a], [b]) => byName(a, b))),
|
|
361
|
+
}));
|
|
362
|
+
written.add("efforge.jsonc");
|
|
363
|
+
}
|
|
364
|
+
const result = {
|
|
365
|
+
written: [...written].toSorted(),
|
|
366
|
+
transformed: [...transformed].toSorted(),
|
|
367
|
+
skipped: skipped.toSorted(),
|
|
368
|
+
removed: [...removed].toSorted(),
|
|
369
|
+
};
|
|
370
|
+
if (opts.dryRun)
|
|
371
|
+
return result;
|
|
372
|
+
// all-or-nothing: only now touch the disk
|
|
373
|
+
for (const rel of removed)
|
|
374
|
+
yield* fs.remove(path.join(dir, rel), { force: true }).pipe(io(rel));
|
|
375
|
+
for (const [rel, content] of out) {
|
|
376
|
+
const abs = path.join(dir, rel);
|
|
377
|
+
yield* fs.makeDirectory(path.dirname(abs), { recursive: true }).pipe(io(rel));
|
|
378
|
+
yield* fs.writeFileString(abs, content).pipe(io(rel));
|
|
379
|
+
}
|
|
380
|
+
if ((out.size > 0 || removed.size > 0) && opts.install !== false) {
|
|
381
|
+
yield* exec("pnpm", ["install"], dir);
|
|
382
|
+
// add-ons may add or replace routes
|
|
383
|
+
yield* generateRouteTree(dir);
|
|
384
|
+
}
|
|
385
|
+
return result;
|
|
386
|
+
});
|
|
387
|
+
// --- CLI -----------------------------------------------------------------------
|
|
388
|
+
const describe = (e) => {
|
|
389
|
+
switch (e._tag) {
|
|
390
|
+
case "NotAnEfforgeProject":
|
|
391
|
+
return `${String(e.dir)} is not an efforge project (${String(e.reason)}). Run this from the project root or pass --cwd.`;
|
|
392
|
+
case "IncompatibleStack":
|
|
393
|
+
return `Can't add "${String(e.value)}" (${String(e.slot)}): ${typeof e.reason === "string" ? e.reason : "not compatible with the current stack."} Options: ${e.allowed.join(", ")}`;
|
|
394
|
+
case "OverlayConflict":
|
|
395
|
+
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)}`;
|
|
396
|
+
case "TransformAnchorMissing":
|
|
397
|
+
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)}`;
|
|
398
|
+
case "CommandFailed":
|
|
399
|
+
return `\`${String(e.command)}\` failed. The files were written; run it yourself.\n${String(e.output).trim()}`;
|
|
400
|
+
default:
|
|
401
|
+
return e.message ?? e._tag;
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
export const addConfig = {
|
|
405
|
+
ids: Argument.String("ids").pipe(Argument.withDescription("Add-ons to add: email, extras/email, ai/effect-ai-byok, ..."), Argument.atLeast(1)),
|
|
406
|
+
overwrite: Flag.Boolean("overwrite").pipe(Flag.withDescription("Replace generated files you have edited"), Flag.withDefault(false)),
|
|
407
|
+
dryRun: Flag.Boolean("dry-run").pipe(Flag.withDescription("Print what would change; write nothing"), Flag.withDefault(false)),
|
|
408
|
+
json: Flag.Boolean("json").pipe(Flag.withDescription("Print the result as JSON on stdout"), Flag.withDefault(false)),
|
|
409
|
+
cwd: Flag.String("cwd").pipe(Flag.withDescription("Project directory (default: current directory)"), Flag.withDefault(".")),
|
|
410
|
+
install: Flag.Boolean("install").pipe(Flag.withDescription("Run pnpm install afterwards (--no-install to skip)"), Flag.withDefault(true)),
|
|
411
|
+
};
|
|
412
|
+
export const addHandler = (input) => Effect.gen(function* () {
|
|
413
|
+
const path = yield* Path.Path;
|
|
414
|
+
const result = yield* addAddons(path.resolve(input.cwd), input.ids, {
|
|
415
|
+
overwrite: input.overwrite,
|
|
416
|
+
dryRun: input.dryRun,
|
|
417
|
+
install: input.install,
|
|
418
|
+
});
|
|
419
|
+
if (input.json)
|
|
420
|
+
return yield* Console.log(JSON.stringify({ dryRun: input.dryRun, ...result }, null, 2));
|
|
421
|
+
const section = (title, files) => files.length === 0 ? [] : [`${pc.bold(title)}\n${files.map((f) => ` ${f}`).join("\n")}`];
|
|
422
|
+
yield* Console.log([
|
|
423
|
+
...section(input.dryRun ? "Would write" : "Wrote", result.written),
|
|
424
|
+
...section(input.dryRun ? "Would edit" : "Edited", result.transformed),
|
|
425
|
+
...section(input.dryRun ? "Would remove" : "Removed", result.removed),
|
|
426
|
+
...section("Unchanged", result.skipped),
|
|
427
|
+
...(result.written.length === 0 &&
|
|
428
|
+
result.transformed.length === 0 &&
|
|
429
|
+
result.removed.length === 0
|
|
430
|
+
? ["Nothing to do."]
|
|
431
|
+
: []),
|
|
432
|
+
...(input.dryRun ? [pc.dim("dry run: nothing written")] : []),
|
|
433
|
+
].join("\n\n"));
|
|
434
|
+
}).pipe(Effect.catch((e) => Effect.gen(function* () {
|
|
435
|
+
yield* Console.error(pc.red(describe(e)));
|
|
436
|
+
yield* Effect.sync(() => {
|
|
437
|
+
process.exitCode = 1;
|
|
438
|
+
});
|
|
439
|
+
})));
|
|
440
|
+
export const addCommand = Command.make("add", addConfig, addHandler).pipe(Command.withDescription("Add an add-on to an existing efforge project"));
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { dirname, join } from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { Console, Context, Effect, FileSystem, Option, Path } from "effect";
|
|
4
|
+
import { Argument, Command, Flag } from "effect/cli";
|
|
5
|
+
import pc from "picocolors";
|
|
6
|
+
import { loadAddons } from "../addon.js";
|
|
7
|
+
import { exec } from "../exec.js";
|
|
8
|
+
import { generate } from "../generate.js";
|
|
9
|
+
import { askStack, DEFAULT_NAME } from "../prompts/flow.js";
|
|
10
|
+
import { PromptIO } from "../prompts/navigable.js";
|
|
11
|
+
import { generateRouteTree } from "../routeTree.js";
|
|
12
|
+
import { slots } from "../slots.js";
|
|
13
|
+
import { toCommand } from "../stack.js";
|
|
14
|
+
import { TargetNotEmpty, writeTree } from "../write.js";
|
|
15
|
+
/** The package's `templates/` dir, resolved relative to this module (same depth in `src/` and `dist/`). */
|
|
16
|
+
export const defaultTemplatesDir = join(dirname(fileURLToPath(import.meta.url)), "..", "..", "templates");
|
|
17
|
+
/** Where add-on templates live; tests point it at fixtures. */
|
|
18
|
+
export const TemplatesDir = Context.Reference("efforge/TemplatesDir", {
|
|
19
|
+
defaultValue: () => defaultTemplatesDir,
|
|
20
|
+
});
|
|
21
|
+
const summary = (stack) => [
|
|
22
|
+
`${pc.dim("name".padEnd(9))} ${stack.name}`,
|
|
23
|
+
...slots.map((s) => {
|
|
24
|
+
const v = s.id === "extras" ? stack.extras.join(", ") : stack[s.id];
|
|
25
|
+
return `${pc.dim(s.id.padEnd(9))} ${v === "" ? "none" : v}`;
|
|
26
|
+
}),
|
|
27
|
+
].join("\n");
|
|
28
|
+
const silentIO = (io) => ({
|
|
29
|
+
...io,
|
|
30
|
+
intro: () => Effect.void,
|
|
31
|
+
section: () => Effect.void,
|
|
32
|
+
info: () => Effect.void,
|
|
33
|
+
note: () => Effect.void,
|
|
34
|
+
outro: () => Effect.void,
|
|
35
|
+
step: (_label, eff) => eff,
|
|
36
|
+
});
|
|
37
|
+
/** Files `vp fmt` can format; the lockfile and dotfiles like .gitignore are left alone. */
|
|
38
|
+
const formattable = /\.(?:tsx?|jsx?|json|jsonc|css|md)$/;
|
|
39
|
+
/** validate -> generate -> (dry-run | write -> git init -> pnpm install -> route tree -> format -> commit). */
|
|
40
|
+
export const create = (opts) => Effect.gen(function* () {
|
|
41
|
+
const live = yield* PromptIO;
|
|
42
|
+
const path = yield* Path.Path;
|
|
43
|
+
const fs = yield* FileSystem.FileSystem;
|
|
44
|
+
const templatesDir = yield* TemplatesDir;
|
|
45
|
+
// --json must keep stdout machine-readable: it never prompts and prints no chrome.
|
|
46
|
+
const quiet = opts.json;
|
|
47
|
+
const yes = opts.yes || opts.json;
|
|
48
|
+
const io = quiet ? silentIO(live) : live;
|
|
49
|
+
if (!quiet)
|
|
50
|
+
yield* io.intro(`${pc.bgCyan(pc.black(" create-efforge "))}${pc.dim(" Effect apps on Cloudflare, already wired")}`);
|
|
51
|
+
const stack = yield* askStack(opts.flags, io, { yes });
|
|
52
|
+
const command = toCommand(stack);
|
|
53
|
+
const dirName = stack.name.split("/").pop() ?? stack.name;
|
|
54
|
+
const dir = path.resolve(dirName);
|
|
55
|
+
// Fail before generating (and before the confirm prompt) rather than after the user answered everything.
|
|
56
|
+
if (!opts.dryRun && (yield* fs.exists(dir)) && (yield* fs.readDirectory(dir)).length > 0) {
|
|
57
|
+
return yield* new TargetNotEmpty({ dir });
|
|
58
|
+
}
|
|
59
|
+
const addons = yield* loadAddons(templatesDir);
|
|
60
|
+
const tree = yield* generate(stack, addons);
|
|
61
|
+
const files = [...tree.keys()].toSorted();
|
|
62
|
+
if (opts.dryRun) {
|
|
63
|
+
if (opts.json) {
|
|
64
|
+
yield* Console.log(JSON.stringify({ stack, command, files }, null, 2));
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
yield* io.note(summary(stack), "Your stack");
|
|
68
|
+
yield* Console.log(files.join("\n"));
|
|
69
|
+
yield* io.outro(`${pc.dim("dry run: nothing written")} ${command}`);
|
|
70
|
+
}
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
yield* io.note(`${summary(stack)}\n\n${pc.dim("re-run non-interactively:")}\n${command}`, "Your stack");
|
|
74
|
+
if (!yes) {
|
|
75
|
+
const ok = yield* io.confirm({ id: "confirm", message: "Create it?", initial: true });
|
|
76
|
+
if (ok !== true) {
|
|
77
|
+
yield* io.outro("Nothing written.");
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
yield* io.step("Writing files", writeTree(dir, tree));
|
|
82
|
+
// Init before formatting: oxfmt honors .gitignore files up the directory tree, so a project
|
|
83
|
+
// outside any repo would otherwise pick up a parent's ignore rules (a `*` hides every file).
|
|
84
|
+
// A failure here must not fail the scaffold.
|
|
85
|
+
const gitReady = opts.git
|
|
86
|
+
? yield* io.step("Initializing git", exec("git", ["init"], dir)).pipe(Effect.as(true), Effect.catchTag("CommandFailed", (e) => io
|
|
87
|
+
.info(`git setup skipped: ${e.command} failed. ${e.output.trim().split("\n")[0] ?? ""}`)
|
|
88
|
+
.pipe(Effect.as(false))))
|
|
89
|
+
: false;
|
|
90
|
+
if (opts.install) {
|
|
91
|
+
yield* io.step("Installing dependencies (pnpm install)", exec("pnpm", ["install"], dir));
|
|
92
|
+
// routeTree.gen.ts isn't committed to templates: write it now, so typecheck and format see it.
|
|
93
|
+
yield* io
|
|
94
|
+
.step("Generating the route tree (tsr generate)", generateRouteTree(dir))
|
|
95
|
+
.pipe(Effect.catchTag("CommandFailed", (e) => io.info(`route tree skipped: ${e.command} failed. ${e.output.trim().split("\n")[0] ?? ""}; \`pnpm typecheck\` regenerates it`)));
|
|
96
|
+
// Transforms insert code and bindings unformatted, so tidy them now: the first `pnpm check`
|
|
97
|
+
// passes. Without a repo of its own, a parent repo's ignore rules could hide every file, so
|
|
98
|
+
// name the files: explicit paths bypass ignore files. Anything oxfmt can't fix is the user's to see.
|
|
99
|
+
const targets = gitReady ? [] : files.filter((f) => formattable.test(f));
|
|
100
|
+
yield* io
|
|
101
|
+
.step("Formatting (vp fmt)", exec("pnpm", ["format", ...targets], dir))
|
|
102
|
+
.pipe(Effect.catchTag("CommandFailed", () => Effect.void));
|
|
103
|
+
}
|
|
104
|
+
if (gitReady) {
|
|
105
|
+
// Plain commit: whatever identity git is configured with.
|
|
106
|
+
yield* io
|
|
107
|
+
.step("Creating the initial commit", Effect.gen(function* () {
|
|
108
|
+
yield* exec("git", ["add", "-A"], dir);
|
|
109
|
+
yield* exec("git", ["commit", "-m", "Initial commit from efforge"], dir);
|
|
110
|
+
}))
|
|
111
|
+
.pipe(Effect.catchTag("CommandFailed", (e) => io.info(`git commit skipped: ${e.command} failed. ${e.output.trim().split("\n")[0] ?? ""}`)));
|
|
112
|
+
}
|
|
113
|
+
if (opts.json) {
|
|
114
|
+
yield* Console.log(JSON.stringify({ stack, command, files }, null, 2));
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
yield* io.outro(`${pc.green("Done.")}\n\n cd ${dirName}\n ${opts.install ? "" : "pnpm install\n "}pnpm dev\n\n${pc.dim("Same stack later:")} ${command}`);
|
|
118
|
+
});
|
|
119
|
+
// --- CLI -----------------------------------------------------------------------
|
|
120
|
+
const slotFlag = (id) => {
|
|
121
|
+
const slot = slots.find((s) => s.id === id);
|
|
122
|
+
return Flag.String(id).pipe(Flag.withDescription(`${slot.title}: ${slot.options.map((o) => o.id).join(" | ")}${slot.multi ? " (comma-separated)" : ""}`), Flag.optional);
|
|
123
|
+
};
|
|
124
|
+
/** Shared by the root command and the explicit `create` subcommand. */
|
|
125
|
+
export const createConfig = {
|
|
126
|
+
name: Argument.String("name").pipe(Argument.withDescription(`Project name and directory (default prompt: ${DEFAULT_NAME})`), Argument.optional),
|
|
127
|
+
runtime: slotFlag("runtime"),
|
|
128
|
+
database: slotFlag("database"),
|
|
129
|
+
orm: slotFlag("orm"),
|
|
130
|
+
auth: slotFlag("auth"),
|
|
131
|
+
infra: slotFlag("infra"),
|
|
132
|
+
ai: slotFlag("ai"),
|
|
133
|
+
extras: slotFlag("extras"),
|
|
134
|
+
yes: Flag.Boolean("yes").pipe(Flag.withAlias("y"), Flag.withDescription("Accept defaults for anything a flag didn't set"), Flag.withDefault(false)),
|
|
135
|
+
dryRun: Flag.Boolean("dry-run").pipe(Flag.withDescription("Print the files that would be written; write nothing"), Flag.withDefault(false)),
|
|
136
|
+
json: Flag.Boolean("json").pipe(Flag.withDescription("Print only {stack, command, files} as JSON on stdout (implies --yes)"), Flag.withDefault(false)),
|
|
137
|
+
install: Flag.Boolean("install").pipe(Flag.withDescription("Run pnpm install (--no-install to skip)"), Flag.withDefault(true)),
|
|
138
|
+
git: Flag.Boolean("git").pipe(Flag.withDescription("git init + initial commit (--no-git to skip)"), Flag.withDefault(true)),
|
|
139
|
+
};
|
|
140
|
+
const opt = (o) => Option.getOrUndefined(o);
|
|
141
|
+
export const toCreateOptions = (input) => ({
|
|
142
|
+
flags: {
|
|
143
|
+
name: opt(input.name),
|
|
144
|
+
runtime: opt(input.runtime),
|
|
145
|
+
database: opt(input.database),
|
|
146
|
+
orm: opt(input.orm),
|
|
147
|
+
auth: opt(input.auth),
|
|
148
|
+
infra: opt(input.infra),
|
|
149
|
+
ai: opt(input.ai),
|
|
150
|
+
extras: Option.match(input.extras, {
|
|
151
|
+
onNone: () => undefined,
|
|
152
|
+
onSome: (s) => s
|
|
153
|
+
.split(",")
|
|
154
|
+
.map((x) => x.trim())
|
|
155
|
+
.filter((x) => x !== ""),
|
|
156
|
+
}),
|
|
157
|
+
},
|
|
158
|
+
yes: input.yes,
|
|
159
|
+
dryRun: input.dryRun,
|
|
160
|
+
json: input.json,
|
|
161
|
+
install: input.install,
|
|
162
|
+
git: input.git,
|
|
163
|
+
});
|
|
164
|
+
const describe = (e) => {
|
|
165
|
+
switch (e._tag) {
|
|
166
|
+
case "IncompatibleStack":
|
|
167
|
+
return `--${String(e.slot)}=${String(e.value)} isn't available with the other choices. Options: ${e.allowed.join(", ")}${typeof e.reason === "string" ? `. ${e.reason}` : ""}`;
|
|
168
|
+
case "TargetNotEmpty":
|
|
169
|
+
return `${String(e.dir)} already exists and is not empty. Pick another name or remove it.`;
|
|
170
|
+
case "CommandFailed":
|
|
171
|
+
return `\`${String(e.command)}\` failed. The project was written; run it yourself.\n${String(e.output).trim()}`;
|
|
172
|
+
default:
|
|
173
|
+
return e.message ?? e._tag;
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
/** Command handler: reports failures as one clear line and exit code 1; cancelling exits 0. */
|
|
177
|
+
export const createHandler = (input) => create(toCreateOptions(input)).pipe(Effect.catch((e) => Effect.gen(function* () {
|
|
178
|
+
const io = yield* PromptIO;
|
|
179
|
+
if (e._tag === "Cancelled")
|
|
180
|
+
return yield* io.outro("Cancelled.");
|
|
181
|
+
yield* Console.error(pc.red(describe(e)));
|
|
182
|
+
yield* Effect.sync(() => {
|
|
183
|
+
process.exitCode = 1;
|
|
184
|
+
});
|
|
185
|
+
})));
|
|
186
|
+
export const createCommand = Command.make("create", createConfig, createHandler).pipe(Command.withDescription("Create a new app"));
|