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

feat(crwrsca): Add support for --template #11123

Merged
merged 2 commits into from
Jul 30, 2024
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
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
Loading