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

feat: tutorialkit eject command #81

Merged
merged 31 commits into from
Jul 4, 2024
Merged

feat: tutorialkit eject command #81

merged 31 commits into from
Jul 4, 2024

Conversation

Nemikolh
Copy link
Member

This PR adds an eject command to the tutorialkit CLI so that you can get back full control if you ever need to.

It works by modifying the astro.config.ts file to turn off the defaultRoutes and then copy the content of @tutorialkit/astro/dist/default into the src folder of the TutorialKit project.

I renamed env.d.ts in astro/src/default/ to env-default.d.ts so that they are no collision as they have different content. Ideally we should merge them but having two separate file is simpler to start with.

Note that --force will be required because of the env.d.ts for TutorialKit project that have already be generated. The collision will only be avoided for future projects.

Copy link

stackblitz bot commented Jun 19, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link

changeset-bot bot commented Jun 19, 2024

⚠️ No Changeset found

Latest commit: e8a1f7a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

@d3lm d3lm left a comment

Choose a reason for hiding this comment

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

Cool 👏. Minor remarks.

packages/cli/src/commands/eject/index.ts Outdated Show resolved Hide resolved
packages/cli/src/commands/eject/index.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@SamVerschueren SamVerschueren left a comment

Choose a reason for hiding this comment

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

Added some comments and questions.

Can we add a test for this as well? I think it should be pretty straight forward in the create-tutorial.test.ts by creating the project, ejecting, and taking a snapshot.


const supportedCommands = new Set(['version', 'help', 'create']);
const supportedCommands = new Set<string>(['version', 'help', 'create', 'eject'] satisfies CLICommand[]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't we just type this as Set<CLICommand>?

Copy link
Member Author

Choose a reason for hiding this comment

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

We can't because then we get a type error below when trying to do supportedCommands.has(string).

The reason I made that change was to make sure the values are checked against the CLICommand type.

packages/cli/src/commands/eject/index.ts Outdated Show resolved Hide resolved
packages/cli/src/utils/astro-config.ts Show resolved Hide resolved
packages/cli/src/commands/eject/index.ts Outdated Show resolved Hide resolved
Copy link

cloudflare-workers-and-pages bot commented Jun 20, 2024

Deploying tutorialkit-demo-page with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0e2b6d8
Status: ✅  Deploy successful!
Preview URL: https://a52b6d9b.tutorialkit-demo-page.pages.dev
Branch Preview URL: https://joan-eject-command.tutorialkit-demo-page.pages.dev

View logs

Copy link

cloudflare-workers-and-pages bot commented Jun 20, 2024

Deploying tutorialkit-docs-page with  Cloudflare Pages  Cloudflare Pages

Latest commit: 75925a8
Status: ✅  Deploy successful!
Preview URL: https://57f5341d.tutorialkit-docs-page.pages.dev
Branch Preview URL: https://joan-eject-command.tutorialkit-docs-page.pages.dev

View logs


for (const dep of REQUIRED_DEPENDENCIES) {
if (!(dep in pkgJson.dependencies) && !(dep in pkgJson.devDependencies)) {
pkgJson.dependencies[dep] = astroIntegrationPkgJson.dependencies[dep];
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe not super important for now, but we might want to check the version of the dependency and warn the user that they might need to upgrade/downgrade or something.

Copy link
Member Author

Choose a reason for hiding this comment

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

We do! The let hasChanged = false; is used for that purpose.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hm so technically this can result in false positives if say I have nanostores in my package.json already but the range just doesn't match but the version would be the same, e.g.

^1.1.1 !== 1.1.1

Given that there's no new patch version and 1.1.1 is the latest. Wouldn't it be better to check node_modules or check the lock file? But maybe this is good enough and a warning is good in any case which would force the user to double check the dependency.

Copy link
Member Author

Choose a reason for hiding this comment

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

If they have an incompatible version of nanostores, there's not much we can do as they'll have to update the code they've just ejected. Hopefully there's a TypeScript error so that they know.

If it's compatible, then this code uses their version, so I think this code is good?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yea I think it's fine cause we only show the warning if that dependency is missing entirely in the package.json. I was just wondering if we can be smarter here and maybe check the version and print a hint if the version doesn't match to tell them that there may be incompatibilities.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah good point! Oh yeah that would be really nice actually.

Copy link
Contributor

Choose a reason for hiding this comment

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

One possible way would be to leverage npm why --json to see what version is installed and what version we require.

if (hasChanged) {
fs.writeFileSync(pkgJsonPath, JSON.stringify(pkgJson, undefined, 2), { encoding: 'utf-8' });

console.log(primaryLabel('INFO'), 'New dependencies added, install the new dependencies before proceeding');
Copy link
Contributor

Choose a reason for hiding this comment

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

Technically we could do this for them. Infer the package manager based on the lock file or something, or ignore if there are no node_modules yet? But I'm fine with this info for now.

Copy link
Member Author

Choose a reason for hiding this comment

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

We could indeed to it for them but I kept it simple for now.

If there are no node_modules then the eject command won't work because it moves the files of the version of @tutorialkit/astro that they use (instead of using a possibily incompatible one).

@Nemikolh Nemikolh requested a review from AriPerkkio June 25, 2024 09:35
Copy link
Contributor

@d3lm d3lm left a comment

Choose a reason for hiding this comment

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

Nice one. Left some minor comments.

packages/cli/src/commands/eject/index.ts Outdated Show resolved Hide resolved
packages/cli/src/commands/eject/index.ts Show resolved Hide resolved
packages/cli/src/commands/eject/index.ts Outdated Show resolved Hide resolved

for (const dep of REQUIRED_DEPENDENCIES) {
if (!(dep in pkgJson.dependencies) && !(dep in pkgJson.devDependencies)) {
pkgJson.dependencies[dep] = astroIntegrationPkgJson.dependencies[dep];
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm so technically this can result in false positives if say I have nanostores in my package.json already but the range just doesn't match but the version would be the same, e.g.

^1.1.1 !== 1.1.1

Given that there's no new patch version and 1.1.1 is the latest. Wouldn't it be better to check node_modules or check the lock file? But maybe this is good enough and a warning is good in any case which would force the user to double check the dependency.

packages/cli/src/commands/eject/index.ts Outdated Show resolved Hide resolved
packages/cli/src/commands/eject/index.ts Outdated Show resolved Hide resolved
packages/cli/tests/create-tutorial.test.ts Show resolved Hide resolved
packages/cli/tests/create-tutorial.test.ts Show resolved Hide resolved
packages/cli/tests/create-tutorial.test.ts Show resolved Hide resolved
@Nemikolh Nemikolh requested a review from d3lm June 26, 2024 09:49
Copy link
Contributor

@SamVerschueren SamVerschueren left a comment

Choose a reason for hiding this comment

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

Awesome work on this @Nemikolh 🙏 !

Copy link
Contributor

@d3lm d3lm left a comment

Choose a reason for hiding this comment

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

👏

packages/cli/package.json Outdated Show resolved Hide resolved
packages/cli/src/commands/eject/index.ts Outdated Show resolved Hide resolved
packages/cli/src/commands/eject/index.ts Outdated Show resolved Hide resolved
@Nemikolh Nemikolh merged commit c802668 into main Jul 4, 2024
9 checks passed
@Nemikolh Nemikolh deleted the joan/eject-command branch July 4, 2024 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants