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.
Files changed (55) hide show
  1. package/README.md +5 -5
  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 -10
  6. package/dist/build-allowance.js.map +1 -1
  7. package/dist/command.d.ts.map +1 -1
  8. package/dist/command.js +7 -2
  9. package/dist/command.js.map +1 -1
  10. package/dist/errors.d.ts.map +1 -1
  11. package/dist/index.d.ts +1 -0
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +1 -0
  14. package/dist/index.js.map +1 -1
  15. package/dist/manifest.d.ts.map +1 -1
  16. package/dist/package-managers.d.ts +6 -0
  17. package/dist/package-managers.d.ts.map +1 -1
  18. package/dist/package-managers.js +6 -0
  19. package/dist/package-managers.js.map +1 -1
  20. package/dist/project-name.d.ts +4 -2
  21. package/dist/project-name.d.ts.map +1 -1
  22. package/dist/project-name.js +12 -2
  23. package/dist/project-name.js.map +1 -1
  24. package/dist/scaffolder.d.ts.map +1 -1
  25. package/dist/scaffolder.js +181 -72
  26. package/dist/scaffolder.js.map +1 -1
  27. package/dist/templates/.claude/settings.json.ejs +4 -3
  28. package/dist/templates/.gitignore.ejs +3 -0
  29. package/dist/templates/.mcp.json.ejs +1 -1
  30. package/dist/templates/src/app.tsx.ejs +1 -1
  31. package/dist/templates/src/gtkx-env.d.ts.ejs +0 -1
  32. package/dist/templates/tsconfig.json.ejs +3 -2
  33. package/dist/templates.d.ts +3 -0
  34. package/dist/templates.d.ts.map +1 -1
  35. package/dist/templates.js +10 -6
  36. package/dist/templates.js.map +1 -1
  37. package/dist/workspace-mcp.d.ts +33 -0
  38. package/dist/workspace-mcp.d.ts.map +1 -0
  39. package/dist/workspace-mcp.js +75 -0
  40. package/dist/workspace-mcp.js.map +1 -0
  41. package/package.json +16 -47
  42. package/src/build-allowance.ts +488 -10
  43. package/src/command.ts +9 -4
  44. package/src/index.ts +1 -0
  45. package/src/package-managers.ts +6 -0
  46. package/src/project-name.ts +19 -2
  47. package/src/scaffolder.ts +278 -94
  48. package/src/templates/.claude/settings.json.ejs +4 -3
  49. package/src/templates/.gitignore.ejs +3 -0
  50. package/src/templates/.mcp.json.ejs +1 -1
  51. package/src/templates/src/app.tsx.ejs +1 -1
  52. package/src/templates/src/gtkx-env.d.ts.ejs +0 -1
  53. package/src/templates/tsconfig.json.ejs +3 -2
  54. package/src/templates.ts +17 -8
  55. package/src/workspace-mcp.ts +111 -0
@@ -1,32 +1,233 @@
1
- import { writeFileSync } from "node:fs";
2
- import { join } from "node:path";
3
- import { stringify } from "yaml";
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";
4
6
  import type { PackageManager } from "./package-managers.js";
5
7
  import { updateManifest } from "./manifest.js";
6
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
+
7
22
  const BUILT_DEPENDENCIES = ["@swc/core", "esbuild"];
23
+ const RELEASE_AGE_EXEMPT_PACKAGES = ["@gtkx/*", "create-gtkx"];
24
+ const YARN_NODE_LINKER = "node-modules";
8
25
  const PNPM_WORKSPACE_FILE = "pnpm-workspace.yaml";
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));
117
+
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
+ };
184
+
9
185
  const writePnpmAllowance = (root: string): void => {
10
- const workspace = {
11
- packages: ["."],
12
- allowBuilds: Object.fromEntries(BUILT_DEPENDENCIES.map((name) => [name, true])),
13
- };
14
- writeFileSync(join(root, PNPM_WORKSPACE_FILE), stringify(workspace));
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
+ });
15
201
  };
16
202
 
17
203
  const writeNpmAllowance = (root: string): void => {
18
204
  updateManifest(root, (manifest) => {
19
205
  manifest.allowScripts = Object.fromEntries(BUILT_DEPENDENCIES.map((name) => [name, true]));
20
206
  });
207
+ writeNpmExemptions(root);
21
208
  };
22
209
 
23
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
+ });
24
220
  updateManifest(root, (manifest) => {
25
221
  manifest.dependenciesMeta = Object.fromEntries(BUILT_DEPENDENCIES.map((name) => [name, { built: true }]));
26
222
  });
27
223
  };
28
224
 
