create-gtkx 2.0.0-beta.9 → 2.0.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.
Files changed (71) hide show
  1. package/README.md +8 -166
  2. package/dependency-ranges.json +7 -0
  3. package/dist/build-allowance.d.ts +43 -2
  4. package/dist/build-allowance.d.ts.map +1 -1
  5. package/dist/build-allowance.js +360 -9
  6. package/dist/build-allowance.js.map +1 -1
  7. package/dist/cli.js +1 -1
  8. package/dist/cli.js.map +1 -1
  9. package/dist/command.d.ts +3 -2
  10. package/dist/command.d.ts.map +1 -1
  11. package/dist/command.js +93 -90
  12. package/dist/command.js.map +1 -1
  13. package/dist/errors.d.ts.map +1 -1
  14. package/dist/index.d.ts +1 -0
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +1 -0
  17. package/dist/index.js.map +1 -1
  18. package/dist/manifest.d.ts.map +1 -1
  19. package/dist/manifest.js +2 -10
  20. package/dist/manifest.js.map +1 -1
  21. package/dist/package-managers.d.ts +6 -3
  22. package/dist/package-managers.d.ts.map +1 -1
  23. package/dist/package-managers.js +6 -3
  24. package/dist/package-managers.js.map +1 -1
  25. package/dist/project-name.d.ts +4 -2
  26. package/dist/project-name.d.ts.map +1 -1
  27. package/dist/project-name.js +12 -2
  28. package/dist/project-name.js.map +1 -1
  29. package/dist/scaffolder.d.ts.map +1 -1
  30. package/dist/scaffolder.js +199 -199
  31. package/dist/scaffolder.js.map +1 -1
  32. package/dist/templates/.claude/settings.json.ejs +4 -3
  33. package/dist/templates/.gitignore.ejs +4 -0
  34. package/dist/templates/.mcp.json.ejs +1 -1
  35. package/dist/templates/gtkx.config.ts.ejs +2 -2
  36. package/dist/templates/package.json.ejs +4 -1
  37. package/dist/templates/src/app.tsx.ejs +1 -1
  38. package/dist/templates/src/gtkx-env.d.ts.ejs +0 -1
  39. package/dist/templates/tsconfig.json.ejs +4 -2
  40. package/dist/templates.d.ts +3 -0
  41. package/dist/templates.d.ts.map +1 -1
  42. package/dist/templates.js +10 -6
  43. package/dist/templates.js.map +1 -1
  44. package/dist/workspace-mcp.d.ts +33 -0
  45. package/dist/workspace-mcp.d.ts.map +1 -0
  46. package/dist/workspace-mcp.js +75 -0
  47. package/dist/workspace-mcp.js.map +1 -0
  48. package/package.json +20 -39
  49. package/src/build-allowance.ts +486 -9
  50. package/src/cli.ts +1 -1
  51. package/src/command.ts +114 -110
  52. package/src/index.ts +1 -0
  53. package/src/manifest.ts +2 -10
  54. package/src/package-managers.ts +6 -3
  55. package/src/project-name.ts +19 -2
  56. package/src/scaffolder.ts +294 -286
  57. package/src/templates/.claude/settings.json.ejs +4 -3
  58. package/src/templates/.gitignore.ejs +4 -0
  59. package/src/templates/.mcp.json.ejs +1 -1
  60. package/src/templates/gtkx.config.ts.ejs +2 -2
  61. package/src/templates/package.json.ejs +4 -1
  62. package/src/templates/src/app.tsx.ejs +1 -1
  63. package/src/templates/src/gtkx-env.d.ts.ejs +0 -1
  64. package/src/templates/tsconfig.json.ejs +4 -2
  65. package/src/templates.ts +17 -8
  66. package/src/workspace-mcp.ts +111 -0
  67. package/dist/install-hints.d.ts +0 -3
  68. package/dist/install-hints.d.ts.map +0 -1
  69. package/dist/install-hints.js +0 -35
  70. package/dist/install-hints.js.map +0 -1
  71. package/src/install-hints.ts +0 -54
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "create-gtkx",
3
- "version": "2.0.0-beta.9",
4
- "description": "Create a native GNOME app with React, Adwaita, and GTK4.",
3
+ "version": "2.0.0",
4
+ "description": "Create native GTK and Adwaita apps with React and GTKX.",
5
5
  "keywords": [
6
6
  "gtkx",
7
7
  "gtk",
@@ -43,6 +43,7 @@
43
43
  "sideEffects": false,
44
44
  "files": [
45
45
  "bin",
46
+ "dependency-ranges.json",
46
47
  "dist",
47
48
  "src"
48
49
  ],
@@ -50,48 +51,28 @@
50
51
  "node": ">=26.7.0"
51
52
  },
