Skip to content

Commit

Permalink
refactor(cna): use Commander args instead of process.argv (#68454)
Browse files Browse the repository at this point in the history
### Why?

Resolve left-out refactors from #68233

### How?

Used Commander's `args` and `opts` values, which remove a few lines.
Synced the variable name for the `app` value for consistency and
possible future destructuring.
  • Loading branch information
devjiwonchoi authored Aug 2, 2024
1 parent 115ce29 commit 86230b2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
6 changes: 3 additions & 3 deletions packages/create-next-app/create-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function createApp({
typescript,
tailwind,
eslint,
appRouter,
app,
srcDir,
importAlias,
skipInstall,
Expand All @@ -45,7 +45,7 @@ export async function createApp({
typescript: boolean
tailwind: boolean
eslint: boolean
appRouter: boolean
app: boolean
srcDir: boolean
importAlias: string
skipInstall: boolean
Expand All @@ -54,7 +54,7 @@ export async function createApp({
}): Promise<void> {
let repoInfo: RepoInfo | undefined
const mode: TemplateMode = typescript ? 'ts' : 'js'
const template: TemplateType = `${appRouter ? 'app' : 'default'}${tailwind ? '-tw' : ''}${empty ? '-empty' : ''}`
const template: TemplateType = `${app ? 'app' : 'default'}${tailwind ? '-tw' : ''}${empty ? '-empty' : ''}`

if (example) {
let repoUrl: URL | undefined
Expand Down
35 changes: 13 additions & 22 deletions packages/create-next-app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const program = new Command(packageJson.name)
.parse(process.argv)

const opts = program.opts()
// const { args } = program
const { args } = program

const packageManager: PackageManager = !!opts.useNpm
? 'npm'
Expand Down Expand Up @@ -273,10 +273,7 @@ async function run(): Promise<void> {
}
}

if (
!process.argv.includes('--eslint') &&
!process.argv.includes('--no-eslint')
) {
if (!opts.eslint && !args.includes('--no-eslint')) {
if (skipPrompt) {
opts.eslint = getPrefOrDefault('eslint')
} else {
Expand All @@ -295,10 +292,7 @@ async function run(): Promise<void> {
}
}

if (
!process.argv.includes('--tailwind') &&
!process.argv.includes('--no-tailwind')
) {
if (!opts.tailwind && !args.includes('--no-tailwind')) {
if (skipPrompt) {
opts.tailwind = getPrefOrDefault('tailwind')
} else {
Expand All @@ -317,10 +311,7 @@ async function run(): Promise<void> {
}
}

if (
!process.argv.includes('--src-dir') &&
!process.argv.includes('--no-src-dir')
) {
if (!opts.srcDir && !args.includes('--no-src-dir')) {
if (skipPrompt) {
opts.srcDir = getPrefOrDefault('srcDir')
} else {
Expand All @@ -339,26 +330,26 @@ async function run(): Promise<void> {
}
}

if (!process.argv.includes('--app') && !process.argv.includes('--no-app')) {
if (!opts.app && !args.includes('--no-app')) {
if (skipPrompt) {
opts.app = getPrefOrDefault('app')
} else {
const styledAppDir = blue('App Router')
const { appRouter } = await prompts({
const { app } = await prompts({
onState: onPromptState,
type: 'toggle',
name: 'appRouter',
name: 'app',
message: `Would you like to use ${styledAppDir}? (recommended)`,
initial: getPrefOrDefault('app'),
active: 'Yes',
inactive: 'No',
})
opts.app = Boolean(appRouter)
preferences.app = Boolean(appRouter)
opts.app = Boolean(app)
preferences.app = Boolean(app)
}
}

if (!opts.turbo && !process.argv.includes('--no-turbo')) {
if (!opts.turbo && !args.includes('--no-turbo')) {
if (skipPrompt) {
opts.turbo = getPrefOrDefault('turbo')
} else {
Expand All @@ -385,7 +376,7 @@ async function run(): Promise<void> {
if (skipPrompt) {
// We don't use preferences here because the default value is @/* regardless of existing preferences
opts.importAlias = defaults.importAlias
} else if (process.argv.includes('--no-import-alias')) {
} else if (args.includes('--no-import-alias')) {
opts.importAlias = defaults.importAlias
} else {
const styledImportAlias = blue('import alias')
Expand Down Expand Up @@ -431,7 +422,7 @@ async function run(): Promise<void> {
typescript: opts.typescript,
tailwind: opts.tailwind,
eslint: opts.eslint,
appRouter: opts.app,
app: opts.app,
srcDir: opts.srcDir,
importAlias: opts.importAlias,
skipInstall: opts.skipInstall,
Expand Down Expand Up @@ -462,7 +453,7 @@ async function run(): Promise<void> {
typescript: opts.typescript,
eslint: opts.eslint,
tailwind: opts.tailwind,
appRouter: opts.app,
app: opts.app,
srcDir: opts.srcDir,
importAlias: opts.importAlias,
skipInstall: opts.skipInstall,
Expand Down

0 comments on commit 86230b2

Please sign in to comment.