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

Accept template name as positional argument from command line #1

Merged
merged 2 commits into from
Apr 6, 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
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"semi": false
}
37 changes: 22 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ async function main() {

let args = yargsParser(process.argv.slice(2))

const target = process.argv[2] || '.'
const target = String(args._[0]) || '.'
const templateArg = args.template

const templateDirs = await viaContentsApi(config)
const templates = {}
Expand All @@ -42,25 +43,31 @@ async function main() {
}
}
})
let templateNames = [...Object.values(templates)]

const templateName = (
await prompts({
type: 'select',
name: 'template',
message: 'Which template do you want to use?',
choices: templateNames.map((template: any) => ({
title: template.name,
value: template.name,
})),
initial: 0,
})
).template
let templateNames = [...Object.values(templates)] as { name: string }[]

const templateName =
templateArg ||
(
await prompts({
type: 'select',
name: 'template',
message: 'Which template do you want to use?',
choices: templateNames.map((template: any) => ({
title: template.name,
value: template.name,
})),
initial: 0,
})
).template

if (!templateName) {
throw new Error('No template selected')
}

if (!templateNames.find((t) => t.name === templateName)) {
throw new Error(`Invalid template selected: ${templateName}`)
}

if (fs.existsSync(target)) {
if (fs.readdirSync(target).length > 0) {
const response = await prompts({
Expand Down