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

fix(crwrsca): Fix handling of positional args #11138

Merged
merged 1 commit into from
Aug 1, 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
98 changes: 53 additions & 45 deletions packages/create-redwood-rsc-app/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,61 +13,69 @@ export function initConfig() {
verbose: false,
}

const args = {
help: false,
template: '',
verbose: false,
version: false,
}
const positionals: string[] = []

// Skipping the first two arguments, which are the path to the node executable
// and the path to the script being executed, we find the first argument that
// does not start with a dash. This is the installation directory.
const installationDir = process.argv
.slice(2)
.find((arg) => !arg.startsWith('-'))
const args = process.argv.slice(2)

if (process.argv.includes('--verbose') || process.argv.includes('-v')) {
args.verbose = true
}

if (process.argv.includes('--help') || process.argv.includes('-h')) {
args.help = true
}
let i = 0
while (i < args.length) {
const arg = args[i]

if (process.argv.includes('--version') || process.argv.includes('-V')) {
args.version = true
}
if (arg === '--verbose' || arg === '-v') {
config.verbose = true
} else if (arg === '--help' || arg === '-h') {
console.log()
console.log('--help is not implemented yet')
console.log('PR welcome!')
console.log()
} else if (arg === '--version' || arg === '-V') {
console.log()
console.log('--version is not implemented yet')
console.log('PR welcome!')
console.log()
} else if (arg.startsWith('--template') || arg.startsWith('-t')) {
// +2 because we do slice(2) above
const templateIndex = i + 2

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]
if (process.argv[templateIndex].includes('=')) {
config.template = process.argv[templateIndex].split('=')[1]
} else if (
process.argv[templateIndex + 1] &&
!process.argv[templateIndex + 1].startsWith('-')
) {
config.template = process.argv[templateIndex + 1]
// skip looping over the next argument as we've already consumed it
i++
} else {
throw new ExitCodeError(
1,
`Error: No template provided after ${arg} flag`,
)
}
} else if (arg === '--npx') {
// Do nothing. Intended for internal use only.
} else if (arg === '--no-check-latest') {
// Do nothing. Intended for internal use only.
} else if (arg.startsWith('-')) {
console.log('Unknown argument:', arg)
} else {
throw new ExitCodeError(
1,
'Error: No template provided after --template flag',
)
positionals.push(arg)
}

i++
}

if (args.verbose) {
console.log('process.argv', process.argv)
console.log('Parsed command line arguments:')
console.log(' arguments:', args)
console.log(' installationDir:', installationDir)
if (positionals.length === 0) {
throw new ExitCodeError(1, 'Error: No installation directory provided')
}

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

if (config.verbose) {
console.log('process.argv', process.argv)
console.log('config', config)
}

return config
}
12 changes: 6 additions & 6 deletions packages/create-redwood-rsc-app/src/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ export async function unzip(config: Config, zipFilePath: string) {
console.log(zip.entryCount, 'entries in zip file')
}

try {
for await (const entry of zip) {
const baseDir = `redwood-main/__fixtures__/${config.template}/`
const baseDir = `redwood-main/__fixtures__/${config.template}/`

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

try {
for await (const entry of zip) {
const isDir = entry.filename.endsWith('/')

if (isDir || !entry.filename.includes(baseDir)) {
Expand Down
Loading