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

CRWA: Prompt for installation dir #8955

Merged
merged 5 commits into from
Jul 25, 2023
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
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