Skip to content

Commit 0c53fc9

Browse files
fix(generate): show help output when no arguments or config are provided (#853)
* fix(generate): show help output when no arguments or config are provided * fix(generate): enforce target or config file requirement for generate command * fix(configuration): add assertRunnableOptions to validate target or config file presence * fix(generate): validate options after configuration is set * assert on merged config
1 parent 7a48e2f commit 0c53fc9

4 files changed

Lines changed: 51 additions & 1 deletion

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ Commands:
5454

5555
### `generate`
5656

57+
You must provide either `--target` (one or more generators to run) or
58+
`--config-file` (which supplies the targets). Running `generate` without either
59+
exits with an error pointing you to the help output.
60+
5761
```
5862
Usage: @node-core/doc-kit generate [options]
5963

bin/commands/generate.mjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { Command, Option } from 'commander';
22

33
import { publicGenerators } from '../../src/generators/index.mjs';
44
import createGenerator from '../../src/generators.mjs';
5-
import { setConfig } from '../../src/utils/configuration/index.mjs';
5+
import {
6+
assertRunnableOptions,
7+
setConfig,
8+
} from '../../src/utils/configuration/index.mjs';
69
import { errorWrap } from '../utils.mjs';
710

811
const { runGenerators } = createGenerator();
@@ -62,6 +65,8 @@ export default new Command('generate')
6265
.action(
6366
errorWrap(async opts => {
6467
const config = await setConfig(opts);
68+
assertRunnableOptions(config);
69+
6570
await runGenerators(config);
6671
})
6772
);

src/utils/configuration/__tests__/index.test.mjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ mock.module('../../loaders.mjs', {
3333
});
3434

3535
const {
36+
assertRunnableOptions,
3637
loadConfigFile,
3738
createConfigFromCLIOptions,
3839
createRunConfiguration,
@@ -125,6 +126,28 @@ describe('config.mjs', () => {
125126
});
126127
});
127128

129+
describe('assertRunnableOptions', () => {
130+
it('should throw when target is missing', () => {
131+
assert.throws(
132+
() => assertRunnableOptions({ global: { input: 'src/' } }),
133+
/Both a `target` and an `input` must be provided/
134+
);
135+
});
136+
137+
it('should throw when input is missing', () => {
138+
assert.throws(
139+
() => assertRunnableOptions({ target: ['json'], global: {} }),
140+
/Both a `target` and an `input` must be provided/
141+
);
142+
});
143+
144+
it('should not throw when both target and input are provided', () => {
145+
assert.doesNotThrow(() =>
146+
assertRunnableOptions({ target: ['json'], global: { input: 'src/' } })
147+
);
148+
});
149+
});
150+
128151
describe('createRunConfiguration', () => {
129152
it('should merge config sources in correct order', async () => {
130153
mockImportFromURL.mock.mockImplementationOnce(async () =>

src/utils/configuration/index.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,24 @@ export const createConfigFromCLIOptions = options => ({
104104
chunkSize: options.chunkSize,
105105
});
106106

107+
/**
108+
* Asserts that the resolved configuration has everything needed to run:
109+
* at least one generator `target` and an `input` to read source files from.
110+
* These may come from CLI flags or a config file; by this point both sources
111+
* have been merged, so we validate the result rather than the raw options.
112+
*
113+
* @param {import('./types').Configuration} config - The merged configuration
114+
*/
115+
export const assertRunnableOptions = config => {
116+
if (!config.target || !config.global?.input) {
117+
throw new Error(
118+
'Both a `target` and an `input` must be provided, either via ' +
119+
'`--target`/`--input` or a `--config-file`. ' +
120+
'Run `doc-kit generate --help` for usage.'
121+
);
122+
}
123+
};
124+
107125
/**
108126
* Creates a complete run configuration by merging config file, user options, and defaults.
109127
* Processes and validates configuration values including version coercion, changelog parsing,

0 commit comments

Comments
 (0)