feat(auto-readme): add generate subcommand for default config#290
feat(auto-readme): add generate subcommand for default config#290stephansama wants to merge 2 commits into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request introduces a new generate subcommand to the CLI, allowing users to create default configuration files in JSON, TOML, or YAML formats. Feedback suggests using dynamic imports in the CLI entry point to optimize startup time by deferring the loading of heavy dependencies like yaml and smol-toml. Additionally, it is recommended to use the project's internal logging and error utilities for consistency and to remove an unused type definition in the new module.
| import { generate } from "./generate"; | ||
| import { run } from "./index"; | ||
| await run(); | ||
|
|
||
| const [subcommand, ...rest] = process.argv.slice(2); | ||
|
|
||
| await (subcommand === "generate" ? generate(rest) : run()); |
There was a problem hiding this comment.
Importing the generate module at the top level causes heavy dependencies like yaml and smol-toml to be loaded even when the generate subcommand is not used. This increases the startup time for the main run command. Using a dynamic import for the generate subcommand will improve the CLI's responsiveness for the primary use case.
import { run } from "./index";
const [subcommand, ...rest] = process.argv.slice(2);
if (subcommand === "generate") {
const { generate } = await import("./generate");
await generate(rest);
} else {
await run();
}| import * as fsp from "node:fs/promises"; | ||
| import path from "node:path"; | ||
| import { stringify as stringifyToml } from "smol-toml"; | ||
| import yaml from "yaml"; | ||
|
|
||
| import { configSchema } from "./schema"; | ||
|
|
||
| export type GenerateOptions = { | ||
| filename?: string; | ||
| force?: boolean; | ||
| }; | ||
|
|
||
| const DEFAULT_FILENAME = ".autoreadmerc.json"; | ||
|
|
||
| export async function generate(argv: readonly string[]): Promise<void> { | ||
| const force = argv.includes("--force") || argv.includes("-f"); | ||
| const positional = argv.find((argument) => !argument.startsWith("-")); | ||
| const filename = positional || DEFAULT_FILENAME; | ||
|
|
||
| if (!force && (await fileExists(filename))) { | ||
| throw new Error( | ||
| `Refusing to overwrite existing file: ${filename}. Pass --force to overwrite.`, | ||
| ); | ||
| } | ||
|
|
||
| const defaults = buildDefaults(); | ||
| const contents = serialize(filename, defaults); | ||
|
|
||
| await fsp.writeFile(filename, contents, "utf8"); | ||
| // eslint-disable-next-line no-console | ||
| console.log(`Wrote ${filename}`); | ||
| } |
There was a problem hiding this comment.
This refactor improves the generate module by:
- Removing the unused
GenerateOptionstype to keep the codebase clean. - Adhering to the project's established logging and error handling patterns by using
INFOandERRORfrom./loginstead ofconsole.logand rawErrorthrows. This ensures that the output respects verbosity settings and follows the consistent UI style of the tool.
import * as fsp from "node:fs/promises";
import path from "node:path";
import { stringify as stringifyToml } from "smol-toml";
import yaml from "yaml";
import { ERROR, INFO } from "./log";
import { configSchema } from "./schema";
const DEFAULT_FILENAME = ".autoreadmerc.json";
export async function generate(argv: readonly string[]): Promise<void> {
const force = argv.includes("--force") || argv.includes("-f");
const positional = argv.find((argument) => !argument.startsWith("-"));
const filename = positional || DEFAULT_FILENAME;
if (!force && (await fileExists(filename))) {
return ERROR(
`Refusing to overwrite existing file: ${filename}. Pass --force to overwrite.`,
);
}
const defaults = buildDefaults();
const contents = serialize(filename, defaults);
await fsp.writeFile(filename, contents, "utf8");
INFO(`Wrote ${filename}`);
}
|
Closes STE-13
Adds an
auto-readme generate [filename]subcommand that writes a fully-populated default config file. Output format is detected from the filename extension:.json(default) → pretty-printed JSON.yaml/.yml→ YAML via theyamlpackage.toml→ TOML viasmol-tomlDefaults are derived by passing an empty seed through
configSchema(with empty arrays for the two top-level fields that have no schema default —affectedRegexes,collapseHeadings).Refuses to overwrite an existing file unless
--force/-fis passed.Acceptance criteria
auto-readme generatewrites.autoreadmerc.jsonwith all defaultsauto-readme generate .autoreadmerc.yamlwrites YAMLauto-readme generate .autoreadmerc.tomlwrites TOML--forceis not passedconfigSchema.parse()without errors (round-trip safe by construction)