Skip to content

Commit 8518113

Browse files
brownieboyMichael Brownbluwy
authored
fix(create-vite): improve project name inference from path (#16490)
Co-authored-by: Michael Brown <[email protected]> Co-authored-by: bluwy <[email protected]>
1 parent b2fa726 commit 8518113

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

packages/create-vite/__tests__/cli.spec.ts

+42-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const CLI_PATH = path.join(__dirname, '..')
88

99
const projectName = 'test-app'
1010
const genPath = path.join(__dirname, projectName)
11+
const genPathWithSubfolder = path.join(__dirname, 'subfolder', projectName)
1112

1213
const run = <SO extends SyncOptions>(
1314
args: string[],
@@ -17,12 +18,13 @@ const run = <SO extends SyncOptions>(
1718
}
1819

1920
// Helper to create a non-empty directory
20-
const createNonEmptyDir = () => {
21+
const createNonEmptyDir = (overrideFolder?: string) => {
2122
// Create the temporary directory
22-
fs.mkdirSync(genPath, { recursive: true })
23+
const newNonEmptyFolder = overrideFolder || genPath
24+
fs.mkdirSync(newNonEmptyFolder, { recursive: true })
2325

2426
// Create a package.json file
25-
const pkgJson = path.join(genPath, 'package.json')
27+
const pkgJson = path.join(newNonEmptyFolder, 'package.json')
2628
fs.writeFileSync(pkgJson, '{ "foo": "bar" }')
2729
}
2830

@@ -33,8 +35,24 @@ const templateFiles = fs
3335
.map((filePath) => (filePath === '_gitignore' ? '.gitignore' : filePath))
3436
.sort()
3537

36-
beforeAll(() => fs.rmSync(genPath, { recursive: true, force: true }))
37-
afterEach(() => fs.rmSync(genPath, { recursive: true, force: true }))
38+
// React starter template
39+
const templateFilesReact = fs
40+
.readdirSync(path.join(CLI_PATH, 'template-react'))
41+
// _gitignore is renamed to .gitignore
42+
.map((filePath) => (filePath === '_gitignore' ? '.gitignore' : filePath))
43+
.sort()
44+
45+
const clearAnyPreviousFolders = () => {
46+
if (fs.existsSync(genPath)) {
47+
fs.rmSync(genPath, { recursive: true, force: true })
48+
}
49+
if (fs.existsSync(genPathWithSubfolder)) {
50+
fs.rmSync(genPathWithSubfolder, { recursive: true, force: true })
51+
}
52+
}
53+
54+
beforeAll(() => clearAnyPreviousFolders())
55+
afterEach(() => clearAnyPreviousFolders())
3856

3957
test('prompts for the project name if none supplied', () => {
4058
const { stdout } = run([])
@@ -70,6 +88,14 @@ test('asks to overwrite non-empty target directory', () => {
7088
expect(stdout).toContain(`Target directory "${projectName}" is not empty.`)
7189
})
7290

91+
test('asks to overwrite non-empty target directory with subfolder', () => {
92+
createNonEmptyDir(genPathWithSubfolder)
93+
const { stdout } = run([`subfolder/${projectName}`], { cwd: __dirname })
94+
expect(stdout).toContain(
95+
`Target directory "subfolder/${projectName}" is not empty.`,
96+
)
97+
})
98+
7399
test('asks to overwrite non-empty current directory', () => {
74100
createNonEmptyDir()
75101
const { stdout } = run(['.'], { cwd: genPath })
@@ -87,6 +113,17 @@ test('successfully scaffolds a project based on vue starter template', () => {
87113
expect(templateFiles).toEqual(generatedFiles)
88114
})
89115

116+
test('successfully scaffolds a project with subfolder based on react starter template', () => {
117+
const { stdout } = run([`subfolder/${projectName}`, '--template', 'react'], {
118+
cwd: __dirname,
119+
})
120+
const generatedFiles = fs.readdirSync(genPathWithSubfolder).sort()
121+
122+
// Assertions
123+
expect(stdout).toContain(`Scaffolding project in ${genPathWithSubfolder}`)
124+
expect(templateFilesReact).toEqual(generatedFiles)
125+
})
126+
90127
test('works with the -t alias', () => {
91128
const { stdout } = run([projectName, '-t', 'vue'], {
92129
cwd: __dirname,

packages/create-vite/src/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,7 @@ async function init() {
310310
}
311311

312312
let targetDir = argTargetDir || defaultTargetDir
313-
const getProjectName = () =>
314-
targetDir === '.' ? path.basename(path.resolve()) : targetDir
313+
const getProjectName = () => path.basename(path.resolve(targetDir))
315314

316315
let result: prompts.Answers<
317316
'projectName' | 'overwrite' | 'packageName' | 'framework' | 'variant'

0 commit comments

Comments
 (0)