Skip to content

Commit 846be16

Browse files
committed
fix: improve name validation
fix #32
1 parent 9d7bab9 commit 846be16

File tree

4 files changed

+8
-6
lines changed

4 files changed

+8
-6
lines changed

src/cli.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {
1212
import { Command } from './command';
1313
import { generateHelp, Renderers } from './render-help';
1414
import { camelCase } from './utils/convert-case';
15-
import { isScriptNamePattern } from './utils/is-script-name';
15+
import { isValidScriptName } from './utils/script-name';
1616

1717
const { stringify } = JSON;
1818

@@ -361,15 +361,15 @@ function cli<
361361
throw new Error('Options is required');
362362
}
363363

364-
if ('name' in options && (!options.name || !isScriptNamePattern.test(options.name))) {
364+
if ('name' in options && (!options.name || !isValidScriptName(options.name))) {
365365
throw new Error(`Invalid script name: ${stringify(options.name)}`);
366366
}
367367

368368
const potentialCommand = argv[0];
369369

370370
if (
371371
options.commands
372-
&& isScriptNamePattern.test(potentialCommand)
372+
&& isValidScriptName(potentialCommand)
373373
) {
374374
const command = getCommand(potentialCommand, options.commands);
375375

src/command.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
ParseArgv,
77
parsedType,
88
} from './types';
9-
import { isScriptNamePattern } from './utils/is-script-name';
9+
import { isValidScriptName } from './utils/script-name';
1010

1111
export type CommandOptions<Parameters = string[]> = {
1212
/**
@@ -61,7 +61,7 @@ export function command<
6161
throw new Error('Command name is required');
6262
}
6363

64-
if (!isScriptNamePattern.test(name)) {
64+
if (!isValidScriptName(name)) {
6565
throw new Error(`Invalid command name ${JSON.stringify(name)}. Command names must be one word.`);
6666
}
6767

src/utils/is-script-name.ts

-1
This file was deleted.

src/utils/script-name.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const isValidScriptName = (
2+
name: string,
3+
) => name.length > 0 && !name.includes(' ');

0 commit comments

Comments
 (0)