Skip to content

Commit

Permalink
feat(crwrsca): Add support for --template (#11123)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe authored Jul 30, 2024
1 parent 115911d commit 33f2b79
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
27 changes: 26 additions & 1 deletion packages/create-redwood-rsc-app/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { ExitCodeError } from './error.js'

export interface Config {
installationDir: string
template: string
verbose: boolean
}

export function initConfig() {
const config: Config = {
installationDir: '',
template: '',
verbose: false,
}

const args = {
verbose: false,
help: false,
template: '',
verbose: false,
version: false,
}

Expand All @@ -34,6 +39,25 @@ export function initConfig() {
args.version = true
}

const templateIndex = process.argv.findIndex(
(arg) => arg.startsWith('--template') || arg.startsWith('-t'),
)
if (templateIndex >= 0) {
if (process.argv[templateIndex].includes('=')) {
args.template = process.argv[templateIndex].split('=')[1]
} else if (
process.argv[templateIndex + 1] &&
!process.argv[templateIndex + 1].startsWith('-')
) {
args.template = process.argv[templateIndex + 1]
} else {
throw new ExitCodeError(
1,
'Error: No template provided after --template flag',
)
}
}

if (args.verbose) {
console.log('Parsed command line arguments:')
console.log(' arguments:', args)
Expand All @@ -42,6 +66,7 @@ export function initConfig() {

config.verbose = !!args.verbose
config.installationDir = installationDir ?? ''
config.template = args.template || 'test-project-rsc-kitchen-sink'

return config
}
10 changes: 9 additions & 1 deletion packages/create-redwood-rsc-app/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node

import type { Config } from './config.js'

import { initConfig } from './config.js'
import { downloadTemplate } from './download.js'
import { handleError } from './error.js'
Expand All @@ -12,7 +14,13 @@ import { checkNodeVersion, checkYarnInstallation } from './prerequisites.js'
import { upgradeToLatestCanary } from './upgradeToLatestCanary.js'
import { unzip } from './zip.js'

const config = initConfig()
let config: Config | null = null

try {
config = initConfig()
} catch (e) {
handleError(e)
}

if (shouldRelaunch(config)) {
await relaunchOnLatest(config)
Expand Down
6 changes: 5 additions & 1 deletion packages/create-redwood-rsc-app/src/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export async function unzip(config: Config, zipFilePath: string) {

try {
for await (const entry of zip) {
const baseDir = 'redwood-main/__fixtures__/test-project-rsc-kitchen-sink/'
const baseDir = `redwood-main/__fixtures__/${config.template}/`

if (config.verbose) {
console.log('baseDir:', baseDir)
}

const isDir = entry.filename.endsWith('/')

Expand Down

0 comments on commit 33f2b79

Please sign in to comment.