Fix create-app package names for path targets#11
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a derivePackageName utility to handle package name generation from project names or paths, including support for scoped packages. It updates the scaffolding logic to use this utility when writing to package.json and adds relevant unit tests. Feedback suggests trimming the project name input and ensuring derived package names are lowercased to meet NPM standards.
| @@ -67,6 +68,7 @@ program | |||
| const projectName = response.projectName || name; | |||
There was a problem hiding this comment.
Trimming the projectName ensures that leading or trailing whitespace from user input doesn't result in unexpected directory names or invalid package metadata. This improves the robustness of the scaffolding process.
| const projectName = response.projectName || name; | |
| const projectName = (response.projectName || name).trim(); |
| const trimmedName = inputName.trim(); | ||
|
|
||
| if (scopedPackagePattern.test(trimmedName)) { | ||
| return trimmedName.replace("\\", "/"); |
There was a problem hiding this comment.
NPM package names must be lowercase. Lowercasing the scoped name here ensures that the generated package.json adheres to NPM naming requirements, even if the user provided uppercase characters in the input.
| return trimmedName.replace("\\", "/"); | |
| return trimmedName.replace("\\", "/").toLowerCase(); |
| return trimmedName.replace("\\", "/"); | ||
| } | ||
|
|
||
| return path.basename(path.resolve(targetDir)); |
There was a problem hiding this comment.
Similar to scoped names, the basename of the target directory should be lowercased to ensure a valid NPM package name. This prevents issues where a directory named with uppercase letters would otherwise produce an invalid package.json.
| return path.basename(path.resolve(targetDir)); | |
| return path.basename(path.resolve(targetDir)).toLowerCase(); |
Motivation
/tmp/my-app) intopackage.json.name, producing invalid npm package metadata and breaking first-run DX.namederived from the target directory while preserving scoped package names like@scope/app.Description
derivePackageNameutility atpackages/create-app/src/project-name.tsthat returns a scoped name unchanged or the basename of the resolvedtargetDirfor path-like names.derivePackageNameinpackages/create-app/src/index.tsto computepackageNameand setprojPkg.name = packageNamewhen post-processing templatepackage.jsonfiles.packages/create-app/tests/scaffold.test.tsto cover simple names, absolute/nested path inputs, and scoped package names to prevent regressions.@evjs/*versions from workspace to^pkg.version).Testing
npm run build --workspace @evjs/create-app, which completed successfully.package.jsonname withnode packages/create-app/dist/index.js /tmp/evjs-dx-real-app-fixed --template basicandnode -e "console.log(JSON.parse(require('fs').readFileSync('/tmp/evjs-dx-real-app-fixed/package.json','utf8')).name)", which showed the expected basename value.npm run test --workspace @evjs/create-appfailed in this environment becausevitestis not available (sh: 1: vitest: not found).biomeis not installed (sh: 1: biome: not found).Codex Task