Skip to content

Commit

Permalink
CRWA: Prompt for installation dir (#8955)
Browse files Browse the repository at this point in the history
Instead of failing if the user didn't provide a dir on the command line,
let's prompt for one!

I was inspired by t3 where you don't have to specify any arguments at
all to get started

![image](https://github.com/redwoodjs/redwood/assets/30793/575775c1-555e-4e0f-851a-cd2bad49b5a9)

This is what our implementation looks like now
Default suggestion `my-redwood-app`

![image](https://github.com/redwoodjs/redwood/assets/30793/738856e9-e6f3-436d-bb56-a74ffc44ead2)

And here's when it's done

![image](https://github.com/redwoodjs/redwood/assets/30793/55893785-52e3-41a2-9b75-1f88c4380293)

Here's with a couple of options specified as arguments/flags

![image](https://github.com/redwoodjs/redwood/assets/30793/df65951c-b8aa-41f4-b586-11121f20a2be)

@Josh-Walker-GM Can you please double check I didn't mess anything up
with Telemetry?
@ahaywood Hope I'm not stepping on your toes here 🦶
  • Loading branch information
Tobbe authored Jul 25, 2023
1 parent fc29c9c commit 93f213d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
1 change: 1 addition & 0 deletions packages/create-redwood-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"semver": "7.5.3",
"systeminformation": "5.18.5",
"terminal-link": "2.1.1",
"untildify": "4.0.0",
"uuid": "9.0.0",
"yargs": "17.7.2"
},
Expand Down
70 changes: 46 additions & 24 deletions packages/create-redwood-app/src/create-redwood-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import path from 'path'

import { trace, SpanStatusCode } from '@opentelemetry/api'
import chalk from 'chalk'
import checkNodeVersionCb from 'check-node-version'
import execa from 'execa'
import fs from 'fs-extra'
import semver from 'semver'
import terminalLink from 'terminal-link'
import untildify from 'untildify'
import { hideBin, Parser } from 'yargs/helpers'
import yargs from 'yargs/yargs'

Expand Down Expand Up @@ -404,6 +404,46 @@ async function initializeGit(newAppDir, commitMessage) {
tui.stopReactive()
}

async function handleTargetDirPreference(targetDir) {
if (targetDir) {
tui.drawText(
`${RedwoodStyling.green(
'✔'
)} Creating your Redwood app in ${targetDir} based on command line argument`
)

return targetDir
}

// Prompt user for preference
try {
const response = await tui.prompt({
type: 'input',
name: 'targetDir',
message: 'Where would you like to create your Redwood app?',
initial: 'my-redwood-app',
})

if (/^~\w/.test(response.targetDir)) {
tui.stopReactive(true)
tui.displayError(
'The `~username` syntax is not supported here',
'Please use the full path or specify the target directory on the command line.'
)

recordErrorViaTelemetry('Target dir prompt path syntax not supported')
await shutdownTelemetry()
process.exit(1)
}

return untildify(response.targetDir)
} catch {
recordErrorViaTelemetry('User cancelled install at target dir prompt')
await shutdownTelemetry()
process.exit(1)
}
}

async function handleTypescriptPreference(typescriptFlag) {
// Handle case where flag is set
if (typescriptFlag !== null) {
Expand Down Expand Up @@ -603,35 +643,15 @@ async function createRedwoodApp() {
trace.getActiveSpan()?.setAttribute('overwrite', overwrite)

// Get the directory for installation from the args
const targetDir = String(args).replace(/,/g, '-')
let targetDir = String(args).replace(/,/g, '-')

// Throw an error if there is no target directory specified
if (!targetDir) {
tui.displayError(
'No target directory specified',
[
'Please specify the project directory',
` ${chalk.cyan('yarn create redwood-app')} ${chalk.green(
'<project-directory>'
)}`,
'',
'For example:',
` ${chalk.cyan('yarn create redwood-app')} ${chalk.green(
'my-redwood-app'
)}`,
].join('\n')
)
recordErrorViaTelemetry('No target directory specified')
await shutdownTelemetry()
process.exit(1)
}

const newAppDir = path.resolve(process.cwd(), targetDir)
const templatesDir = path.resolve(__dirname, '../templates')

// Engine check
await executeCompatibilityCheck(path.join(templatesDir, 'ts'))

targetDir = await handleTargetDirPreference(targetDir)

// Determine ts/js preference
const useTypescript = await handleTypescriptPreference(typescriptFlag)
trace.getActiveSpan()?.setAttribute('typescript', useTypescript)
Expand All @@ -654,6 +674,8 @@ async function createRedwoodApp() {
yarnInstall = await handleYarnInstallPreference(yarnInstallFlag)
}

const newAppDir = path.resolve(process.cwd(), targetDir)

// Create project files
await createProjectFiles(newAppDir, { templateDir, overwrite })

Expand Down
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14998,6 +14998,7 @@ __metadata:
semver: 7.5.3
systeminformation: 5.18.5
terminal-link: 2.1.1
untildify: 4.0.0
uuid: 9.0.0
yargs: 17.7.2
bin:
Expand Down Expand Up @@ -30769,7 +30770,7 @@ __metadata:
languageName: node
linkType: hard

"untildify@npm:^4.0.0":
"untildify@npm:4.0.0, untildify@npm:^4.0.0":
version: 4.0.0
resolution: "untildify@npm:4.0.0"
checksum: d758e624c707d49f76f7511d75d09a8eda7f2020d231ec52b67ff4896bcf7013be3f9522d8375f57e586e9a2e827f5641c7e06ee46ab9c435fc2b2b2e9de517a
Expand Down

0 comments on commit 93f213d

Please sign in to comment.