Skip to content

Commit

Permalink
chore(create-vite): add current directory description (#8501)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Jun 9, 2022
1 parent fba82d0 commit 8d08220
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
2 changes: 2 additions & 0 deletions packages/create-vite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ Currently supported template presets include:
- `svelte`
- `svelte-ts`

You can use `.` for the project name to scaffold in the current directory.

## Community Templates

create-vite is a tool to quickly start a project from a basic template for popular frameworks. Check out Awesome Vite for [community maintained templates](https://github.com/vitejs/awesome-vite#templates) that include other tools or target different frameworks. You can use a tool like [degit](https://github.com/Rich-Harris/degit) to scaffold your project with one of the templates.
Expand Down
8 changes: 7 additions & 1 deletion packages/create-vite/__tests__/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ test('prompts for the project name if none supplied', () => {
expect(stdout).toContain('Project name:')
})

test('prompts for the framework if none supplied when target dir is current directory', () => {
mkdirpSync(genPath)
const { stdout } = run(['.'], { cwd: genPath })
expect(stdout).toContain('Select a framework:')
})

test('prompts for the framework if none supplied', () => {
const { stdout } = run([projectName])
expect(stdout).toContain('Select a framework:')
Expand All @@ -65,7 +71,7 @@ test('asks to overwrite non-empty target directory', () => {

test('asks to overwrite non-empty current directory', () => {
createNonEmptyDir()
const { stdout } = run(['.'], { cwd: genPath, input: 'test-app\n' })
const { stdout } = run(['.'], { cwd: genPath })
expect(stdout).toContain(`Current directory is not empty.`)
})

Expand Down
29 changes: 18 additions & 11 deletions packages/create-vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ const renameFiles = {
}

async function init() {
let targetDir = argv._[0]
let targetDir = formatTargetDir(argv._[0])
let template = argv.template || argv.t

const defaultProjectName = !targetDir
? 'vite-project'
: targetDir.trim().replace(/\/+$/g, '')
const defaultTargetDir = 'vite-project'
const getProjectName = () =>
targetDir === '.' ? path.basename(path.resolve()) : targetDir

let result = {}

Expand All @@ -146,10 +146,10 @@ async function init() {
type: targetDir ? null : 'text',
name: 'projectName',
message: reset('Project name:'),
initial: defaultProjectName,
onState: (state) =>
(targetDir =
state.value.trim().replace(/\/+$/g, '') || defaultProjectName)
initial: defaultTargetDir,
onState: (state) => {
targetDir = formatTargetDir(state.value) || defaultTargetDir
}
},
{
type: () =>
Expand All @@ -171,10 +171,10 @@ async function init() {
name: 'overwriteChecker'
},
{
type: () => (isValidPackageName(targetDir) ? null : 'text'),
type: () => (isValidPackageName(getProjectName()) ? null : 'text'),
name: 'packageName',
message: reset('Package name:'),
initial: () => toValidPackageName(targetDir),
initial: () => toValidPackageName(getProjectName()),
validate: (dir) =>
isValidPackageName(dir) || 'Invalid package.json name'
},
Expand Down Expand Up @@ -265,7 +265,7 @@ async function init() {
fs.readFileSync(path.join(templateDir, `package.json`), 'utf-8')
)

pkg.name = packageName || targetDir
pkg.name = packageName || getProjectName()

write('package.json', JSON.stringify(pkg, null, 2))

Expand All @@ -289,6 +289,13 @@ async function init() {
console.log()
}

/**
* @param {string | undefined} targetDir
*/
function formatTargetDir(targetDir) {
return targetDir?.trim().replace(/\/+$/g, '')
}

function copy(src, dest) {
const stat = fs.statSync(src)
if (stat.isDirectory()) {
Expand Down

0 comments on commit 8d08220

Please sign in to comment.