-
-
Notifications
You must be signed in to change notification settings - Fork 287
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
[cli] Introduce --core
#3304
Conversation
There was a problem hiding this 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 = { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
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')); |
There was a problem hiding this comment.
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({ |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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 }); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
} 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.
There was a problem hiding this 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.
There was a problem hiding this 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( |
There was a problem hiding this comment.
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
export default async function writeCoreFiles( | |
export default async function writeFiles( |
Running
create-toolpad-app --core
will bootstrap an empty Next.js application with the following file structure:Placeholder start page:
Screen.Recording.2024-03-19.at.10.48.56.PM.mov