@kidd-cli/cli 0.2.0 → 0.3.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.
@@ -1,5 +1,6 @@
1
- import { n as isKebabCase, r as renderTemplate, t as writeFiles } from "../../write-CdoqLFeH.mjs";
2
- import { t as detectProject } from "../../detect-DcO0_CWy.mjs";
1
+ import { n as renderTemplate, t as writeFiles } from "../../write-BF8u0sRy.mjs";
2
+ import { t as isKebabCase } from "../../validate-D3mbQfJT.mjs";
3
+ import { t as detectProject } from "../../detect-Cxj1jrYq.mjs";
3
4
  import { command } from "@kidd-cli/core";
4
5
  import { join } from "node:path";
5
6
  import { loadConfig } from "@kidd-cli/config/loader";
@@ -48,7 +49,7 @@ const addCommandCommand = command({
48
49
  }
49
50
  ctx.spinner.stop("Command created!");
50
51
  const summary = [...result.written.map((file) => ` created ${file}`), ...result.skipped.map((file) => ` skipped ${file} (already exists)`)].join("\n");
51
- if (summary.length > 0) ctx.output.raw(summary);
52
+ if (summary.length > 0) ctx.logger.print(summary);
52
53
  }
53
54
  });
54
55
  /**
@@ -0,0 +1,6 @@
1
+ import { Command } from "@kidd-cli/core";
2
+
3
+ //#region src/commands/add/config.d.ts
4
+ declare const addConfigCommand: Command;
5
+ //#endregion
6
+ export { addConfigCommand as default };
@@ -0,0 +1,55 @@
1
+ import { n as renderTemplate, t as writeFiles } from "../../write-BF8u0sRy.mjs";
2
+ import { t as detectProject } from "../../detect-Cxj1jrYq.mjs";
3
+ import { command } from "@kidd-cli/core";
4
+ import { join } from "node:path";
5
+ import { readManifest } from "@kidd-cli/utils/manifest";
6
+ //#region src/commands/add/config.ts
7
+ const addConfigCommand = command({
8
+ description: "Add a config schema to your project",
9
+ handler: async (ctx) => {
10
+ const [detectError, project] = await detectProject(process.cwd());
11
+ if (detectError) return ctx.fail(detectError.message);
12
+ if (!project) return ctx.fail("Not in a kidd project. Run `kidd init` first.");
13
+ const projectName = await resolveProjectName(project.rootDir);
14
+ ctx.spinner.start("Generating config...");
15
+ const [renderError, rendered] = await renderTemplate({
16
+ templateDir: join(import.meta.dirname, "..", "..", "lib", "templates", "config"),
17
+ variables: { name: projectName }
18
+ });
19
+ if (renderError) {
20
+ ctx.spinner.stop("Failed");
21
+ return ctx.fail(renderError.message);
22
+ }
23
+ const [writeError, result] = await writeFiles({
24
+ files: rendered,
25
+ outputDir: join(project.rootDir, "src"),
26
+ overwrite: false
27
+ });
28
+ if (writeError) {
29
+ ctx.spinner.stop("Failed");
30
+ return ctx.fail(writeError.message);
31
+ }
32
+ ctx.spinner.stop("Config created!");
33
+ const summary = [...result.written.map((file) => ` created ${file}`), ...result.skipped.map((file) => ` skipped ${file} (already exists)`)].join("\n");
34
+ if (summary.length > 0) ctx.logger.print(summary);
35
+ ctx.logger.newline();
36
+ ctx.logger.print("Next steps:");
37
+ ctx.logger.print(" 1. Add fields to the config schema in src/config.ts");
38
+ ctx.logger.print(" 2. Import and pass the schema to cli({ config: { schema: configSchema } })");
39
+ }
40
+ });
41
+ const DEFAULT_NAME = "my-app";
42
+ /**
43
+ * Resolve the project name from the project's package.json.
44
+ *
45
+ * @param rootDir - The project root directory.
46
+ * @returns The project name from package.json, or a default fallback.
47
+ * @private
48
+ */
49
+ async function resolveProjectName(rootDir) {
50
+ const [error, manifest] = await readManifest(rootDir);
51
+ if (error || !manifest.name) return DEFAULT_NAME;
52
+ return manifest.name;
53
+ }
54
+ //#endregion
55
+ export { addConfigCommand as default };
@@ -1,5 +1,5 @@
1
1
  import { command } from "@kidd-cli/core";
2
2
  //#region src/commands/add/index.ts
3
- const addCommand = command({ description: "Add a command or middleware to your project" });
3
+ const addCommand = command({ description: "Add a command, middleware, or config to your project" });
4
4
  //#endregion
5
5
  export { addCommand as default };
@@ -1,5 +1,6 @@
1
- import { n as isKebabCase, r as renderTemplate, t as writeFiles } from "../../write-CdoqLFeH.mjs";
2
- import { t as detectProject } from "../../detect-DcO0_CWy.mjs";
1
+ import { n as renderTemplate, t as writeFiles } from "../../write-BF8u0sRy.mjs";
2
+ import { t as isKebabCase } from "../../validate-D3mbQfJT.mjs";
3
+ import { t as detectProject } from "../../detect-Cxj1jrYq.mjs";
3
4
  import { command } from "@kidd-cli/core";
4
5
  import { join } from "node:path";
5
6
  import { z } from "zod";
@@ -43,7 +44,7 @@ const addMiddlewareCommand = command({
43
44
  }
44
45
  ctx.spinner.stop("Middleware created!");
45
46
  const summary = [...result.written.map((file) => ` created ${file}`), ...result.skipped.map((file) => ` skipped ${file} (already exists)`)].join("\n");
46
- if (summary.length > 0) ctx.output.raw(summary);
47
+ if (summary.length > 0) ctx.logger.print(summary);
47
48
  }
48
49
  });
49
50
  /**
@@ -22,10 +22,10 @@ const commandsCommand = command({
22
22
  const tree = await buildTree(await autoload({ dir: commandsDir }));
23
23
  ctx.spinner.stop("Commands");
24
24
  if (tree.length === 0) {
25
- ctx.output.write("No commands found");
25
+ ctx.logger.print("No commands found");
26
26
  return;
27
27
  }
28
- ctx.output.raw(`${renderTree(tree)}\n`);
28
+ ctx.logger.print(renderTree(tree));
29
29
  }
30
30
  });
31
31
  /**
@@ -538,7 +538,7 @@ const doctorCommand = command({
538
538
  total: results.length,
539
539
  warnings
540
540
  });
541
- ctx.output.raw(summary);
541
+ ctx.logger.print(summary);
542
542
  if (failed > 0) ctx.fail(`${failed} ${pluralizeCheck(failed)} failed`);
543
543
  }
544
544
  });
@@ -607,7 +607,7 @@ async function applyFixes(results, context) {
607
607
  */
608
608
  function displayResults(ctx, results, fixResults) {
609
609
  const output = results.map((result) => formatResultLine(result, fixResults)).join("");
610
- if (output.length > 0) ctx.output.raw(output);
610
+ if (output.length > 0) ctx.logger.print(output);
611
611
  }
612
612
  /**
613
613
  * Format a single check result line with optional hint.
@@ -1,4 +1,5 @@
1
- import { n as isKebabCase, r as renderTemplate, t as writeFiles } from "../write-CdoqLFeH.mjs";
1
+ import { n as renderTemplate, t as writeFiles } from "../write-BF8u0sRy.mjs";
2
+ import { t as isKebabCase } from "../validate-D3mbQfJT.mjs";
2
3
  import { createRequire } from "node:module";
3
4
  import { command } from "@kidd-cli/core";
4
5
  import { dirname, join } from "node:path";
@@ -26,6 +27,7 @@ const TSDOWN_VERSION = "^0.21.1";
26
27
  //#region src/commands/init.ts
27
28
  const initCommand = command({
28
29
  args: z.object({
30
+ config: z.boolean().describe("Include config schema setup").optional(),
29
31
  description: z.string().describe("Project description").optional(),
30
32
  example: z.boolean().describe("Include example command").optional(),
31
33
  name: z.string().describe("Project name (kebab-case)").optional(),
@@ -41,6 +43,7 @@ const initCommand = command({
41
43
  const projectDescription = await resolveDescription(ctx);
42
44
  const packageManager = await resolvePackageManager(ctx);
43
45
  const includeExample = await resolveIncludeExample(ctx);
46
+ const includeConfig = await resolveIncludeConfig(ctx);
44
47
  ctx.spinner.start("Scaffolding project...");
45
48
  const coreVersion = await resolveDependencyVersion("@kidd-cli/core");
46
49
  const cliVersion = await resolveSelfVersion();
@@ -50,6 +53,7 @@ const initCommand = command({
50
53
  cliVersion,
51
54
  coreVersion,
52
55
  description: projectDescription,
56
+ includeConfig,
53
57
  name: projectName,
54
58
  packageManager,
55
59
  tsdownVersion: TSDOWN_VERSION,
@@ -63,7 +67,10 @@ const initCommand = command({
63
67
  return ctx.fail(renderError.message);
64
68
  }
65
69
  const [writeError] = await writeFiles({
66
- files: selectFiles(includeExample, rendered),
70
+ files: selectFiles({
71
+ includeConfig,
72
+ includeExample
73
+ }, rendered),
67
74
  outputDir: join(process.cwd(), projectName),
68
75
  overwrite: false
69
76
  });
@@ -72,10 +79,10 @@ const initCommand = command({
72
79
  return ctx.fail(writeError.message);
73
80
  }
74
81
  ctx.spinner.stop("Project created!");
75
- ctx.output.raw("");
76
- ctx.output.raw(`Next steps:`);
77
- ctx.output.raw(` cd ${projectName}`);
78
- ctx.output.raw(` ${packageManager} install`);
82
+ ctx.logger.newline();
83
+ ctx.logger.print("Next steps:");
84
+ ctx.logger.print(` cd ${projectName}`);
85
+ ctx.logger.print(` ${packageManager} install`);
79
86
  }
80
87
  });
81
88
  /**
@@ -155,26 +162,29 @@ async function resolveIncludeExample(ctx) {
155
162
  });
156
163
  }
157
164
  /**
158
- * Select the rendered files to write, optionally excluding the example command.
165
+ * Resolve whether to include config schema setup from args or prompt.
159
166
  *
160
- * @param includeExample - Whether to include the example hello command.
161
- * @param rendered - The full set of rendered files.
162
- * @returns The filtered file list.
167
+ * @param ctx - Command context.
168
+ * @returns True when the config schema file should be included.
163
169
  * @private
164
170
  */
