Skip to content
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

chore(create-vite): add current directory description #8501

Merged
merged 7 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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