Skip to content

Commit 24bf91d

Browse files
committed
fix: cache
1 parent ff0edad commit 24bf91d

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

src/parser/index.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { refineMeta } from "./utils"
44
import { isAbsolute, join } from "pathe"
55
import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs'
66
import { withBase } from "ufo"
7+
import { hash } from "crypto"
78

89
export interface Options {
910
rootDir: string
@@ -16,14 +17,23 @@ export function getComponentMeta(component: string, options?: Options): Componen
1617
const opts = {
1718
cache: false,
1819
rootDir,
19-
cacheDir: join(rootDir, ".data/cache"),
20+
cacheDir: join(rootDir, ".data/nuxt-component-meta"),
2021
...options
2122
}
2223
const fullPath = isAbsolute(component) ? component : withBase(component, opts.rootDir)
23-
const cachePath = join(opts.cacheDir, `${component}.json`)
24+
let cachePath = join(opts.cacheDir, `${component}.json`)
25+
if (opts.cache) {
26+
try {
27+
const content = readFileSync(fullPath, { encoding: 'utf8', flag: 'r' })
28+
const cacheId = component.split('/').pop()?.replace(/\./g, '_') + '-' + hash('sha1', content).slice(0, 12)
29+
cachePath = join(opts.cacheDir, `${cacheId}.json`)
30+
} catch (error) {
31+
throw new Error(`Error reading file ${fullPath}: ${error}`)
32+
}
2433

25-
if (opts.cache && existsSync(cachePath)) {
26-
return JSON.parse(readFileSync(cachePath, { encoding: 'utf8', flag: 'r' })) as ComponentMeta
34+
if (existsSync(cachePath)) {
35+
return JSON.parse(readFileSync(cachePath, { encoding: 'utf8', flag: 'r' })) as ComponentMeta
36+
}
2737
}
2838

2939
const checker = createCheckerByJson(
@@ -40,7 +50,10 @@ export function getComponentMeta(component: string, options?: Options): Componen
4050
const refinedMeta = refineMeta(meta)
4151

4252
if (opts.cache) {
43-
const cache = JSON.stringify(refinedMeta, null, 2)
53+
const cache = JSON.stringify({
54+
cachedAt: Date.now(),
55+
...refinedMeta,
56+
})
4457
if (!existsSync(opts.cacheDir)) {
4558
mkdirSync(opts.cacheDir, { recursive: true })
4659
}

test/get-component-meta.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { beforeAll, describe, expect, test } from "vitest";
2+
import { getComponentMeta } from "../src/parser";
3+
import { join } from "path";
4+
import { rmSync } from "fs";
5+
6+
describe("get-component-meta", () => {
7+
const rootDir = join(__dirname, "./fixtures/basic")
8+
beforeAll(() => {
9+
try {
10+
rmSync(join(rootDir, '.data/nuxt-component-meta'), { recursive: true })
11+
} catch {
12+
// Ignore
13+
}
14+
})
15+
16+
test("parse NormalScript fresh parse", () => {
17+
const meta = getComponentMeta("components/NormalScript.vue", {
18+
rootDir,
19+
})
20+
expect(meta.props.length).toEqual(4);
21+
expect((meta as unknown as Record<string, unknown>).cachedAt).toBeUndefined();
22+
});
23+
24+
test("parse NormalScript fresh parse (cache enabled)", () => {
25+
const meta = getComponentMeta("components/NormalScript.vue", {
26+
rootDir,
27+
cache: true
28+
})
29+
expect(meta.props.length).toEqual(4);
30+
expect((meta as unknown as Record<string, unknown>).cachedAt).toBeUndefined();
31+
});
32+
33+
test("parse NormalScript cached", () => {
34+
const meta = getComponentMeta("components/NormalScript.vue", {
35+
rootDir,
36+
cache: true
37+
})
38+
expect(meta.props.length).toEqual(4);
39+
expect((meta as unknown as Record<string, unknown>).cachedAt).toBeDefined();
40+
});
41+
});

0 commit comments

Comments
 (0)