29
- 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 => {
30
231
  if (packageManager === "pnpm") {
31
232
  writePnpmAllowance(root);
32
233
 
@@ -42,4 +243,281 @@ const writeBuildAllowance = (root: string, packageManager: PackageManager): void
42
243
  writeNpmAllowance(root);
43
244
  };
44
245
 
45
- 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/command.ts CHANGED
@@ -92,7 +92,9 @@ const createStrictOptions = (): ParseArgsOptionsConfig => {
92
92
  const STRICT_OPTIONS = createStrictOptions();
93
93
 
94
94
  const BOOLEAN_OPTIONS = new Set(
95
- Object.entries(STRICT_OPTIONS).filter(([, option]) => option.type === "boolean").map(([name]) => `--${name}`),
95
+ Object.entries(STRICT_OPTIONS)
96
+ .filter(([, option]) => option.type === "boolean")
97
+ .map(([name]) => `--${name}`),
96
98
  );
97
99
 
98
100
  const normalizeBooleanArgument = (argument: string): string => {
@@ -109,8 +111,10 @@ const normalizeBooleanArgument = (argument: string): string => {
109
111
  return value === "false" ? `--no-${option.slice(2)}` : argument;
110
112
  };
111
113
 
112
- const canonicalOption = (token: OptionToken): [string, string | boolean] =>
113
- [kebabCase(token.name), token.value ?? !token.rawName.startsWith("--no-")];
114
+ const canonicalOption = (token: OptionToken): [string, string | boolean] => [
115
+ kebabCase(token.name),
116
+ token.value ?? !token.rawName.startsWith("--no-"),
117
+ ];
114
118
 
115
119
  /* TODO: Keep ordered token parsing until Citty respects the order of positive and negated flags.
116
120
  * https://github.com/gtkx-org/gtkx/issues/732
@@ -118,7 +122,8 @@ const canonicalOption = (token: OptionToken): [string, string | boolean] =>
118
122
  const parseCreateArguments = (rawArgs: string[]): CreateCommandArgs => {
119
123
  const { tokens, positionals } = parseArgs({
120
124
  args: rawArgs.map((argument) =>
121
- normalizeBooleanArgument(argument === "--noInteractive" ? "--no-interactive" : argument)),
125
+ normalizeBooleanArgument(argument === "--noInteractive" ? "--no-interactive" : argument),
126
+ ),
122
127
  options: STRICT_OPTIONS,
123
128
  allowPositionals: true,
124
129
  allowNegative: true,
package/src/index.ts CHANGED
@@ -1 +1,2 @@
1
+ export { writeReleaseAgeExemptions } from "./build-allowance.js";
1
2
  export { scaffoldCommand } from "./command.js";
@@ -6,6 +6,8 @@ const PACKAGE_MANAGERS = [
6
6
  label: "pnpm",
7
7
  devCommand: "pnpm dev",
8
8
  installCommand: "pnpm install",
9
+ runCommand: "pnpm",
10
+ testCommand: "pnpm test",
9
11
  isRecommended: true,
10
12
  },
11
13
  {
@@ -13,6 +15,8 @@ const PACKAGE_MANAGERS = [
13
15
  label: "npm",
14
16
  devCommand: "npm run dev",
15
17
  installCommand: "npm install",
18
+ runCommand: "npm run",
19
+ testCommand: "npm test",
16
20
  isRecommended: false,
17
21
  },
18
22
  {
@@ -20,6 +24,8 @@ const PACKAGE_MANAGERS = [
20
24
  label: "yarn",
21
25
  devCommand: "yarn dev",
22
26
  installCommand: "yarn install",
27
+ runCommand: "yarn",
28
+ testCommand: "yarn test",
23
29
  isRecommended: false,
24
30
  },
25
31
  ] as const;
@@ -1,3 +1,20 @@
1
- const isValidProjectName = (name: string): boolean => /^[a-z0-9-]+$/.test(name);
1
+ const PACKAGE_NAME_MAX_LENGTH = 214;
2
+ const PACKAGE_NAME_PATTERN = /^(?:@[a-z\d\-*~][a-z\d\-*._~]*\/)?[a-z\d\-~][a-z\d\-._~]*$/;
2
3
 
3
- export { isValidProjectName };
4
+ const withoutAccents = (name: string): string => name.normalize("NFKD").replaceAll(/\p{M}/gu, "");
5
+
6
+ const isValidPackageName = (name: string): boolean =>
7
+ name.length <= PACKAGE_NAME_MAX_LENGTH && PACKAGE_NAME_PATTERN.test(name);
8
+
9
+ const toValidPackageName = (name: string): string =>
10
+ withoutAccents(name)
11
+ .toLowerCase()
12
+ .replaceAll(/[^a-z0-9]+/g, "-")
13
+ .replaceAll(/^-+|-+$/g, "")
14
+ .slice(0, PACKAGE_NAME_MAX_LENGTH)
15
+ .replace(/-+$/, "");
16
+
17
+ const packageNameFromDirectory = (directoryName: string): string =>
18
+ isValidPackageName(directoryName) ? directoryName : toValidPackageName(directoryName);
19
+
20
+ export { isValidPackageName, packageNameFromDirectory, withoutAccents };