@@ -6,6 +6,7 @@ import { hyperproject, vfile, hyperbook } from "@hyperbook/fs";
66import { runArchive } from "./archive" ;
77import { makeDir } from "./helpers/make-dir" ;
88import { rimraf } from "rimraf" ;
9+ import extractZip from "extract-zip" ;
910import {
1011 Link ,
1112 Hyperproject ,
@@ -22,7 +23,7 @@ export async function runBuildProject(
2223 project : Hyperproject ,
2324 rootProject : Hyperproject ,
2425 out ?: string ,
25- filter ?: string
26+ filter ?: string ,
2627) : Promise < void > {
2728 const name = hyperproject . getName ( project ) ;
2829 if ( project . type === "book" ) {
@@ -33,7 +34,7 @@ export async function runBuildProject(
3334 project . basePath ,
3435 name ,
3536 out ,
36- filter
37+ filter ,
3738 ) ;
3839 } else {
3940 console . log ( `${ chalk . cyan ( `[${ name } ]` ) } Building Library.` ) ;
@@ -53,7 +54,7 @@ async function runBuild(
5354 basePath ?: string ,
5455 prefix ?: string ,
5556 out ?: string ,
56- filter ?: string
57+ filter ?: string ,
5758) : Promise < void > {
5859 console . log ( `${ chalk . blue ( `[${ prefix } ]` ) } Reading hyperbook.json.` ) ;
5960 const hyperbookJson = await hyperbook . getJson ( root ) ;
@@ -91,7 +92,7 @@ async function runBuild(
9192 rootOut = path . join ( out , ".hyperbook" , "out" , basePath || "" ) ;
9293 }
9394 console . log (
94- `${ chalk . blue ( `[${ prefix } ]` ) } Cleaning output folder ${ rootOut } .`
95+ `${ chalk . blue ( `[${ prefix } ]` ) } Cleaning output folder ${ rootOut } .` ,
9596 ) ;
9697
9798 await runArchive ( root , rootOut , prefix ) ;
@@ -133,7 +134,7 @@ async function runBuild(
133134 const pagesAndSections = await hyperbook . getPagesAndSections ( root ) ;
134135 const pageList = hyperbook . getPageList (
135136 pagesAndSections . sections ,
136- pagesAndSections . pages
137+ pagesAndSections . pages ,
137138 ) ;
138139 const searchDocuments : any [ ] = [ ] ;
139140
@@ -180,7 +181,7 @@ async function runBuild(
180181 } ) ;
181182 const permaFileOut = path . join (
182183 permaOut ,
183- file . markdown . data . permaid + ".html"
184+ file . markdown . data . permaid + ".html" ,
184185 ) ;
185186 await fs . writeFile ( permaFileOut , result . value ) ;
186187 }
@@ -190,7 +191,7 @@ async function runBuild(
190191 readline . cursorTo ( process . stdout , 0 ) ;
191192 }
192193 process . stdout . write (
193- `${ chalk . blue ( `[${ prefix } ]` ) } Buildung book: [${ i ++ } /${ bookFiles . length } ]`
194+ `${ chalk . blue ( `[${ prefix } ]` ) } Buildung book: [${ i ++ } /${ bookFiles . length } ]` ,
194195 ) ;
195196 if ( process . env . CI ) {
196197 process . stdout . write ( "\n" ) ;
@@ -202,7 +203,7 @@ async function runBuild(
202203 const glossaryOut = path . join ( rootOut , "glossary" ) ;
203204 if ( filter ) {
204205 glossaryFiles = glossaryFiles . filter ( ( f ) =>
205- f . path . absolute ?. endsWith ( filter )
206+ f . path . absolute ?. endsWith ( filter ) ,
206207 ) ;
207208 }
208209
@@ -242,44 +243,68 @@ async function runBuild(
242243 readline . cursorTo ( process . stdout , 0 ) ;
243244 }
244245 process . stdout . write (
245- `${ chalk . blue ( `[${ prefix } ]` ) } Buildung glossary: [${ i ++ } /${ glossaryFiles . length } ]`
246+ `${ chalk . blue ( `[${ prefix } ]` ) } Buildung glossary: [${ i ++ } /${ glossaryFiles . length } ]` ,
246247 ) ;
247248 if ( process . env . CI ) {
248249 process . stdout . write ( "\n" ) ;
249250 }
250251 }
251252 process . stdout . write ( "\n" ) ;
252253
254+ const assetsPath = path . join ( __dirname , "assets" ) ;
255+ const assetsOut = path . join ( rootOut , ASSETS_FOLDER ) ;
256+ await mkdir ( assetsOut , {
257+ recursive : true ,
258+ } ) ;
259+
253260 let otherFiles = await vfile . listForFolder ( root , "public" ) ;
254261 i = 1 ;
255262 for ( let file of otherFiles ) {
256263 const directoryOut = path . join ( rootOut , file . path . directory ) ;
257264 await makeDir ( directoryOut , {
258265 recursive : true ,
259266 } ) ;
260- if ( file . path . href ) {
267+ if ( file . path . href && file . extension !== ".h5p" ) {
261268 const fileOut = path . join ( rootOut , file . path . href ) ;
262269 await cp ( file . path . absolute , fileOut ) ;
270+ } else if ( file . path . href && file . extension === ".h5p" ) {
271+ const fileOut = path . join ( rootOut , file . path . href ) ;
272+ await extractZip ( file . path . absolute , {
273+ dir : fileOut ,
274+ } ) ;
275+
276+ const h5pLibraries = path . join ( assetsOut , "directive-h5p" , "libraries" ) ;
277+ await makeDir ( h5pLibraries , { recursive : true } ) ;
278+
279+ const files = await fs . readdir ( fileOut ) ;
280+ const libraryFolders = files . filter (
281+ ( file ) => ! [ "content" , "h5p.json" ] . includes ( file ) ,
282+ ) ;
283+ for ( const libraryFolder of libraryFolders ) {
284+ await cp (
285+ path . join ( fileOut , libraryFolder ) ,
286+ path . join ( h5pLibraries , libraryFolder ) ,
287+ {
288+ recursive : true ,
289+ force : true ,
290+ } ,
291+ ) ;
292+ await rimraf ( path . join ( fileOut , libraryFolder ) ) ;
293+ }
263294 }
264295 if ( ! process . env . CI ) {
265296 readline . clearLine ( process . stdout , 0 ) ;
266297 readline . cursorTo ( process . stdout , 0 ) ;
267298 }
268299 process . stdout . write (
269- `${ chalk . blue ( `[${ prefix } ]` ) } Copying public files: [${ i ++ } /${ otherFiles . length } ]`
300+ `${ chalk . blue ( `[${ prefix } ]` ) } Copying public files: [${ i ++ } /${ otherFiles . length } ]` ,
270301 ) ;
271302 if ( process . env . CI ) {
272303 process . stdout . write ( "\n" ) ;
273304 }
274305 }
275306 process . stdout . write ( "\n" ) ;
276307
277- const assetsPath = path . join ( __dirname , "assets" ) ;
278- const assetsOut = path . join ( rootOut , ASSETS_FOLDER ) ;
279- await mkdir ( assetsOut , {
280- recursive : true ,
281- } ) ;
282-
283308 i = 1 ;
284309 for ( let directive of directives ) {
285310 const assetsDirectivePath = path . join ( assetsPath , `directive-${ directive } ` ) ;
@@ -289,7 +314,7 @@ async function runBuild(
289314 readline . cursorTo ( process . stdout , 0 ) ;
290315 }
291316 process . stdout . write (
292- `${ chalk . blue ( `[${ prefix } ]` ) } Copying directive assets: [${ i ++ } /${ directives . size } ]`
317+ `${ chalk . blue ( `[${ prefix } ]` ) } Copying directive assets: [${ i ++ } /${ directives . size } ]` ,
293318 ) ;
294319 if ( process . env . CI ) {
295320 process . stdout . write ( "\n" ) ;
@@ -303,7 +328,7 @@ async function runBuild(
303328 } catch ( e ) {
304329 process . stdout . write ( "\n" ) ;
305330 process . stdout . write (
306- `${ chalk . red ( `[${ prefix } ]` ) } Failed copying directive assets: ${ directive } `
331+ `${ chalk . red ( `[${ prefix } ]` ) } Failed copying directive assets: ${ directive } ` ,
307332 ) ;
308333 process . stdout . write ( "\n" ) ;
309334 }
@@ -337,7 +362,7 @@ async function runBuild(
337362 readline . cursorTo ( process . stdout , 0 ) ;
338363 }
339364 process . stdout . write (
340- `${ chalk . blue ( `[${ prefix } ]` ) } Copying hyperbook assets: [${ i ++ } /${ mainAssets . length } ]`
365+ `${ chalk . blue ( `[${ prefix } ]` ) } Copying hyperbook assets: [${ i ++ } /${ mainAssets . length } ]` ,
341366 ) ;
342367 if ( process . env . CI ) {
343368 process . stdout . write ( "\n" ) ;
@@ -357,7 +382,7 @@ async function runBuild(
357382 foundLanguage = true ;
358383 } catch ( e ) {
359384 console . log (
360- `${ chalk . yellow ( `[${ prefix } ]` ) } ${ hyperbookJson . language } is no valid value for the lanuage key. See https://github.com/MihaiValentin/lunr-languages for possible values. Falling back to English.`
385+ `${ chalk . yellow ( `[${ prefix } ]` ) } ${ hyperbookJson . language } is no valid value for the lanuage key. See https://github.com/MihaiValentin/lunr-languages for possible values. Falling back to English.` ,
361386 ) ;
362387 }
363388 }
@@ -396,17 +421,31 @@ const SEARCH_DOCUMENTS = ${JSON.stringify(documents)};
396421 . readdir ( path . join ( __dirname , "locales" ) )
397422 . then ( ( files ) => files . map ( ( file ) => file . split ( "." ) [ 0 ] ) ) ;
398423 let language = "en" ;
399- if ( hyperbookJson . language && supportLanguages . includes ( hyperbookJson . language ) ) {
424+ if (
425+ hyperbookJson . language &&
426+ supportLanguages . includes ( hyperbookJson . language )
427+ ) {
400428 language = hyperbookJson . language ;
401429 }
402430
403- const i18nJS = await fs . readFile ( path . join ( rootOut , ASSETS_FOLDER , "i18n.js" ) , "utf-8" ) ;
404- const locales = await fs . readFile ( path . join ( __dirname , "locales" , `${ language } .json` ) , "utf-8" ) ;
405- await fs . writeFile ( path . join ( rootOut , ASSETS_FOLDER , "i18n.js" ) , i18nJS . replace ( / \/ \/ [ \s ] * L O C A L E S [ \s \S ] * ?[ \s ] * \/ \/ [ \s ] * L O C A L E S / g, `
431+ const i18nJS = await fs . readFile (
432+ path . join ( rootOut , ASSETS_FOLDER , "i18n.js" ) ,
433+ "utf-8" ,
434+ ) ;
435+ const locales = await fs . readFile (
436+ path . join ( __dirname , "locales" , `${ language } .json` ) ,
437+ "utf-8" ,
438+ ) ;
439+ await fs . writeFile (
440+ path . join ( rootOut , ASSETS_FOLDER , "i18n.js" ) ,
441+ i18nJS . replace (
442+ / \/ \/ [ \s ] * L O C A L E S [ \s \S ] * ?[ \s ] * \/ \/ [ \s ] * L O C A L E S / g,
443+ `
406444 // GENERATED
407445 const locales = ${ locales } ;
408- ` ) ) ;
409-
446+ ` ,
447+ ) ,
448+ ) ;
410449
411450 console . log ( `${ chalk . green ( `[${ prefix } ]` ) } Build success: ${ rootOut } ` ) ;
412451}
0 commit comments