-
Notifications
You must be signed in to change notification settings - Fork 4.9k
feat(cli): support init in current directory #19273
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
Jarvis636431
wants to merge
2
commits into
NervJS:main
Choose a base branch
from
Jarvis636431:init-current-directory
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+104
−2
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import Project from '../create/project' | ||
|
|
||
| jest.mock('@tarojs/helper', () => ({ | ||
| chalk: { | ||
| blueBright: jest.fn((text) => text), | ||
| green: jest.fn((text) => text), | ||
| red: jest.fn((text) => text) | ||
| }, | ||
| DEFAULT_TEMPLATE_SRC: 'https://github.com/NervJS/taro-project-templates', | ||
| DEFAULT_TEMPLATE_SRC_GITEE: 'https://gitee.com/o2team/taro-project-templates', | ||
| fs: { | ||
| ensureDirSync: jest.fn(), | ||
| existsSync: jest.fn(() => true) | ||
| }, | ||
| getUserHomeDir: jest.fn(() => '/tmp'), | ||
| SOURCE_DIR: 'src', | ||
| TARO_BASE_CONFIG: 'index.json', | ||
| TARO_CONFIG_FOLDER: '.taro' | ||
| })) | ||
|
|
||
| jest.mock('@tarojs/shared', () => ({ | ||
| isArray: Array.isArray | ||
| }), { virtual: true }) | ||
|
|
||
| jest.mock('@tarojs/binding', () => ({ | ||
| CompilerType: { | ||
| Solid: 'solid', | ||
| Vite: 'vite', | ||
| Webpack5: 'webpack5' | ||
| }, | ||
| createProject: jest.fn(() => Promise.resolve()), | ||
| CSSType: { | ||
| None: 'none' | ||
| }, | ||
| FrameworkType: { | ||
| Preact: 'preact', | ||
| React: 'react', | ||
| Solid: 'solid', | ||
| Vue3: 'vue3' | ||
| }, | ||
| NpmType: { | ||
| Npm: 'npm' | ||
| }, | ||
| PeriodType: { | ||
| CreateAPP: 'createApp' | ||
| } | ||
| })) | ||
|
|
||
| describe('create project', () => { | ||
| let logSpy: jest.SpyInstance | ||
|
|
||
| beforeEach(() => { | ||
| logSpy = jest.spyOn(console, 'log').mockImplementation() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| logSpy.mockRestore() | ||
| }) | ||
|
|
||
| it('should resolve dot project name to current directory', () => { | ||
| const project = new Project({ | ||
| projectDir: '/a/b/current-project', | ||
| projectName: '.', | ||
| sourceRoot: __dirname, | ||
| template: 'default', | ||
| templateSource: 'default-template', | ||
| npm: 'npm', | ||
| css: 'none', | ||
| framework: 'react' | ||
| } as any) | ||
|
|
||
| expect(project.conf.projectDir).toBe('/a/b') | ||
| expect(project.conf.projectName).toBe('current-project') | ||
| }) | ||
|
|
||
| it('should resolve current directory path project name to current directory', () => { | ||
| const project = new Project({ | ||
| projectDir: '/a/b/current-project', | ||
| projectName: './', | ||
| sourceRoot: __dirname, | ||
| template: 'default', | ||
| templateSource: 'default-template', | ||
| npm: 'npm', | ||
| css: 'none', | ||
| framework: 'react' | ||
| } as any) | ||
|
|
||
| expect(project.conf.projectDir).toBe('/a/b') | ||
| expect(project.conf.projectName).toBe('current-project') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🧩 Analysis chain
🏁 Script executed:
Repository: NervJS/taro
Length of output: 2553
🏁 Script executed:
Repository: NervJS/taro
Length of output: 250
🏁 Script executed:
Repository: NervJS/taro
Length of output: 250
🏁 Script executed:
Repository: NervJS/taro
Length of output: 11182
🏁 Script executed:
Repository: NervJS/taro
Length of output: 1100
🏁 Script executed:
Repository: NervJS/taro
Length of output: 5065
🏁 Script executed:
Repository: NervJS/taro
Length of output: 890
🏁 Script executed:
Repository: NervJS/taro
Length of output: 3572
🏁 Script executed:
Repository: NervJS/taro
Length of output: 9312
🏁 Script executed:
Repository: NervJS/taro
Length of output: 7327
🏁 Script executed:
Repository: NervJS/taro
Length of output: 8133
🏁 Script executed:
Repository: NervJS/taro
Length of output: 8096
🏁 Script executed:
Repository: NervJS/taro
Length of output: 544
修正
taro init .下askProjectName存在性校验的路径不一致问题resolveProjectConf在projectName为././时会把配置改成projectDir: path.dirname(cwd)、projectName: path.basename(cwd)(packages/taro-cli/src/create/project.ts第 59-68 行)。但askProjectName的存在性校验在projectName已是字符串时直接做fs.existsSync(conf.projectName!)(第 158-190 行),这会导致taro init .时实际检查的是<cwd>/<basename>(例如/a/b/current-project/current-project),而目标目录其实是<cwd>。只有当该“嵌套目录”恰好存在时才会误报“当前目录已经存在同名项目”。建议让校验依据与实际创建目标路径一致:改为检查
fs.existsSync(path.join(conf.projectDir, conf.projectName)),或在projectName输入为././时跳过/使用匹配目标目录的验证逻辑。现有packages/taro-cli/src/__tests__/project.spec.ts只覆盖././转换结果,未覆盖askProjectName的校验行为。🤖 Prompt for AI Agents