Skip to content

Commit ec3e773

Browse files
committed
fix up embedding logic and few nits
1 parent e7e4733 commit ec3e773

22 files changed

+1537
-63
lines changed

bin/dory.ts

Lines changed: 121 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,19 @@ const __dirname = dirname(__filename);
1313
// Get the root directory of the Dory package
1414
const 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!');

docs/embed-example.html

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Dory Embedded Documentation Example</title>
7+
<style>
8+
* {
9+
margin: 0;
10+
padding: 0;
11+
box-sizing: border-box;
12+
}
13+
14+
body {
15+
font-family: system-ui, -apple-system, sans-serif;
16+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
17+
min-height: 100vh;
18+
display: flex;
19+
align-items: center;
20+
justify-content: center;
21+
padding: 2rem;
22+
}
23+
24+
.container {
25+
max-width: 800px;
26+
background: white;
27+
border-radius: 1rem;
28+
padding: 3rem;
29+
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
30+
}
31+
32+
h1 {
33+
font-size: 2.5rem;
34+
margin-bottom: 1rem;
35+
color: #1f2937;
36+
}
37+
38+
p {
39+
font-size: 1.125rem;
40+
color: #6b7280;
41+
margin-bottom: 2rem;
42+
line-height: 1.6;
43+
}
44+
45+
.buttons {
46+
display: flex;
47+
flex-wrap: wrap;
48+
gap: 1rem;
49+
margin-bottom: 2rem;
50+
}
51+
52+
button {
53+
padding: 0.75rem 1.5rem;
54+
font-size: 1rem;
55+
font-weight: 600;
56+
border: none;
57+
border-radius: 0.5rem;
58+
cursor: pointer;
59+
transition: all 0.2s;
60+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
61+
}
62+
63+
.btn-primary {
64+
background: #3b82f6;
65+
color: white;
66+
}
67+
68+
.btn-primary:hover {
69+
background: #2563eb;
70+
transform: translateY(-2px);
71+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
72+
}
73+
74+
.btn-secondary {
75+
background: #f3f4f6;
76+
color: #374151;
77+
}
78+
79+
.btn-secondary:hover {
80+
background: #e5e7eb;
81+
}
82+
83+
.code-block {
84+
background: #1f2937;
85+
color: #f9fafb;
86+
padding: 1.5rem;
87+
border-radius: 0.5rem;
88+
overflow-x: auto;
89+
margin-bottom: 2rem;
90+
font-family: 'Monaco', 'Menlo', monospace;
91+
font-size: 0.875rem;
92+
line-height: 1.6;
93+
}
94+
95+
.status {
96+
padding: 1rem;
97+
background: #f0fdf4;
98+
border: 1px solid #86efac;
99+
border-radius: 0.5rem;
100+
color: #166534;
101+
margin-bottom: 1rem;
102+
}
103+
</style>
104+
</head>
105+
<body>
106+
<div class="container">
107+
<h1>🐟 Dory Embedded Documentation</h1>
108+
<p>
109+
This example demonstrates how to embed Dory documentation in your application.
110+
</p>
111+
112+
<div id="status" class="status" style="display: none;"></div>
113+
114+
<div class="buttons">
115+
<button class="btn-primary" onclick="DoryDocs.open()">
116+
📚 Open Documentation
117+
</button>
118+
<button class="btn-secondary" onclick="DoryDocs.open('/features')">
119+
⚡ Open Features
120+
</button>
121+
<button class="btn-secondary" onclick="DoryDocs.toggle()">
122+
🔄 Toggle
123+
</button>
124+
</div>
125+
126+
<h2 style="margin-top: 2rem; margin-bottom: 1rem; color: #1f2937;">Integration Code</h2>
127+
<div class="code-block">
128+
&lt;script src="http://localhost:3000/dist/embed.js"&gt;&lt;/script&gt;
129+
130+
&lt;script&gt;
131+
DoryDocs.init({
132+
baseUrl: 'http://localhost:3000',
133+
position: 'right',
134+
width: 600,
135+
closeOnEscape: true,
136+
});
137+
&lt;/script&gt;
138+
139+
&lt;button onclick="DoryDocs.open()"&gt;Help&lt;/button&gt;
140+
</div>
141+
</div>
142+
143+
<!-- Load Dory Embed Script -->
144+
<script src="http://localhost:3000/dist/embed.js"></script>
145+
146+
<!-- Initialize Dory -->
147+
<script>
148+
window.addEventListener('dory:ready', () => {
149+
const status = document.getElementById('status');
150+
status.style.display = 'block';
151+
status.textContent = '✅ DoryDocs loaded and ready!';
152+
153+
DoryDocs.init({
154+
baseUrl: 'http://localhost:3000',
155+
position: 'right',
156+
width: 400,
157+
theme: 'inherit',
158+
closeOnEscape: true,
159+
closeOnOverlayClick: true,
160+
onOpen: () => {
161+
console.log('Docs opened');
162+
status.textContent = '📖 Documentation opened';
163+
},
164+
onClose: () => {
165+
console.log('Docs closed');
166+
status.textContent = '✅ DoryDocs ready';
167+
},
168+
});
169+
});
170+
171+
if (window.DoryDocs) {
172+
window.dispatchEvent(new Event('dory:ready'));
173+
}
174+
</script>
175+
</body>
176+
</html>

package.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@clidey/dory",
33
"private": false,
4-
"version": "0.29.0",
4+
"version": "0.30.0",
55
"type": "module",
66
"description": "A CLI tool for building and previewing documentation sites",
77
"keywords": [
@@ -14,6 +14,9 @@
1414
"bin": {
1515
"dory": "./bin/dist/dory.js"
1616
},
17+
"files": [
18+
"bin/dist/**/*"
19+
],
1720
"repository": {
1821
"type": "git",
1922
"url": "https://github.com/clidey/dory.git"
@@ -28,9 +31,13 @@
2831
"dev": "vite",
2932
"build": "npm run build:cli && tsc -b && vite build",
3033
"build:cli": "tsc -p tsconfig.bin.json",
34+
"build:embed": "npm run build:embed:loader && npm run build:embed:widget && npm run build:embed:app",
35+
"build:embed:loader": "vite build -c vite.config.embed-loader.ts",
36+
"build:embed:widget": "vite build -c vite.config.embed-widget.ts",
37+
"build:embed:app": "vite build -c vite.config.embed-app.ts",
3138
"preview": "vite preview",
32-
"publish": "npm run build && npm publish --access public",
33-
"prepublishOnly": "npm run build"
39+
"publish": "npm run build && npm publish --access public --no-git-checks",
40+
"prepublishOnly": "echo 'Skipping prepublishOnly - build handled by publish script'"
3441
},
3542
"dependencies": {
3643
"@algolia/autocomplete-core": "^1.19.2",
@@ -73,6 +80,7 @@
7380
},
7481
"packageManager": "pnpm@10.10.0+sha512.d615db246fe70f25dcfea6d8d73dee782ce23e2245e3c4f6f888249fb568149318637dca73c2c5c8ef2a4ca0d5657fb9567188bfab47f566d1ee6ce987815c39",
7582
"devDependencies": {
76-
"@types/node": "^24.0.12"
83+
"@types/node": "^24.0.12",
84+
"terser": "^5.44.1"
7785
}
7886
}

0 commit comments

Comments
 (0)