Skip to content

Commit

Permalink
fix: improved internal type safety
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Nov 28, 2024
1 parent d0d93ab commit 9ad7d6f
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
13 changes: 7 additions & 6 deletions src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ export default defineCommand({
async run(ctx) {
const cwd = resolve(ctx.args.cwd || '.')

const template = ctx.args.template
const templateName = ctx.args.template
const template = templates[templateName]
const ext = extname(ctx.args.name)
const name
= ext === '.vue' || ext === '.ts'
? ctx.args.name.replace(ext, '')
: ctx.args.name

// Validate template name
if (!templates[template]) {
if (!template) {
consola.error(
`Template ${template} is not supported. Possible values: ${Object.keys(
`Template ${templateName} is not supported. Possible values: ${Object.keys(
templates,
).join(', ')}`,
)
Expand All @@ -60,7 +61,7 @@ export default defineCommand({
const config = await kit.loadNuxtConfig({ cwd })

// Resolve template
const res = templates[template]({ name, args: ctx.args })
const res = template({ name, args: ctx.args })

// Resolve full path to generated file
const path = resolve(config.srcDir, res.path)
Expand All @@ -77,14 +78,14 @@ export default defineCommand({
const parentDir = dirname(path)
if (!existsSync(parentDir)) {
consola.info('Creating directory', parentDir)
if (template === 'page') {
if (templateName === 'page') {
consola.info('This enables vue-router functionality!')
}
await fsp.mkdir(parentDir, { recursive: true })
}

// Write file
await fsp.writeFile(path, res.contents.trim() + '\n')
consola.info(`🪄 Generated a new ${template} in ${path}`)
consola.info(`🪄 Generated a new ${templateName} in ${path}`)
},
})
8 changes: 3 additions & 5 deletions src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ function _resolveListenOptions(
?? process.env.NITRO_HOST
?? process.env.HOST
// TODO: Default host in schema should be undefined instead of ''
?? nuxtOptions._layers?.[0].config?.devServer?.host
?? nuxtOptions._layers?.[0]?.config?.devServer?.host
?? undefined

const _public: boolean | undefined
Expand All @@ -264,17 +264,15 @@ function _resolveListenOptions(
|| (args.sslCert as string)
|| process.env.NUXT_SSL_CERT
|| process.env.NITRO_SSL_CERT
|| (typeof nuxtOptions.devServer.https !== 'boolean'
&& nuxtOptions.devServer.https?.cert)
|| (typeof nuxtOptions.devServer.https !== 'boolean' && nuxtOptions.devServer.https?.cert)
|| ''

const _httpsKey
= args['https.key']
|| (args.sslKey as string)
|| process.env.NUXT_SSL_KEY
|| process.env.NITRO_SSL_KEY
|| (typeof nuxtOptions.devServer.https !== 'boolean'
&& nuxtOptions.devServer.https?.key)
|| (typeof nuxtOptions.devServer.https !== 'boolean' && nuxtOptions.devServer.https?.key)
|| ''

const httpsEnabled
Expand Down
2 changes: 1 addition & 1 deletion src/commands/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default defineCommand({
if (label.length > maxLength) {
maxLength = label.length
}
return [label, val || '-']
return [label, val || '-'] as const
})
let infoStr = ''
for (const [label, value] of entries) {
Expand Down
14 changes: 7 additions & 7 deletions src/commands/module/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ async function resolveModule(

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

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

if (authTokenMatch) {
return authTokenMatch[1].trim()
return authTokenMatch.trim()
}
}
}
Expand Down Expand Up @@ -346,17 +346,17 @@ async function getRegistryFromFile(paths: string[], scope: string | null) {

if (scope) {
const scopedRegex = new RegExp(`^${scope}:registry=(.+)$`, 'm')
const scopedMatch = npmrcContent.match(scopedRegex)
const scopedMatch = npmrcContent.match(scopedRegex)?.[1]
if (scopedMatch) {
return scopedMatch[1].trim()
return scopedMatch.trim()
}
}

// If no scoped registry found or no scope provided, look for the default registry
const defaultRegex = /^\s*registry=(.+)$/m
const defaultMatch = npmrcContent.match(defaultRegex)
const defaultMatch = npmrcContent.match(defaultRegex)?.[1]
if (defaultMatch) {
return defaultMatch[1].trim()
return defaultMatch.trim()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/module/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async function findModuleByKeywords(query: string, nuxtVersion: string) {
if (label.length > maxLength) {
maxLength = label.length
}
return [label, val || '-']
return [label, val || '-'] as const
})
let infoStr = ''
for (const [label, value] of entries) {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"moduleResolution": "Node",
"strict": true,
"noImplicitAny": true,
"noUncheckedIndexedAccess": true,
"allowJs": true,
"noEmit": true,
"noUnusedLocals": true,
Expand Down

0 comments on commit 9ad7d6f

Please sign in to comment.