create-gtkx 2.0.0-beta.10 → 2.0.0-beta.11

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/src/command.ts CHANGED
@@ -1,50 +1,99 @@
1
1
  import * as p from "@clack/prompts";
2
2
  import { assertSupportedNodeVersion } from "@gtkx/config/internal";
3
- import { errorMessage } from "@gtkx/utils";
4
- import { defineCommand } from "citty";
3
+ import { camelCase, errorMessage, kebabCase } from "@gtkx/utils";
4
+ import { type ArgsDef, defineCommand, type ParsedArgs } from "citty";
5
5
  import { parseArgs, type ParseArgsOptionsConfig } from "node:util";
6
6
  import { OperationCanceledError, ScaffoldAbortedError } from "./errors.js";
7
7
  import { PACKAGE_MANAGER_FLAG_DESCRIPTION } from "./package-managers.js";
8
8
  import { scaffold } from "./scaffolder.js";
9
9
 
10
- type CreateCommandArgs = {
11
- name?: string | undefined;
12
- "application-id"?: string | undefined;
13
- "display-name"?: string | undefined;
14
- "package-manager"?: string | undefined;
15
- typescript?: boolean | undefined;
16
- vitest?: boolean | undefined;
17
- yes?: boolean | undefined;
18
- "no-interactive"?: boolean | undefined;
19
- overwrite?: boolean | undefined;
20
- "skip-install"?: boolean | undefined;
21
- };
10
+ const CREATE_ARGS = {
11
+ name: {
12
+ type: "positional",
13
+ description: "Target directory or project name (e.g. my-app, ., apps/my-app)",
14
+ required: false,
15
+ },
16
+ "application-id": {
17
+ type: "string",
18
+ description: "Application ID (e.g., com.example.myapp)",
19
+ },
20
+ "display-name": {
21
+ type: "string",
22
+ description: "Application display name (e.g., My App)",
23
+ },
24
+ "package-manager": {
25
+ type: "string",
26
+ alias: "p",
27
+ description: PACKAGE_MANAGER_FLAG_DESCRIPTION,
28
+ },
29
+ typescript: {
30
+ type: "boolean",
31
+ negativeDescription: "Scaffold the application in JavaScript instead of TypeScript",
32
+ description: "Scaffold the application in TypeScript",
33
+ },
34
+ vitest: {
35
+ type: "boolean",
36
+ description: "Include a Vitest testing setup",
37
+ },
38
+ yes: {
39
+ type: "boolean",
40
+ alias: "y",
41
+ description: "Skip prompts and accept defaults for unspecified options",
42
+ },
43
+ interactive: {
44
+ type: "boolean",
45
+ description: "Prompt for unspecified options when running in a terminal",
46
+ negativeDescription:
47
+ "Run without prompts, using the default for every option not passed on the command line " +
48
+ "(same as --yes)",
49
+ },
50
+ overwrite: {
51
+ type: "boolean",
52
+ alias: "f",
53
+ description: "Replace scaffold files in a non-empty target directory when running without prompts",
54
+ },
55
+ "skip-install": {
56
+ type: "boolean",
57
+ description: "Create the project without installing dependencies",
58
+ },
59
+ } as const satisfies ArgsDef;
60
+
61
+ type OptionToken = Extract<
62
+ ReturnType<typeof parseArgs<{ tokens: true; strict: false }>>["tokens"][number],
63
+ { kind: "option" }
64
+ >;
65
+
66
+ type CreateCommandArgs = Pick<ParsedArgs<typeof CREATE_ARGS>, keyof typeof CREATE_ARGS>;
67
+ type CreateOption = Exclude<(typeof CREATE_ARGS)[keyof typeof CREATE_ARGS], { type: "positional" }>;
22
68
 
