create-gtkx 2.0.0-beta.12 → 2.0.0-beta.13

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.
Files changed (48) hide show
  1. package/dependency-ranges.json +7 -0
  2. package/dist/build-allowance.d.ts +43 -2
  3. package/dist/build-allowance.d.ts.map +1 -1
  4. package/dist/build-allowance.js +360 -10
  5. package/dist/build-allowance.js.map +1 -1
  6. package/dist/index.d.ts +1 -0
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +1 -0
  9. package/dist/index.js.map +1 -1
  10. package/dist/package-managers.d.ts +6 -0
  11. package/dist/package-managers.d.ts.map +1 -1
  12. package/dist/package-managers.js +6 -0
  13. package/dist/package-managers.js.map +1 -1
  14. package/dist/project-name.d.ts +4 -2
  15. package/dist/project-name.d.ts.map +1 -1
  16. package/dist/project-name.js +12 -2
  17. package/dist/project-name.js.map +1 -1
  18. package/dist/scaffolder.d.ts.map +1 -1
  19. package/dist/scaffolder.js +171 -64
  20. package/dist/scaffolder.js.map +1 -1
  21. package/dist/templates/.claude/settings.json.ejs +4 -3
  22. package/dist/templates/.gitignore.ejs +3 -0
  23. package/dist/templates/.mcp.json.ejs +1 -1
  24. package/dist/templates/src/app.tsx.ejs +1 -1
  25. package/dist/templates/src/gtkx-env.d.ts.ejs +0 -1
  26. package/dist/templates/tsconfig.json.ejs +3 -2
  27. package/dist/templates.d.ts +3 -0
  28. package/dist/templates.d.ts.map +1 -1
  29. package/dist/templates.js +10 -6
  30. package/dist/templates.js.map +1 -1
  31. package/dist/workspace-mcp.d.ts +33 -0
  32. package/dist/workspace-mcp.d.ts.map +1 -0
  33. package/dist/workspace-mcp.js +75 -0
  34. package/dist/workspace-mcp.js.map +1 -0
  35. package/package.json +10 -6
  36. package/src/build-allowance.ts +488 -10
  37. package/src/index.ts +1 -0
  38. package/src/package-managers.ts +6 -0
  39. package/src/project-name.ts +19 -2
  40. package/src/scaffolder.ts +250 -70
  41. package/src/templates/.claude/settings.json.ejs +4 -3
  42. package/src/templates/.gitignore.ejs +3 -0
  43. package/src/templates/.mcp.json.ejs +1 -1
  44. package/src/templates/src/app.tsx.ejs +1 -1
  45. package/src/templates/src/gtkx-env.d.ts.ejs +0 -1
  46. package/src/templates/tsconfig.json.ejs +3 -2
  47. package/src/templates.ts +17 -8
  48. package/src/workspace-mcp.ts +111 -0
package/src/templates.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { sortStrings, sourceStringLiteral, toPosixPath } from "@gtkx/utils";
1
+ import { sortStrings, sourceStringLiteral } from "@gtkx/utils";
2
+ import type { PackageManager } from "./package-managers.js";
2
3
  import ejs from "ejs";
3
4
  import { readdirSync } from "node:fs";
4
5
  import { join } from "node:path";
@@ -12,18 +13,26 @@ type TemplateContext = {
12
13
  importExtension: string;
13
14
  developerName: string;
14
15
  developerEmail: string | null;
16
+ packageManager: PackageManager;
17
+ scriptCommands: string[];
15
18
  };
16
19
 
17
20
  const TEMPLATES_DIR = join(import.meta.dirname, "templates");
18
21
 
22
+ // Yarn PnP reports relative `parentPath`s for recursive listings: https://github.com/yarnpkg/berry/issues/7315
23
+ const listFiles = (directory: string, prefix: string): string[] =>
24
+ readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
25
+ const relative = `${prefix}${entry.name}`;
26
+
27
+ if (entry.isDirectory()) {
28
+ return listFiles(join(directory, entry.name), `${relative}/`);
29
+ }
30
+
31
+ return entry.isFile() ? [relative] : [];
32
+ });
33
+
19
34
  const listTemplates = (): string[] =>
20
- sortStrings(
21
- readdirSync(TEMPLATES_DIR, { recursive: true, withFileTypes: true })
22
- .filter((entry) => entry.isFile())
23
- .map((entry) => join(entry.parentPath, entry.name))
24
- .map((absolute) => toPosixPath(absolute.slice(TEMPLATES_DIR.length + 1)))
25
- .map((relative) => relative.replace(/\.ejs$/, "")),
26
- );
35
+ sortStrings(listFiles(TEMPLATES_DIR, "").map((relative) => relative.replace(/\.ejs$/, "")));
27
36
 
28
37
  const renderFile = async (templateName: string, context: TemplateContext): Promise<string> =>
29
38
  ejs.renderFile(join(TEMPLATES_DIR, `${templateName}.ejs`), { ...context, sourceStringLiteral });
