Skip to content

Commit fd1abe7

Browse files
committed
chore: update utility
1 parent 51c492e commit fd1abe7

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/parser/index.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ export function getComponentMeta(component: string, options?: Options): Componen
2121
...options
2222
}
2323
const fullPath = isAbsolute(component) ? component : withBase(component, opts.rootDir)
24-
let cachePath = join(opts.cacheDir, `${component}.json`)
24+
let cachePath = undefined
2525
if (opts.cache) {
2626
try {
2727
const content = readFileSync(fullPath, { encoding: 'utf8', flag: 'r' })
28-
const cacheId = component.split('/').pop()?.replace(/\./g, '_') + '-' + hash('sha1', content).slice(0, 12)
28+
const cacheId = component.split('/').pop()?.replace(/\./g, '_') + '--' + hash('sha1', content).slice(0, 12)
2929
cachePath = join(opts.cacheDir, `${cacheId}.json`)
3030
} catch (error) {
3131
throw new Error(`Error reading file ${fullPath}: ${error}`)
@@ -36,6 +36,27 @@ export function getComponentMeta(component: string, options?: Options): Componen
3636
}
3737
}
3838

39+
const componentMeta = _getComponentMeta(fullPath, opts)
40+
41+
if (cachePath) {
42+
const cache = JSON.stringify({ cachedAt: Date.now(), ...componentMeta })
43+
if (!existsSync(opts.cacheDir)) {
44+
mkdirSync(opts.cacheDir, { recursive: true })
45+
}
46+
writeFileSync(cachePath, cache, { encoding: 'utf8', flag: 'w' })
47+
}
48+
49+
return componentMeta
50+
}
51+
52+
/**
53+
* Create new checker and get component meta
54+
*
55+
* @param fullPath full path of the component
56+
* @param opts options
57+
* @returns component meta
58+
*/
59+
function _getComponentMeta(fullPath: string, opts: Options) {
3960
const checker = createCheckerByJson(
4061
opts.rootDir,
4162
{
@@ -45,20 +66,7 @@ export function getComponentMeta(component: string, options?: Options): Componen
4566
exclude: []
4667
},
4768
)
48-
49-
const meta = checker.getComponentMeta(fullPath)
50-
const refinedMeta = refineMeta(meta)
51-
52-
if (opts.cache) {
53-
const cache = JSON.stringify({
54-
cachedAt: Date.now(),
55-
...refinedMeta,
56-
})
57-
if (!existsSync(opts.cacheDir)) {
58-
mkdirSync(opts.cacheDir, { recursive: true })
59-
}
60-
writeFileSync(cachePath, cache, { encoding: 'utf8', flag: 'w' })
61-
}
62-
63-
return refinedMeta
64-
}
69+
return refineMeta(
70+
checker.getComponentMeta(fullPath)
71+
)
72+
}

0 commit comments

Comments
 (0)