52
53
  "dependencies": {
53
- "@clack/prompts": "^1.8.0",
54
- "@gtkx/config": "2.0.0-beta.9",
55
- "@gtkx/utils": "2.0.0-beta.9",
56
- "ejs": "^6.0.1",
54
+ "@clack/prompts": "^1.9.0",
55
+ "@gtkx/config": "^2.0.0",
56
+ "@gtkx/utils": "^2.0.0",
57
57
  "citty": "^0.2.2",
58
- "nypm": "^0.6.9",
59
- "tinyexec": "^1.3.1"
58
+ "ejs": "^7.0.2",
59
+ "nypm": "^0.6.12",
60
+ "tinyexec": "^1.4.0",
61
+ "yaml": "^2.9.1"
60
62
  },
61
63
  "devDependencies": {
62
64
  "@types/ejs": "^3.1.5",
63
- "@types/node": "^26.5.0",
64
- "typescript": "^6.0.3",
65
- "vitest": "^5.0.0"
66
- },
67
- "nx": {
68
- "targets": {
69
- "lint": {
70
- "options": {
71
- "cwd": ".",
72
- "command": "eslint {projectRoot}"
73
- }
74
- },
75
- "copy-templates": {
76
- "cache": true,
77
- "inputs": [
78
- "{projectRoot}/src/templates/**/*"
79
- ],
80
- "outputs": [
81
- "{projectRoot}/dist/templates/**"
82
- ]
83
- },
84
- "build": {
85
- "dependsOn": [
86
- "^build",
87
- "gtkx:codegen",
88
- "copy-templates"
89
- ]
90
- }
91
- }
65
+ "@types/node": "^26.6.11",
66
+ "typescript": "npm:@typescript/typescript6@^6.0.2",
67
+ "vitest": "5.1.2",
68
+ "test-console": "^2.0.0",
69
+ "@types/test-console": "^2.0.3",
70
+ "semver": "^7.8.6",
71
+ "@types/semver": "^7.8.0"
92
72
  },
93
73
  "scripts": {
94
- "copy-templates": "mkdir -p dist/templates && cp -R src/templates/. dist/templates/",
95
- "release": "tsx ../../scripts/release-package.ts"
74
+ "_build:ranges": "node scripts/dependency-ranges.ts",
75
+ "_build:templates": "mkdir -p dist/templates && cp -R src/templates/. dist/templates/",
76
+ "release": "node ../../scripts/release/publish-package.ts"
96
77
  }
97
78
  }
@@ -1,33 +1,233 @@
1
- import { writeFileSync } from "node:fs";
2
- import { join } from "node:path";
1
+ import { tryResolveExecutable } from "@gtkx/utils";
2
+ import { execFileSync } from "node:child_process";
3
+ import { existsSync, lstatSync, readFileSync, rmSync, writeFileSync } from "node:fs";
4
+ import { dirname, join, matchesGlob, posix, relative, sep } from "node:path";
5
+ import { type Document, isMap, isSeq, parse, parseDocument, stringify } from "yaml";
3
6
  import type { PackageManager } from "./package-managers.js";
4
7
  import { updateManifest } from "./manifest.js";
5
8
 
9
+ type MissingSetting = {
10
+ file: string;
11
+ section: string | null;
12
+ entries: string;
13
+ };
14
+
15
+ type Workspace = {
16
+ packageManager: PackageManager;
17
+ root: string;
18
+ member: string;
19
+ missingSettings: MissingSetting[];
20
+ };
21
+
6
22
  const BUILT_DEPENDENCIES = ["@swc/core", "esbuild"];
23
+ const RELEASE_AGE_EXEMPT_PACKAGES = ["@gtkx/*", "create-gtkx"];
24
+ const YARN_NODE_LINKER = "node-modules";
7
25
  const PNPM_WORKSPACE_FILE = "pnpm-workspace.yaml";
