Skip to content

Commit 9141014

Browse files
committed
fix(commands): fix command args prompt
1 parent f66094c commit 9141014

5 files changed

Lines changed: 31 additions & 16 deletions

File tree

packages/whook-example/src/cli.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ On air 🚀🌕
4949

5050
test('with env', async () => {
5151
const { stdout, stderr } = await execCommand(
52-
'npm run whook --silent -- env --name NODE_ENV',
52+
'NO_PROMPT=1 npm run whook --silent -- env --name NODE_ENV',
5353
);
5454

5555
expect({
@@ -73,7 +73,7 @@ On air 🚀🌕
7373

7474
test('with config', async () => {
7575
const { stdout, stderr } = await execCommand(
76-
'npm run whook --silent -- config --name HOST',
76+
'NO_PROMPT=1 npm run whook --silent -- config --name HOST',
7777
);
7878

7979
expect({

packages/whook/src/libs/args.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ export function parseArgs(rawArgs: string[]): WhookRawCommandArgs {
2020
(cleanArgs, key) => ({
2121
...cleanArgs,
2222
// Avoid having the --arg shortcut for --arg=true to
23-
// provide a boolean since we coerce the args later
24-
[key]: typeof args[key] === 'boolean' ? args[key].toString() : args[key],
23+
// provide a boolean since we coerce the args later
24+
[key]:
25+
typeof args[key] === 'boolean' ? args[key].toString() : args[key],
2526
}),
2627
{},
2728
),
@@ -53,16 +54,7 @@ export async function promptArgs(
5354
API,
5455
schema.$ref,
5556
)) as WhookCommandSchema;
56-
newNamedArgs[argument.name] = await input({
57-
message: `Enter the value for "${argument.name}": `,
58-
default: schema.default?.toString(),
59-
required: argument.required,
60-
});
61-
continue;
62-
}
6357

64-
if (!('default' in schema)) {
65-
newNamedArgs[argument.name] = schema.default?.toString();
6658
continue;
6759
}
6860

@@ -107,7 +99,15 @@ export async function promptArgs(
10799
newNamedArgs[argument.name] = await input({
108100
message: `Enter the value for "${argument.name}": `,
109101
required: argument.required,
102+
default: schema.default?.toString(),
110103
});
104+
105+
continue;
106+
}
107+
108+
if ('default' in schema) {
109+
newNamedArgs[argument.name] = schema.default;
110+
continue;
111111
}
112112
}
113113
}

packages/whook/src/services/command.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ describe('command', () => {
3535
schemaValidators.mockReturnValue(validator as unknown as ValidateFunction);
3636

3737
await initCommand({
38+
ENV: {
39+
NO_PROMPT: '1',
40+
},
3841
API,
3942
COMMAND_DEFINITION: initEnvCommandDefinition,
4043
COERCION_OPTIONS: DEFAULT_COERCION_OPTIONS,
@@ -88,6 +91,9 @@ describe('command', () => {
8891
schemaValidators.mockReturnValue(validator as unknown as ValidateFunction);
8992

9093
await initCommand({
94+
ENV: {
95+
NO_PROMPT: '1',
96+
},
9197
API,
9298
COMMAND_DEFINITION: initEnvCommandDefinition,
9399
COERCION_OPTIONS: DEFAULT_COERCION_OPTIONS,

packages/whook/src/services/command.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ import { getCasterForSchema } from '../libs/validation.js';
2121
import { ensureResolvedObject } from 'ya-open-api-types';
2222
import { type ExpressiveJSONSchema } from 'ya-json-schema-types';
2323

24-
export default location(autoService(initCommand), import.meta.url);
24+
export type WhookCommandEnv = {
25+
CI?: string;
26+
NO_PROMPT?: string;
27+
};
2528

2629
async function initCommand({
30+
ENV,
2731
API,
2832
COMMAND_DEFINITION,
2933
COERCION_OPTIONS = DEFAULT_COERCION_OPTIONS,
@@ -35,6 +39,7 @@ async function initCommand({
3539
args,
3640
log,
3741
}: {
42+
ENV: WhookCommandEnv;
3843
API: WhookOpenAPI;
3944
COMMAND_DEFINITION: WhookCommandDefinition;
4045
COERCION_OPTIONS?: WhookCoercionOptions;
@@ -50,7 +55,7 @@ async function initCommand({
5055
await $ready;
5156

5257
try {
53-
if (COMMAND_DEFINITION.config?.promptArgs) {
58+
if (COMMAND_DEFINITION.config?.promptArgs && !ENV.CI && !ENV.NO_PROMPT) {
5459
// Required to ensure any logs are printed
5560
await Promise.resolve();
5661

@@ -134,3 +139,5 @@ async function initCommand({
134139
}
135140
commandRunner();
136141
}
142+
143+
export default location(autoService(initCommand), import.meta.url);

packages/whook/src/types/base.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { type OpenAPITypesConfig } from '../commands/generateOpenAPITypes.js';
2323
import { type WhookErrorHandlerConfig } from '../services/errorHandler.js';
2424
import { type WhookSchemaValidatorsConfig } from '../services/schemaValidators.js';
2525
import { type WhookQueryParserBuilderConfig } from '../services/queryParserBuilder.js';
26+
import { type WhookCommandEnv } from '../services/command.js';
2627

2728
export type WhookBaseMain = {
2829
AppEnv: string;
@@ -31,7 +32,8 @@ export type WhookBaseMain = {
3132
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
3233
export interface WhookMain extends WhookBaseMain {}
3334

34-
export type WhookBaseEnv = WhookHTTPServerEnv &
35+
export type WhookBaseEnv = WhookCommandEnv &
36+
WhookHTTPServerEnv &
3537
WhookBaseURLEnv &
3638
WhookHostEnv &
3739
WhookPortEnv;

0 commit comments

Comments
 (0)