Skip to content

Commit

Permalink
feat: Accept template name as positional argument from command line (#1)
Browse files Browse the repository at this point in the history
* Accept template name as positional argument from command line

* Changing from positional arg to --template flag
  • Loading branch information
jculvey authored Apr 6, 2023
1 parent a9244b9 commit 0b224b8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
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

0 comments on commit 0b224b8

Please sign in to comment.