@@ -4,24 +4,22 @@ process.env.NODE_ENV = "development";
44
55import cls from "@triliumnext/server/src/services/cls.js" ;
66import { dirname , join , resolve } from "path" ;
7- import fs , { copyFile } from "fs/promises" ;
8- import fsExtra , { createWriteStream , type WriteStream } from "fs-extra" ;
7+ import * as fs from "fs/promises" ;
8+ import * as fsExtra from "fs-extra" ;
99import archiver from "archiver" ;
10+ import { WriteStream } from "fs" ;
11+ import { execSync } from "child_process" ;
12+ import BuildContext from "./context.js" ;
1013
1114const DOCS_ROOT = "../../../docs" ;
1215const OUTPUT_DIR = "../../site" ;
1316
14- async function main ( ) {
15- const i18n = await import ( "@triliumnext/server/src/services/i18n.js" ) ;
16- await i18n . initializeTranslations ( ) ;
17-
18- const sqlInit = ( await import ( "../../server/src/services/sql_init.js" ) ) . default ;
19- await sqlInit . createInitialDatabase ( true ) ;
20-
21- const note = await importData ( join ( __dirname , DOCS_ROOT , "User Guide" ) ) ;
17+ async function importAndExportDocs ( sourcePath : string , outputSubDir : string ) {
18+ const note = await importData ( sourcePath ) ;
2219
23- // Export
24- const zipFilePath = "output.zip" ;
20+ // Use a meaningful name for the temporary zip file
21+ const zipName = outputSubDir || "user-guide" ;
22+ const zipFilePath = `output-${ zipName } .zip` ;
2523 try {
2624 const { exportToZip } = ( await import ( "@triliumnext/server/src/services/export/zip.js" ) ) . default ;
2725 const branch = note . getParentBranches ( ) [ 0 ] ;
@@ -30,28 +28,53 @@ async function main() {
3028 "export" ,
3129 null
3230 ) ;
33- const fileOutputStream = createWriteStream ( zipFilePath ) ;
31+ const fileOutputStream = fsExtra . createWriteStream ( zipFilePath ) ;
3432 await exportToZip ( taskContext , branch , "share" , fileOutputStream ) ;
3533 await waitForStreamToFinish ( fileOutputStream ) ;
36- await extractZip ( zipFilePath , OUTPUT_DIR ) ;
34+
35+ // Output to root directory if outputSubDir is empty, otherwise to subdirectory
36+ const outputPath = outputSubDir ? join ( OUTPUT_DIR , outputSubDir ) : OUTPUT_DIR ;
37+ await extractZip ( zipFilePath , outputPath ) ;
3738 } finally {
3839 if ( await fsExtra . exists ( zipFilePath ) ) {
3940 await fsExtra . rm ( zipFilePath ) ;
4041 }
4142 }
43+ }
44+
45+ async function buildDocsInner ( ) {
46+ const i18n = await import ( "@triliumnext/server/src/services/i18n.js" ) ;
47+ await i18n . initializeTranslations ( ) ;
48+
49+ const sqlInit = ( await import ( "../../server/src/services/sql_init.js" ) ) . default ;
50+ await sqlInit . createInitialDatabase ( true ) ;
51+
52+ // Wait for becca to be loaded before importing data
53+ const beccaLoader = await import ( "../../server/src/becca/becca_loader.js" ) ;
54+ await beccaLoader . beccaLoaded ;
55+
56+ // Build User Guide
57+ console . log ( "Building User Guide..." ) ;
58+ await importAndExportDocs ( join ( __dirname , DOCS_ROOT , "User Guide" ) , "user-guide" ) ;
59+
60+ // Build Developer Guide
61+ console . log ( "Building Developer Guide..." ) ;
62+ await importAndExportDocs ( join ( __dirname , DOCS_ROOT , "Developer Guide" ) , "developer-guide" ) ;
4263
4364 // Copy favicon.
44- await copyFile ( "../../apps/website/src/assets/favicon.ico" , join ( OUTPUT_DIR , "favicon.ico" ) ) ;
65+ await fs . copyFile ( "../../apps/website/src/assets/favicon.ico" , join ( OUTPUT_DIR , "favicon.ico" ) ) ;
66+ await fs . copyFile ( "../../apps/website/src/assets/favicon.ico" , join ( OUTPUT_DIR , "user-guide" , "favicon.ico" ) ) ;
67+ await fs . copyFile ( "../../apps/website/src/assets/favicon.ico" , join ( OUTPUT_DIR , "developer-guide" , "favicon.ico" ) ) ;
4568
4669 console . log ( "Documentation built successfully!" ) ;
4770}
4871
4972export async function importData ( path : string ) {
5073 const buffer = await createImportZip ( path ) ;
51- const importService = ( await import ( "@triliumnext /server/src/services/import/zip.js" ) ) . default ;
52- const TaskContext = ( await import ( "@triliumnext /server/src/services/task_context.js" ) ) . default ;
74+ const importService = ( await import ( "../.. /server/src/services/import/zip.js" ) ) . default ;
75+ const TaskContext = ( await import ( "../.. /server/src/services/task_context.js" ) ) . default ;
5376 const context = new TaskContext ( "no-progress-reporting" , "importNotes" , null ) ;
54- const becca = ( await import ( "@triliumnext /server/src/becca/becca.js" ) ) . default ;
77+ const becca = ( await import ( "../.. /server/src/becca/becca.js" ) ) . default ;
5578
5679 const rootNote = becca . getRoot ( ) ;
5780 if ( ! rootNote ) {
@@ -106,4 +129,19 @@ export async function extractZip(zipFilePath: string, outputPath: string, ignore
106129 } ) ;
107130}
108131
109- cls . init ( main ) ;
132+ export default async function buildDocs ( { gitRootDir } : BuildContext ) {
133+ // Build the share theme.
134+ execSync ( `pnpm run --filter share-theme build` , {
135+ stdio : "inherit" ,
136+ cwd : gitRootDir
137+ } ) ;
138+
139+ // Trigger the actual build.
140+ await new Promise ( ( res , rej ) => {
141+ cls . init ( ( ) => {
142+ buildDocsInner ( )
143+ . catch ( rej )
144+ . then ( res ) ;
145+ } ) ;
146+ } ) ;
147+ }
0 commit comments