|
2 | 2 |
|
3 | 3 | import * as fs from 'node:fs'
|
4 | 4 | import * as path from 'node:path'
|
| 5 | +import { parseArgs } from 'node:util' |
5 | 6 | import prompts, { type PromptObject } from 'prompts'
|
6 | 7 | import ejs from 'ejs'
|
| 8 | +import packageJson from '../package.json' |
7 | 9 |
|
8 | 10 | async function prompt(options: Omit<PromptObject, 'name'>) {
|
9 | 11 | try {
|
@@ -46,6 +48,10 @@ async function togglePrompt(message: string, initial = false, active = 'Yes', in
|
46 | 48 | })
|
47 | 49 | }
|
48 | 50 |
|
| 51 | +async function textPromptIf(condition: boolean, message: string, initial?: string): Promise<string> { |
| 52 | + return condition ? textPrompt(message, initial) : initial ?? '' |
| 53 | +} |
| 54 | + |
49 | 55 | async function togglePromptIf(condition: boolean, message: string, initial = false, active = 'Yes', inactive = 'No'): Promise<boolean> {
|
50 | 56 | return condition ? togglePrompt(message, initial, active, inactive) : initial
|
51 | 57 | }
|
@@ -75,9 +81,80 @@ type Config = {
|
75 | 81 | includeEsLintStylistic: boolean
|
76 | 82 | }
|
77 | 83 |
|
| 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 | + |
78 | 153 | async function init() {
|
79 | 154 | const cwd = process.cwd()
|
80 | 155 |
|
| 156 | + const { extended } = processArgs() |
| 157 | + |
81 | 158 | const scopedPackageName = await textPrompt('Package name', '@skirtle/test-project')
|
82 | 159 |
|
83 | 160 | // TODO: Tightening this check, e.g. for hyphen positions
|
@@ -110,14 +187,14 @@ async function init() {
|
110 | 187 | }
|
111 | 188 | }
|
112 | 189 |
|
113 |
| - const mainPackageDirName = await textPrompt('Main package directory', unscopedPackageName) |
| 190 | + const mainPackageDirName = await textPromptIf(extended, 'Main package directory', unscopedPackageName) |
114 | 191 |
|
115 | 192 | if (!/^[\w-]+$/.test(mainPackageDirName)) {
|
116 | 193 | console.log('Invalid directory name: ' + mainPackageDirName)
|
117 | 194 | process.exit(1)
|
118 | 195 | }
|
119 | 196 |
|
120 |
| - const globalVariableName = await textPrompt('Global variable name', projectName.replace(/ /g, '')) |
| 197 | + const globalVariableName = await textPromptIf(extended, 'Global variable name', projectName.replace(/ /g, '')) |
121 | 198 |
|
122 | 199 | if (!/^[a-zA-Z$_][\w$]*$/.test(globalVariableName)) {
|
123 | 200 | console.log('Invalid variable name: ' + globalVariableName)
|
|
0 commit comments