Skip to content

Commit 9ad7d6f

Browse files
committedNov 28, 2024··
fix: improved internal type safety
1 parent d0d93ab commit 9ad7d6f

File tree

6 files changed

+20
-20
lines changed

6 files changed

+20
-20
lines changed
 

‎src/commands/add.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,18 @@ export default defineCommand({
3232
async run(ctx) {
3333
const cwd = resolve(ctx.args.cwd || '.')
3434

35-
const template = ctx.args.template
35+
const templateName = ctx.args.template
36+
const template = templates[templateName]
3637
const ext = extname(ctx.args.name)
3738
const name
3839
= ext === '.vue' || ext === '.ts'
3940
? ctx.args.name.replace(ext, '')
4041
: ctx.args.name
4142

4243
// Validate template name
43-
if (!templates[template]) {
44+
if (!template) {
4445
consola.error(
45-
`Template ${template} is not supported. Possible values: ${Object.keys(
46+
`Template ${templateName} is not supported. Possible values: ${Object.keys(
4647
templates,
4748
).join(', ')}`,
4849
)
@@ -60,7 +61,7 @@ export default defineCommand({
6061
const config = await kit.loadNuxtConfig({ cwd })
6162

6263
// Resolve template
63-
const res = templates[template]({ name, args: ctx.args })
64+
const res = template({ name, args: ctx.args })
6465

6566
// Resolve full path to generated file
6667
const path = resolve(config.srcDir, res.path)
@@ -77,14 +78,14 @@ export default defineCommand({
7778
const parentDir = dirname(path)
7879
if (!existsSync(parentDir)) {
7980
consola.info('Creating directory', parentDir)
80-
if (template === 'page') {
81+
if (templateName === 'page') {
8182
consola.info('This enables vue-router functionality!')
8283
}
8384
await fsp.mkdir(parentDir, { recursive: true })
8485
}
8586

8687
// Write file
8788
await fsp.writeFile(path, res.contents.trim() + '\n')
88-
consola.info(`🪄 Generated a new ${template} in ${path}`)
89+
consola.info(`🪄 Generated a new ${templateName} in ${path}`)
8990
},
9091
})

‎src/commands/dev.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ function _resolveListenOptions(
250250
?? process.env.NITRO_HOST
251251
?? process.env.HOST
252252
// TODO: Default host in schema should be undefined instead of ''
253-
?? nuxtOptions._layers?.[0].config?.devServer?.host
253+
?? nuxtOptions._layers?.[0]?.config?.devServer?.host
254254
?? undefined
255255

256256
const _public: boolean | undefined
@@ -264,17 +264,15 @@ function _resolveListenOptions(
264264
|| (args.sslCert as string)
265265
|| process.env.NUXT_SSL_CERT
266266
|| process.env.NITRO_SSL_CERT
267-
|| (typeof nuxtOptions.devServer.https !== 'boolean'
268-
&& nuxtOptions.devServer.https?.cert)
267+
|| (typeof nuxtOptions.devServer.https !== 'boolean' && nuxtOptions.devServer.https?.cert)
269268
|| ''
270269

271270
const _httpsKey
272271
= args['https.key']
273272
|| (args.sslKey as string)
274273
|| process.env.NUXT_SSL_KEY
275274
|| process.env.NITRO_SSL_KEY
276-
|| (typeof nuxtOptions.devServer.https !== 'boolean'
277-
&& nuxtOptions.devServer.https?.key)
275+
|| (typeof nuxtOptions.devServer.https !== 'boolean' && nuxtOptions.devServer.https?.key)
278276
|| ''
279277

280278
const httpsEnabled

‎src/commands/info.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export default defineCommand({
101101
if (label.length > maxLength) {
102102
maxLength = label.length
103103
}
104-
return [label, val || '-']
104+
return [label, val || '-'] as const
105105
})
106106
let infoStr = ''
107107
for (const [label, value] of entries) {

‎src/commands/module/add.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ async function resolveModule(
231231

232232
// Fetch package on npm
233233
pkgVersion = pkgVersion || 'latest'
234-
const pkgScope = pkgName.startsWith('@') ? pkgName.split('/')[0] : null
234+
const pkgScope = pkgName.startsWith('@') ? pkgName.split('/')[0]! : null
235235
const meta: RegistryMeta = await detectNpmRegistry(pkgScope)
236236
const headers: HeadersInit = {}
237237

@@ -295,10 +295,10 @@ async function getAuthToken(registry: RegistryMeta['registry']): Promise<Registr
295295
fd = await fs.promises.open(npmrcPath, 'r')
296296
if (await fd.stat().then(r => r.isFile())) {
297297
const npmrcContent = await fd.readFile('utf-8')
298-
const authTokenMatch = npmrcContent.match(authTokenRegex)
298+
const authTokenMatch = npmrcContent.match(authTokenRegex)?.[1]
299299

300300
if (authTokenMatch) {
301-
return authTokenMatch[1].trim()
301+
return authTokenMatch.trim()
302302
}
303303
}
304304
}
@@ -346,17 +346,17 @@ async function getRegistryFromFile(paths: string[], scope: string | null) {
346346

347347
if (scope) {
348348
const scopedRegex = new RegExp(`^${scope}:registry=(.+)$`, 'm')
349-
const scopedMatch = npmrcContent.match(scopedRegex)
349+
const scopedMatch = npmrcContent.match(scopedRegex)?.[1]
350350
if (scopedMatch) {
351-
return scopedMatch[1].trim()
351+
return scopedMatch.trim()
352352
}
353353
}
354354

355355
// If no scoped registry found or no scope provided, look for the default registry
356356
const defaultRegex = /^\s*registry=(.+)$/m
357-
const defaultMatch = npmrcContent.match(defaultRegex)
357+
const defaultMatch = npmrcContent.match(defaultRegex)?.[1]
358358
if (defaultMatch) {
359-
return defaultMatch[1].trim()
359+
return defaultMatch.trim()
360360
}
361361
}
362362
}

‎src/commands/module/search.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ async function findModuleByKeywords(query: string, nuxtVersion: string) {
9999
if (label.length > maxLength) {
100100
maxLength = label.length
101101
}
102-
return [label, val || '-']
102+
return [label, val || '-'] as const
103103
})
104104
let infoStr = ''
105105
for (const [label, value] of entries) {

‎tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"moduleResolution": "Node",
99
"strict": true,
1010
"noImplicitAny": true,
11+
"noUncheckedIndexedAccess": true,
1112
"allowJs": true,
1213
"noEmit": true,
1314
"noUnusedLocals": true,

0 commit comments

Comments
 (0)
Please sign in to comment.