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

Invert direction of dependency between @faststore/core and @faststore/cli #2377

Merged
merged 15 commits into from
Jul 18, 2024

Conversation

gvc
Copy link
Contributor

@gvc gvc commented Jul 9, 2024

What's the purpose of this pull request?

This PR aims to make @faststore/core a dependency of the @faststore/cli. It just makes sense. Someone can fork @faststore/core and never need to use the CLI, but there's hardly any usefulness to the CLI without @faststore/core.

How it works?

The only thing linking Core to the CLI was the generate-graphql command on the CLI. It just called back 4 scripts on core's package.json: generate:schema, generate:codegen, format:generated and generate:copy-back. The copy-back command was only needed to improve the DX of customers customizing their GraphQL schema [ref] and it was the only part of the script that needed information available at the CLI level only.

Since it was only a DX improvement, all that was needed was, during the faststore dev execution I had to generate the new schema, copy the files to the correct node_modules dir, and then start the NextJS dev server. In order to do this I split the dev script into predev, dev, and dev-only*. From the FastStore CLI I could now call yarn predev, copy the files to the correct place, and then run yarn dev-only to start the NextJS dev server.

Someone developing on the faststore monorepo (us VTEX developers, or someone contributing to our project) will continue to run core via yarn dev. npm, yarn, and pnpm support the lifecycle scripts out of the box. I did the same thing to build for equivalence.

One thing that can be up to discussion is the existence of runCommandSync util. It was only used on generate-graphql and can be easily removed (in this PR or in a future one).

How to test it?

Add this PR @faststore/cli from codesandbox as a dependency of the starter store, remove @faststore/core as an explicit dependency, and @faststore/cli as a devDependency. Run your store and see that everything still works fine. Or go to the starter preview [PR] here and see the store running in "production".

Copy link

vercel bot commented Jul 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
faststore-site ⬜️ Ignored (Inspect) Visit Preview Jul 17, 2024 7:56pm

Copy link

codesandbox-ci bot commented Jul 9, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@gvc gvc changed the title [WIP]] Invert Core -> CLI dependency Invert direction of dependency between @faststore/core and @faststore/cli Jul 11, 2024
@gvc gvc marked this pull request as ready for review July 11, 2024 00:12
@gvc gvc requested a review from a team as a code owner July 11, 2024 00:12
@gvc gvc requested review from emersonlaurentino and lariciamota and removed request for a team July 11, 2024 00:12
@gvc gvc self-assigned this Jul 11, 2024
@gvc gvc requested a review from vaporwavie July 11, 2024 00:40
Comment on lines +21 to +22
"dev": "next dev",
"dev-only": "next dev",
Copy link
Member

Choose a reason for hiding this comment

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

the same? Why 2 scripts?
edit: I think this is intentional. So if we need to change, we will change the dev/predev, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey, I thought I had given you an answer here.

Yes it's intentional and if we need to change something that should happen before the next dev call, we should add it to the predev script, never directly to the dev one.

@@ -13,17 +13,19 @@
"scripts": {
"generate:schema": "tsx src/server/generator/generateGraphQLSchemaFile.ts",
"generate:codegen": "graphql-codegen",
"generate:copy-back": "copyfiles \"@generated/**/*\" $DESTINATION",
"generate": "faststore generate-graphql -c -d",
Copy link
Member

Choose a reason for hiding this comment

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

I miss the -d (debug) flag.
Historically, some clients complain about generic errors in the generated graphql process. This debug flag adds more visibility into the graphql generation flow. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I belive that the flag and better error messages are two different problems, but I'll add this back

Copy link
Member

Choose a reason for hiding this comment

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

I linked these two topics because we previously saw the specific error messages when using the flag.

packages/cli/src/commands/dev.ts Outdated Show resolved Hide resolved
runCommandSync({
cmd: 'yarn predev',
errorMessage:
'GraphQL was not optimized and TS files were not updated. Changes in the GraphQL layer did not take effect',
Copy link
Contributor

Choose a reason for hiding this comment

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

question: here other commands are run, right? yarn generate:schema && yarn generate:codegen && yarn format:generated not only the yarn generate:codegen. What would happen if only the format returned error? It would fail but the previous commands would have made changes on the files?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but the changes are made to ephemeral files so, theoretically, no harm is done. Because of throws: 'error' here, the behavior is the same. Whatever step fails there will be no rollback.


return { success: true }
} catch (err) {
return { success: false }
Copy link
Contributor

Choose a reason for hiding this comment

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

question: an error to copy would abort the command run?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should not. But I believe you asked this question because the code is returning this and we're not using it anywhere else. I'll adjust the code to warn the user of the impact of the failure to copy.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks!

Copy link
Member

@eduardoformiga eduardoformiga left a comment

Choose a reason for hiding this comment

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

LGTM!
It worked as expected!
I've tested in the playground and starter stores.

remove @faststore/core as an explicit dependency,

One thing to pay attention:
It's very uncommon to remove the core dependency because some functions from @faststore/core are used in our features as API extensions, Section Overrides etc.

but I've tested in both scenarios (with and without the core) and it worked :)

@gvc
Copy link
Contributor Author

gvc commented Jul 17, 2024

@eduardoformiga

It's very uncommon to remove the core dependency because some functions from @faststore/core are used in our features as API extensions, Section Overrides etc.

but since the CLI depends on Core, it will always be there :)

@gvc gvc merged commit e95b7e7 into main Jul 18, 2024
7 checks passed
@gvc gvc deleted the invert-cli-core-dependency branch July 18, 2024 13:13
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