@open-workspace/create 0.3.0 → 0.4.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/README.md +29 -6
- package/create.ts +86 -0
- package/import/args.ts +57 -0
- package/import/cli.ts +432 -0
- package/import/concurrency.ts +52 -0
- package/import/csv.ts +56 -0
- package/import/directory/command.ts +320 -0
- package/import/directory/google.ts +510 -0
- package/import/directory/plan.ts +367 -0
- package/import/gmail.ts +195 -0
- package/import/headers.ts +105 -0
- package/import/imap-command.ts +182 -0
- package/import/imap-source.ts +397 -0
- package/import/invariant.ts +22 -0
- package/import/mbox-source.ts +174 -0
- package/import/mbox.ts +198 -0
- package/import/oauth.ts +423 -0
- package/import/protocol.ts +59 -0
- package/import/uploader.ts +235 -0
- package/import/workspace.ts +218 -0
- package/index.ts +13 -115
- package/module-templates.ts +409 -0
- package/module.ts +105 -0
- package/package.json +18 -6
- package/setup/alchemy-helper.ts +258 -0
- package/setup/alchemy-protocol.ts +63 -0
- package/setup/alchemy.ts +163 -0
- package/setup/args.ts +128 -0
- package/setup/cloudflare-api.ts +156 -0
- package/setup/domains.ts +102 -0
- package/setup/flow.ts +987 -0
- package/setup/ui.ts +146 -0
- package/templates.ts +191 -125
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `bunx @open-workspace/create module <name>`: the files of a new module in
|
|
5
|
+
* a deployment project (`modules/<name>/`), and the edits that wire it in.
|
|
6
|
+
* Pure functions; module.ts writes their results.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Module names: lowercase words joined by dashes. They also name scopes, so no digits. */
|
|
10
|
+
export const MODULE_NAME = /^[a-z]+(?:-[a-z]+)*$/u;
|
|
11
|
+
|
|
12
|
+
/** What the generated module imports, at the versions core is built with. */
|
|
13
|
+
export const moduleDependencies = {
|
|
14
|
+
"@tanstack/react-query": "5.107.1",
|
|
15
|
+
"drizzle-orm": "0.45.4",
|
|
16
|
+
hono: "4.13.17",
|
|
17
|
+
"lucide-react": "1.71.0",
|
|
18
|
+
react: "19.3.1",
|
|
19
|
+
"react-router": "8.5.1",
|
|
20
|
+
zod: "4.6.7",
|
|
21
|
+
} satisfies Record<string, string>;
|
|
22
|
+
|
|
23
|
+
export const moduleDevDependencies = {
|
|
24
|
+
"@types/react": "19.3.0",
|
|
25
|
+
"drizzle-kit": "0.31.11",
|
|
26
|
+
} satisfies Record<string, string>;
|
|
27
|
+
|
|
28
|
+
export type ModuleNames = {
|
|
29
|
+
/** The module's id and folder, e.g. "time-off". */
|
|
30
|
+
name: string;
|
|
31
|
+
/** Its Alchemy factory, e.g. "TimeOff". */
|
|
32
|
+
pascal: string;
|
|
33
|
+
/** Its server factory, e.g. "timeOff". */
|
|
34
|
+
camel: string;
|
|
35
|
+
/** Its constants' prefix, e.g. "TIME_OFF". */
|
|
36
|
+
constant: string;
|
|
37
|
+
/** Its scope group and table prefix, e.g. "time_off". */
|
|
38
|
+
snake: string;
|
|
39
|
+
/** What people read, e.g. "Time off". */
|
|
40
|
+
label: string;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export function moduleNames(name: string): ModuleNames {
|
|
44
|
+
if (!MODULE_NAME.test(name)) {
|
|
45
|
+
throw new Error(
|
|
46
|
+
`"${name}" is not a module name: use lowercase letters, words joined by dashes (e.g. time-off)`
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const words = name.split("-");
|
|
51
|
+
const [first = "", ...rest] = words;
|
|
52
|
+
|
|
53
|
+
const capital = (word: string) =>
|
|
54
|
+
word.charAt(0).toUpperCase() + word.slice(1);
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
name,
|
|
58
|
+
pascal: words.map(capital).join(""),
|
|
59
|
+
camel: first + rest.map(capital).join(""),
|
|
60
|
+
constant: words.join("_").toUpperCase(),
|
|
61
|
+
snake: words.join("_"),
|
|
62
|
+
label: capital(words.join(" ")),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The module's files, by path relative to the project. */
|
|
67
|
+
export function moduleFiles(names: ModuleNames) {
|
|
68
|
+
const { name, pascal, camel, constant, snake, label } = names;
|
|
69
|
+
const dir = `modules/${name}`;
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
[`${dir}/shared/module.ts`]: `import type { WebhookEventDeclaration } from "@open-workspace/core/shared";
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The module's id, repeated by its three halves. Its migrations are
|
|
76
|
+
* recorded as modules/${name}/<file>, so it never changes.
|
|
77
|
+
*/
|
|
78
|
+
export const ${constant}_MODULE = "${name}";
|
|
79
|
+
|
|
80
|
+
/** Where its app lives in the browser. */
|
|
81
|
+
export const ${constant}_PATH = "/${name}";
|
|
82
|
+
|
|
83
|
+
/** The OAuth scopes an app (an MCP client) needs for its routes and tools. */
|
|
84
|
+
export const ${constant}_READ_SCOPE = "${snake}.read";
|
|
85
|
+
export const ${constant}_WRITE_SCOPE = "${snake}.write";
|
|
86
|
+
|
|
87
|
+
/** The scopes as the consent screen lists them. An app that asks for nothing may read. */
|
|
88
|
+
export const ${camel}OauthScopes = {
|
|
89
|
+
group: "${snake}",
|
|
90
|
+
title: "${label}",
|
|
91
|
+
scopes: [
|
|
92
|
+
{ id: ${constant}_READ_SCOPE, label: "Read your ${label.toLowerCase()} items", byDefault: true },
|
|
93
|
+
{ id: ${constant}_WRITE_SCOPE, label: "Add ${label.toLowerCase()} items as you" },
|
|
94
|
+
],
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/** The webhook events the module sends, offered in the webhook editor. */
|
|
98
|
+
export const ${camel}WebhookEvents: Record<string, WebhookEventDeclaration> = {
|
|
99
|
+
"${snake}.item_created": {
|
|
100
|
+
label: "${label} item created",
|
|
101
|
+
description: "When someone adds an item, with its title and author.",
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
`,
|
|
105
|
+
[`${dir}/worker/schema.ts`]: `import { sql } from "drizzle-orm";
|
|
106
|
+
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
|
|
107
|
+
import { users } from "@open-workspace/core/module";
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The module's D1 tables, next to core's in the workspace's database. After
|
|
111
|
+
* a change, \`bun run db:generate:${name}\` writes the migration.
|
|
112
|
+
*/
|
|
113
|
+
export const ${camel}Items = sqliteTable("${snake}_items", {
|
|
114
|
+
id: text("id").primaryKey(),
|
|
115
|
+
authorUserId: text("author_user_id")
|
|
116
|
+
.notNull()
|
|
117
|
+
.references(() => users.id, { onDelete: "cascade" }),
|
|
118
|
+
title: text("title").notNull(),
|
|
119
|
+
createdAt: integer("created_at", { mode: "timestamp_ms" })
|
|
120
|
+
.notNull()
|
|
121
|
+
.default(sql\`(unixepoch() * 1000)\`),
|
|
122
|
+
});
|
|
123
|
+
`,
|
|
124
|
+
[`${dir}/worker/routes.ts`]: `import { desc, eq } from "drizzle-orm";
|
|
125
|
+
import { Hono } from "hono";
|
|
126
|
+
import { z } from "zod";
|
|
127
|
+
import {
|
|
128
|
+
type AppEnv,
|
|
129
|
+
createDb,
|
|
130
|
+
createId,
|
|
131
|
+
deferWebhookTask,
|
|
132
|
+
oauthScope,
|
|
133
|
+
queueWebhookEvent,
|
|
134
|
+
requireAuth,
|
|
135
|
+
zValidator,
|
|
136
|
+
} from "@open-workspace/core/module";
|
|
137
|
+
import { ${constant}_READ_SCOPE, ${constant}_WRITE_SCOPE } from "../shared/module";
|
|
138
|
+
import { ${camel}Items } from "./schema";
|
|
139
|
+
|
|
140
|
+
/** The signed-in person's items, under /api/${name}; each route names the scope a token needs. */
|
|
141
|
+
const items = new Hono<AppEnv>()
|
|
142
|
+
.use("*", requireAuth)
|
|
143
|
+
.get("/", oauthScope(${constant}_READ_SCOPE), async (c) => {
|
|
144
|
+
const rows = await createDb(c.env.DB).select().from(${camel}Items)
|
|
145
|
+
.where(eq(${camel}Items.authorUserId, c.get("user").id))
|
|
146
|
+
.orderBy(desc(${camel}Items.createdAt));
|
|
147
|
+
return c.json({ ok: true as const, items: rows.map((row) => ({ id: row.id, title: row.title })) });
|
|
148
|
+
})
|
|
149
|
+
.post("/", oauthScope(${constant}_WRITE_SCOPE), zValidator("json", z.object({ title: z.string().trim().min(1).max(200) })), async (c) => {
|
|
150
|
+
const user = c.get("user");
|
|
151
|
+
const item = { id: createId("${snake.slice(0, 8)}"), title: c.req.valid("json").title };
|
|
152
|
+
await createDb(c.env.DB).insert(${camel}Items).values({ ...item, authorUserId: user.id });
|
|
153
|
+
deferWebhookTask((task) => c.executionCtx.waitUntil(task), (eventId) => queueWebhookEvent(c.env, {
|
|
154
|
+
eventId,
|
|
155
|
+
eventType: "${snake}.item_created",
|
|
156
|
+
occurredAt: Date.now(),
|
|
157
|
+
source: { kind: "data", data: { item, author: { id: user.id, name: user.name } } },
|
|
158
|
+
}));
|
|
159
|
+
return c.json({ ok: true as const, item }, 201);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
/** Routes under /api: the browser's, and the MCP tools' backend. */
|
|
163
|
+
export const ${camel}AccountApi = new Hono<AppEnv>().route("/${name}", items);
|
|
164
|
+
|
|
165
|
+
/** The routes as the browser's typed client sees them. */
|
|
166
|
+
export const ${camel}BrowserApi = new Hono<AppEnv>().route("/api", ${camel}AccountApi);
|
|
167
|
+
export type ${pascal}AppType = typeof ${camel}BrowserApi;
|
|
168
|
+
`,
|
|
169
|
+
[`${dir}/worker/mcp.ts`]: `import { type McpToolRegistrar, runTool } from "@open-workspace/core/module";
|
|
170
|
+
import { ${constant}_READ_SCOPE } from "../shared/module";
|
|
171
|
+
|
|
172
|
+
/** MCP tools; each registered through \`tool(scope)\` and backed by the module's own routes. */
|
|
173
|
+
export const register${pascal}Tools: McpToolRegistrar = (tool, { api }) => {
|
|
174
|
+
tool(${constant}_READ_SCOPE)(
|
|
175
|
+
"list_${snake}_items",
|
|
176
|
+
{
|
|
177
|
+
title: "List ${label.toLowerCase()} items",
|
|
178
|
+
description: "List the signed-in person's ${label.toLowerCase()} items, newest first.",
|
|
179
|
+
annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false },
|
|
180
|
+
},
|
|
181
|
+
() => runTool(() => api.json("/${name}")),
|
|
182
|
+
);
|
|
183
|
+
};
|
|
184
|
+
`,
|
|
185
|
+
[`${dir}/worker/module.ts`]: `import type { WorkspaceModule } from "@open-workspace/core/module";
|
|
186
|
+
import { ${constant}_MODULE, ${camel}OauthScopes, ${camel}WebhookEvents } from "../shared/module";
|
|
187
|
+
import { ${camel}AccountApi } from "./routes";
|
|
188
|
+
|
|
189
|
+
/** The module's Worker half; the generated Worker entry calls this factory (../index.ts). */
|
|
190
|
+
export function ${camel}(): WorkspaceModule {
|
|
191
|
+
return {
|
|
192
|
+
name: ${constant}_MODULE,
|
|
193
|
+
accountApi: ${camel}AccountApi,
|
|
194
|
+
oauthScopes: ${camel}OauthScopes,
|
|
195
|
+
webhookEvents: ${camel}WebhookEvents,
|
|
196
|
+
mcpTools: () => import("./mcp").then((tools) => tools.register${pascal}Tools),
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
`,
|
|
200
|
+
[`${dir}/index.ts`]: `// The module's server entry: its default export is the factory of its WorkspaceModule.
|
|
201
|
+
export { ${camel} as default } from "./worker/module";
|
|
202
|
+
`,
|
|
203
|
+
[`${dir}/alchemy.ts`]: `import { join } from "node:path";
|
|
204
|
+
import type { WorkspaceModuleInfra } from "@open-workspace/core/alchemy";
|
|
205
|
+
import { ${constant}_MODULE } from "./shared/module";
|
|
206
|
+
|
|
207
|
+
/** The module's Alchemy half: \`OpenWorkspace({ modules: [${pascal}()] })\`. */
|
|
208
|
+
export function ${pascal}(): WorkspaceModuleInfra {
|
|
209
|
+
return {
|
|
210
|
+
name: ${constant}_MODULE,
|
|
211
|
+
server: { entry: join(import.meta.dirname, "index.ts") },
|
|
212
|
+
migrations: join(import.meta.dirname, "migrations"),
|
|
213
|
+
client: { entry: join(import.meta.dirname, "src", "client.tsx") },
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
`,
|
|
217
|
+
[`${dir}/src/client.tsx`]: `import { Boxes } from "lucide-react";
|
|
218
|
+
import { lazy } from "react";
|
|
219
|
+
import type { ClientModule } from "@open-workspace/core/ui";
|
|
220
|
+
import { ${constant}_MODULE, ${constant}_PATH, ${camel}WebhookEvents } from "../shared/module";
|
|
221
|
+
|
|
222
|
+
/** The module's part of the browser app; keep it light and lazy-load screens. */
|
|
223
|
+
const ${camel}Client: ClientModule = {
|
|
224
|
+
name: ${constant}_MODULE,
|
|
225
|
+
app: { label: "${label}", Icon: Boxes, path: ${constant}_PATH },
|
|
226
|
+
routes: [{
|
|
227
|
+
path: ${constant}_PATH,
|
|
228
|
+
Component: lazy(() => import("./home").then((module) => ({ default: module.${pascal}Home }))),
|
|
229
|
+
}],
|
|
230
|
+
webhookEvents: ${camel}WebhookEvents,
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
export default ${camel}Client;
|
|
234
|
+
`,
|
|
235
|
+
[`${dir}/src/home.tsx`]: `import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
236
|
+
import { hc } from "hono/client";
|
|
237
|
+
import { useState, type FormEvent } from "react";
|
|
238
|
+
import {
|
|
239
|
+
AppSwitcher,
|
|
240
|
+
Button,
|
|
241
|
+
Input,
|
|
242
|
+
apiFetch,
|
|
243
|
+
responseJson,
|
|
244
|
+
useWorkspaceBrand,
|
|
245
|
+
WorkspaceLogo,
|
|
246
|
+
} from "@open-workspace/core/ui";
|
|
247
|
+
import { ${constant}_MODULE } from "../shared/module";
|
|
248
|
+
import type { ${pascal}AppType } from "../worker/routes";
|
|
249
|
+
|
|
250
|
+
const api = hc<${pascal}AppType>(window.location.origin, { fetch: apiFetch });
|
|
251
|
+
const itemsKey = ["${name}", "items"] as const;
|
|
252
|
+
|
|
253
|
+
/** The app's screen: its header with the app switcher, a form, and the list. */
|
|
254
|
+
export function ${pascal}Home() {
|
|
255
|
+
const brand = useWorkspaceBrand();
|
|
256
|
+
const client = useQueryClient();
|
|
257
|
+
const [title, setTitle] = useState("");
|
|
258
|
+
const items = useQuery({ queryKey: itemsKey, queryFn: async () => responseJson(await api.api["${name}"].$get()) });
|
|
259
|
+
const add = useMutation({
|
|
260
|
+
mutationFn: async () => responseJson(await api.api["${name}"].$post({ json: { title } })),
|
|
261
|
+
onSuccess: async () => {
|
|
262
|
+
setTitle("");
|
|
263
|
+
await client.invalidateQueries({ queryKey: itemsKey });
|
|
264
|
+
},
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
function submit(event: FormEvent) {
|
|
268
|
+
event.preventDefault();
|
|
269
|
+
if (title.trim()) add.mutate();
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return (
|
|
273
|
+
<div className="min-h-dvh bg-background">
|
|
274
|
+
<header className="flex h-12 items-center gap-2.5 px-3 lg:px-4">
|
|
275
|
+
<WorkspaceLogo src={brand.logoUrl} flat />
|
|
276
|
+
<AppSwitcher current={${constant}_MODULE} />
|
|
277
|
+
</header>
|
|
278
|
+
<main className="mx-auto flex max-w-2xl flex-col gap-6 px-4 pt-8">
|
|
279
|
+
<form onSubmit={submit} className="flex gap-2">
|
|
280
|
+
<Input aria-label="Title" value={title} onChange={(event) => setTitle(event.target.value)} placeholder="Add an item" />
|
|
281
|
+
<Button type="submit" disabled={!title.trim() || add.isPending}>Add</Button>
|
|
282
|
+
</form>
|
|
283
|
+
<ul className="divide-y divide-border/70 overflow-hidden rounded-xl border border-border bg-surface">
|
|
284
|
+
{items.data?.items.map((item) => <li key={item.id} className="px-4 py-3 text-sm">{item.title}</li>)}
|
|
285
|
+
{items.data?.items.length === 0 ? <li className="px-4 py-8 text-center text-sm text-muted-foreground">Nothing yet</li> : null}
|
|
286
|
+
</ul>
|
|
287
|
+
</main>
|
|
288
|
+
</div>
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
`,
|
|
292
|
+
[`${dir}/drizzle.config.ts`]: `import { defineConfig } from "drizzle-kit";
|
|
293
|
+
|
|
294
|
+
// Migrations ship with the module and run on deploy, recorded as
|
|
295
|
+
// modules/${name}/<file>: never rename the module or a migration.
|
|
296
|
+
export default defineConfig({
|
|
297
|
+
schema: "./worker/schema.ts",
|
|
298
|
+
out: "./migrations",
|
|
299
|
+
dialect: "sqlite",
|
|
300
|
+
});
|
|
301
|
+
`,
|
|
302
|
+
[`${dir}/tsconfig.json`]: `${JSON.stringify(
|
|
303
|
+
{
|
|
304
|
+
compilerOptions: {
|
|
305
|
+
target: "ES2023",
|
|
306
|
+
lib: ["ES2023", "DOM", "DOM.Iterable"],
|
|
307
|
+
module: "ESNext",
|
|
308
|
+
moduleResolution: "Bundler",
|
|
309
|
+
jsx: "react-jsx",
|
|
310
|
+
strict: true,
|
|
311
|
+
skipLibCheck: true,
|
|
312
|
+
noEmit: true,
|
|
313
|
+
isolatedModules: true,
|
|
314
|
+
resolveJsonModule: true,
|
|
315
|
+
allowImportingTsExtensions: true,
|
|
316
|
+
types: ["node"],
|
|
317
|
+
},
|
|
318
|
+
include: ["."],
|
|
319
|
+
},
|
|
320
|
+
null,
|
|
321
|
+
2
|
|
322
|
+
)}\n`,
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** The lines that put the module in `alchemy.run.ts`, for people to add by hand. */
|
|
327
|
+
export function wiringLines(names: ModuleNames) {
|
|
328
|
+
return {
|
|
329
|
+
importLine: `import { ${names.pascal} } from "./modules/${names.name}/alchemy.ts";`,
|
|
330
|
+
entry: `${names.pascal}()`,
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* `alchemy.run.ts` with the module imported and listed in `modules`, or null
|
|
336
|
+
* when that cannot be done safely: no single `modules: [...]` on one line.
|
|
337
|
+
* An already wired file comes back unchanged.
|
|
338
|
+
*/
|
|
339
|
+
export function wireAlchemyRun(
|
|
340
|
+
source: string,
|
|
341
|
+
names: ModuleNames
|
|
342
|
+
): string | null {
|
|
343
|
+
const { importLine, entry } = wiringLines(names);
|
|
344
|
+
|
|
345
|
+
if (source.includes(importLine)) return source;
|
|
346
|
+
|
|
347
|
+
const lists = [
|
|
348
|
+
...source.matchAll(/^(\s*modules:\s*\[)([^\]\n]*)(\],?)\s*$/gmu),
|
|
349
|
+
];
|
|
350
|
+
|
|
351
|
+
const imports = [...source.matchAll(/^import .*;\s*$/gmu)];
|
|
352
|
+
const [list] = lists;
|
|
353
|
+
const lastImport = imports.at(-1);
|
|
354
|
+
|
|
355
|
+
if (lists.length !== 1 || !list || !lastImport) return null;
|
|
356
|
+
const [line, opening, listItems, closing] = list;
|
|
357
|
+
const items = listItems.trim();
|
|
358
|
+
const listed = `${opening}${items ? `${items.replace(/,\s*$/u, "")}, ` : ""}${entry}${closing}`;
|
|
359
|
+
|
|
360
|
+
let wired =
|
|
361
|
+
source.slice(0, list.index) +
|
|
362
|
+
listed +
|
|
363
|
+
source.slice(list.index + line.replace(/\s*$/u, "").length);
|
|
364
|
+
|
|
365
|
+
const end = lastImport.index + lastImport[0].trimEnd().length;
|
|
366
|
+
wired = `${wired.slice(0, end)}\n${importLine}${wired.slice(end)}`;
|
|
367
|
+
|
|
368
|
+
return wired;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const stringRecord = z.record(z.string(), z.string());
|
|
372
|
+
|
|
373
|
+
/** A package.json, the fields edited here; every other field is kept as it is. */
|
|
374
|
+
const packageManifestSchema = z.looseObject({
|
|
375
|
+
dependencies: stringRecord.optional(),
|
|
376
|
+
devDependencies: stringRecord.optional(),
|
|
377
|
+
scripts: stringRecord.optional(),
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* The project's package.json with what the module needs added: its
|
|
382
|
+
* dependencies (versions already there are kept) and its migration script.
|
|
383
|
+
*/
|
|
384
|
+
export function withModuleDependencies(
|
|
385
|
+
packageJson: string,
|
|
386
|
+
names: ModuleNames
|
|
387
|
+
) {
|
|
388
|
+
const manifest = packageManifestSchema.parse(JSON.parse(packageJson));
|
|
389
|
+
|
|
390
|
+
manifest.dependencies = { ...moduleDependencies, ...manifest.dependencies };
|
|
391
|
+
manifest.devDependencies = {
|
|
392
|
+
...moduleDevDependencies,
|
|
393
|
+
...manifest.devDependencies,
|
|
394
|
+
};
|
|
395
|
+
manifest.scripts = {
|
|
396
|
+
...manifest.scripts,
|
|
397
|
+
[`db:generate:${names.name}`]: `cd modules/${names.name} && drizzle-kit generate`,
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
const sort = (record: Record<string, string>) =>
|
|
401
|
+
Object.fromEntries(
|
|
402
|
+
Object.entries(record).sort(([a], [b]) => a.localeCompare(b))
|
|
403
|
+
);
|
|
404
|
+
|
|
405
|
+
manifest.dependencies = sort(manifest.dependencies);
|
|
406
|
+
manifest.devDependencies = sort(manifest.devDependencies);
|
|
407
|
+
|
|
408
|
+
return `${JSON.stringify(manifest, null, 2)}\n`;
|
|
409
|
+
}
|
package/module.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join, resolve } from "node:path";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
moduleFiles,
|
|
6
|
+
moduleNames,
|
|
7
|
+
wireAlchemyRun,
|
|
8
|
+
wiringLines,
|
|
9
|
+
withModuleDependencies,
|
|
10
|
+
} from "./module-templates";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* `bunx @open-workspace/create module <name>`, run in a deployment project:
|
|
14
|
+
* writes `modules/<name>/` and wires it into `alchemy.run.ts`. Returns the
|
|
15
|
+
* exit code.
|
|
16
|
+
*/
|
|
17
|
+
export function main(
|
|
18
|
+
args: string[],
|
|
19
|
+
cwd = process.cwd(),
|
|
20
|
+
log = console.log
|
|
21
|
+
): number {
|
|
22
|
+
const [name] = args;
|
|
23
|
+
|
|
24
|
+
if (!name || args.length > 1) {
|
|
25
|
+
log(
|
|
26
|
+
"Usage: bunx @open-workspace/create module <name> (e.g. time-off), in your deployment's directory"
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
return 1;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let names;
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
names = moduleNames(name);
|
|
36
|
+
} catch (error) {
|
|
37
|
+
log(error instanceof Error ? error.message : String(error));
|
|
38
|
+
|
|
39
|
+
return 1;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const project = resolve(cwd);
|
|
43
|
+
const dir = join(project, "modules", name);
|
|
44
|
+
|
|
45
|
+
if (existsSync(dir)) {
|
|
46
|
+
log(`${dir} already exists; nothing was written.`);
|
|
47
|
+
|
|
48
|
+
return 1;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
for (const [path, content] of Object.entries(moduleFiles(names))) {
|
|
52
|
+
mkdirSync(dirname(join(project, path)), { recursive: true });
|
|
53
|
+
writeFileSync(join(project, path), content);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
mkdirSync(join(dir, "migrations"), { recursive: true });
|
|
57
|
+
log(`Wrote modules/${name}/`);
|
|
58
|
+
|
|
59
|
+
const manifest = join(project, "package.json");
|
|
60
|
+
|
|
61
|
+
if (existsSync(manifest)) {
|
|
62
|
+
writeFileSync(
|
|
63
|
+
manifest,
|
|
64
|
+
withModuleDependencies(readFileSync(manifest, "utf-8"), names)
|
|
65
|
+
);
|
|
66
|
+
log(
|
|
67
|
+
`Added its dependencies and the db:generate:${name} script to package.json`
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const alchemyRun = join(project, "alchemy.run.ts");
|
|
72
|
+
|
|
73
|
+
const wired = existsSync(alchemyRun)
|
|
74
|
+
? wireAlchemyRun(readFileSync(alchemyRun, "utf-8"), names)
|
|
75
|
+
: null;
|
|
76
|
+
|
|
77
|
+
if (wired === null) {
|
|
78
|
+
const { importLine, entry } = wiringLines(names);
|
|
79
|
+
log(
|
|
80
|
+
[
|
|
81
|
+
"",
|
|
82
|
+
"alchemy.run.ts was left as it is. Add the module to it:",
|
|
83
|
+
` ${importLine}`,
|
|
84
|
+
` OpenWorkspace({ …, modules: [/* … */, ${entry}] })`,
|
|
85
|
+
].join("\n")
|
|
86
|
+
);
|
|
87
|
+
} else {
|
|
88
|
+
writeFileSync(alchemyRun, wired);
|
|
89
|
+
log(`Listed ${names.pascal}() in alchemy.run.ts`);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
log(
|
|
93
|
+
[
|
|
94
|
+
"",
|
|
95
|
+
"Next:",
|
|
96
|
+
" bun install",
|
|
97
|
+
` bun run db:generate:${name} # its first migration, from modules/${name}/worker/schema.ts`,
|
|
98
|
+
" bun run deploy",
|
|
99
|
+
"",
|
|
100
|
+
"See docs/modules.md in the OpenWorkspace repository for the contract and the public API.",
|
|
101
|
+
].join("\n")
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
return 0;
|
|
105
|
+
}
|
package/package.json
CHANGED
|
@@ -1,33 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-workspace/create",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Create an OpenWorkspace deployment
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Create an OpenWorkspace deployment (`bun create @open-workspace`), and import people and mail into one.",
|
|
5
|
+
"homepage": "https://openworkspace.dev",
|
|
5
6
|
"license": "Apache-2.0",
|
|
6
|
-
"type": "module",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "git+https://github.com/iivankin/openworkspace.git",
|
|
10
10
|
"directory": "packages/create"
|
|
11
11
|
},
|
|
12
|
-
"homepage": "https://openworkspace.dev",
|
|
13
12
|
"bin": {
|
|
14
13
|
"create-open-workspace": "./index.ts"
|
|
15
14
|
},
|
|
16
15
|
"files": [
|
|
17
16
|
"index.ts",
|
|
17
|
+
"create.ts",
|
|
18
18
|
"templates.ts",
|
|
19
|
-
"
|
|
19
|
+
"module.ts",
|
|
20
|
+
"setup",
|
|
21
|
+
"module-templates.ts",
|
|
22
|
+
"import",
|
|
20
23
|
"README.md",
|
|
21
24
|
"LICENSE"
|
|
22
25
|
],
|
|
26
|
+
"type": "module",
|
|
23
27
|
"scripts": {
|
|
28
|
+
"test": "bun run test:unit",
|
|
29
|
+
"test:unit": "bun test ./test/unit",
|
|
24
30
|
"typecheck": "tsc -p tsconfig.json"
|
|
25
31
|
},
|
|
26
32
|
"dependencies": {
|
|
27
|
-
"@clack/prompts": "1.
|
|
33
|
+
"@clack/prompts": "1.9.0",
|
|
34
|
+
"imapflow": "2.11.0",
|
|
35
|
+
"zod": "4.6.7"
|
|
28
36
|
},
|
|
29
37
|
"devDependencies": {
|
|
38
|
+
"@alchemy.run/cloudflare": "2.0.0-beta.85",
|
|
30
39
|
"@types/bun": "1.4.4",
|
|
40
|
+
"alchemy": "2.0.0-beta.85",
|
|
41
|
+
"effect": "4.1.3",
|
|
42
|
+
"hoodiecrow-imap": "3.3.1",
|
|
31
43
|
"typescript": "7.0.2"
|
|
32
44
|
}
|
|
33
45
|
}
|