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

fix: cli generate platform issue #859

Merged
merged 1 commit into from
Oct 15, 2024

Conversation

chilingling
Copy link
Member

@chilingling chilingling commented Oct 15, 2024

English | 简体中文

PR

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • Built its own designer, fully self-validated

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

Background and solution

What is the current behavior?

  1. 修复生成的平台模板仍然使用 useResourceAlias 配置的 bug
  2. 修复生成的 package.json 未格式化的 bug

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

  • New Features

    • Improved JSON output readability for platform creation, making it easier to read the generated package.json file.
  • Bug Fixes

    • Removed unnecessary configuration property to streamline the setup process for users.

1. 修复生成的平台模板仍然使用 useResourceAlias 配置的 bug
2. 修复生成的 package.json 未格式化的 bug
Copy link
Contributor

coderabbitai bot commented Oct 15, 2024

Walkthrough

The changes in this pull request involve two main modifications: an enhancement to the createPlatform function in create.js for better JSON output readability by adding formatting options, and the removal of the useSourceAlias property from the configuration object in vite.config.js. These updates aim to improve code clarity and maintainability.

Changes

File Path Change Summary
packages/engine-cli/src/commands/create.js Updated createPlatform function to enhance JSON output readability by adding formatting options.
packages/engine-cli/template/designer/vite.config.js Removed useSourceAlias property from the configuration object in useTinyEngineBaseConfig.

Possibly related PRs

Suggested labels

bug, ready_for_review, refactor-main

Suggested reviewers

  • hexqi
  • gene9831

🐇 In the code we hop and play,
With JSON neat, we pave the way.
The useSourceAlias is no more,
A cleaner path we now explore!
Let's code with joy, let errors flee,
For every change, a victory! 🎉


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added bug Something isn't working refactor-main refactor/develop branch feature labels Oct 15, 2024
@chilingling chilingling added the ready_for_review This PR requires more reviews label Oct 15, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
packages/engine-cli/src/commands/create.js (2)

Line range hint 26-52: Consider enhancing error handling and consistency

While the current implementation works well, here are a few suggestions to further improve the createPlatform function:

  1. For consistency, consider using fs.outputFileSync for both engine.config.js and package.json. You can use JSON.stringify(pkgContent, null, 2) to achieve the same formatting.

  2. Enhance error handling by wrapping file system operations in try-catch blocks. This will provide more informative error messages if something goes wrong during file creation or copying.

  3. Consider using a logging library or a custom logger instead of console.log directly. This will make it easier to manage logs across different environments.

Here's an example of how you might implement these suggestions:

import { promises as fsPromises } from 'fs'

// ... (rest of the imports)

const logger = console // Consider replacing this with a proper logging library

export async function createPlatform(name, options = {}) {
  const destPath = path.join(cwd(), name)

  try {
    if (await fsPromises.access(destPath).then(() => true).catch(() => false)) {
      throw new Error(`The ${name} folder already exists.`)
    }

    const mergedOptions = { ...defaultOptions, ...options }
    const templatePath = path.join(__dirname, '../template/designer/')

    await fsPromises.cp(templatePath, destPath, { recursive: true })

    const configContent = generateConfig(mergedOptions)
    const pkgContent = generatePackageJson(name, mergedOptions, templatePath)

    await fsPromises.writeFile(path.resolve(destPath, 'engine.config.js'), configContent)
    await fsPromises.writeFile(path.resolve(destPath, 'package.json'), JSON.stringify(pkgContent, null, 2))

    logger.log(chalk.green(`Create finished. Run the following command to start the project:\ncd ${name} && npm install && npm run dev`))
  } catch (error) {
    logger.error(chalk.red(`Creation failed: ${error.message}`))
  }
}

This refactored version uses async/await for better readability and error handling, and it uses consistent file writing methods.


Line range hint 54-61: Consider aligning createPlugin with createPlatform improvements

While not directly related to the PR changes, the createPlugin function could benefit from similar improvements as suggested for createPlatform:

  1. Add error handling for file system operations.
  2. Use async/await for consistency with an improved createPlatform function.
  3. Check if the destination folder already exists before copying.

Here's a suggested implementation:

export async function createPlugin(name) {
  const sourcePath = path.join(__dirname, '../template/plugin/')
  const destPath = path.join(cwd(), name)

  try {
    if (await fsPromises.access(destPath).then(() => true).catch(() => false)) {
      throw new Error(`The ${name} folder already exists.`)
    }

    await fsPromises.cp(sourcePath, destPath, { recursive: true })

    logger.log(chalk.green(`Create finished. Run the following command to start the project:\ncd ${name} && npm install && npm run dev`))
  } catch (error) {
    logger.error(chalk.red(`Creation failed: ${error.message}`))
  }
}

This change would make createPlugin more robust and consistent with the improved createPlatform function.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between a508255 and 3d41e23.

📒 Files selected for processing (2)
  • packages/engine-cli/src/commands/create.js (1 hunks)
  • packages/engine-cli/template/designer/vite.config.js (0 hunks)
💤 Files with no reviewable changes (1)
  • packages/engine-cli/template/designer/vite.config.js
🧰 Additional context used
🔇 Additional comments (2)
packages/engine-cli/src/commands/create.js (2)

49-49: Excellent improvement to JSON formatting!

The addition of { spaces: 2 } as an option to fs.outputJSONSync is a great enhancement. This change will make the generated package.json file more readable and easier to maintain. It aligns well with best practices for generating human-readable JSON files and addresses one of the PR objectives.


Line range hint 1-61: Summary of review for create.js

  1. The main change to improve JSON formatting in createPlatform has been approved. It effectively addresses one of the PR objectives.

  2. Suggestions for further improvements have been provided for both createPlatform and createPlugin functions, focusing on error handling, consistency, and code robustness.

  3. While these additional suggestions are not directly related to the PR's main objective, implementing them could significantly enhance the overall quality and maintainability of the code.

Overall, the PR change is a positive improvement, and the additional suggestions, if implemented, would further elevate the code quality.

@yy-wow yy-wow merged commit 0e8dc1f into opentiny:refactor/develop Oct 15, 2024
6 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Oct 29, 2024
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ready_for_review This PR requires more reviews refactor-main refactor/develop branch feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants