@@ -7,73 +7,47 @@ import {
7
7
import { exec } from "node:child_process" ;
8
8
import * as fs from "node:fs" ;
9
9
import * as path from "node:path" ;
10
- import { cwd } from "node:process" ;
11
10
import { promisify } from "node:util" ;
12
11
import { type DefaultTheme } from "vitepress" ;
13
- import type { Component , ImportPaths } from "../types/docs .ts" ;
14
- import { getFileMeta } from "./utils .ts" ;
12
+ import { getFileMeta } from "../../../scripts/utils .ts" ;
13
+ import type { Component , ImportPaths , Metadata } from "../../../types/docs .ts" ;
15
14
16
15
const asyncExec = promisify ( exec ) ;
17
16
18
17
const { dirname } = getFileMeta ( import . meta. url ) ;
19
- const workspaceDir = path . resolve ( dirname , ".." ) ;
20
- const webComponentsSrcDir = path . join (
21
- workspaceDir ,
22
- "packages/web-components/src" ,
23
- ) ;
18
+ const packageDir = path . join ( dirname , ".." ) ;
19
+ const packageSrcDir = path . join ( packageDir , "src" ) ;
24
20
25
- const workspaceDistDir = path . join ( workspaceDir , "dist" ) ;
26
- const metadataFile = path . join ( workspaceDistDir , "components-metadata.json" ) ;
21
+ const metadataFile = path . join ( packageDir , "components-metadata.json" ) ;
27
22
28
23
const generateCem = async ( ) : Promise < Package > => {
29
- const entrypoint = path . resolve (
30
- workspaceDir ,
31
- "./packages/web-components/src" ,
32
- ) ;
33
-
34
- const distDir = path . relative ( cwd ( ) , path . join ( workspaceDir , "dist" ) ) ;
35
-
36
24
const globs : string [ ] = [
37
- `${ entrypoint } /**/index.ts` ,
38
- `${ entrypoint } /**/*/index.ts` ,
39
- `!${ entrypoint } /utils/**` ,
40
- `!${ entrypoint } /internals/**` ,
25
+ `${ packageSrcDir } /**/index.ts` ,
26
+ `${ packageSrcDir } /**/*/index.ts` ,
27
+ `!${ packageSrcDir } /utils/**` ,
28
+ `!${ packageSrcDir } /internals/**` ,
41
29
] ;
42
30
43
- await asyncExec ( `shx rm -rf ${ distDir } ` ) ;
44
31
const { stderr : cemAnalyzeStderr , stdout : cemAnalyzeStdout } =
45
32
await asyncExec (
46
33
[
47
34
"cem" ,
48
35
"analyze" ,
49
36
"--litelement" ,
50
- "--outdir" ,
51
- distDir ,
37
+ "--packagejson" ,
52
38
globs . map ( g => `--globs ${ g } ` ) . join ( " " ) ,
53
39
] . join ( " " ) ,
54
40
) ;
55
41
56
42
if ( cemAnalyzeStdout ) console . log ( cemAnalyzeStdout ) ;
57
43
if ( cemAnalyzeStderr ) console . error ( cemAnalyzeStderr ) ;
58
44
59
- const cemFile = path . join ( workspaceDistDir , "custom-elements.json" ) ;
45
+ const cemFile = path . join ( packageDir , "custom-elements.json" ) ;
60
46
61
47
return JSON . parse ( fs . readFileSync ( cemFile , "utf8" ) ) as Package ;
62
48
} ;
63
49
64
- const getKebabCaseComponentName = ( component : Declaration ) => {
65
- if ( ! ( "tagName" in component ) || ! component . tagName ) return null ;
66
-
67
- const tagName = component . tagName ;
68
-
69
- return tagName . replace ( "tapsi-" , "" ) ;
70
- } ;
71
-
72
- void ( async ( ) => {
73
- console . log ( "🧩 generating metadata..." ) ;
74
-
75
- const cem = await generateCem ( ) ;
76
-
50
+ const generateMetadataFromCem = async ( cem : Package ) : Promise < Metadata > => {
77
51
const sidebarItemsMap : Record < string , DefaultTheme . SidebarItem > = { } ;
78
52
const components : Component [ ] = [ ] ;
79
53
@@ -89,7 +63,7 @@ void (async () => {
89
63
filteredModules . forEach ( module => {
90
64
const moduleSrc = module . path ;
91
65
const moduleDir = path . dirname ( moduleSrc ) ;
92
- const relativePath = path . relative ( webComponentsSrcDir , moduleDir ) ;
66
+ const relativePath = path . relative ( packageSrcDir , moduleDir ) ;
93
67
94
68
if ( ! relativePath ) return ;
95
69
@@ -199,17 +173,27 @@ void (async () => {
199
173
200
174
const sidebarItems = [ iconsSidebarItem , componentSidebarItems ] ;
201
175
202
- fs . writeFileSync (
203
- metadataFile ,
204
- JSON . stringify (
205
- {
206
- components,
207
- sidebarItems,
208
- } ,
209
- null ,
210
- 2 ,
211
- ) ,
212
- ) ;
176
+ return {
177
+ sidebarItems,
178
+ components,
179
+ } ;
180
+ } ;
181
+
182
+ const getKebabCaseComponentName = ( component : Declaration ) => {
183
+ if ( ! ( "tagName" in component ) || ! component . tagName ) return null ;
184
+
185
+ const tagName = component . tagName ;
186
+
187
+ return tagName . replace ( "tapsi-" , "" ) ;
188
+ } ;
189
+
190
+ void ( async ( ) => {
191
+ console . log ( "🧩 generating metadata..." ) ;
192
+
193
+ const cem = await generateCem ( ) ;
194
+ const metadata = await generateMetadataFromCem ( cem ) ;
195
+
196
+ fs . writeFileSync ( metadataFile , JSON . stringify ( metadata , null , 2 ) ) ;
213
197
214
198
console . log ( "✅ docs metadata generated." ) ;
215
199
} ) ( ) ;
0 commit comments