create-gtkx 2.0.0-beta.11 → 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.
- package/README.md +5 -5
- package/dependency-ranges.json +7 -0
- package/dist/build-allowance.d.ts +43 -2
- package/dist/build-allowance.d.ts.map +1 -1
- package/dist/build-allowance.js +360 -10
- package/dist/build-allowance.js.map +1 -1
- package/dist/command.d.ts.map +1 -1
- package/dist/command.js +7 -2
- package/dist/command.js.map +1 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/manifest.d.ts.map +1 -1
- package/dist/package-managers.d.ts +6 -0
- package/dist/package-managers.d.ts.map +1 -1
- package/dist/package-managers.js +6 -0
- package/dist/package-managers.js.map +1 -1
- package/dist/project-name.d.ts +4 -2
- package/dist/project-name.d.ts.map +1 -1
- package/dist/project-name.js +12 -2
- package/dist/project-name.js.map +1 -1
- package/dist/scaffolder.d.ts.map +1 -1
- package/dist/scaffolder.js +181 -72
- package/dist/scaffolder.js.map +1 -1
- package/dist/templates/.claude/settings.json.ejs +4 -3
- package/dist/templates/.gitignore.ejs +3 -0
- package/dist/templates/.mcp.json.ejs +1 -1
- package/dist/templates/src/app.tsx.ejs +1 -1
- package/dist/templates/src/gtkx-env.d.ts.ejs +0 -1
- package/dist/templates/tsconfig.json.ejs +3 -2
- package/dist/templates.d.ts +3 -0
- package/dist/templates.d.ts.map +1 -1
- package/dist/templates.js +10 -6
- package/dist/templates.js.map +1 -1
- package/dist/workspace-mcp.d.ts +33 -0
- package/dist/workspace-mcp.d.ts.map +1 -0
- package/dist/workspace-mcp.js +75 -0
- package/dist/workspace-mcp.js.map +1 -0
- package/package.json +16 -47
- package/src/build-allowance.ts +488 -10
- package/src/command.ts +9 -4
- package/src/index.ts +1 -0
- package/src/package-managers.ts +6 -0
- package/src/project-name.ts +19 -2
- package/src/scaffolder.ts +278 -94
- package/src/templates/.claude/settings.json.ejs +4 -3
- package/src/templates/.gitignore.ejs +3 -0
- package/src/templates/.mcp.json.ejs +1 -1
- package/src/templates/src/app.tsx.ejs +1 -1
- package/src/templates/src/gtkx-env.d.ts.ejs +0 -1
- package/src/templates/tsconfig.json.ejs +3 -2
- package/src/templates.ts +17 -8
- package/src/workspace-mcp.ts +111 -0
package/src/scaffolder.ts
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import * as p from "@clack/prompts";
|
|
2
2
|
import { APPLICATION_ID_MAX_LENGTH, isValidApplicationId } from "@gtkx/config/internal";
|
|
3
|
-
import { errorMessage, tryResolveExecutable, upperFirst } from "@gtkx/utils";
|
|
3
|
+
import { errorMessage, HEADLESS_DISPLAY_TOOLS, missingToolLines, tryResolveExecutable, upperFirst } from "@gtkx/utils";
|
|
4
4
|
import { execFileSync } from "node:child_process";
|
|
5
|
-
import { lstatSync, mkdirSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { lstatSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { homedir } from "node:os";
|
|
6
7
|
import { basename, dirname, join, relative, resolve, sep } from "node:path";
|
|
7
8
|
import { addDependency, detectPackageManager as nypmDetectPackageManager } from "nypm";
|
|
8
9
|
import { x } from "tinyexec";
|
|
9
10
|
import packageManifest from "../package.json" with { type: "json" };
|
|
10
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
PACKAGE_MANAGER_CONFIG_FILES,
|
|
13
|
+
enclosingWorkspace,
|
|
14
|
+
packageManagerPin,
|
|
15
|
+
type Workspace,
|
|
16
|
+
writePackageManagerSettings,
|
|
17
|
+
} from "./build-allowance.js";
|
|
11
18
|
import { OperationCanceledError, ScaffoldAbortedError } from "./errors.js";
|
|
12
19
|
import { updateManifest } from "./manifest.js";
|
|
13
20
|
import {
|
|
@@ -16,8 +23,9 @@ import {
|
|
|
16
23
|
PACKAGE_MANAGERS,
|
|
17
24
|
type PackageManager,
|
|
18
25
|
} from "./package-managers.js";
|
|
19
|
-
import {
|
|
26
|
+
import { isValidPackageName, packageNameFromDirectory, withoutAccents } from "./project-name.js";
|
|
20
27
|
import { listTemplates, renderFile, type TemplateContext } from "./templates.js";
|
|
28
|
+
import { addWorkspaceMcpServer } from "./workspace-mcp.js";
|
|
21
29
|
|
|
22
30
|
type CreateOptions = {
|
|
23
31
|
name?: string | undefined;
|
|
@@ -38,6 +46,7 @@ type ResolvedOptions = {
|
|
|
38
46
|
displayName: string;
|
|
39
47
|
applicationId: string;
|
|
40
48
|
packageManager: PackageManager;
|
|
49
|
+
workspace: Workspace | null;
|
|
41
50
|
isTypescript: boolean;
|
|
42
51
|
shouldIncludeTesting: boolean;
|
|
43
52
|
shouldInitializeGit: boolean;
|
|
@@ -52,6 +61,12 @@ type ScaffoldPlan = {
|
|
|
52
61
|
files: ScaffoldFile[];
|
|
53
62
|
replaceDestinations: string[];
|
|
54
63
|
changes: ScaffoldChanges;
|
|
64
|
+
packageManagerPin: string | null;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
type Progress = {
|
|
68
|
+
stop: (message: string) => void;
|
|
69
|
+
error: (message: string) => void;
|
|
55
70
|
};
|
|
56
71
|
|
|
57
72
|
type SpinnerStep = {
|
|
@@ -65,11 +80,18 @@ type SpinnerStep = {
|
|
|
65
80
|
const selfVersion = packageManifest.version;
|
|
66
81
|
const ICON_TEMPLATE_NAME = "icon.svg";
|
|
67
82
|
const TYPESCRIPT_VERSION = "^6.0.3";
|
|
83
|
+
const DEPENDENCY_RANGES: Record<string, string | undefined> = {
|
|
84
|
+
...(JSON.parse(readFileSync(new URL("../dependency-ranges.json", import.meta.url), "utf8")) as Record<
|
|
85
|
+
string,
|
|
86
|
+
string
|
|
87
|
+
>),
|
|
88
|
+
typescript: TYPESCRIPT_VERSION,
|
|
89
|
+
};
|
|
68
90
|
|
|
69
91
|
const DEPENDENCIES = ["@gtkx/cairo", "@gtkx/css", "@gtkx/runtime", "@gtkx/react", "react"];
|
|
70
|
-
const DEV_DEPENDENCIES = ["@gtkx/cli", "@gtkx/config", "@gtkx/mcp", "vite"];
|
|
71
|
-
const TYPESCRIPT_DEV_DEPENDENCIES = ["@types/node", "
|
|
72
|
-
const TESTING_DEV_DEPENDENCIES = ["
|
|
92
|
+
const DEV_DEPENDENCIES = ["@gtkx/cli", "@gtkx/config", "@gtkx/mcp", "@gtkx/testing", "@types/react", "vite"];
|
|
93
|
+
const TYPESCRIPT_DEV_DEPENDENCIES = ["@types/node", "typescript"];
|
|
94
|
+
const TESTING_DEV_DEPENDENCIES = ["vitest"];
|
|
73
95
|
|
|
74
96
|
const DEV_COMMAND: Record<PackageManager, string> = Object.fromEntries(
|
|
75
97
|
PACKAGE_MANAGERS.map((manager) => [manager.value, manager.devCommand]),
|
|
@@ -79,25 +101,33 @@ const INSTALL_COMMAND: Record<PackageManager, string> = Object.fromEntries(
|
|
|
79
101
|
PACKAGE_MANAGERS.map((manager) => [manager.value, manager.installCommand]),
|
|
80
102
|
) as Record<PackageManager, string>;
|
|
81
103
|
|
|
104
|
+
const RUN_COMMAND: Record<PackageManager, string> = Object.fromEntries(
|
|
105
|
+
PACKAGE_MANAGERS.map((manager) => [manager.value, manager.runCommand]),
|
|
106
|
+
) as Record<PackageManager, string>;
|
|
107
|
+
|
|
108
|
+
const TEST_COMMAND: Record<PackageManager, string> = Object.fromEntries(
|
|
109
|
+
PACKAGE_MANAGERS.map((manager) => [manager.value, manager.testCommand]),
|
|
110
|
+
) as Record<PackageManager, string>;
|
|
111
|
+
|
|
82
112
|
const INHERITED_SCRIPT_POLICY_VARS = ["npm_config_allow_scripts", "npm_config_allow-scripts"];
|
|
113
|
+
const LENIENT_PNPM_BUILDS_ENV = { pnpm_config_strict_dep_builds: "false" };
|
|
83
114
|
const TESTING_TEMPLATES = new Set(["vitest.config.ts", "tests/app.test.tsx"]);
|
|
84
115
|
const TYPESCRIPT_TEMPLATES = new Set(["tsconfig.json", "src/gtkx-env.d.ts"]);
|
|
85
116
|
|
|
86
|
-
const HEADLESS_COMPOSITOR_NOTE = `
|
|
87
|
-
|
|
88
|
-
To run tests, you need a headless Wayland compositor installed:
|
|
89
|
-
Fedora: sudo dnf install sway
|
|
90
|
-
Ubuntu: sudo apt install sway`;
|
|
91
|
-
|
|
92
117
|
const APPLICATION_ID_FORMAT_ERROR = "Application ID must be reverse domain notation (e.g., com.example.myapp)";
|
|
118
|
+
const PACKAGE_NAME_FORMAT_ERROR = "Package name must be a valid npm package name: lowercase, no spaces (e.g., my-app)";
|
|
93
119
|
const APPLICATION_ID_PREFIX = "com.";
|
|
94
120
|
const APPLICATION_ID_SUFFIX = ".app";
|
|
95
121
|
|
|
96
|
-
const APPLICATION_ID_SEGMENT_LIMIT =
|
|
97
|
-
APPLICATION_ID_SUFFIX.length;
|
|
122
|
+
const APPLICATION_ID_SEGMENT_LIMIT =
|
|
123
|
+
APPLICATION_ID_MAX_LENGTH - APPLICATION_ID_PREFIX.length - APPLICATION_ID_SUFFIX.length;
|
|
98
124
|
|
|
99
|
-
const
|
|
100
|
-
|
|
125
|
+
const displayNameFromDirectory = (directoryName: string): string =>
|
|
126
|
+
directoryName
|
|
127
|
+
.split(/[\s._-]+/)
|
|
128
|
+
.filter((part) => part.length > 0)
|
|
129
|
+
.map((part) => upperFirst(part))
|
|
130
|
+
.join(" ");
|
|
101
131
|
|
|
102
132
|
const gitConfigValue = (key: string): string | null => {
|
|
103
133
|
const git = tryResolveExecutable("git");
|
|
@@ -113,17 +143,23 @@ const gitConfigValue = (key: string): string | null => {
|
|
|
113
143
|
}
|
|
114
144
|
};
|
|
115
145
|
|
|
116
|
-
const
|
|
117
|
-
|
|
146
|
+
const compactIdentifier = (name: string): string => withoutAccents(name).replaceAll(/[^A-Za-z0-9_]/g, "");
|
|
147
|
+
|
|
148
|
+
const applicationIdSegment = (directoryName: string, name: string): string => {
|
|
149
|
+
const compact = compactIdentifier(directoryName) || compactIdentifier(name);
|
|
118
150
|
const segment = /^[A-Za-z_]/.test(compact) ? compact : `_${compact}`;
|
|
119
151
|
|
|
120
152
|
return segment.slice(0, APPLICATION_ID_SEGMENT_LIMIT);
|
|
121
153
|
};
|
|
122
154
|
|
|
123
|
-
const suggestApplicationId = (name: string): string =>
|
|
124
|
-
`${APPLICATION_ID_PREFIX}${applicationIdSegment(name)}${APPLICATION_ID_SUFFIX}`;
|
|
155
|
+
const suggestApplicationId = (directoryName: string, name: string): string =>
|
|
156
|
+
`${APPLICATION_ID_PREFIX}${applicationIdSegment(directoryName, name)}${APPLICATION_ID_SUFFIX}`;
|
|
157
|
+
|
|
158
|
+
const HOME_PREFIX = /^~(?=$|\/)/;
|
|
159
|
+
|
|
160
|
+
const expandHome = (target: string): string => target.replace(HOME_PREFIX, () => homedir());
|
|
125
161
|
|
|
126
|
-
const
|
|
162
|
+
const directoryNameOf = (target: string): string => basename(resolve(process.cwd(), target));
|
|
127
163
|
|
|
128
164
|
const isDirEmpty = (dir: string): boolean => {
|
|
129
165
|
const entries = readdirSync(dir);
|
|
@@ -165,17 +201,14 @@ const guardCancellation = <T>(value: T | typeof p.CANCEL_SYMBOL): T => {
|
|
|
165
201
|
return value;
|
|
166
202
|
};
|
|
167
203
|
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
return "Project name is required";
|
|
171
|
-
}
|
|
204
|
+
const validatePackageNameFormat = (value: string): string | undefined =>
|
|
205
|
+
isValidPackageName(value) ? undefined : PACKAGE_NAME_FORMAT_ERROR;
|
|
172
206
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
207
|
+
const validatePackageNameInput = (value: string | undefined): string | undefined =>
|
|
208
|
+
value ? validatePackageNameFormat(value) : "Package name is required";
|
|
176
209
|
|
|
177
|
-
|
|
178
|
-
|
|
210
|
+
const validatePackageNameAnswer = (value: string | undefined): string | undefined =>
|
|
211
|
+
value ? validatePackageNameFormat(value) : undefined;
|
|
179
212
|
|
|
180
213
|
const validateApplicationIdFormat = (value: string): string | undefined =>
|
|
181
214
|
isValidApplicationId(value) ? undefined : APPLICATION_ID_FORMAT_ERROR;
|
|
@@ -214,16 +247,11 @@ const requestedPackageManager = (value: string | undefined): PackageManager | un
|
|
|
214
247
|
return value;
|
|
215
248
|
};
|
|
216
249
|
|
|
217
|
-
const validateTargetDir = (target: string): string | undefined =>
|
|
218
|
-
|
|
219
|
-
return "Project directory is required";
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
return validateProjectName(deriveProjectName(target));
|
|
223
|
-
};
|
|
250
|
+
const validateTargetDir = (target: string): string | undefined =>
|
|
251
|
+
target ? undefined : "Project directory is required";
|
|
224
252
|
|
|
225
253
|
const validateTargetAnswer = (value: string | undefined): string | undefined =>
|
|
226
|
-
validateTargetDir(value ?? "");
|
|
254
|
+
validateTargetDir(expandHome(value ?? ""));
|
|
227
255
|
|
|
228
256
|
const promptTarget = async (): Promise<string> =>
|
|
229
257
|
guardCancellation(
|
|
@@ -234,8 +262,17 @@ const promptTarget = async (): Promise<string> =>
|
|
|
234
262
|
}),
|
|
235
263
|
);
|
|
236
264
|
|
|
237
|
-
const
|
|
238
|
-
|
|
265
|
+
const promptPackageName = async (derived: string): Promise<string> =>
|
|
266
|
+
guardCancellation(
|
|
267
|
+
await p.text({
|
|
268
|
+
message: "Package name",
|
|
269
|
+
...(derived === "" ? {} : { placeholder: derived, defaultValue: derived }),
|
|
270
|
+
validate: derived === "" ? validatePackageNameInput : validatePackageNameAnswer,
|
|
271
|
+
}),
|
|
272
|
+
);
|
|
273
|
+
|
|
274
|
+
const promptApplicationId = async (directoryName: string, name: string): Promise<string> => {
|
|
275
|
+
const defaultApplicationId = suggestApplicationId(directoryName, name);
|
|
239
276
|
|
|
240
277
|
return guardCancellation(
|
|
241
278
|
await p.text({
|
|
@@ -247,8 +284,8 @@ const promptApplicationId = async (name: string): Promise<string> => {
|
|
|
247
284
|
);
|
|
248
285
|
};
|
|
249
286
|
|
|
250
|
-
const promptDisplayName = async (
|
|
251
|
-
const defaultDisplayName =
|
|
287
|
+
const promptDisplayName = async (directoryName: string): Promise<string> => {
|
|
288
|
+
const defaultDisplayName = displayNameFromDirectory(directoryName);
|
|
252
289
|
|
|
253
290
|
return guardCancellation(
|
|
254
291
|
await p.text({
|
|
@@ -299,8 +336,7 @@ const isOptionEnabled = async (
|
|
|
299
336
|
value: boolean | undefined,
|
|
300
337
|
isInteractive: boolean | undefined,
|
|
301
338
|
message: string,
|
|
302
|
-
): Promise<boolean> =>
|
|
303
|
-
value ?? (!isInteractive || guardCancellation(await p.confirm({ message, initialValue: true })));
|
|
339
|
+
): Promise<boolean> => value ?? (!isInteractive || guardCancellation(await p.confirm({ message, initialValue: true })));
|
|
304
340
|
|
|
305
341
|
const formatFileList = (heading: string, files: string[]): string => {
|
|
306
342
|
const indentedFiles = files.map((file) => ` ${file}`).join("\n");
|
|
@@ -394,27 +430,50 @@ const validateTargetAvailability = async (
|
|
|
394
430
|
|
|
395
431
|
const resolveTarget = async (options: CreateOptions): Promise<string> => {
|
|
396
432
|
if (options.name === undefined && options.isInteractive) {
|
|
397
|
-
return promptTarget();
|
|
433
|
+
return expandHome(await promptTarget());
|
|
398
434
|
}
|
|
399
435
|
|
|
400
|
-
const target = options.name ?? "";
|
|
436
|
+
const target = expandHome(options.name ?? "");
|
|
401
437
|
const error = validateTargetDir(target);
|
|
402
438
|
|
|
403
439
|
return error === undefined ? target : fail(error);
|
|
404
440
|
};
|
|
405
441
|
|
|
406
|
-
const
|
|
407
|
-
const
|
|
408
|
-
|
|
442
|
+
const resolvePackageName = async (options: CreateOptions, directoryName: string): Promise<string> => {
|
|
443
|
+
const derived = packageNameFromDirectory(directoryName);
|
|
444
|
+
|
|
445
|
+
if (derived === directoryName) {
|
|
446
|
+
return derived;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
if (options.isInteractive) {
|
|
450
|
+
return promptPackageName(derived);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
return derived === ""
|
|
454
|
+
? fail(
|
|
455
|
+
`Directory name "${directoryName}" has no Latin letters or numbers to derive a package name from. ` +
|
|
456
|
+
"Rename it, or run interactively to enter a package name",
|
|
457
|
+
)
|
|
458
|
+
: derived;
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
const resolveApplicationId = async (options: CreateOptions, directoryName: string, name: string): Promise<string> => {
|
|
462
|
+
const applicationId =
|
|
463
|
+
options.applicationId ??
|
|
464
|
+
(options.isInteractive
|
|
465
|
+
? await promptApplicationId(directoryName, name)
|
|
466
|
+
: suggestApplicationId(directoryName, name));
|
|
409
467
|
|
|
410
468
|
const error = validateApplicationIdInput(applicationId);
|
|
411
469
|
|
|
412
470
|
return error === undefined ? applicationId : fail(error);
|
|
413
471
|
};
|
|
414
472
|
|
|
415
|
-
const resolveDisplayName = async (options: CreateOptions,
|
|
416
|
-
const value =
|
|
417
|
-
|
|
473
|
+
const resolveDisplayName = async (options: CreateOptions, directoryName: string): Promise<string> => {
|
|
474
|
+
const value =
|
|
475
|
+
options.displayName ??
|
|
476
|
+
(options.isInteractive ? await promptDisplayName(directoryName) : displayNameFromDirectory(directoryName));
|
|
418
477
|
const error = validateDisplayName(value);
|
|
419
478
|
|
|
420
479
|
return error === undefined ? value.trim() : fail(error);
|
|
@@ -438,9 +497,10 @@ const resolvePackageManager = async (
|
|
|
438
497
|
const resolveOptions = async (options: CreateOptions): Promise<ResolvedOptions> => {
|
|
439
498
|
const requested = requestedPackageManager(options.packageManager);
|
|
440
499
|
const target = await resolveTarget(options);
|
|
441
|
-
const
|
|
442
|
-
const
|
|
443
|
-
const
|
|
500
|
+
const directoryName = directoryNameOf(target);
|
|
501
|
+
const name = await resolvePackageName(options, directoryName);
|
|
502
|
+
const displayName = await resolveDisplayName(options, directoryName);
|
|
503
|
+
const applicationId = await resolveApplicationId(options, directoryName, name);
|
|
444
504
|
const root = resolve(process.cwd(), target);
|
|
445
505
|
const shouldInitialize = shouldInitializeGit(root);
|
|
446
506
|
const packageManager = await resolvePackageManager(requested, options);
|
|
@@ -459,6 +519,7 @@ const resolveOptions = async (options: CreateOptions): Promise<ResolvedOptions>
|
|
|
459
519
|
displayName,
|
|
460
520
|
applicationId,
|
|
461
521
|
packageManager,
|
|
522
|
+
workspace: enclosingWorkspace(root, packageManager),
|
|
462
523
|
isTypescript,
|
|
463
524
|
shouldIncludeTesting,
|
|
464
525
|
shouldInitializeGit: shouldInitialize,
|
|
@@ -486,6 +547,17 @@ const destinationPath = (template: string, resolved: ResolvedOptions): string =>
|
|
|
486
547
|
: scriptPath;
|
|
487
548
|
};
|
|
488
549
|
|
|
550
|
+
const scriptCommands = ({
|
|
551
|
+
packageManager,
|
|
552
|
+
isTypescript,
|
|
553
|
+
shouldIncludeTesting,
|
|
554
|
+
}: Pick<ResolvedOptions, "packageManager" | "isTypescript" | "shouldIncludeTesting">): string[] => [
|
|
555
|
+
...["dev", "build", "codegen", ...(isTypescript ? ["typecheck"] : [])].map(
|
|
556
|
+
(script) => `${RUN_COMMAND[packageManager]} ${script}`,
|
|
557
|
+
),
|
|
558
|
+
...(shouldIncludeTesting ? [TEST_COMMAND[packageManager]] : []),
|
|
559
|
+
];
|
|
560
|
+
|
|
489
561
|
const templateContext = (resolved: ResolvedOptions): TemplateContext => ({
|
|
490
562
|
name: resolved.name,
|
|
491
563
|
applicationId: resolved.applicationId,
|
|
@@ -495,6 +567,8 @@ const templateContext = (resolved: ResolvedOptions): TemplateContext => ({
|
|
|
495
567
|
importExtension: resolved.isTypescript ? ".js" : ".jsx",
|
|
496
568
|
developerName: gitConfigValue("user.name") ?? resolved.displayName,
|
|
497
569
|
developerEmail: gitConfigValue("user.email"),
|
|
570
|
+
packageManager: resolved.packageManager,
|
|
571
|
+
scriptCommands: scriptCommands(resolved),
|
|
498
572
|
});
|
|
499
573
|
|
|
500
574
|
const assertSafeAncestor = (ancestor: string): void => {
|
|
@@ -553,22 +627,31 @@ const plannedScaffoldFiles = async (root: string, resolved: ResolvedOptions): Pr
|
|
|
553
627
|
return files;
|
|
554
628
|
};
|
|
555
629
|
|
|
556
|
-
const
|
|
557
|
-
resolved.
|
|
630
|
+
const configDestinations = (root: string, resolved: ResolvedOptions): string[] =>
|
|
631
|
+
resolved.workspace === null
|
|
632
|
+
? PACKAGE_MANAGER_CONFIG_FILES[resolved.packageManager].map((file) => join(root, file))
|
|
633
|
+
: [];
|
|
558
634
|
|
|
559
635
|
const planScaffoldProject = async (root: string, resolved: ResolvedOptions): Promise<ScaffoldPlan> => {
|
|
560
636
|
const files = await plannedScaffoldFiles(root, resolved);
|
|
561
|
-
|
|
562
|
-
const
|
|
637
|
+
|
|
638
|
+
for (const destination of configDestinations(root, resolved)) {
|
|
639
|
+
hasExistingDestination(root, destination);
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
const replaceDestinations = files
|
|
643
|
+
.map((file) => file.destination)
|
|
644
|
+
.filter((destination) => hasExistingDestination(root, destination));
|
|
563
645
|
|
|
564
646
|
return {
|
|
565
647
|
files,
|
|
566
648
|
replaceDestinations,
|
|
567
649
|
changes: replaceDestinations.map((destination) => relative(root, destination)),
|
|
650
|
+
packageManagerPin: packageManagerPin(root, resolved.packageManager, resolved.workspace !== null),
|
|
568
651
|
};
|
|
569
652
|
};
|
|
570
653
|
|
|
571
|
-
const scaffoldProject = (plan: ScaffoldPlan): void => {
|
|
654
|
+
const scaffoldProject = (root: string, plan: ScaffoldPlan): void => {
|
|
572
655
|
for (const destination of plan.replaceDestinations) {
|
|
573
656
|
rmSync(destination);
|
|
574
657
|
}
|
|
@@ -577,6 +660,14 @@ const scaffoldProject = (plan: ScaffoldPlan): void => {
|
|
|
577
660
|
mkdirSync(dirname(file.destination), { recursive: true });
|
|
578
661
|
writeFileSync(file.destination, file.contents);
|
|
579
662
|
}
|
|
663
|
+
|
|
664
|
+
const pin = plan.packageManagerPin;
|
|
665
|
+
|
|
666
|
+
if (pin !== null) {
|
|
667
|
+
updateManifest(root, (manifest) => {
|
|
668
|
+
manifest.packageManager = pin;
|
|
669
|
+
});
|
|
670
|
+
}
|
|
580
671
|
};
|
|
581
672
|
|
|
582
673
|
const withoutInheritedScriptPolicy = async (run: () => Promise<void>): Promise<void> => {
|
|
@@ -597,60 +688,116 @@ const withoutInheritedScriptPolicy = async (run: () => Promise<void>): Promise<v
|
|
|
597
688
|
}
|
|
598
689
|
};
|
|
599
690
|
|
|
600
|
-
const dependencyVersions = (names: string[]): Record<string, string> =>
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
691
|
+
const dependencyVersions = (names: string[]): Record<string, string> =>
|
|
692
|
+
Object.fromEntries(
|
|
693
|
+
names.map((dependency) => {
|
|
694
|
+
if (dependency.startsWith("@gtkx/")) {
|
|
695
|
+
return [dependency, `^${selfVersion}`];
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
const range = DEPENDENCY_RANGES[dependency];
|
|
699
|
+
|
|
700
|
+
if (range === undefined) {
|
|
701
|
+
throw new Error(`No supported version range for ${dependency}`);
|
|
702
|
+
}
|
|
605
703
|
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
);
|
|
704
|
+
return [dependency, range];
|
|
705
|
+
}),
|
|
706
|
+
);
|
|
609
707
|
|
|
610
708
|
const dependencyArguments = (names: string[]): string[] =>
|
|
611
|
-
Object.entries(dependencyVersions(names)).map(([name, version]) =>
|
|
612
|
-
|
|
709
|
+
Object.entries(dependencyVersions(names)).map(([name, version]) => `${name}@${version}`);
|
|
710
|
+
|
|
711
|
+
const declaredDependencies = (value: unknown): Record<string, unknown> =>
|
|
712
|
+
typeof value === "object" && value !== null ? { ...value } : {};
|
|
613
713
|
|
|
614
714
|
const writeDependencies = (root: string, devDependencies: string[]): void => {
|
|
615
715
|
updateManifest(root, (manifest) => {
|
|
616
|
-
manifest.dependencies =
|
|
617
|
-
|
|
716
|
+
manifest.dependencies = {
|
|
717
|
+
...dependencyVersions(DEPENDENCIES),
|
|
718
|
+
...declaredDependencies(manifest.dependencies),
|
|
719
|
+
};
|
|
720
|
+
manifest.devDependencies = {
|
|
721
|
+
...dependencyVersions(devDependencies),
|
|
722
|
+
...declaredDependencies(manifest.devDependencies),
|
|
723
|
+
};
|
|
618
724
|
});
|
|
619
725
|
};
|
|
620
726
|
|
|
621
727
|
const formatRecovery = (resolved: ResolvedOptions): string =>
|
|
622
728
|
`To finish setup, run ${INSTALL_COMMAND[resolved.packageManager]} in ${resolved.root}.`;
|
|
623
729
|
|
|
730
|
+
const startProgress = (message: string): Progress => {
|
|
731
|
+
if (process.stdout.isTTY) {
|
|
732
|
+
const spinner = p.spinner();
|
|
733
|
+
spinner.start(message);
|
|
734
|
+
|
|
735
|
+
return spinner;
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
p.log.step(message);
|
|
739
|
+
|
|
740
|
+
return { stop: (done) => p.log.step(done), error: (failed) => p.log.error(failed) };
|
|
741
|
+
};
|
|
742
|
+
|
|
624
743
|
const runWithSpinner = async (step: SpinnerStep): Promise<void> => {
|
|
625
|
-
const
|
|
626
|
-
spinner.start(step.pending);
|
|
744
|
+
const progress = startProgress(step.pending);
|
|
627
745
|
|
|
628
746
|
try {
|
|
629
747
|
await step.run();
|
|
630
|
-
|
|
748
|
+
progress.stop(step.done);
|
|
631
749
|
} catch (error) {
|
|
632
|
-
|
|
750
|
+
progress.error(step.failed);
|
|
633
751
|
p.log.error(errorMessage(error));
|
|
634
752
|
step.explain?.();
|
|
635
753
|
throw new ScaffoldAbortedError(step.failed);
|
|
636
754
|
}
|
|
637
755
|
};
|
|
638
756
|
|
|
757
|
+
const printWorkspaceAllowances = ({ root, packageManager, workspace }: ResolvedOptions): void => {
|
|
758
|
+
const installed = workspace === null ? null : enclosingWorkspace(root, packageManager);
|
|
759
|
+
|
|
760
|
+
if (installed === null || installed.missingSettings.length === 0) {
|
|
761
|
+
return;
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
p.log.warn(
|
|
765
|
+
`The ${installed.packageManager} workspace lacks settings this app needs. ` +
|
|
766
|
+
`Set these entries, then run ${INSTALL_COMMAND[installed.packageManager]}:\n\n` +
|
|
767
|
+
installed.missingSettings
|
|
768
|
+
.map(
|
|
769
|
+
({ file, section, entries }) =>
|
|
770
|
+
`${section === null ? file : `${file} (under ${section})`}:\n${entries}`,
|
|
771
|
+
)
|
|
772
|
+
.join("\n\n"),
|
|
773
|
+
);
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
const hasMissingAllowances = (resolved: ResolvedOptions): boolean =>
|
|
777
|
+
resolved.workspace?.packageManager === "pnpm" && resolved.workspace.missingSettings.length > 0;
|
|
778
|
+
|
|
639
779
|
const installAllDependencies = async (resolved: ResolvedOptions, devDependencies: string[]): Promise<void> => {
|
|
640
780
|
const { root, packageManager } = resolved;
|
|
781
|
+
const env = hasMissingAllowances(resolved) ? LENIENT_PNPM_BUILDS_ENV : {};
|
|
641
782
|
|
|
642
783
|
await runWithSpinner({
|
|
643
784
|
pending: "Installing dependencies...",
|
|
644
785
|
done: "Dependencies installed",
|
|
645
786
|
failed: "Failed to install dependencies",
|
|
646
787
|
run: async () => {
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
788
|
+
try {
|
|
789
|
+
await withoutInheritedScriptPolicy(async () => {
|
|
790
|
+
const options = { cwd: root, packageManager, env, silent: true };
|
|
791
|
+
await addDependency(dependencyArguments(DEPENDENCIES), options);
|
|
792
|
+
await addDependency(dependencyArguments(devDependencies), { ...options, dev: true });
|
|
793
|
+
});
|
|
794
|
+
} catch (error) {
|
|
795
|
+
writeDependencies(root, devDependencies);
|
|
796
|
+
throw error;
|
|
797
|
+
}
|
|
652
798
|
},
|
|
653
799
|
explain: () => {
|
|
800
|
+
printWorkspaceAllowances(resolved);
|
|
654
801
|
p.log.info(formatRecovery(resolved));
|
|
655
802
|
},
|
|
656
803
|
});
|
|
@@ -677,40 +824,75 @@ const initializeGitRepo = async (root: string, shouldInitialize: boolean): Promi
|
|
|
677
824
|
return;
|
|
678
825
|
}
|
|
679
826
|
|
|
680
|
-
const
|
|
681
|
-
spinner.start("Initializing git repository...");
|
|
827
|
+
const progress = startProgress("Initializing git repository...");
|
|
682
828
|
|
|
683
829
|
try {
|
|
684
830
|
const opts = { nodeOptions: { cwd: root }, throwOnError: true } as const;
|
|
685
831
|
await x("git", ["init"], opts);
|
|
686
832
|
await x("git", ["add", "-A"], opts);
|
|
687
833
|
await x("git", ["commit", "-m", "Initial commit"], opts);
|
|
688
|
-
|
|
834
|
+
progress.stop("Git repository initialized");
|
|
689
835
|
} catch {
|
|
690
|
-
|
|
836
|
+
progress.error("Failed to initialize git repository");
|
|
837
|
+
}
|
|
838
|
+
};
|
|
839
|
+
|
|
840
|
+
const missingHeadlessToolsNote = (): string => {
|
|
841
|
+
const missing = Object.values(HEADLESS_DISPLAY_TOOLS).filter(
|
|
842
|
+
(tool) => tryResolveExecutable(tool.command) === undefined,
|
|
843
|
+
);
|
|
844
|
+
|
|
845
|
+
return missing.length === 0
|
|
846
|
+
? ""
|
|
847
|
+
: ["", "", "To run tests, install these tools:", ...missingToolLines(missing)].join("\n");
|
|
848
|
+
};
|
|
849
|
+
|
|
850
|
+
const addRootMcpServer = ({ workspace, name, root }: ResolvedOptions): void => {
|
|
851
|
+
if (workspace === null) {
|
|
852
|
+
return;
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
const result = addWorkspaceMcpServer(workspace, name);
|
|
856
|
+
|
|
857
|
+
if (result.kind === "added") {
|
|
858
|
+
p.log.info(
|
|
859
|
+
`Added the MCP server "${result.key}" to ${result.path}, so an AI client opened at the workspace ` +
|
|
860
|
+
"root reaches this app.",
|
|
861
|
+
);
|
|
862
|
+
} else if (result.kind === "shared") {
|
|
863
|
+
p.log.info(
|
|
864
|
+
`The MCP server "${result.key}" in ${result.path} drives every running GTKX app, including this one, ` +
|
|
865
|
+
`so no server was added for it. To look up this app's bindings, pass projectRoot "${root}" to its ` +
|
|
866
|
+
"reference tools.",
|
|
867
|
+
);
|
|
868
|
+
} else if (result.kind === "unreadable") {
|
|
869
|
+
p.log.warn(
|
|
870
|
+
`Could not add this app's MCP server to ${result.path}: ${result.reason}. For an AI client opened ` +
|
|
871
|
+
`at the workspace root, add this entry under "mcpServers":\n\n` +
|
|
872
|
+
JSON.stringify({ [result.key]: result.server }, null, 4),
|
|
873
|
+
);
|
|
691
874
|
}
|
|
692
875
|
};
|
|
693
876
|
|
|
694
877
|
const printNextSteps = (resolved: ResolvedOptions): void => {
|
|
695
878
|
const devCmd = DEV_COMMAND[resolved.packageManager];
|
|
696
879
|
const installStep = resolved.shouldInstallDependencies ? "" : `${INSTALL_COMMAND[resolved.packageManager]}\n`;
|
|
697
|
-
const testingNote = resolved.shouldIncludeTesting ?
|
|
880
|
+
const testingNote = resolved.shouldIncludeTesting ? missingHeadlessToolsNote() : "";
|
|
698
881
|
p.note(`${installStep}${devCmd}${testingNote}`, `Run in ${resolved.root}`);
|
|
699
882
|
};
|
|
700
883
|
|
|
701
|
-
const createProjectStructure = async (
|
|
702
|
-
root: string,
|
|
703
|
-
resolved: ResolvedOptions,
|
|
704
|
-
plan: ScaffoldPlan,
|
|
705
|
-
): Promise<void> => {
|
|
884
|
+
const createProjectStructure = async (root: string, resolved: ResolvedOptions, plan: ScaffoldPlan): Promise<void> => {
|
|
706
885
|
await runWithSpinner({
|
|
707
886
|
pending: "Creating project structure...",
|
|
708
887
|
done: "Project structure created",
|
|
709
888
|
failed: "Failed to create the project structure",
|
|
710
889
|
run: () => {
|
|
711
890
|
mkdirSync(root, { recursive: true });
|
|
712
|
-
scaffoldProject(plan);
|
|
713
|
-
|
|
891
|
+
scaffoldProject(root, plan);
|
|
892
|
+
|
|
893
|
+
if (resolved.workspace === null) {
|
|
894
|
+
writePackageManagerSettings(root, resolved.packageManager);
|
|
895
|
+
}
|
|
714
896
|
},
|
|
715
897
|
});
|
|
716
898
|
};
|
|
@@ -726,12 +908,14 @@ const scaffold = async (options: CreateOptions = {}): Promise<void> => {
|
|
|
726
908
|
await createProjectStructure(root, resolved, plan);
|
|
727
909
|
writeDependencies(root, devDeps);
|
|
728
910
|
reportCompletedChanges(plan.changes);
|
|
911
|
+
addRootMcpServer(resolved);
|
|
729
912
|
|
|
730
913
|
if (resolved.shouldInstallDependencies) {
|
|
731
914
|
await installAllDependencies(resolved, devDeps);
|
|
732
915
|
}
|
|
733
916
|
|
|
734
917
|
await initializeGitRepo(root, resolved.shouldInitializeGit);
|
|
918
|
+
printWorkspaceAllowances(resolved);
|
|
735
919
|
printNextSteps(resolved);
|
|
736
920
|
};
|
|
737
921
|
|
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
"allow": [
|
|
4
4
|
"Bash(npx gtkx codegen:*)",
|
|
5
5
|
"Bash(npx gtkx dev:*)",
|
|
6
|
-
"Bash(npx gtkx build:*)"
|
|
7
|
-
"Bash(npx tsc --noEmit)"<% if (shouldIncludeTesting) { %>,
|
|
8
|
-
"Bash(npx vitest run:*)"<% }
|
|
6
|
+
"Bash(npx gtkx build:*)"<% if (isTypescript) { %>,
|
|
7
|
+
"Bash(npx tsc --noEmit)"<% } %><% if (shouldIncludeTesting) { %>,
|
|
8
|
+
"Bash(npx vitest run:*)"<% } %><% for (const command of scriptCommands) { %>,
|
|
9
|
+
"Bash(<%= command %>:*)"<% } %>
|
|
9
10
|
],
|
|
10
11
|
"deny": [],
|
|
11
12
|
"ask": []
|