@@ -0,0 +1,111 @@
1
+ import { errorMessage, indentOf } from "@gtkx/utils";
2
+ import { readFileSync, writeFileSync } from "node:fs";
3
+ import { join } from "node:path";
4
+ import { isDeepStrictEqual } from "node:util";
5
+ import { isClassicYarn, type Workspace } from "./build-allowance.js";
6
+ import type { PackageManager } from "./package-managers.js";
7
+
8
+ type McpServer = { command: string; args: string[] };
9
+
10
+ type McpConfig = { config: Record<string, unknown>; indent: string };
11
+
12
+ type WorkspaceMcpResult =
13
+ | { kind: "added"; path: string; key: string }
14
+ | { kind: "present"; path: string; key: string }
15
+ | { kind: "shared"; path: string; key: string }
16
+ | { kind: "unreadable"; path: string; key: string; server: McpServer; reason: string };
17
+
18
+ const MCP_CONFIG_FILE = ".mcp.json";
19
+ const SERVERS_KEY = "mcpServers";
20
+ const SERVER_NAME = "gtkx";
21
+ const SERVER_COMMAND = ["gtkx", "mcp"];
22
+ const JSON_INDENT = " ";
23
+
24
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
25
+ typeof value === "object" && value !== null && !Array.isArray(value);
26
+
27
+ const yarnServer = (workspace: Workspace, name: string): McpServer =>
28
+ isClassicYarn(workspace.root)
29
+ ? { command: "yarn", args: ["--silent", "--cwd", workspace.member, ...SERVER_COMMAND] }
30
+ : { command: "yarn", args: ["workspace", name, "exec", ...SERVER_COMMAND] };
31
+
32
+ const WORKSPACE_SERVERS: Record<PackageManager, (workspace: Workspace, name: string) => McpServer> = {
33
+ pnpm: ({ member }) => ({ command: "pnpm", args: ["--dir", member, "exec", ...SERVER_COMMAND] }),
34
+ npm: ({ member }) => ({ command: "npm", args: ["exec", "--no", "--workspace", member, "--", ...SERVER_COMMAND] }),
35
+ yarn: yarnServer,
36
+ };
37
+
38
+ const readConfig = (path: string): McpConfig => {
39
+ let source: string;
40
+
41
+ try {
42
+ source = readFileSync(path, "utf8");
43
+ } catch (error) {
44
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") {
45
+ return { config: {}, indent: JSON_INDENT };
46
+ }
47
+
48
+ throw error;
49
+ }
50
+
51
+ const config: unknown = JSON.parse(source);
52
+
53
+ if (!isRecord(config) || (Object.hasOwn(config, SERVERS_KEY) && !isRecord(config[SERVERS_KEY]))) {
54
+ throw new Error(`its contents are not an object with an object "${SERVERS_KEY}"`);
55
+ }
56
+
57
+ return { config, indent: indentOf(source, JSON_INDENT) };
58
+ };
59
+
60
+ const isGtkxServer = (key: string, server: unknown): boolean => {
61
+ if (key === SERVER_NAME) {
62
+ return true;
63
+ }
64
+
65
+ const args = isRecord(server) && Array.isArray(server.args) ? server.args : [];
66
+
67
+ return isDeepStrictEqual(args.slice(-SERVER_COMMAND.length), SERVER_COMMAND);
68
+ };
69
+
70
+ /**
71
+ * Adds a server that starts the app's own `gtkx mcp` through the workspace's package manager to the
72
+ * workspace root's `.mcp.json`, where AI clients opened at the repository root look for it. Other servers and
73
+ * the file's indentation stay. When the file already has a GTKX server, such as an earlier app's, no second one
74
+ * is added: the GTKX servers of a session share one socket, and that server drives every running app.
75
+ */
76
+ const addWorkspaceMcpServer = (workspace: Workspace, name: string): WorkspaceMcpResult => {
77
+ const path = join(workspace.root, MCP_CONFIG_FILE);
78
+ const server = WORKSPACE_SERVERS[workspace.packageManager](workspace, name);
79
+ let document: McpConfig;
80
+
81
+ try {
82
+ document = readConfig(path);
83
+ } catch (error) {
84
+ return { kind: "unreadable", path, key: SERVER_NAME, server, reason: errorMessage(error) };
85
+ }
86
+
87
+ const { config, indent } = document;
88
+
89
+ const servers = isRecord(config[SERVERS_KEY]) ? config[SERVERS_KEY] : {};
90
+ const keys = Object.keys(servers);
91
+ const present = keys.find((key) => isDeepStrictEqual(servers[key], server));
92
+
93
+ if (present !== undefined) {
94
+ return { kind: "present", path, key: present };
95
+ }
96
+
97
+ const shared = keys.find((key) => isGtkxServer(key, servers[key]));
98
+
99
+ if (shared !== undefined) {
100
+ return { kind: "shared", path, key: shared };
101
+ }
102
+
103
+ writeFileSync(
104
+ path,
105
+ `${JSON.stringify({ ...config, [SERVERS_KEY]: { ...servers, [SERVER_NAME]: server } }, null, indent)}\n`,
106
+ );
107
+
108
+ return { kind: "added", path, key: SERVER_NAME };
109
+ };
110
+
111
+ export { addWorkspaceMcpServer };