@@ -68,8 +68,6 @@ class HTMLToGutenberg {
68
68
69
69
try {
70
70
for ( const HTMLFile of this . HTMLFiles ) {
71
- const files = [ ] ;
72
-
73
71
const HTMLFileContent = fs . readFileSync ( HTMLFile , "utf-8" ) ;
74
72
75
73
const blockData = await getBlockData ( HTMLFileContent , {
@@ -89,14 +87,30 @@ class HTMLToGutenberg {
89
87
printRenderPHP ( blockData ) ,
90
88
] ) ;
91
89
92
- files . push ( { type : "index" , content : indexJS } ) ;
93
- files . push ( { type : "edit" , content : editJS } ) ;
94
- files . push ( { type : "json" , content : blockJSON } ) ;
95
- files . push ( { type : "php" , content : renderPHP } ) ;
90
+ const blockPath = this . generateBlockPath ( HTMLFile ) ;
91
+
92
+ generatedFiles . push ( {
93
+ filename : "index.js" ,
94
+ content : indexJS ,
95
+ blockPath,
96
+ } ) ;
97
+
98
+ generatedFiles . push ( {
99
+ filename : "edit.js" ,
100
+ content : editJS ,
101
+ blockPath,
102
+ } ) ;
103
+
104
+ generatedFiles . push ( {
105
+ filename : "block.json" ,
106
+ content : blockJSON ,
107
+ blockPath,
108
+ } ) ;
96
109
97
110
generatedFiles . push ( {
98
- source : HTMLFile ,
99
- files,
111
+ filename : "render.php" ,
112
+ content : renderPHP ,
113
+ blockPath,
100
114
} ) ;
101
115
}
102
116
} catch ( err ) {
@@ -106,55 +120,81 @@ class HTMLToGutenberg {
106
120
return generatedFiles ;
107
121
}
108
122
123
+ async getFilesOverrides ( ) {
124
+ const overrides = [ ] ;
125
+
126
+ const htmlFiles = this . HTMLFiles . map ( ( htmlPath ) => ( {
127
+ fullPath : path . resolve ( htmlPath ) ,
128
+ baseName : path . basename ( htmlPath , ".html" ) ,
129
+ dir : path . dirname ( htmlPath ) ,
130
+ } ) ) ;
131
+
132
+ const htmlFilesMap = new Map ( ) ;
133
+ for ( const { baseName, dir } of htmlFiles ) {
134
+ htmlFilesMap . set ( path . join ( dir , baseName ) , true ) ;
135
+ }
136
+
137
+ const allFiles = glob . sync ( path . join ( this . inputDirectory , "/**/*.*" ) ) ;
138
+
139
+ for ( const file of allFiles ) {
140
+ const resolved = path . resolve ( file ) ;
141
+
142
+ if ( resolved . endsWith ( ".html" ) ) continue ;
143
+
144
+ const filename = path . basename ( file ) ;
145
+ const dirname = path . dirname ( file ) ;
146
+
147
+ const match = filename . match ( / ^ ( .+ ?) \. ( .+ ?) \. ( .+ ) $ / ) ;
148
+ if ( ! match ) continue ;
149
+
150
+ const [ _ , blockSlug , innerName , ext ] = match ;
151
+ const htmlFileKey = path . join ( dirname , blockSlug ) ;
152
+
153
+ if ( ! htmlFilesMap . has ( htmlFileKey ) ) continue ;
154
+
155
+ const blockPath = path . join ( this . outputDirectory , blockSlug ) ;
156
+ const content = fs . readFileSync ( resolved , "utf-8" ) ;
157
+
158
+ overrides . push ( {
159
+ filename : `${ innerName } .${ ext } ` ,
160
+ content,
161
+ blockPath,
162
+ } ) ;
163
+ }
164
+
165
+ return overrides ;
166
+ }
167
+
109
168
createDirectoryIfNotExists ( directoryPath ) {
110
169
try {
111
170
fs . mkdirSync ( directoryPath , { recursive : true } ) ;
112
171
} catch { }
113
172
}
114
173
115
- writeFiles ( generatedFiles ) {
116
- const generatedBlocksPaths = [ ] ;
174
+ cleanDirectory ( directoryPath ) {
175
+ const filesInDirectory = fs . readdirSync ( directoryPath ) ;
117
176
118
- generatedFiles . forEach ( ( generatedFile ) => {
119
- const blockPath = this . generateBlockPath ( generatedFile . source ) ;
120
- this . createDirectoryIfNotExists ( blockPath ) ;
177
+ for ( const file of filesInDirectory ) {
178
+ if ( ! [ "block.json" , "edit.js" , "index.js" , "render.php" ] . includes ( file ) ) {
179
+ fs . unlinkSync ( path . join ( directoryPath , file ) , ( err ) => {
180
+ if ( err ) throw err ;
181
+ } ) ;
182
+ }
183
+ }
184
+ }
121
185
122
- generatedBlocksPaths . push ( blockPath ) ;
123
-
124
- generatedFile . files . forEach ( ( { type, content } ) => {
125
- switch ( type ) {
126
- case "index" :
127
- fs . writeFileSync (
128
- path . join ( blockPath , "index.js" ) ,
129
- content ,
130
- "utf-8" ,
131
- ) ;
132
- break ;
133
-
134
- case "edit" :
135
- fs . writeFileSync ( path . join ( blockPath , "edit.js" ) , content , "utf-8" ) ;
136
- break ;
137
-
138
- case "json" :
139
- fs . writeFileSync (
140
- path . join ( blockPath , "block.json" ) ,
141
- content ,
142
- "utf-8" ,
143
- ) ;
144
- break ;
145
-
146
- case "php" :
147
- fs . writeFileSync (
148
- path . join ( blockPath , "render.php" ) ,
149
- content ,
150
- "utf-8" ,
151
- ) ;
152
- break ;
153
- }
154
- } ) ;
186
+ cleanOutputAndWriteFiles ( files ) {
187
+ const blockPaths = new Set ( files . map ( ( file ) => file . blockPath ) ) ;
188
+
189
+ blockPaths . forEach ( ( blockPath ) => {
190
+ this . createDirectoryIfNotExists ( blockPath ) ;
191
+ this . cleanDirectory ( blockPath ) ;
155
192
} ) ;
156
193
157
- return generatedBlocksPaths ;
194
+ files . forEach ( ( file ) => {
195
+ const { blockPath, filename, content } = file ;
196
+ fs . writeFileSync ( path . join ( blockPath , filename ) , content , "utf-8" ) ;
197
+ } ) ;
158
198
}
159
199
160
200
removeDeletedBlocks ( ) {
0 commit comments