@@ -13,9 +13,19 @@ const __dirname = dirname(__filename);
1313// Get the root directory of the Dory package
1414const getDoryRoot = ( ) : string => {
1515 // In development: bin/dory.ts -> root is parent
16- // In production: bin/dist/dory.js -> root is parent of parent
16+ // In production (local): bin/dist/dory.js -> root is parent of parent
17+ // In production (installed): node_modules/@clidey/dory/bin/dist/dory.js -> root is parent of parent
1718 const isDevelopment = __dirname . endsWith ( 'bin' ) ;
18- return isDevelopment ? resolve ( __dirname , '..' ) : resolve ( __dirname , '..' , '..' ) ;
19+ const isLocalDist = __dirname . endsWith ( resolve ( 'bin' , 'dist' ) ) ;
20+
21+ if ( isDevelopment ) {
22+ return resolve ( __dirname , '..' ) ;
23+ } else if ( isLocalDist ) {
24+ return resolve ( __dirname , '..' , '..' ) ;
25+ } else {
26+ // Installed package: node_modules/@clidey /dory/bin/dist/dory.js
27+ return resolve ( __dirname , '..' , '..' ) ;
28+ }
1929} ;
2030
2131// Get the user's current working directory
@@ -198,45 +208,128 @@ const commands = {
198208
199209 console . log ( '✨ Documentation ready in dist/' ) ;
200210
211+ // Step 6.5: Build embed files
212+ console . log ( '📦 Building embed files...' ) ;
213+
214+ try {
215+ // Build embed loader
216+ console . log ( ' Building embed loader...' ) ;
217+ execSync ( 'pnpm exec vite build -c vite.config.embed-loader.ts' , {
218+ stdio : 'inherit' ,
219+ cwd : doryRoot ,
220+ env : { ...process . env }
221+ } ) ;
222+
223+ // Build embed widget
224+ console . log ( ' Building embed widget...' ) ;
225+ execSync ( 'pnpm exec vite build -c vite.config.embed-widget.ts' , {
226+ stdio : 'inherit' ,
227+ cwd : doryRoot ,
228+ env : { ...process . env }
229+ } ) ;
230+
231+ // Build embed app
232+ console . log ( ' Building embed app...' ) ;
233+ execSync ( 'pnpm exec vite build -c vite.config.embed-app.ts' , {
234+ stdio : 'inherit' ,
235+ cwd : doryRoot ,
236+ env : { ...process . env }
237+ } ) ;
238+
239+ // Copy embed files to user's dist if different
240+ if ( doryDistDir !== userDistDir ) {
241+ const embedFiles = [ 'embed.js' , 'embed-widget.js' , 'embed-app.js' , 'embed.css' ] ;
242+ embedFiles . forEach ( file => {
243+ const srcPath = resolve ( doryDistDir , file ) ;
244+ const destPath = resolve ( userDistDir , file ) ;
245+ if ( existsSync ( srcPath ) ) {
246+ cpSync ( srcPath , destPath , { force : true } ) ;
247+ }
248+ } ) ;
249+
250+ // Copy any chunk files (embed-*.js)
251+ const files = readdirSync ( doryDistDir ) ;
252+ files . forEach ( file => {
253+ if ( file . startsWith ( 'embed-' ) && file . endsWith ( '.js' ) ) {
254+ const srcPath = resolve ( doryDistDir , file ) ;
255+ const destPath = resolve ( userDistDir , file ) ;
256+ cpSync ( srcPath , destPath , { force : true } ) ;
257+ }
258+ } ) ;
259+ }
260+
261+ console . log ( '✨ Embed files built successfully!' ) ;
262+ console . log ( '' ) ;
263+ console . log ( ' 📝 Add to your site:' ) ;
264+ console . log ( ' <script src="https://your-docs.com/embed.js"></script>' ) ;
265+ console . log ( ' <button onclick="DoryDocs.open()">Help</button>' ) ;
266+ console . log ( '' ) ;
267+
268+ } catch ( error ) {
269+ console . warn ( '⚠️ Embed build failed, but main build succeeded' ) ;
270+ console . warn ( ' Your documentation site is still available at dist/' ) ;
271+ if ( error instanceof Error ) {
272+ console . warn ( ` Error: ${ error . message } ` ) ;
273+ }
274+ }
275+
201276 } finally {
202277 // Step 7: Always clean up temp directories and restore backup
203278 console . log ( '🧹 Cleaning up...' ) ;
204279
205- try {
206- // Remove the temporary docs directory
207- if ( existsSync ( tempDocsDir ) ) {
208- rmSync ( tempDocsDir , { recursive : true , force : true } ) ;
209- }
280+ // First priority: Restore the original docs directory if it existed
281+ if ( docsExistedBefore && existsSync ( docsBackupDir ) ) {
282+ console . log ( '📦 Restoring original docs directory...' ) ;
210283
211- // Restore the original docs directory if it existed before
212- if ( docsExistedBefore && existsSync ( docsBackupDir ) ) {
213- console . log ( '📦 Restoring original docs directory...' ) ;
284+ let restoredSuccessfully = false ;
285+ try {
286+ // Remove the temporary docs directory
287+ if ( existsSync ( tempDocsDir ) ) {
288+ rmSync ( tempDocsDir , { recursive : true , force : true } ) ;
289+ }
290+
291+ // Restore from backup
214292 cpSync ( docsBackupDir , tempDocsDir , { recursive : true , force : true } ) ;
293+ restoredSuccessfully = true ;
294+
295+ // Only delete the backup after successful restoration
215296 rmSync ( docsBackupDir , { recursive : true , force : true } ) ;
297+ } catch ( error ) {
298+ console . error ( '❌ Failed to restore docs directory!' ) ;
299+ console . error ( ` Backup is preserved at: ${ docsBackupDir } ` ) ;
300+ console . error ( ' You can manually restore by running:' ) ;
301+ console . error ( ` cp -r "${ docsBackupDir } " "${ tempDocsDir } "` ) ;
302+
303+ if ( error instanceof Error ) {
304+ console . error ( ` Error: ${ error . message } ` ) ;
305+ }
306+
307+ // Critical: Don't delete the backup if restoration failed
308+ if ( ! restoredSuccessfully ) {
309+ console . error ( ' ⚠️ Your original docs are safe in the backup directory' ) ;
310+ }
311+ }
312+ } else if ( docsExistedBefore && ! existsSync ( docsBackupDir ) ) {
313+ console . warn ( '⚠️ Warning: Original docs directory existed but backup not found' ) ;
314+ console . warn ( ` Expected backup at: ${ docsBackupDir } ` ) ;
315+ } else {
316+ // No docs existed before, just clean up the temporary directory
317+ try {
318+ if ( existsSync ( tempDocsDir ) ) {
319+ rmSync ( tempDocsDir , { recursive : true , force : true } ) ;
320+ }
321+ } catch ( error ) {
322+ console . warn ( '⚠️ Could not remove temporary docs directory' ) ;
216323 }
324+ }
217325
218- // Only clean up doryDistDir if it's different from userDistDir
219- // (in production they're different, in dev they might be the same)
326+ // Clean up doryDistDir if it's different from userDistDir
327+ try {
220328 if ( doryDistDir !== userDistDir && existsSync ( doryDistDir ) ) {
221329 rmSync ( doryDistDir , { recursive : true , force : true } ) ;
222330 }
223331 } catch ( error ) {
224- console . warn ( '⚠️ Could not clean up temp directories' ) ;
225- // Try to restore backup even if cleanup failed
226- if ( docsExistedBefore && existsSync ( docsBackupDir ) ) {
227- console . log ( '🔄 Attempting to restore backup...' ) ;
228- try {
229- if ( existsSync ( tempDocsDir ) ) {
230- rmSync ( tempDocsDir , { recursive : true , force : true } ) ;
231- }
232- cpSync ( docsBackupDir , tempDocsDir , { recursive : true , force : true } ) ;
233- rmSync ( docsBackupDir , { recursive : true , force : true } ) ;
234- } catch ( restoreError ) {
235- console . error ( '❌ Failed to restore docs backup!' ) ;
236- console . error ( ` Backup location: ${ docsBackupDir } ` ) ;
237- console . error ( ' You may need to manually restore your docs directory' ) ;
238- }
239- }
332+ console . warn ( '⚠️ Could not clean up dory dist directory' ) ;
240333 }
241334
242335 console . log ( '✅ Done!' ) ;
0 commit comments