8
- const SAFE_YAML_KEY = /^[A-Za-z][A-Za-z0-9-]*$/;
9
- const PNPM_PACKAGES_BLOCK = "packages:\n - '.'\n";
26
+ const YARN_CONFIG_FILE = ".yarnrc.yml";
27
+ const NPM_CONFIG_FILE = ".npmrc";
28
+ const PACKAGE_JSON_FILE = "package.json";
29
+ const PNPM_RELEASE_AGE_EXCLUDE = "minimumReleaseAgeExclude";
30
+ const YARN_PREAPPROVED_PACKAGES = "npmPreapprovedPackages";
31
+ const NPM_RELEASE_AGE_EXCLUDE = "min-release-age-exclude[]";
32
+ const NPM_RELEASE_AGE_EXCLUDE_PATTERN = /^\s*min-release-age-exclude(?:\[\])?\s*=\s*(["']?)(.*?)\1\s*$/;
33
+ const NEGATION_PREFIX = "!";
34
+ const YARN_USER_AGENT_PATTERN = /^yarn\/(\d+\.\d+\.\d+\S*)/;
35
+ const FIRST_PINNED_YARN_MAJOR = 2;
36
+ const YARN_PIN_PREFIX = "yarn@";
37
+ const YARN_RELEASE_PATTERN = /^(\d+)\.(\d+)\./;
38
+ const FIRST_PREAPPROVING_YARN = { major: 4, minor: 10 };
39
+
40
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
41
+ typeof value === "object" && value !== null && !Array.isArray(value);
42
+
43
+ const readSettings = (path: string, read: (source: string) => unknown): unknown => {
44
+ try {
45
+ return read(readFileSync(path, "utf8"));
46
+ } catch {
47
+ return undefined;
48
+ }
49
+ };
50
+
51
+ /**
52
+ * Writes `contents` to a new file at `path` in place of a file or symlink there, keeping the permissions of a
53
+ * replaced file, such as a private `.npmrc` that holds registry credentials.
54
+ */
55
+ const replaceFile = (path: string, contents: string): void => {
56
+ const replaced = lstatSync(path, { throwIfNoEntry: false });
57
+
58
+ rmSync(path, { force: true });
59
+ writeFileSync(path, contents, { mode: replaced?.isFile() ? replaced.mode & 0o777 : undefined });
60
+ };
61
+
62
+ const updateYamlFile = (path: string, update: (document: Document) => void): void => {
63
+ const document = parseDocument(existsSync(path) ? readFileSync(path, "utf8") : "");
64
+ update(document);
65
+ replaceFile(path, document.toString());
66
+ };
67
+
68
+ const missingExemptions = (exempted: unknown): string[] =>
69
+ RELEASE_AGE_EXEMPT_PACKAGES.filter((name) => !Array.isArray(exempted) || !exempted.includes(name));
70
+
71
+ const addReleaseAgeExemptions = (document: Document, key: string): void => {
72
+ const current = document.get(key);
73
+ const missing = missingExemptions(isSeq(current) ? current.toJSON() : []);
74
+
75
+ if (!isSeq(current)) {
76
+ document.set(key, document.createNode([]));
77
+ }
78
+
79
+ for (const name of missing) {
80
+ document.addIn([key], name);
81
+ }
82
+ };
83
+
84
+ const npmExemptions = (source: string): string[] =>
85
+ source.split(/\r?\n/).flatMap((line) => {
86
+ const value = NPM_RELEASE_AGE_EXCLUDE_PATTERN.exec(line)?.[2];
87
+
88
+ return value === undefined ? [] : [value];
89
+ });
90
+
91
+ const npmExemptionLines = (names: string[]): string =>
92
+ names.map((name) => `${NPM_RELEASE_AGE_EXCLUDE}=${name}`).join("\n");
93
+
94
+ const writeNpmExemptions = (root: string): string[] => {
95
+ const path = join(root, NPM_CONFIG_FILE);
96
+ const source = existsSync(path) ? readFileSync(path, "utf8") : "";
97
+ const missing = missingExemptions(npmExemptions(source));
98
+
99
+ if (missing.length === 0) {
100
+ return [];
101
+ }
102
+
103
+ const separator = source === "" || source.endsWith("\n") ? "" : "\n";
104
+ replaceFile(path, `${source}${separator}${npmExemptionLines(missing)}\n`);
105
+
106
+ return [path];
107
+ };
108
+
109
+ const writeYamlExemptions = (path: string, key: string): string[] => {
110
+ const settings = readSettings(path, parse);
111
+
112
+ if (missingExemptions(isRecord(settings) ? settings[key] : undefined).length === 0) {
113
+ return [];
114
+ }
115
+
116
+ updateYamlFile(path, (document) => addReleaseAgeExemptions(document, key));
10
117
 
11
- const quoteYamlKey = (name: string): string => (SAFE_YAML_KEY.test(name) ? name : `'${name}'`);
118
+ return [path];
119
+ };
120
+
121
+ const reportedYarnRelease = (root: string): string | null => {
122
+ const corepack = tryResolveExecutable("corepack");
123
+ const command = corepack ?? tryResolveExecutable("yarn");
124
+
125
+ if (command === undefined) {
126
+ return null;
127
+ }
128
+
129
+ try {
130
+ return execFileSync(command, corepack === undefined ? ["--version"] : ["yarn", "--version"], {
131
+ cwd: root,
132
+ encoding: "utf8",
133
+ stdio: ["ignore", "pipe", "ignore"],
134
+ }).trim();
135
+ } catch {
136
+ return null;
137
+ }
138
+ };
139
+
140
+ /**
141
+ * The Yarn release that installs a project without a pin at `root`: the Yarn running the scaffold, otherwise the
142
+ * one `yarn --version` reports there through Corepack.
143
+ */
144
+ const installingYarnRelease = (root: string): string | null =>
145
+ YARN_USER_AGENT_PATTERN.exec(process.env.npm_config_user_agent ?? "")?.[1] ?? reportedYarnRelease(root);
146
+
147
+ /**
148
+ * The Yarn release that `yarn` runs in the project or workspace at `root`: the one `yarn --version` reports
149
+ * there when its `.yarnrc.yml` sets `yarnPath`, which overrides a pin, otherwise the release its
150
+ * `packageManager` field pins, otherwise the Yarn running the scaffold, otherwise the one `yarn --version`
151
+ * reports there through Corepack.
152
+ */
153
+ const yarnRelease = (root: string): string | null => {
154
+ const settings = readSettings(join(root, YARN_CONFIG_FILE), parse);
155
+
156
+ if (isRecord(settings) && typeof settings.yarnPath === "string") {
157
+ return reportedYarnRelease(root);
158
+ }
159
+
160
+ const manifest = readSettings(join(root, PACKAGE_JSON_FILE), JSON.parse);
161
+ const pin = isRecord(manifest) && typeof manifest.packageManager === "string" ? manifest.packageManager : "";
162
+
163
+ if (pin.startsWith(YARN_PIN_PREFIX)) {
164
+ return pin.slice(YARN_PIN_PREFIX.length);
165
+ }
166
+
167
+ return installingYarnRelease(root);
168
+ };
169
+
170
+ /**
171
+ * Whether the Yarn installing the project at `root` reads `npmPreapprovedPackages`. Yarn 4.10 added it next to
172
+ * its release-age gate, and older releases refuse to run with a setting they do not know.
173
+ */
174
+ const readsPreapprovedPackages = (root: string): boolean => {
175
+ const matched = YARN_RELEASE_PATTERN.exec(yarnRelease(root) ?? "");
176
+ const major = Number(matched?.[1]);
177
+ const minor = Number(matched?.[2]);
178
+
179
+ return (
180
+ major > FIRST_PREAPPROVING_YARN.major ||
181
+ (major === FIRST_PREAPPROVING_YARN.major && minor >= FIRST_PREAPPROVING_YARN.minor)
182
+ );
183
+ };
12
184
 
13
185
  const writePnpmAllowance = (root: string): void => {
14
- const entries = BUILT_DEPENDENCIES.map((name) => ` ${quoteYamlKey(name)}: true`).join("\n");
15
- writeFileSync(join(root, PNPM_WORKSPACE_FILE), `${PNPM_PACKAGES_BLOCK}allowBuilds:\n${entries}\n`);
186
+ updateYamlFile(join(root, PNPM_WORKSPACE_FILE), (document) => {
187
+ if (!document.has("packages")) {
188
+ document.set("packages", document.createNode(["."]));
189
+ }
190
+
191
+ if (!isMap(document.get("allowBuilds"))) {
192
+ document.set("allowBuilds", document.createNode({}));
193
+ }
194
+
195
+ for (const name of BUILT_DEPENDENCIES) {
196
+ document.setIn(["allowBuilds", name], true);
197
+ }
198
+
199
+ addReleaseAgeExemptions(document, PNPM_RELEASE_AGE_EXCLUDE);
200
+ });
16
201
  };
17
202
 
18
203
  const writeNpmAllowance = (root: string): void => {
19
204
  updateManifest(root, (manifest) => {
20
205
  manifest.allowScripts = Object.fromEntries(BUILT_DEPENDENCIES.map((name) => [name, true]));
21
206
  });
207
+ writeNpmExemptions(root);
22
208
  };
23
209
 
24
210
  const writeYarnAllowance = (root: string): void => {
211
+ const isPreapproving = readsPreapprovedPackages(root);
212
+
213
+ updateYamlFile(join(root, YARN_CONFIG_FILE), (document) => {
214
+ document.set("nodeLinker", YARN_NODE_LINKER);
215
+
216
+ if (isPreapproving) {
217
+ addReleaseAgeExemptions(document, YARN_PREAPPROVED_PACKAGES);
218
+ }
219
+ });
25
220
  updateManifest(root, (manifest) => {
26
221
  manifest.dependenciesMeta = Object.fromEntries(BUILT_DEPENDENCIES.map((name) => [name, { built: true }]));
27
222
  });
28
223
  };
29
224
 
30
- const writeBuildAllowance = (root: string, packageManager: PackageManager): void => {
225
+ /**
226
+ * Configures the package manager to run the build scripts of the scaffold's native dependencies and to exempt
227
+ * GTKX's own packages from its release-age gate, so a project created on the day of a GTKX release installs it.
228
+ * Adds to an existing `pnpm-workspace.yaml`, `.yarnrc.yml` or `.npmrc` instead of replacing it.
229
+ */
230
+ const writePackageManagerSettings = (root: string, packageManager: PackageManager): void => {
31
231
  if (packageManager === "pnpm") {
32
232
  writePnpmAllowance(root);
33
233
 
@@ -43,4 +243,281 @@ const writeBuildAllowance = (root: string, packageManager: PackageManager): void
43
243
  writeNpmAllowance(root);
44
244
  };
45
245
 
46
- export { writeBuildAllowance };
246
+ const RELEASE_AGE_EXEMPTION_WRITERS: Record<PackageManager, (root: string) => string[]> = {
247
+ pnpm: (root) => writeYamlExemptions(join(root, PNPM_WORKSPACE_FILE), PNPM_RELEASE_AGE_EXCLUDE),
248
+ yarn: (root) =>
249
+ readsPreapprovedPackages(root)
250
+ ? writeYamlExemptions(join(root, YARN_CONFIG_FILE), YARN_PREAPPROVED_PACKAGES)
251
+ : [],
252
+ npm: writeNpmExemptions,
253
+ };
254
+
255
+ /**
256
+ * Exempts GTKX's own packages from the release-age gate of the package manager that installs the project or
257
+ * workspace at `root`, adding only the entries its `pnpm-workspace.yaml`, `.yarnrc.yml` (Yarn 4.10 or newer)
258
+ * or `.npmrc` lacks and keeping everything else in it, and returns the files it changed.
259
+ */
260
+ const writeReleaseAgeExemptions = (root: string, packageManager: PackageManager): string[] =>
261
+ RELEASE_AGE_EXEMPTION_WRITERS[packageManager](root);
262
+
263
+ const PACKAGE_MANAGER_CONFIG_FILES: Record<PackageManager, string[]> = {
264
+ pnpm: [PNPM_WORKSPACE_FILE],
265
+ yarn: [YARN_CONFIG_FILE],
266
+ npm: [NPM_CONFIG_FILE],
267
+ };
268
+
269
+ /**
270
+ * The `packageManager` field of the project's existing `package.json` when it names `packageManager`, so
271
+ * replacing the manifest keeps the version the project pins.
272
+ */
273
+ const pinnedPackageManager = (root: string, packageManager: PackageManager): string | null => {
274
+ const path = join(root, PACKAGE_JSON_FILE);
275
+
276
+ if (!existsSync(path)) {
277
+ return null;
278
+ }
279
+
280
+ try {
281
+ const manifest: unknown = JSON.parse(readFileSync(path, "utf8"));
282
+ const pin = isRecord(manifest) ? manifest.packageManager : undefined;
283
+
284
+ return typeof pin === "string" && pin.startsWith(`${packageManager}@`) ? pin : null;
285
+ } catch {
286
+ return null;
287
+ }
288
+ };
289
+
290
+ const nearestDirectory = (path: string): string =>
291
+ existsSync(path) || dirname(path) === path ? path : nearestDirectory(dirname(path));
292
+
293
+ /**
294
+ * The `packageManager` pin for the Yarn 2 or newer release that installs a new project at `root`, as
295
+ * `yarn init -2` writes it, or null when that is Yarn 1. The project may not exist yet, so Corepack is asked in
296
+ * its nearest existing directory, which resolves the same release for a project without a pin.
297
+ */
298
+ const installingYarnPin = (root: string): string | null => {
299
+ const release = installingYarnRelease(nearestDirectory(root));
300
+ const major = Number(YARN_RELEASE_PATTERN.exec(release ?? "")?.[1]);
301
+
302
+ return release !== null && major >= FIRST_PINNED_YARN_MAJOR ? `${YARN_PIN_PREFIX}${release}` : null;
303
+ };
304
+
305
+ /**
306
+ * The `packageManager` field for the new `package.json`: the existing project's pin when it names
307
+ * `packageManager`, otherwise, outside an enclosing workspace whose root owns the pin, the Yarn 2 or newer
308
+ * release that installs the project.
309
+ */
310
+ const packageManagerPin = (root: string, packageManager: PackageManager, isInWorkspace: boolean): string | null =>
311
+ pinnedPackageManager(root, packageManager) ??
312
+ (packageManager === "yarn" && !isInWorkspace ? installingYarnPin(root) : null);
313
+
314
+ /**
315
+ * The workspace globs of `field`, given as a list or, as npm and Yarn also accept, as an object with a
316
+ * `packages` list.
317
+ */
318
+ const workspacePatterns = (settings: unknown, field: string): string[] => {
319
+ const value = isRecord(settings) ? settings[field] : undefined;
320
+ const patterns = isRecord(value) ? value.packages : value;
321
+
322
+ return Array.isArray(patterns) ? patterns.filter((pattern) => typeof pattern === "string") : [];
323
+ };
324
+
325
+ const matchesWorkspacePattern = (project: string, pattern: string): boolean =>
326
+ matchesGlob(project, posix.normalize(pattern).replace(/\/+$/, ""));
327
+
328
+ const includesProject = (patterns: string[], project: string): boolean =>
329
+ patterns.some((pattern) => !pattern.startsWith(NEGATION_PREFIX) && matchesWorkspacePattern(project, pattern)) &&
330
+ !patterns.some(
331
+ (pattern) =>
332
+ pattern.startsWith(NEGATION_PREFIX) &&
333
+ matchesWorkspacePattern(project, pattern.slice(NEGATION_PREFIX.length)),
334
+ );
335
+
336
+ const ancestorFiles = (root: string, name: string): string[] => {
337
+ const files: string[] = [];
338
+
339
+ for (let directory = dirname(root); ; directory = dirname(directory)) {
340
+ const file = join(directory, name);
341
+
342
+ if (existsSync(file)) {
343
+ files.push(file);
344
+ }
345
+
346
+ if (dirname(directory) === directory) {
347
+ return files;
348
+ }
349
+ }
350
+ };
351
+
352
+ const projectPath = (file: string, root: string): string => relative(dirname(file), root).split(sep).join(posix.sep);
353
+
354
+ const recordField = (settings: unknown, field: string): Record<string, unknown> =>
355
+ isRecord(settings) && isRecord(settings[field]) ? settings[field] : {};
356
+
357
+ const missingBuilds = (allowances: Record<string, unknown>): string[] =>
358
+ BUILT_DEPENDENCIES.filter((name) => !Object.hasOwn(allowances, name));
359
+
360
+ const settingsToAdd = (settings: (MissingSetting | null)[]): MissingSetting[] =>
361
+ settings.filter((setting) => setting !== null);
362
+
363
+ const missingYamlExemptions = (file: string, settings: unknown, key: string): MissingSetting | null => {
364
+ const listed = isRecord(settings) ? settings[key] : undefined;
365
+ const missing = missingExemptions(listed);
366
+
367
+ if (missing.length === 0) {
368
+ return null;
369
+ }
370
+
371
+ return Array.isArray(listed)
372
+ ? { file, section: key, entries: stringify(missing).trimEnd().replace(/^/gm, " ") }
373
+ : { file, section: null, entries: stringify({ [key]: missing }).trimEnd() };
374
+ };
375
+
376
+ const pnpmWorkspaceAt = (file: string, project: string): Workspace | null => {
377
+ const settings: unknown = parse(readFileSync(file, "utf8"));
378
+
379
+ if (!includesProject(workspacePatterns(settings, "packages"), project)) {
380
+ return null;
381
+ }
382
+
383
+ const allowances = recordField(settings, "allowBuilds");
384
+ const missing = BUILT_DEPENDENCIES.filter((name) => typeof allowances[name] !== "boolean");
385
+ const required = Object.fromEntries(missing.map((name) => [name, true]));
386
+ const setting: MissingSetting =
387
+ isRecord(settings) && isRecord(settings.allowBuilds)
388
+ ? { file, section: "allowBuilds", entries: stringify(required).trimEnd().replace(/^/gm, " ") }
389
+ : { file, section: null, entries: stringify({ allowBuilds: required }).trimEnd() };
390
+
391
+ return {
392
+ packageManager: "pnpm",
393
+ root: dirname(file),
394
+ member: project,
395
+ missingSettings: settingsToAdd([
396
+ missing.length > 0 ? setting : null,
397
+ missingYamlExemptions(file, settings, PNPM_RELEASE_AGE_EXCLUDE),
398
+ ]),
399
+ };
400
+ };
401
+
402
+ const npmWorkspaceAt = (file: string, project: string): Workspace | null => {
403
+ const manifest: unknown = JSON.parse(readFileSync(file, "utf8"));
404
+
405
+ if (!includesProject(workspacePatterns(manifest, "workspaces"), project)) {
406
+ return null;
407
+ }
408
+
409
+ const missing = missingBuilds(recordField(manifest, "allowScripts"));
410
+ const entries = `"allowScripts": ${JSON.stringify(Object.fromEntries(missing.map((name) => [name, true])), null, 4)}`;
411
+ const configFile = join(dirname(file), NPM_CONFIG_FILE);
412
+ const exemptions = missingExemptions(npmExemptions(existsSync(configFile) ? readFileSync(configFile, "utf8") : ""));
413
+
414
+ return {
415
+ packageManager: "npm",
416
+ root: dirname(file),
417
+ member: project,
418
+ missingSettings: settingsToAdd([
419
+ missing.length > 0 ? { file, section: null, entries } : null,
420
+ exemptions.length > 0 ? { file: configFile, section: null, entries: npmExemptionLines(exemptions) } : null,
421
+ ]),
422
+ };
423
+ };
424
+
425
+ /**
426
+ * The `nodeLinker` Yarn applies in `directory`: Yarn merges the `.yarnrc.yml` files of a project and its
427
+ * ancestors, and the closest one that sets a value wins.
428
+ */
429
+ const yarnNodeLinker = (directory: string): unknown => {
430
+ for (const file of [join(directory, YARN_CONFIG_FILE), ...ancestorFiles(directory, YARN_CONFIG_FILE)]) {
431
+ const settings: unknown = existsSync(file) ? parse(readFileSync(file, "utf8")) : undefined;
432
+
433
+ if (isRecord(settings) && Object.hasOwn(settings, "nodeLinker")) {
434
+ return settings.nodeLinker;
435
+ }
436
+ }
437
+
438
+ return undefined;
439
+ };
440
+
441
+ const yarnWorkspaceAt = (file: string, project: string): Workspace | null => {
442
+ const manifest: unknown = JSON.parse(readFileSync(file, "utf8"));
443
+
444
+ if (!includesProject(workspacePatterns(manifest, "workspaces"), project)) {
445
+ return null;
446
+ }
447
+
448
+ const missing = missingBuilds(recordField(manifest, "dependenciesMeta"));
449
+ const dependenciesMeta = Object.fromEntries(missing.map((name) => [name, { built: true }]));
450
+ const configFile = join(dirname(file), YARN_CONFIG_FILE);
451
+ const exemptions = readsPreapprovedPackages(dirname(file))
452
+ ? missingYamlExemptions(configFile, readSettings(configFile, parse), YARN_PREAPPROVED_PACKAGES)
453
+ : null;
454
+
455
+ return {
456
+ packageManager: "yarn",
457
+ root: dirname(file),
458
+ member: project,
459
+ missingSettings: settingsToAdd([
460
+ yarnNodeLinker(dirname(file)) === YARN_NODE_LINKER
461
+ ? null
462
+ : { file: configFile, section: null, entries: stringify({ nodeLinker: YARN_NODE_LINKER }).trimEnd() },
463
+ missing.length > 0
464
+ ? { file, section: null, entries: `"dependenciesMeta": ${JSON.stringify(dependenciesMeta, null, 4)}` }
465
+ : null,
466
+ exemptions,
467
+ ]),
468
+ };
469
+ };
470
+
471
+ const MANIFEST_WORKSPACES: Record<
472
+ Exclude<PackageManager, "pnpm">,
473
+ (file: string, project: string) => Workspace | null
474
+ > = {
475
+ npm: npmWorkspaceAt,
476
+ yarn: yarnWorkspaceAt,
477
+ };
478
+
479
+ /**
480
+ * The pnpm, npm or Yarn workspace above `root` that includes it, with the settings its root configuration
481
+ * still lacks for the app, or null when `root` is not part of an enclosing workspace. pnpm reads
482
+ * `allowBuilds` and `minimumReleaseAgeExclude`, npm `allowScripts` and `min-release-age-exclude`, and Yarn
483
+ * `nodeLinker`, `npmPreapprovedPackages` and `dependenciesMeta` only for the whole workspace, from its root.
484
+ */
485
+ const enclosingWorkspace = (root: string, packageManager: PackageManager): Workspace | null => {
486
+ if (packageManager === "pnpm") {
487
+ const [nearest] = ancestorFiles(root, PNPM_WORKSPACE_FILE);
488
+
489
+ return nearest === undefined ? null : pnpmWorkspaceAt(nearest, projectPath(nearest, root));
490
+ }
491
+
492
+ const workspaceAt = MANIFEST_WORKSPACES[packageManager];
493
+
494
+ for (const file of ancestorFiles(root, PACKAGE_JSON_FILE)) {
495
+ const workspace = workspaceAt(file, projectPath(file, root));
496
+
497
+ if (workspace !== null) {
498
+ return workspace;
499
+ }
500
+ }
501
+
502
+ return null;
503
+ };
504
+
505
+ /**
506
+ * Whether `yarn` in the workspace at `root` is Yarn 1, judged by the release it runs there, and Yarn 1 when no
507
+ * release can be determined.
508
+ */
509
+ const isClassicYarn = (root: string): boolean => {
510
+ const major = Number(YARN_RELEASE_PATTERN.exec(yarnRelease(root) ?? "")?.[1]);
511
+
512
+ return Number.isNaN(major) || major < FIRST_PINNED_YARN_MAJOR;
513
+ };
514
+
515
+ export {
516
+ PACKAGE_MANAGER_CONFIG_FILES,
517
+ enclosingWorkspace,
518
+ isClassicYarn,
519
+ packageManagerPin,
520
+ type Workspace,
521
+ writePackageManagerSettings,
522
+ writeReleaseAgeExemptions,
523
+ };
package/src/cli.ts CHANGED
@@ -9,7 +9,7 @@ const main = defineCommand({
9
9
  meta: {
10
10
  name: "create-gtkx",
11
11
  version,
12
- description: "Scaffold a new Adwaita-first GNOME application with GTKX",
12
+ description: "Scaffold a Linux application with GTKX",
13
13
  },
14
14
  });
15
15