Skip to content

Commit 95cdf6e

Browse files
committed
Add command-line arguments
1 parent be56d74 commit 95cdf6e

File tree

1 file changed

+79
-2
lines changed

1 file changed

+79
-2
lines changed

src/index.ts

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import * as fs from 'node:fs'
44
import * as path from 'node:path'
5+
import { parseArgs } from 'node:util'
56
import prompts, { type PromptObject } from 'prompts'
67
import ejs from 'ejs'
8+
import packageJson from '../package.json'
79

810
async function prompt(options: Omit<PromptObject, 'name'>) {
911
try {
@@ -46,6 +48,10 @@ async function togglePrompt(message: string, initial = false, active = 'Yes', in
4648
})
4749
}
4850

51+
async function textPromptIf(condition: boolean, message: string, initial?: string): Promise<string> {
52+
return condition ? textPrompt(message, initial) : initial ?? ''
53+
}
54+
4955
async function togglePromptIf(condition: boolean, message: string, initial = false, active = 'Yes', inactive = 'No'): Promise<boolean> {
5056
return condition ? togglePrompt(message, initial, active, inactive) : initial
5157
}
@@ -75,9 +81,80 @@ type Config = {
7581
includeEsLintStylistic: boolean
7682
}
7783

84+
type Args = {
85+
extended: boolean
86+
}
87+
88+
// TODO: Add link to docs
89+
const helpMessage = `\
90+
Usage: create-vue-lib [OPTIONS...]
91+
92+
Create a new Vite project to build a Vue-based library.
93+
94+
Options:
95+
--extended, -x
96+
Prompt for extra configuration options.
97+
--help, -h
98+
Display this help message.
99+
--version, -v
100+
Display the version number for create-vue-lib.
101+
`
102+
103+
function processArgs(): Args {
104+
let argValues: object = {}
105+
106+
const options = {
107+
extended: {
108+
short: 'x',
109+
type: 'boolean'
110+
},
111+
help: {
112+
short: 'h',
113+
type: 'boolean'
114+
},
115+
version: {
116+
short: 'v',
117+
type: 'boolean'
118+
}
119+
} as const
120+
121+
try {
122+
const args = parseArgs({
123+
options
124+
})
125+
126+
argValues = args.values
127+
} catch (err) {
128+
if (err.code === 'ERR_PARSE_ARGS_UNKNOWN_OPTION') {
129+
console.log('Error:')
130+
console.log(err.message)
131+
console.log('See --help for valid options')
132+
process.exit(1)
133+
} else {
134+
throw err
135+
}
136+
}
137+
138+
if (argValues.help) {
139+
console.log(helpMessage)
140+
process.exit(0)
141+
}
142+
143+
if (argValues.version) {
144+
console.log(`${packageJson.name} v${packageJson.version}`)
145+
process.exit(0)
146+
}
147+
148+
return {
149+
extended: !!argValues.extended
150+
}
151+
}
152+
78153
async function init() {
79154
const cwd = process.cwd()
80155

156+
const { extended } = processArgs()
157+
81158
const scopedPackageName = await textPrompt('Package name', '@skirtle/test-project')
82159

83160
// TODO: Tightening this check, e.g. for hyphen positions
@@ -110,14 +187,14 @@ async function init() {
110187
}
111188
}
112189

113-
const mainPackageDirName = await textPrompt('Main package directory', unscopedPackageName)
190+
const mainPackageDirName = await textPromptIf(extended, 'Main package directory', unscopedPackageName)
114191

115192
if (!/^[\w-]+$/.test(mainPackageDirName)) {
116193
console.log('Invalid directory name: ' + mainPackageDirName)
117194
process.exit(1)
118195
}
119196

120-
const globalVariableName = await textPrompt('Global variable name', projectName.replace(/ /g, ''))
197+
const globalVariableName = await textPromptIf(extended, 'Global variable name', projectName.replace(/ /g, ''))
121198

122199
if (!/^[a-zA-Z$_][\w$]*$/.test(globalVariableName)) {
123200
console.log('Invalid variable name: ' + globalVariableName)

0 commit comments

Comments
 (0)