165
- function selectFiles(includeExample, rendered) {
166
- if (includeExample) return rendered;
167
- return rendered.filter(excludeHelloCommand);
171
+ async function resolveIncludeConfig(ctx) {
172
+ if (ctx.args.config !== void 0) return ctx.args.config;
173
+ return ctx.prompts.confirm({
174
+ initialValue: false,
175
+ message: "Include config schema?"
176
+ });
168
177
  }
169
178
  /**
170
- * Filter predicate that excludes the hello.ts example command.
179
+ * Select the rendered files to write, optionally excluding the example command and config.
171
180
  *
172
- * @param file - A rendered file to check.
173
- * @returns True when the file is not the hello command.
181
+ * @param options - Flags controlling which optional files to include.
182
+ * @param rendered - The full set of rendered files.
183
+ * @returns The filtered file list.
174
184
  * @private
175
185
  */
176
- function excludeHelloCommand(file) {
177
- return !file.relativePath.includes("commands/hello.ts");
186
+ function selectFiles(options, rendered) {
187
+ return rendered.filter((file) => options.includeExample || !file.relativePath.includes("commands/hello.ts")).filter((file) => options.includeConfig || !file.relativePath.includes("config.ts"));
178
188
  }
179
189
  const DEFAULT_VERSION = "0.0.0";
180
190
  /**
@@ -189,7 +199,10 @@ const DEFAULT_VERSION = "0.0.0";
189
199
  */
190
200
  async function resolveSelfVersion() {
191
201
  const [error, manifest] = await readManifest(join(import.meta.dirname, "..", ".."));
192
- if (error || !manifest.version) return DEFAULT_VERSION;
202
+ if (error || !manifest.version) {
203
+ console.warn("Warning: Could not resolve CLI version, using fallback 0.0.0");
204
+ return DEFAULT_VERSION;
205
+ }
193
206
  return manifest.version;
194
207
  }
195
208
  /**
@@ -206,9 +219,15 @@ async function resolveSelfVersion() {
206
219
  async function resolveDependencyVersion(packageName) {
207
220
  const require = createRequire(import.meta.url);
208
221
  const [resolveError, entryPath] = attempt(() => require.resolve(packageName));
209
- if (resolveError || entryPath === null) return DEFAULT_VERSION;
222
+ if (resolveError || entryPath === null) {
223
+ console.warn(`Warning: Could not resolve version for ${packageName}, using fallback 0.0.0`);
224
+ return DEFAULT_VERSION;
225
+ }
210
226
  const [manifestError, manifest] = await readManifest(join(dirname(entryPath), ".."));
211
- if (manifestError || !manifest.version) return DEFAULT_VERSION;
227
+ if (manifestError || !manifest.version) {
228
+ console.warn(`Warning: Could not resolve version for ${packageName}, using fallback 0.0.0`);
229
+ return DEFAULT_VERSION;
230
+ }
212
231
  return manifest.version;
213
232
  }
214
233
  //#endregion
package/dist/index.mjs CHANGED
@@ -7,27 +7,31 @@ import { readManifest } from "@kidd-cli/utils/manifest";
7
7
  *
8
8
  * Reads package.json one directory above `baseDir` (the dist output sits
9
9
  * one level below the package root) and ensures all required fields are
10
- * present. Throws immediately if the manifest cannot be read or any
11
- * required field is missing — this is an unrecoverable entry-point guard.
10
+ * present. Returns an error Result if the manifest cannot be read or any
11
+ * required field is missing.
12
12
  *
13
13
  * @param baseDir - The directory the CLI entry file lives in (typically `import.meta.dirname`).
14
- * @returns A validated {@link CLIManifest} with all required fields.
14
+ * @returns A Result tuple: error on failure, validated {@link CLIManifest} on success.
15
15
  */
16
16
  async function loadCLIManifest(baseDir) {
17
17
  const [manifestError, manifest] = await readManifest(join(baseDir, ".."));
18
- if (manifestError) throw new Error(`Failed to read CLI manifest: ${manifestError.message}`);
19
- if (!manifest.name) throw new Error("CLI manifest is missing required field: name");
20
- if (!manifest.version) throw new Error("CLI manifest is missing required field: version");
21
- if (!manifest.description) throw new Error("CLI manifest is missing required field: description");
22
- return {
18
+ if (manifestError) return [/* @__PURE__ */ new Error(`Failed to read CLI manifest: ${manifestError.message}`), null];
19
+ if (!manifest.name) return [/* @__PURE__ */ new Error("CLI manifest is missing required field: name"), null];
20
+ if (!manifest.version) return [/* @__PURE__ */ new Error("CLI manifest is missing required field: version"), null];
21
+ if (!manifest.description) return [/* @__PURE__ */ new Error("CLI manifest is missing required field: description"), null];
22
+ return [null, {
23
23
  description: manifest.description,
24
24
  name: manifest.name,
25
25
  version: manifest.version
26
- };
26
+ }];
27
27
  }
28
28
  //#endregion
29
29
  //#region src/index.ts
30
- const manifest = await loadCLIManifest(import.meta.dirname);
30
+ const [manifestError, manifest] = await loadCLIManifest(import.meta.dirname);
31
+ if (manifestError) {
32
+ console.error(manifestError.message);
33
+ process.exit(1);
34
+ }
31
35
  await cli({
32
36
  commands: `${import.meta.dirname}/commands`,
33
37
  description: manifest.description,
@@ -0,0 +1,16 @@
1
+ import type { ConfigType } from '@kidd-cli/core'
2
+ import { z } from 'zod'
3
+
4
+ /**
5
+ * Runtime config schema for the {{ name }} CLI.
6
+ *
7
+ * This schema validates config files loaded at startup (e.g. `.{{ name }}.jsonc`).
8
+ * The inferred type is merged into `ctx.config` via module augmentation below.
9
+ */
10
+ export const configSchema = z.object({
11
+ // Add your config fields here
12
+ })
13
+
14
+ declare module '@kidd-cli/core' {
15
+ interface CliConfig extends ConfigType<typeof configSchema> {}
16
+ }
@@ -7,6 +7,6 @@ export default command({
7
7
  name: z.string().describe('Name to greet').default('world'),
8
8
  }),
9
9
  handler: async (ctx) => {
10
- ctx.output.raw(`Hello, ${ctx.args.name}!`)
10
+ ctx.logger.print(`Hello, ${ctx.args.name}!`)
11
11
  },
12
12
  })
@@ -0,0 +1,16 @@
1
+ import type { ConfigType } from '@kidd-cli/core'
2
+ import { z } from 'zod'
3
+
4
+ /**
5
+ * Runtime config schema for the {{ name }} CLI.
6
+ *
7
+ * This schema validates config files loaded at startup (e.g. `.{{ name }}.jsonc`).
8
+ * The inferred type is merged into `ctx.config` via module augmentation below.
9
+ */
10
+ export const configSchema = z.object({
11
+ // Add your config fields here
12
+ })
13
+
14
+ declare module '@kidd-cli/core' {
15
+ interface CliConfig extends ConfigType<typeof configSchema> {}
16
+ }
@@ -1,8 +1,13 @@
1
1
  import { cli } from '@kidd-cli/core'
2
-
2
+ {% if includeConfig %}
3
+ import { configSchema } from './config.js'
4
+ {% endif %}
3
5
  void cli({
4
6
  name: '{{ name }}',
5
7
  version: '0.0.0',
6
8
  description: '{{ description }}',
7
9
  commands: import.meta.dirname + '/commands',
8
- })
10
+ {% if includeConfig %} config: {
11
+ schema: configSchema,
12
+ },
13
+ {% endif %}})
@@ -0,0 +1,16 @@
1
+ //#region src/lib/validate.ts
2
+ const KEBAB_CASE_CHARS_RE = /^[a-z][\da-z-]*$/;
3
+ /**
4
+ * Check whether a string is valid kebab-case.
5
+ *
6
+ * @param value - The string to validate.
7
+ * @returns True when the string is kebab-case.
8
+ */
9
+ function isKebabCase(value) {
10
+ if (!KEBAB_CASE_CHARS_RE.test(value)) return false;
11
+ if (value.endsWith("-")) return false;
12
+ if (value.includes("--")) return false;
13
+ return true;
14
+ }
15
+ //#endregion
16
+ export { isKebabCase as t };
@@ -92,21 +92,6 @@ function isGenerateError(value) {
92
92
  return "type" in value && "message" in value;
93
93
  }
