site-mirror 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/cli.mjs CHANGED
@@ -214,14 +214,42 @@ async function cmdRun(cwd, cliArgs) {
214
214
  }
215
215
 
216
216
  async function cmdServe(cwd, port) {
217
- const outDir = path.join(cwd, 'offline');
218
- const exists = await fs
219
- .access(outDir)
220
- .then(() => true)
221
- .catch(() => false);
217
+ // Try to find the output directory:
218
+ // 1. Check config file for 'out' setting
219
+ // 2. Check for 'offline' folder (default)
220
+ // 3. Check if current directory has index.html (serve current dir)
221
+ const config = await loadConfig(cwd);
222
+ const configOut = config.out ? path.resolve(cwd, config.out) : null;
223
+ const defaultOut = path.join(cwd, 'offline');
224
+
225
+ let outDir = null;
226
+
227
+ // Check config's out directory first
228
+ if (configOut) {
229
+ const configExists = await fs.access(configOut).then(() => true).catch(() => false);
230
+ if (configExists) outDir = configOut;
231
+ }
232
+
233
+ // Check default 'offline' folder
234
+ if (!outDir) {
235
+ const defaultExists = await fs.access(defaultOut).then(() => true).catch(() => false);
236
+ if (defaultExists) outDir = defaultOut;
237
+ }
238
+
239
+ // Check if current directory has index.html (might be serving from inside output folder)
240
+ if (!outDir) {
241
+ const indexExists = await fs.access(path.join(cwd, 'index.html')).then(() => true).catch(() => false);
242
+ if (indexExists) outDir = cwd;
243
+ }
222
244
 
223
- if (!exists) {
224
- console.error(`No offline folder found at ${outDir}`);
245
+ if (!outDir) {
246
+ console.error('No output folder found.');
247
+ console.error('');
248
+ console.error('Tried:');
249
+ if (configOut) console.error(` - ${configOut} (from config)`);
250
+ console.error(` - ${defaultOut} (default)`);
251
+ console.error(` - ${cwd} (current directory)`);
252
+ console.error('');
225
253
  console.error('Run "site-mirror run" first to download the site.');
226
254
  process.exit(1);
227
255
  }
package/lib/mirror.mjs CHANGED
@@ -505,6 +505,38 @@ export async function mirrorSite({ start, out, maxPages, maxDepth, sameOriginOnl
505
505
  } catch {}
506
506
  }
507
507
 
508
+ // In single-page mode, create a root index.html that redirects to the actual page
509
+ if (singlePage && visitedPages.size === 1) {
510
+ const actualPageUrl = [...visitedPages][0];
511
+ const pageUrlObj = new URL(actualPageUrl);
512
+ const pagePath = pageUrlObj.pathname;
513
+
514
+ // Only create redirect if the page isn't at root already
515
+ if (pagePath !== '/' && pagePath !== '') {
516
+ const redirectPath = pagePath.endsWith('/') ? pagePath : pagePath + '/';
517
+ const rootIndexPath = path.join(outRoot, 'index.html');
518
+
519
+ // Check if root index.html already exists (don't overwrite)
520
+ const rootExists = await fs.access(rootIndexPath).then(() => true).catch(() => false);
521
+ if (!rootExists) {
522
+ const redirectHtml = `<!DOCTYPE html>
523
+ <html>
524
+ <head>
525
+ <meta charset="utf-8">
526
+ <meta http-equiv="refresh" content="0; url=${redirectPath}">
527
+ <script>window.location.href = "${redirectPath}";</script>
528
+ <title>Redirecting...</title>
529
+ </head>
530
+ <body>
531
+ <p>Redirecting to <a href="${redirectPath}">${redirectPath}</a>...</p>
532
+ </body>
533
+ </html>`;
534
+ await writeFileSafe(rootIndexPath, redirectHtml);
535
+ console.log(`Created root redirect → ${redirectPath}`);
536
+ }
537
+ }
538
+ }
539
+
508
540
  console.log('');
509
541
  console.log(`Done. Pages saved: ${visitedPages.size}, Assets saved: ${savedAssets.size}`);
510
542
  console.log(`Output: ${outRoot}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "site-mirror",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "CLI tool to mirror websites for offline browsing using Playwright",
5
5
  "type": "module",
6
6
  "bin": {