Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add folder as argument to create-toolpad-app #1795

Merged
merged 18 commits into from
Apr 20, 2023
Merged

Add folder as argument to create-toolpad-app #1795

merged 18 commits into from
Apr 20, 2023

Conversation

bharatkashyap
Copy link
Member

@bharatkashyap bharatkashyap added the core Infrastructure work going on behind the scenes label Mar 23, 2023
@Janpot
Copy link
Member

Janpot commented Mar 23, 2023

Some questions that popped in my head after reading this

  • Can I use . to initialize in the current (empty) directory?
  • Can I use any path? e.g. yarn create toolpad-app ../../foo/bar/toolpad-app

@bharatkashyap
Copy link
Member Author

Some questions that popped in my head after reading this

  • Can I use . to initialize in the current (empty) directory?
  • Can I use any path? e.g. yarn create toolpad-app ../../foo/bar/toolpad-app

I think that depends on whether the option is advertised as a path or a name; in the former case I expect that ., ../foo/bar/ , newDir and ./newDir are all valid options, whereas in the latter case only "names" would work.

We can expand the scope of this PR to include yargs for create-toolpad-app and add path as an option

@Janpot
Copy link
Member

Janpot commented Mar 24, 2023

We can expand the scope of this PR to include yargs for create-toolpad-app and add path as an option

👍 If by "option" you mean "first positional argument" and not --path=...

@bharatkashyap
Copy link
Member Author

We can expand the scope of this PR to include yargs for create-toolpad-app and add path as an option

👍 If by "option" you mean "first positional argument" and not --path=...

Yes ✅

// eslint-disable-next-line no-await-in-loop
projectName = await scaffoldProject(name.projectName, cwd);
// eslint-disable-next-line no-await-in-loop
projectName = await scaffoldProject(name.projectName, cwd);
Copy link
Member

@apedroferreira apedroferreira Mar 27, 2023

Choose a reason for hiding this comment

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

Maybe assign the first argument you're passing to scaffoldProject to a variable so you don't need to write almost the same expression twice (so you can call scaffoldProject in just one line)?

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Mar 27, 2023
@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Mar 27, 2023
} while (!projectName);
absolutePath = await scaffoldProject(absolutePath);
count += 1;
} while (!absolutePath);
Copy link
Member

@Janpot Janpot Mar 28, 2023

Choose a reason for hiding this comment

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

Is it possible to use the inquirer validate function to check the path before scaffolding the project? That way we can avoid the complex control flow above with the do/while and process.exit.

const absolutePath = path.join(process.cwd(), relativePath);

try {
await fs.mkdir(absolutePath);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
await fs.mkdir(absolutePath);
await fs.mkdir(absolutePath, { recursive: true });

Copy link
Member Author

Choose a reason for hiding this comment

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

From https://nodejs.org/api/fs.html#fspromisesmkdirpath-options

Calling fsPromises.mkdir() when path is a directory that exists results in a rejection only when recursive is false

I want to be able to detect when a directory exists on calling mkdir so that I am able to catch that error and then call isDirectoryEmpty on the existing directory, but setting recursive to true causes me to miss that case

Copy link
Member

Choose a reason for hiding this comment

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

Won't you need recursive if you pass a nested directory to the CLI? e.g.

yarn create toolpad-app some/nested/dir

I'd just validate the path before calling fs.mkdir instead of after


return true;
} catch {
// Directory exists, verify if it's empty to proceed
Copy link
Member

Choose a reason for hiding this comment

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

Directory exists

Not necessarily, to be sure, check the error code and rethrow if it's not EEXIST

absolutePath,
)} contains files that could conflict. Either use a new directory, or remove conflicting files.`;
} catch {
return `${chalk.red('error')} - Unable to access directory at ${chalk.red(
Copy link
Member

Choose a reason for hiding this comment

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

This way there is no feedback to the user about what went wrong exactly, since it's unexpected, I'd probably just let the error bubble up and crash the program.

return `${chalk.red('error')} - The directory at ${chalk.blue(
absolutePath,
)} contains files that could conflict. Either use a new directory, or remove conflicting files.`;
} catch (error: any) {
Copy link
Member

@Janpot Janpot Apr 8, 2023

Choose a reason for hiding this comment

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

Avoid any.

Suggested change
} catch (error: any) {
} catch (rawError: unknown) {
const error = errorFrom(rawError);

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you recommend importing errorFrom from @mui/toolpad-core or rewriting that function here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added something local to avoid adding dependencies to this package

@Janpot Janpot enabled auto-merge (squash) April 20, 2023 15:05
@Janpot Janpot merged commit 2e7c0b2 into master Apr 20, 2023
@Janpot Janpot deleted the cta-folder-name branch April 20, 2023 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Infrastructure work going on behind the scenes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants