refactor: add functionality to getNameFromPath

This commit is contained in:
Daniel Roe 2023-10-16 11:50:18 +01:00
parent 1512a2402d
commit 75990c5dbc
2 changed files with 8 additions and 10 deletions

View File

@ -5,7 +5,7 @@ import { compileTemplate, findPath, logger, normalizePlugin, normalizeTemplate,
import type { Nuxt, NuxtApp, NuxtPlugin, NuxtTemplate, ResolvedNuxtTemplate } from 'nuxt/schema'
import * as defaultTemplates from './templates'
import { getNameFromPath, getNameFromPathLocal, hasSuffix, uniqueBy } from './utils'
import { getNameFromPath, hasSuffix, uniqueBy } from './utils'
import { extractMetadata, orderMap } from './plugins/plugin-metadata'
export function createApp (nuxt: Nuxt, options: Partial<NuxtApp> = {}): NuxtApp {
@ -112,7 +112,7 @@ export async function resolveApp (nuxt: Nuxt, app: NuxtApp) {
const layoutDir = (config.rootDir === nuxt.options.rootDir ? nuxt.options : config).dir?.layouts || 'layouts'
const layoutFiles = await resolveFiles(config.srcDir, `${layoutDir}/**/*{${nuxt.options.extensions.join(',')}}`)
for (const file of layoutFiles) {
const name = getNameFromPathLocal(file, resolve(config.srcDir, layoutDir))
const name = getNameFromPath(file, resolve(config.srcDir, layoutDir))
app.layouts[name] = app.layouts[name] || { name, file }
}
}

View File

@ -1,16 +1,14 @@
import { basename, extname } from 'pathe'
import { basename, extname, normalize } from 'pathe'
import { kebabCase } from 'scule'
import { withTrailingSlash } from 'ufo'
export function getNameFromPath (path: string) {
return kebabCase(basename(path).replace(extname(path), '')).replace(/["']/g, '')
export function getNameFromPath (path: string, relativeTo?: string) {
const relativePath = relativeTo
? normalize(path).replace(withTrailingSlash(normalize(relativeTo)), '')
: basename(path)
return kebabCase(relativePath.replace(/[\\/]+/g, '-').replace(/\/index\.\w+$/, '').replace(extname(relativePath), '')).replace(/["']/g, '')
}
export function hasSuffix (path: string, suffix: string) {
return basename(path).replace(extname(path), '').endsWith(suffix)
}
export function getNameFromPathLocal (path: string, localDir: string) {
const file = path.replace(withTrailingSlash(localDir), '')
return getNameFromPath(file.replace(/[\\/]+/g, '-').replace(/\/index\.\w+$/, ''))
}