-
Notifications
You must be signed in to change notification settings - Fork 341
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
Conversation
1. 修复生成的平台模板仍然使用 useResourceAlias 配置的 bug 2. 修复生成的 package.json 未格式化的 bug
WalkthroughThe changes in this pull request involve two main modifications: an enhancement to the Changes
Possibly related PRs
Suggested labels
Suggested reviewers
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
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 consistencyWhile the current implementation works well, here are a few suggestions to further improve the
createPlatform
function:
For consistency, consider using
fs.outputFileSync
for bothengine.config.js
andpackage.json
. You can useJSON.stringify(pkgContent, null, 2)
to achieve the same formatting.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.
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 aligningcreatePlugin
withcreatePlatform
improvementsWhile not directly related to the PR changes, the
createPlugin
function could benefit from similar improvements as suggested forcreatePlatform
:
- Add error handling for file system operations.
- Use async/await for consistency with an improved
createPlatform
function.- 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 improvedcreatePlatform
function.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 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 tofs.outputJSONSync
is a great enhancement. This change will make the generatedpackage.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 forcreate.js
The main change to improve JSON formatting in
createPlatform
has been approved. It effectively addresses one of the PR objectives.Suggestions for further improvements have been provided for both
createPlatform
andcreatePlugin
functions, focusing on error handling, consistency, and code robustness.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.
English | 简体中文
PR
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
Background and solution
What is the current behavior?
Issue Number: N/A
What is the new behavior?
Does this PR introduce a breaking change?
Other information
Summary by CodeRabbit
New Features
package.json
file.Bug Fixes