@@ -3,10 +3,12 @@ import * as path from "node:path";
33import { fileURLToPath } from "node:url" ;
44import {
55 addTitleToGroups ,
6+ formatFiles ,
67 getExampleProjects ,
78 getProjectFiles ,
89 groupProjects ,
910 Project ,
11+ writeGeneratedFile ,
1012} from "./util.js" ;
1113
1214/*
@@ -23,7 +25,7 @@ const EXAMPLES_PAGES_DIR = path.resolve(DOCS_DIR, "./content/examples/");
2325 * Generates the <ExampleBlock> component that has all the source code of the example
2426 * This block can be used both in the /docs and in the /example page
2527 */
26- async function generateCodeForExample ( project : Project ) {
28+ async function generateCodeForExample ( project : Project , written : string [ ] ) {
2729 const projectFiles = getProjectFiles ( project ) ;
2830 const componentTarget = path . join (
2931 COMPONENT_DIR ,
@@ -33,18 +35,18 @@ async function generateCodeForExample(project: Project) {
3335 const indexFile = path . join ( componentTarget , "index.tsx" ) ;
3436 fs . rmSync ( componentTarget , { recursive : true , force : true } ) ;
3537 fs . mkdirSync ( componentTarget , { recursive : true } ) ;
36- fs . writeFileSync (
38+ writeGeneratedFile (
3739 indexFile ,
3840 `"use client";
3941import Component from "./App";
4042
4143export default Component;` ,
44+ written ,
4245 ) ;
4346
4447 projectFiles . forEach ( ( { filename, code } ) => {
4548 const target = path . join ( componentTarget , filename ) ;
46- fs . mkdirSync ( path . dirname ( target ) , { recursive : true } ) ;
47- fs . writeFileSync ( target , code ) ;
49+ writeGeneratedFile ( target , code , written ) ;
4850 } ) ;
4951}
5052
@@ -61,22 +63,25 @@ ${project.readme}
6163 *
6264 * Consists of the contents of the readme
6365 */
64- async function generatePageForExample ( project : Project ) {
66+ async function generatePageForExample ( project : Project , written : string [ ] ) {
6567 const code = templatePageForExample ( project ) ;
6668
6769 const target = path . join ( EXAMPLES_PAGES_DIR , project . fullSlug + ".mdx" ) ;
6870
69- fs . writeFileSync ( target , code ) ;
71+ writeGeneratedFile ( target , code , written ) ;
7072}
7173
7274/**
7375 * generates meta.json file for each example group, so that order is preserved
7476 */
75- async function generateMetaForExampleGroup ( group : {
76- title : string ;
77- slug : string ;
78- projects : Project [ ] ;
79- } ) {
77+ async function generateMetaForExampleGroup (
78+ group : {
79+ title : string ;
80+ slug : string ;
81+ projects : Project [ ] ;
82+ } ,
83+ written : string [ ] ,
84+ ) {
8085 if ( ! fs . existsSync ( path . join ( EXAMPLES_PAGES_DIR , group . slug ) ) ) {
8186 fs . mkdirSync ( path . join ( EXAMPLES_PAGES_DIR , group . slug ) ) ;
8287 }
@@ -90,15 +95,18 @@ async function generateMetaForExampleGroup(group: {
9095
9196 const code = JSON . stringify ( meta , undefined , 2 ) ;
9297
93- fs . writeFileSync ( target , code ) ;
98+ writeGeneratedFile ( target , code , written ) ;
9499}
95100
96101/**
97102 * Generates the exampleGroups.gen.ts file, which contains all the necessary
98103 * data about the examples & their groups for the components we use in the
99104 * docs. E.g. the interactive demos and example cards.
100105 */
101- async function generateExampleGroupsData ( projects : Project [ ] ) {
106+ async function generateExampleGroupsData (
107+ projects : Project [ ] ,
108+ written : string [ ] ,
109+ ) {
102110 const target = path . join ( COMPONENT_DIR , "exampleGroupsData.gen.ts" ) ;
103111
104112 const groups = addTitleToGroups ( groupProjects ( projects ) ) ;
@@ -146,10 +154,10 @@ export type ExampleData = ExampleGroupsData[number]["examplesData"][number];
146154export const exampleGroupsData: ExampleGroupsData = ${ JSON . stringify ( exampleGroupsData , undefined , 2 ) } ;
147155` ;
148156
149- fs . writeFileSync ( target , code ) ;
157+ writeGeneratedFile ( target , code , written ) ;
150158}
151159
152- async function addDependenciesToExample ( project : Project ) {
160+ async function addDependenciesToExample ( project : Project , written : string [ ] ) {
153161 const dependencies = project . config . dependencies || { } ;
154162 const devDependencies = project . config . devDependencies || { } ;
155163 if (
@@ -178,7 +186,11 @@ async function addDependenciesToExample(project: Project) {
178186 packageJsonObject . devDependencies [ key ] = "workspace:*" ;
179187 }
180188 } ) ;
181- fs . writeFileSync ( packageJson , JSON . stringify ( packageJsonObject , null , 2 ) ) ;
189+ writeGeneratedFile (
190+ packageJson ,
191+ JSON . stringify ( packageJsonObject , null , 2 ) ,
192+ written ,
193+ ) ;
182194 }
183195}
184196
@@ -197,17 +209,20 @@ fs.readdirSync(EXAMPLES_PAGES_DIR, { withFileTypes: true }).forEach((file) => {
197209// generate new files
198210const projects = getExampleProjects ( ) . filter ( ( p ) => p . config ?. docs === true ) ;
199211const groups = addTitleToGroups ( groupProjects ( projects ) ) ;
212+ const writtenFiles : string [ ] = [ ] ;
200213
201214for ( const group of Object . values ( groups ) ) {
202- await generateMetaForExampleGroup ( group ) ;
215+ await generateMetaForExampleGroup ( group , writtenFiles ) ;
203216
204217 for ( const project of group . projects ) {
205218 // eslint-disable-next-line no-console
206219 console . log ( "generating docs for" , project . fullSlug ) ;
207- await generateCodeForExample ( project ) ;
208- await generatePageForExample ( project ) ;
209- await addDependenciesToExample ( project ) ;
220+ await generateCodeForExample ( project , writtenFiles ) ;
221+ await generatePageForExample ( project , writtenFiles ) ;
222+ await addDependenciesToExample ( project , writtenFiles ) ;
210223 }
211224}
212225
213- await generateExampleGroupsData ( projects ) ;
226+ await generateExampleGroupsData ( projects , writtenFiles ) ;
227+
228+ formatFiles ( writtenFiles ) ;
0 commit comments