Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions packages/typegpu-cli/src/utils/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,39 @@ import type { PackageJson } from './types.ts';
import { hasDependency, typegpuPkgs, VERSION } from './pkg.ts';

function isValidProjectDirectory(projectDir: string) {
return !/[<>:"\\|?*\s]|\/+$/.test(projectDir.trim());
const trimmedDir = projectDir.trim();
return (
projectDir.length === 0 || (trimmedDir.length > 0 && !/[<>:"\\|?*\s]|\/+$/.test(trimmedDir))
);
}

export function isValidPackageName(packageName: string) {
return /^(?:@[a-z\d][a-z\d\-._]*\/)?[a-z\d][a-z\d\-._]*$/.test(packageName.trim());
const trimmedName = packageName.trim();
return /^(?:@[a-z\d][a-z\d\-._]*\/)?[a-z\d][a-z\d\-._]*$/.test(trimmedName);
}

export async function getProjectName(initialValue: string) {
let projectName = await p.text({
message: 'Project name:',
placeholder: initialValue,
initialValue,
defaultValue: initialValue,
validate: (value) => {
return value && !isValidProjectDirectory(value) ? 'Invalid project name.' : undefined;
return !isValidProjectDirectory(value) ? 'Invalid project name.' : undefined;
},
Comment on lines 21 to 25
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is never empty, there is a default value

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add these lines and we will be fine

projectDir = projectDIr.trim();
projectDir ??= '.';
return projectDir;

});

if (p.isCancel(projectName)) {
cancelExit();
}

projectName ??= '.';
return projectName.trim();
}

export async function getPackageName() {
const packageName = await p.text({
message: 'Package name:',
initialValue: '',
validate: (value) => {
return !value || !isValidPackageName(value) ? 'Invalid package name.' : undefined;
return !isValidPackageName(value) ? 'Invalid package name.' : undefined;
},
});

Expand Down
Loading