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

[cli] Introduce --core #3304

Merged
merged 17 commits into from
Mar 26, 2024
Merged

[cli] Introduce --core #3304

merged 17 commits into from
Mar 26, 2024

Conversation

bharatkashyap
Copy link
Member

@bharatkashyap bharatkashyap commented Mar 19, 2024

  • Running create-toolpad-app --core will bootstrap an empty Next.js application with the following file structure:

    app/
      api/
        auth/
          [...nextauth]/
            route.ts
      auth/
        [...path]/
          page.tsx
      (dashboard)/
        page/
          page.tsx
        layout.tsx
      layout.tsx
    theme.ts
    .gitignore
    tsconfig.json
    .eslintrc.json
    next.config.mjs
    package.json
    
  • Placeholder start page: Screenshot 2024-03-19 at 10 51 22 PM

  • Screen.Recording.2024-03-19.at.10.48.56.PM.mov

@bharatkashyap bharatkashyap added the create-toolpad-app Issues related to the `create-toolpad-app` CLI tool label Mar 19, 2024
Copy link
Member

@apedroferreira apedroferreira left a comment

Choose a reason for hiding this comment

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

Looks good!
I left some comments but nothing necessarily blocking I think.

// eslint-disable-next-line no-console
console.log();

const packageJson: PackageJson = {
Copy link
Member

Choose a reason for hiding this comment

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

Could we have the generated template as a folder instead of all in the file? Probably would be easier to work on.
For example Next.js and Vite have it like that: https://github.com/vercel/next.js/tree/canary/packages/create-next-app/templates https://github.com/vitejs/vite/tree/main/packages/create-vite

Copy link
Member Author

Choose a reason for hiding this comment

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

Jan and I did discuss this and felt like we could eventually move to this structure, but begin with strings for simplicity and efficiency

Copy link
Member

@Janpot Janpot Mar 21, 2024

Choose a reason for hiding this comment

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

If the idea is that in the future we will do things like pre-configure different auth providers, we're going to need to have some templating mechanism. e.g. to have different modules in the package json, or to generate the code that initializes the providers.
I believe this mechanism will be much easier to write and maintain when it's just composition in functions, rather than try to adjust pre-existing file content from a template file.

It'll also make it easier to do things like re-using the templating logic to generate codesandboxes in the browser.

return (
<html lang="en">
<body>
<AppRouterCacheProvider>
Copy link
Member

Choose a reason for hiding this comment

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

Here will just need to use the AppProvider from @toolpad/core:

  <html lang="en">
      <body>
        <AppProvider theme={theme}>{props.children}</AppProvider>
      </body>
    </html>

It should be ready in #3291 I'm just sorting out some CI issues.
I can replace it if I merge after.

await execaCommand(command, { stdio: 'inherit', cwd: absolutePath });

// Create the `app` directory
await fs.mkdir(path.join(absolutePath, 'app'));
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we could use Promise.all to make it faster? Probably not a huge difference though.

display: "swap",
});

const theme = createTheme({
Copy link
Member

Choose a reason for hiding this comment

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

All looks good, I was using the exact same project setup when setting up @toolpad/core!

`;

// Create the `theme.ts` file in the root directory
await fs.writeFile(path.join(absolutePath, 'theme.ts'), themeContent);
Copy link
Member

@apedroferreira apedroferreira Mar 20, 2024

Choose a reason for hiding this comment

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

So we're not using an src folder? I prefer to use one just to organize the files a bit more but any option is fine, we just never discussed it.

@@ -36,7 +36,7 @@ function getPackageManager(): PackageManager {
return 'npm';
}
}
return 'yarn';
return 'pnpm';
Copy link
Member

Choose a reason for hiding this comment

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

The default should probably be npm as that is bundled with each node installation (at least for now)


const installVerb = 'install';
const command = `${packageManager} ${installVerb}`;
await execaCommand(command, { stdio: 'inherit', cwd: absolutePath });
Copy link
Member

@Janpot Janpot Mar 21, 2024

Choose a reason for hiding this comment

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

If possible, I'd use the safer

await execa(packageManager, ['install'], { stdio: 'inherit', cwd: absolutePath });

command. It'll correctly escape arguments. See https://www.npmjs.com/package/execa#execafile-arguments-options

Copy link
Member Author

Choose a reason for hiding this comment

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

https://www.npmjs.com/package/execa#execacommandcommand-options also provides automatic escaping of arguments - is execa safer in any other way?

Copy link
Member

Choose a reason for hiding this comment

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

it escapes spaces as well

fs.mkdir(path.join(absolutePath, dirPath), { recursive: true }),
),
);
} catch (error: any) {
Copy link
Member

@Janpot Janpot Mar 21, 2024

Choose a reason for hiding this comment

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

You can remove this try/catch. mkdir doesn't throw EEXIST when recursive is true

Suggested change
} catch (error: any) {

Also the try/catch is around the Promise.all meaning it triggers immediately on the first error. That doesn't mean the other items are finished creating. This sort of logic would've belonged inside of the loop.

Copy link
Member

@Janpot Janpot left a comment

Choose a reason for hiding this comment

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

Looks good!

Suggestion: to clean it up a bit I'd consider extracting the logic in separate functions.

function generateProject(options): Map<string, { content: string }>;
async function writeFiles(path, files: Map<string, { content: string }>): void;

And place each in their own module.

Copy link
Member

@apedroferreira apedroferreira left a comment

Choose a reason for hiding this comment

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

Looks great!

import path from 'path';
import fs from 'fs/promises';

export default async function writeCoreFiles(
Copy link
Member

Choose a reason for hiding this comment

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

Is this function really specific to core? Seems quite general to me

Suggested change
export default async function writeCoreFiles(
export default async function writeFiles(

@bharatkashyap bharatkashyap merged commit c93afe3 into mui:master Mar 26, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
create-toolpad-app Issues related to the `create-toolpad-app` CLI tool
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants