Skip to content

Feat/template based project creation #3

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

cxalem
Copy link
Collaborator

@cxalem cxalem commented Apr 22, 2025

Refactor: Use degit for Template Cloning and Add Tests

Context:

The previous CLI implementation manually created project files and injected dependencies after running create-next-app or create-vite. This approach was brittle, hard to maintain, and diverged from using standardized templates.

The goal is to leverage official example templates directly. The primary target template, the Next.js quickstart example, resides as a subdirectory within the MetaMask/metamask-sdk-examples monorepo. Standard git clone is unsuitable as it would download the entire monorepo.

Changes:

This PR refactors the core project creation logic to use templates, specifically integrating degit for fetching subdirectories from Git repositories.

  1. Template Cloning Refactor:
    • Removed the legacy manual file generation logic (legacy_code/next.helpers.ts, legacy_code/vite.helpers.ts).
    • Updated src/utils/index.ts (cloneTemplate function) to handle template fetching.
  2. Degit Integration:
    • Added degit as a dependency.
    • Updated src/constants/templates.ts to support a degitSource property alongside the existing repo_url. The Next.js Quickstart template now uses degitSource: "MetaMask/metamask-sdk-examples/examples/quickstart".
    • Modified cloneTemplate to check if a template uses degitSource or repo_url and uses the appropriate tool (degit or git clone via execAsync).
    • Added type guards (isDegitTemplate, isGitTemplate) for clarity.
  3. Testing:
    • Introduced unit/integration tests for core utilities (promptForOptions, cloneTemplate, initializeMonorepo, createProject) using vitest in src/utils/index.test.ts.
    • Implemented mocking for fs, inquirer, execAsync, and degit using vi.mock.
    • Refined mocking strategy to address ESM hoisting issues, ensuring mocks are correctly initialized and reset using beforeEach.
    • Tests cover both the degit and git clone paths within cloneTemplate, as well as standalone vs. monorepo project creation flows in createProject (Not working by now, but will work on testing in following iterations).

How to Test:

  1. Build the CLI: npm run build (or yarn build)
  2. Link the CLI locally: npm run link-cli (or yarn link-cli)
  3. Run the tool: create-web3-app my-degit-test
  4. Select the "Next.js Quickstart (MetaMask SDK Example)" template and desired package manager/tooling.
  5. Verify the created project (my-degit-test or my-degit-test/packages/site) structure and contents match the quickstart example from the metamask-sdk-examples repo.
  6. Run the automated tests: npm test (or yarn test). All tests should pass.

Notes:

  • The "React Web3 Starter" template definition currently still uses the old repo_url and will be cloned using git clone. This can be updated to use degit if/when a suitable React example is available in the examples monorepo.
  • The legacy helper files remain in src/legacy_code for reference but are no longer used.

cxalem added 2 commits April 22, 2025 15:36
- Updated package.json to include 'degit' and its types.
- Modified tsconfig.json to exclude legacy code directories.
- Enhanced template handling in constants and utils, allowing for both degit and git templates.
- Introduced new project creation helpers for React and Next.js with improved structure.
- Added tests for utility functions to ensure proper functionality and error handling.
- Created legacy code files for future reference and potential migration.
@cxalem cxalem requested a review from chakra-guy April 22, 2025 14:27
@andreolf
Copy link
Member

👀👀

- Changed package name from @metamask/create-web3-app to @consensys/create-web3-app.
- Updated version in package.json from 1.0.0 to 1.0.3.
- Introduced a new optional field for Dynamic Environment ID in ProjectOptions.
- Enhanced template handling to include a new MetaMask dynamic template.
- Updated utility functions to support the new options and improved error handling.
- Added spinner feedback for better user experience during project setup.
Copy link

socket-security bot commented Apr 28, 2025

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedora@​8.2.09910010079100

View full report

Copy link

socket-security bot commented Apr 28, 2025

All alerts resolved. Learn more about Socket for GitHub.

This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored.

View full report

cxalem and others added 11 commits April 28, 2025 12:03
- Removed 'degit' from dependencies and added it back to devDependencies for better organization.
- Updated the validation logic to ensure project names are not only non-empty but also conform to kebab-case format (e.g., my-awesome-project).
- Created a new workflow to automate the release publishing process on pushes to the main branch.
- Includes jobs for building the project, uploading artifacts, performing a dry run of the npm publish, and executing the actual publish.
- Added steps to retrieve the release version from package.json.
- Commented out documentation publishing jobs for future implementation.
- Changed package name from @consensys/create-web3-app to @metamask/create-web3-app.
- Updated version from 1.0.6 to 1.0.0.
- Added build script to the scripts section.
- Introduced publishConfig for npm registry settings.
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