Skip to content
Merged
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions packages/typegpu-cli/src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as p from '@clack/prompts';
import { pmFromUserAgent, pmInstall } from './utils/pm.ts';
import { cancelExit, confirmStep, rgbText } from './utils/prompts.ts';
import { scaffoldProject, prepareDirectory } from './utils/files.ts';
import { getPackageName, getProjectDirectory } from './utils/inputs.ts';
import { getProjectName, isValidPackageName, getPackageName } from './utils/inputs.ts';
import { detect, resolveCommand } from 'package-manager-detector';

const DEFAULT_PROJECT_DIR = 'tgpu-project';
Expand All @@ -19,11 +19,11 @@ const PROJECT_TEMPLATES = [
export async function createProject(cwd: string) {
p.intro('Creating a new TypeGPU project.');

const projectDir = await getProjectDirectory(DEFAULT_PROJECT_DIR);
const projectName = await getProjectName(DEFAULT_PROJECT_DIR); // also directory name

const root = await prepareDirectory(cwd, projectDir);
const root = await prepareDirectory(cwd, projectName);

const packageName = await getPackageName(projectDir);
const packageName = isValidPackageName(projectName) ? projectName : await getPackageName();
Comment on lines +22 to +26
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.

Vite creates nested directories, I think it's okay to mimic that behavior.

Image


const projectTemplate = await p.select({
message: 'Select a template:',
Expand All @@ -33,7 +33,7 @@ export async function createProject(cwd: string) {
cancelExit();
}

p.log.step(`Scaffolding project in ${projectDir}...`);
p.log.step(`Scaffolding project in ${projectName}...`);

const templateDir = path.resolve(
import.meta.dirname,
Expand All @@ -42,7 +42,7 @@ export async function createProject(cwd: string) {
);
await scaffoldProject(templateDir, root, packageName);

p.log.success(`Scaffolded project at ${projectDir}.`);
p.log.success(`Scaffolded project at ${projectName}.`);

const detected = await detect({ cwd });
const pm = detected?.agent ?? pmFromUserAgent(process.env.npm_config_user_agent);
Expand Down
21 changes: 10 additions & 11 deletions packages/typegpu-cli/src/utils/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,32 @@ function isValidProjectDirectory(projectDir: string) {
return !/[<>:"\\|?*\s]|\/+$/.test(projectDir.trim());
}

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

export async function getProjectDirectory(initialValue: string) {
let projectDir = await p.text({
message: 'Project directory:',
export async function getProjectName(initialValue: string) {
let projectName = await p.text({
message: 'Project name:',
placeholder: initialValue,
initialValue,
validate: (value) => {
return value && !isValidProjectDirectory(value) ? 'Invalid project directory.' : undefined;
return value && !isValidProjectDirectory(value) ? 'Invalid project name.' : undefined;
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.

The CLI properly gives the user a warning if the current directory is not empty, so it's fine

Image

},
Comment on lines +15 to 21
});

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

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

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