Skip to content

Commit 34c7ec6

Browse files
authored
chore(cli): remove interactive (#585)
1 parent 9e5b143 commit 34c7ec6

File tree

8 files changed

+93
-465
lines changed

8 files changed

+93
-465
lines changed

bin/cli.mjs

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,21 @@ import process from 'node:process';
55
import { Command, Option } from 'commander';
66

77
import commands from './commands/index.mjs';
8-
import { errorWrap } from './utils.mjs';
98
import { LogLevel } from '../src/logger/constants.mjs';
109
import logger from '../src/logger/index.mjs';
1110

12-
const logLevelOption = new Option('--log-level <level>', 'Log level')
13-
.choices(Object.keys(LogLevel))
14-
.default('info');
15-
1611
const program = new Command()
1712
.name('@nodejs/doc-kit')
1813
.description('CLI tool to generate the Node.js API documentation')
19-
.addOption(logLevelOption)
14+
.addOption(
15+
new Option('--log-level <level>', 'Log level')
16+
.default('info')
17+
.choices(Object.keys(LogLevel))
18+
)
2019
.hook('preAction', cmd => logger.setLogLevel(cmd.opts().logLevel));
2120

2221
// Registering commands
23-
commands.forEach(({ name, description, options, action }) => {
24-
const cmd = program.command(name).description(description);
25-
26-
// Add options to the command
27-
Object.values(options).forEach(({ flags, desc, prompt }) => {
28-
const option = new Option(flags.join(', '), desc).default(
29-
prompt.initialValue
30-
);
31-
32-
if (prompt.required) {
33-
option.makeOptionMandatory();
34-
}
35-
36-
if (prompt.type === 'multiselect') {
37-
option.choices(prompt.options.map(({ value }) => value));
38-
}
39-
40-
cmd.addOption(option);
41-
});
42-
43-
// Set the action for the command
44-
cmd.action(errorWrap(action));
45-
});
22+
commands.forEach(command => program.addCommand(command));
4623

4724
// Parse and execute command-line arguments
4825
program.parse(process.argv);

bin/commands/__tests__/index.test.mjs

Lines changed: 0 additions & 30 deletions
This file was deleted.

bin/commands/generate.mjs

Lines changed: 76 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { cpus } from 'node:os';
2-
import { resolve } from 'node:path';
32

3+
import { Command, Option } from 'commander';
44
import { coerce } from 'semver';
55

66
import { NODE_CHANGELOG_URL, NODE_VERSION } from '../../src/constants.mjs';
@@ -10,153 +10,82 @@ import logger from '../../src/logger/index.mjs';
1010
import { parseTypeMap } from '../../src/parsers/json.mjs';
1111
import { parseChangelog, parseIndex } from '../../src/parsers/markdown.mjs';
1212
import { DEFAULT_TYPE_MAP } from '../../src/utils/parser/constants.mjs';
13+
import { errorWrap } from '../utils.mjs';
1314

14-
const availableGenerators = Object.keys(publicGenerators);
15+
export default new Command('generate')
16+
.description('Generate API docs')
17+
.addOption(
18+
new Option(
19+
'-i, --input <patterns...>',
20+
'Input file patterns (glob)'
21+
).makeOptionMandatory()
22+
)
23+
.addOption(
24+
new Option('--ignore <patterns...>', 'Ignore file patterns (glob)')
25+
)
26+
.addOption(new Option('-o, --output <directory>', 'The output directory'))
27+
.addOption(
28+
new Option(
29+
'-p, --threads <number>',
30+
'Number of threads to use (minimum: 1)'
31+
)
32+
.default(cpus().length)
33+
.argParser(parseInt)
34+
)
35+
.addOption(
36+
new Option(
37+
'--chunk-size <number>',
38+
'Number of items to process per worker thread (minimum: 1)'
39+
)
40+
.default(10)
41+
.argParser(parseInt)
42+
)
43+
.addOption(
44+
new Option('-v, --version <semver>', 'Target Node.js version').default(
45+
NODE_VERSION
46+
)
47+
)
48+
.addOption(
49+
new Option('-c, --changelog <url>', 'Changelog URL or path').default(
50+
NODE_CHANGELOG_URL
51+
)
52+
)
53+
.addOption(
54+
new Option('--git-ref', 'Git ref URL').default(
55+
'https://github.com/nodejs/node/tree/HEAD'
56+
)
57+
)
58+
.addOption(
59+
new Option('-t, --target <generator...>', 'Target generator(s)')
60+
.makeOptionMandatory()
61+
.choices(Object.keys(publicGenerators))
62+
)
63+
.addOption(new Option('--index <url>', 'index.md URL or path'))
64+
.addOption(
65+
new Option('--type-map <url>', 'Type map URL or path').default(
66+
DEFAULT_TYPE_MAP
67+
)
68+
)
69+
.action(
70+
errorWrap(async opts => {
71+
logger.debug('Starting doc-kit', opts);
1572

16-
/**
17-
* @type {import('./types').Command}
18-
*/
19-
export default {
20-
description: 'Generate API docs',
21-
name: 'generate',
22-
options: {
23-
input: {
24-
flags: ['-i', '--input <patterns...>'],
25-
desc: 'Input file patterns (glob)',
26-
prompt: {
27-
type: 'text',
28-
message: 'Enter input glob patterns',
29-
variadic: true,
30-
required: true,
31-
},
32-
},
33-
ignore: {
34-
flags: ['--ignore [patterns...]'],
35-
desc: 'Ignore patterns (comma-separated)',
36-
prompt: {
37-
type: 'text',
38-
message: 'Enter ignore patterns',
39-
variadic: true,
40-
},
41-
},
42-
output: {
43-
flags: ['-o', '--output <dir>'],
44-
desc: 'Output directory',
45-
prompt: { type: 'text', message: 'Enter output directory' },
46-
},
47-
threads: {
48-
flags: ['-p', '--threads <number>'],
49-
desc: 'Number of threads to use (minimum: 1)',
50-
prompt: {
51-
type: 'text',
52-
message: 'How many threads to allow',
53-
initialValue: String(cpus().length),
54-
},
55-
},
56-
chunkSize: {
57-
flags: ['--chunk-size <number>'],
58-
desc: 'Number of items to process per worker thread (default: auto)',
59-
prompt: {
60-
type: 'text',
61-
message: 'Items per worker thread',
62-
initialValue: '10',
63-
},
64-
},
65-
version: {
66-
flags: ['-v', '--version <semver>'],
67-
desc: 'Target Node.js version',
68-
prompt: {
69-
type: 'text',
70-
message: 'Enter Node.js version',
71-
initialValue: NODE_VERSION,
72-
},
73-
},
74-
changelog: {
75-
flags: ['-c', '--changelog <url>'],
76-
desc: 'Changelog URL or path',
77-
prompt: {
78-
type: 'text',
79-
message: 'Enter changelog URL',
80-
initialValue: NODE_CHANGELOG_URL,
81-
},
82-
},
83-
gitRef: {
84-
flags: ['--git-ref <url>'],
85-
desc: 'Git ref/commit URL',
86-
prompt: {
87-
type: 'text',
88-
message: 'Enter Git ref URL',
89-
initialValue: 'https://github.com/nodejs/node/tree/HEAD',
90-
},
91-
},
92-
target: {
93-
flags: ['-t', '--target [modes...]'],
94-
desc: 'Target generator modes',
95-
prompt: {
96-
required: true,
97-
type: 'multiselect',
98-
message: 'Choose target generators',
99-
options: availableGenerators.map(g => ({
100-
value: g,
101-
label: `${publicGenerators[g].name || g} (v${publicGenerators[g].version}) - ${publicGenerators[g].description}`,
102-
})),
103-
},
104-
},
105-
index: {
106-
flags: ['--index <path>'],
107-
desc: 'The index document, for getting the titles of various API docs',
108-
prompt: {
109-
message: 'Path to doc/api/index.md',
110-
type: 'text',
111-
},
112-
},
113-
typeMap: {
114-
flags: ['--type-map <path>'],
115-
desc: 'The mapping of types to links',
116-
prompt: {
117-
message: 'Path to doc/api/type_map.json',
118-
type: 'text',
119-
initialValue: DEFAULT_TYPE_MAP,
120-
},
121-
},
122-
},
73+
const { runGenerators } = createGenerator();
12374

124-
/**
125-
* @typedef {Object} Options
126-
* @property {Array<string>|string} input - Specifies the glob/path for input files.
127-
* @property {Array<string>|string} [ignore] - Specifies the glob/path for ignoring files.
128-
* @property {Array<keyof AvailableGenerators>} target - Specifies the generator target mode.
129-
* @property {string} version - Specifies the target Node.js version.
130-
* @property {string} changelog - Specifies the path to the Node.js CHANGELOG.md file.
131-
* @property {string} typeMap - Specifies the path to the Node.js Type Map.
132-
* @property {string} index - Specifies the path to the index document.
133-
* @property {string} [gitRef] - Git ref/commit URL.
134-
* @property {number} [threads] - Number of threads to allow.
135-
* @property {number} [chunkSize] - Number of items to process per worker thread.
136-
*
137-
* Handles the action for generating API docs
138-
* @param {Options} opts - The options to generate API docs.
139-
* @returns {Promise<void>}
140-
*/
141-
async action(opts) {
142-
logger.debug('Starting doc-kit', opts);
75+
logger.debug('Starting generation', { targets: opts.target });
14376

144-
const { runGenerators } = createGenerator();
145-
146-
logger.debug('Starting generation', { targets: opts.target });
147-
148-
await runGenerators({
149-
generators: opts.target,
150-
input: opts.input,
151-
ignore: opts.ignore,
152-
output: opts.output && resolve(opts.output),
153-
version: coerce(opts.version),
154-
releases: await parseChangelog(opts.changelog),
155-
gitRef: opts.gitRef,
156-
threads: Math.max(parseInt(opts.threads, 10), 1),
157-
chunkSize: Math.max(parseInt(opts.chunkSize, 10), 1),
158-
index: await parseIndex(opts.index),
159-
typeMap: await parseTypeMap(opts.typeMap),
160-
});
161-
},
162-
};
77+
await runGenerators({
78+
generators: opts.target,
79+
input: opts.input,
80+
ignore: opts.ignore,
81+
output: opts.output,
82+
version: coerce(opts.version),
83+
releases: await parseChangelog(opts.changelog),
84+
gitRef: opts.gitRef,
85+
threads: Math.max(opts.threads, 1),
86+
chunkSize: Math.max(opts.chunkSize, 1),
87+
index: await parseIndex(opts.index),
88+
typeMap: await parseTypeMap(opts.typeMap),
89+
});
90+
})
91+
);

bin/commands/index.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
import generate from './generate.mjs';
2-
import interactive from './interactive.mjs';
32

4-
export default [generate, interactive];
3+
export default [generate];

0 commit comments

Comments
 (0)