feat: improve shim generation for index components

This commit is contained in:
Andrew Bastin
2021-09-09 01:56:43 +05:30
parent f4f74e223f
commit 4a12cc76fa

View File

@@ -1,6 +1,7 @@
import { resolve } from "path" import { resolve } from "path"
import { Module } from "@nuxt/types" import { Module } from "@nuxt/types"
import ts from "typescript" import ts from "typescript"
import chokidar from "chokidar"
const { readdir, writeFile } = require("fs").promises const { readdir, writeFile } = require("fs").promises
@@ -30,7 +31,7 @@ async function getAllVueComponentPaths(): Promise<string[]> {
vueFilePaths.push( vueFilePaths.push(
`./${f `./${f
.split("/") .split("/")
.slice(componentsIndex + 1) .slice(componentsIndex)
.join("/")}` .join("/")}`
) )
} }
@@ -45,8 +46,9 @@ function resolveComponentName(filename: string): string {
return filename return filename
.split("/") .split("/")
.slice(index + 1) .slice(index + 1)
.filter((x) => x !== "index.vue") // Remove index.vue
.map((x) => x.split(".vue")[0]) // Remove extension .map((x) => x.split(".vue")[0]) // Remove extension
.filter((x) => x.toUpperCase() !== x.toLowerCase()) .filter((x) => x.toUpperCase() !== x.toLowerCase()) // Remove non-word stuff
.map((x) => titleCase(x)) // titlecase it .map((x) => titleCase(x)) // titlecase it
.join("") .join("")
} }
@@ -113,9 +115,7 @@ function generateTypeScriptDef(components: [string, string][]) {
return printer.printFile(source) return printer.printFile(source)
} }
const module: Module<{}> = async function () { async function generateShim() {
if (!this.nuxt.options.dev) return
const results = await getAllVueComponentPaths() const results = await getAllVueComponentPaths()
const fileComponentNameCombo: [string, string][] = results.map((x) => [ const fileComponentNameCombo: [string, string][] = results.map((x) => [
resolveComponentName(x), resolveComponentName(x),
@@ -126,4 +126,14 @@ const module: Module<{}> = async function () {
await writeFile(resolve("shims-volar.d.ts"), typescriptString) await writeFile(resolve("shims-volar.d.ts"), typescriptString)
} }
const module: Module<{}> = async function () {
if (!this.nuxt.options.dev) return
await generateShim()
chokidar.watch(resolve("../components/")).on("all", async () => {
await generateShim()
})
}
export default module export default module