23
- const STRICT_OPTIONS: ParseArgsOptionsConfig = {
24
- "application-id": { type: "string" },
25
- applicationId: { type: "string" },
26
- "display-name": { type: "string" },
27
- displayName: { type: "string" },
28
- "package-manager": { type: "string", short: "p" },
29
- packageManager: { type: "string" },
30
- typescript: { type: "boolean" },
31
- vitest: { type: "boolean" },
32
- yes: { type: "boolean", short: "y" },
33
- "no-interactive": { type: "boolean" },
34
- noInteractive: { type: "boolean" },
35
- overwrite: { type: "boolean", short: "f" },
36
- "skip-install": { type: "boolean" },
37
- skipInstall: { type: "boolean" },
69
+ const strictOption = (definition: CreateOption): ParseArgsOptionsConfig[string] => ({
70
+ type: definition.type,
71
+ ...("alias" in definition && { short: definition.alias }),
72
+ });
73
+
74
+ const createStrictOptions = (): ParseArgsOptionsConfig => {
75
+ const options: ParseArgsOptionsConfig = {};
76
+
77
+ for (const [name, definition] of Object.entries(CREATE_ARGS)) {
78
+ if (definition.type === "positional") {
79
+ continue;
80
+ }
81
+
82
+ options[name] = strictOption(definition);
83
+
84
+ if (camelCase(name) !== name) {
85
+ options[camelCase(name)] = { type: definition.type };
86
+ }
87
+ }
88
+
89
+ return options;
38
90
  };
39
91
 
40
- const BOOLEAN_OPTIONS: ReadonlySet<string> = new Set([
41
- "--typescript",
42
- "--vitest",
43
- "--yes",
44
- "--overwrite",
45
- "--skip-install",
46
- "--skipInstall",
47
- ]);
92
+ const STRICT_OPTIONS = createStrictOptions();
93
+
94
+ const BOOLEAN_OPTIONS = new Set(
95
+ Object.entries(STRICT_OPTIONS).filter(([, option]) => option.type === "boolean").map(([name]) => `--${name}`),
96
+ );
48
97
 
49
98
  const normalizeBooleanArgument = (argument: string): string => {
50
99
  const [option, value, ...rest] = argument.split("=");
@@ -60,89 +109,40 @@ const normalizeBooleanArgument = (argument: string): string => {
60
109
  return value === "false" ? `--no-${option.slice(2)}` : argument;
61
110
  };
62
111
 
63
- const isKnownOption = (name: string, rawName: string): boolean =>
64
- Object.hasOwn(STRICT_OPTIONS, name) || Object.hasOwn(STRICT_OPTIONS, rawName.replace(/^--/, ""));
112
+ const canonicalOption = (token: OptionToken): [string, string | boolean] =>
113
+ [kebabCase(token.name), token.value ?? !token.rawName.startsWith("--no-")];
65
114
 
66
- const unknownOptions = (args: string[]): string[] => {
67
- const { tokens } = parseArgs({
68
- args,
115
+ /* TODO: Keep ordered token parsing until Citty respects the order of positive and negated flags.
116
+ * https://github.com/gtkx-org/gtkx/issues/732
117
+ */
118
+ const parseCreateArguments = (rawArgs: string[]): CreateCommandArgs => {
119
+ const { tokens, positionals } = parseArgs({
120
+ args: rawArgs.map((argument) =>
121
+ normalizeBooleanArgument(argument === "--noInteractive" ? "--no-interactive" : argument)),
69
122
  options: STRICT_OPTIONS,
70
123
  allowPositionals: true,
71
124
  allowNegative: true,
72
- strict: false,
125
+ strict: true,
73
126
  tokens: true,
74
127
  });
75
128
 
76
- return tokens.flatMap((token) =>
77
- token.kind === "option" && !isKnownOption(token.name, token.rawName) ? [token.rawName] : []);
78
- };
79
-
80
- const assertKnownArguments = (rawArgs: string[]): void => {
81
- const args = rawArgs.map((argument) => normalizeBooleanArgument(argument));
82
- const [unknown] = unknownOptions(args);
83
-
84
- if (unknown !== undefined) {
85
- throw new Error(`Unknown option "${unknown}". Pass --help to list the supported options`);
129
+ if (positionals.length > 1) {
130
+ throw new TypeError("Expected one project directory");
86
131
  }
87
132
 
88
- parseArgs({ args, options: STRICT_OPTIONS, allowPositionals: true, allowNegative: true, strict: true });
133
+ return {
134
+ ...Object.fromEntries(tokens.filter((token) => token.kind === "option").map((token) => canonicalOption(token))),
135
+ name: positionals[0],
136
+ } as CreateCommandArgs;
89
137
  };
90
138
 
91
139
  const scaffoldCommand = defineCommand({
92
140
  meta: {
93
141
  name: "create",
94
- description: "Create a new Adwaita-first GNOME application with GTKX",
95
- },
96
- args: {
97
- name: {
98
- type: "positional",
99
- description: "Target directory or project name (e.g. my-app, ., apps/my-app)",
100
- required: false,
101
- },
102
- "application-id": {
103
- type: "string",
104
- description: "Application ID (e.g., com.example.myapp)",
105
- },
106
- "display-name": {
107
- type: "string",
108
- description: "Application display name (e.g., My App)",
109
- },
110
- "package-manager": {
111
- type: "string",
112
- alias: "p",
113
- description: PACKAGE_MANAGER_FLAG_DESCRIPTION,
114
- },
115
- typescript: {
116
- type: "boolean",
117
- negativeDescription: "Scaffold the application in JavaScript instead of TypeScript",
118
- description: "Scaffold the application in TypeScript",
119
- },
120
- vitest: {
121
- type: "boolean",
122
- description: "Include a Vitest testing setup",
123
- },
124
- yes: {
125
- type: "boolean",
126
- alias: "y",
127
- description: "Skip prompts and accept defaults for unspecified options",
128
- },
129
- "no-interactive": {
130
- type: "boolean",
131
- description:
132
- "Run without prompts, using the default for every option not passed on the command line " +
133
- "(same as --yes)",
134
- },
135
- overwrite: {
136
- type: "boolean",
137
- alias: "f",
138
- description: "Replace scaffold files in a non-empty target directory when running without prompts",
139
- },
140
- "skip-install": {
141
- type: "boolean",
142
- description: "Create the project without installing dependencies",
143
- },
142
+ description: "Create a Linux application with GTKX",
144
143
  },
145
- run: ({ args, rawArgs }) => runCreate(args, rawArgs),
144
+ args: CREATE_ARGS,
145
+ run: ({ rawArgs }) => runCreate(rawArgs),
146
146
  });
147
147
 
148
148
  const settleScaffoldFailure = (error: unknown): void => {
@@ -157,11 +157,10 @@ const settleScaffoldFailure = (error: unknown): void => {
157
157
  process.exitCode = 1;
158
158
  };
159
159
 
160
- const runCreate = async (args: CreateCommandArgs, rawArgs: string[]): Promise<void> => {
161
- const isInteractive = args["no-interactive"] || args.yes ? false : process.stdin.isTTY;
162
-
160
+ const runCreate = async (rawArgs: string[]): Promise<void> => {
163
161
  try {
164
- assertKnownArguments(rawArgs);
162
+ const args = parseCreateArguments(rawArgs);
163
+ const isInteractive = args.interactive !== false && !args.yes && process.stdin.isTTY;
165
164
  assertSupportedNodeVersion();
166
165
  await scaffold({
167
166
  name: args.name,
package/src/manifest.ts CHANGED
@@ -1,5 +1,4 @@
1
- import { randomUUID } from "node:crypto";
2
- import { readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
1
+ import { readFileSync, writeFileSync } from "node:fs";
3
2
  import { join } from "node:path";
4
3
 
5
4
  const PACKAGE_JSON_FILE = "package.json";
@@ -8,14 +7,7 @@ const updateManifest = (root: string, mutate: (manifest: Record<string, unknown>
8
7
  const manifestPath = join(root, PACKAGE_JSON_FILE);
9
8
  const manifest = JSON.parse(readFileSync(manifestPath, "utf8")) as Record<string, unknown>;
10
9
  mutate(manifest);
11
- const pendingPath = join(root, `.package-${randomUUID()}.json`);
12
-
13
- try {
14
- writeFileSync(pendingPath, `${JSON.stringify(manifest, null, 4)}\n`, { flag: "wx", mode: 0o600 });
15
- renameSync(pendingPath, manifestPath);
16
- } finally {
17
- rmSync(pendingPath, { force: true });
18
- }
10
+ writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 4)}\n`);
19
11
  };
20
12
 
21
13
  export { updateManifest };
@@ -5,7 +5,6 @@ const PACKAGE_MANAGERS = [
5
5
  value: "pnpm",
6
6
  label: "pnpm",
7
7
  devCommand: "pnpm dev",
8
- addCommand: "pnpm add",
9
8
  installCommand: "pnpm install",
10
9
  isRecommended: true,
11
10
  },
@@ -13,7 +12,6 @@ const PACKAGE_MANAGERS = [
13
12
  value: "npm",
14
13
  label: "npm",
15
14
  devCommand: "npm run dev",
16
- addCommand: "npm install",
17
15
  installCommand: "npm install",
18
16
  isRecommended: false,
19
17
  },
@@ -21,7 +19,6 @@ const PACKAGE_MANAGERS = [
21
19
  value: "yarn",
22
20
  label: "yarn",
23
21
  devCommand: "yarn dev",
24
- addCommand: "yarn add",
25
22
  installCommand: "yarn install",
26
23
  isRecommended: false,
27
24
  },