diff --git a/README.md b/README.md index 607cf69f..583d4371 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,10 @@ Commands: ### `generate` +You must provide either `--target` (one or more generators to run) or +`--config-file` (which supplies the targets). Running `generate` without either +exits with an error pointing you to the help output. + ``` Usage: @node-core/doc-kit generate [options] diff --git a/bin/commands/generate.mjs b/bin/commands/generate.mjs index d4586663..30d9a378 100644 --- a/bin/commands/generate.mjs +++ b/bin/commands/generate.mjs @@ -2,7 +2,10 @@ import { Command, Option } from 'commander'; import { publicGenerators } from '../../src/generators/index.mjs'; import createGenerator from '../../src/generators.mjs'; -import { setConfig } from '../../src/utils/configuration/index.mjs'; +import { + assertRunnableOptions, + setConfig, +} from '../../src/utils/configuration/index.mjs'; import { errorWrap } from '../utils.mjs'; const { runGenerators } = createGenerator(); @@ -62,6 +65,8 @@ export default new Command('generate') .action( errorWrap(async opts => { const config = await setConfig(opts); + assertRunnableOptions(config); + await runGenerators(config); }) ); diff --git a/src/utils/configuration/__tests__/index.test.mjs b/src/utils/configuration/__tests__/index.test.mjs index a45de3d9..607e5c65 100644 --- a/src/utils/configuration/__tests__/index.test.mjs +++ b/src/utils/configuration/__tests__/index.test.mjs @@ -33,6 +33,7 @@ mock.module('../../loaders.mjs', { }); const { + assertRunnableOptions, loadConfigFile, createConfigFromCLIOptions, createRunConfiguration, @@ -125,6 +126,25 @@ describe('config.mjs', () => { }); }); + describe('assertRunnableOptions', () => { + it('should throw when neither target nor config file is provided', () => { + assert.throws( + () => assertRunnableOptions({}), + /Either `--target` or `--config-file` must be provided/ + ); + }); + + it('should not throw when a target is provided', () => { + assert.doesNotThrow(() => assertRunnableOptions({ target: ['json'] })); + }); + + it('should not throw when a config file is provided', () => { + assert.doesNotThrow(() => + assertRunnableOptions({ configFile: 'config.mjs' }) + ); + }); + }); + describe('createRunConfiguration', () => { it('should merge config sources in correct order', async () => { mockImportFromURL.mock.mockImplementationOnce(async () => diff --git a/src/utils/configuration/index.mjs b/src/utils/configuration/index.mjs index b20a634a..a143f6de 100644 --- a/src/utils/configuration/index.mjs +++ b/src/utils/configuration/index.mjs @@ -104,6 +104,21 @@ export const createConfigFromCLIOptions = options => ({ chunkSize: options.chunkSize, }); +/** + * Asserts that the CLI was given somewhere to read generator targets from: + * either explicit `--target` flags or a `--config-file` that supplies them. + * + * @param {import('../../../bin/commands/generate.mjs').CLIOptions} options - User-provided options + */ +export const assertRunnableOptions = options => { + if (!options.target && !options.configFile) { + throw new Error( + 'Either `--target` or `--config-file` must be provided. ' + + 'Run `doc-kit generate --help` for usage.' + ); + } +}; + /** * Creates a complete run configuration by merging config file, user options, and defaults. * Processes and validates configuration values including version coercion, changelog parsing,