94
94
  //#endregion
95
- //#region src/lib/validate.ts
96
- const KEBAB_CASE_CHARS_RE = /^[a-z][\da-z-]*$/;
97
- /**
98
- * Check whether a string is valid kebab-case.
99
- *
100
- * @param value - The string to validate.
101
- * @returns True when the string is kebab-case.
102
- */
103
- function isKebabCase(value) {
104
- if (!KEBAB_CASE_CHARS_RE.test(value)) return false;
105
- if (value.endsWith("-")) return false;
106
- if (value.includes("--")) return false;
107
- return true;
108
- }
109
- //#endregion
110
95
  //#region src/lib/write.ts
111
96
  /**
112
97
  * Write rendered files to disk with optional conflict detection.
@@ -160,4 +145,4 @@ async function writeSingleFile(file, outputDir, overwrite) {
160
145
  }
161
146
  }
162
147
  //#endregion
163
- export { isKebabCase as n, renderTemplate as r, writeFiles as t };
148
+ export { renderTemplate as n, writeFiles as t };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kidd-cli/cli",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "DX companion CLI for the kidd framework",
5
5
  "keywords": [
6
6
  "cli",
@@ -28,8 +28,8 @@
28
28
  "zod": "^4.3.6",
29
29
  "@kidd-cli/bundler": "0.2.0",
30
30
  "@kidd-cli/config": "0.1.4",
31
- "@kidd-cli/core": "0.4.0",
32
- "@kidd-cli/utils": "0.1.4"
31
+ "@kidd-cli/utils": "0.1.4",
32
+ "@kidd-cli/core": "0.5.0"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@types/fs-extra": "^11.0.4",