Skip to content

Commit cf0de2d

Browse files
committed
feat: add zero-config docx2pdf demo
Adds a `demo` command that converts a bundled sample DOCX to PDF using whatever backend is installed, giving the suite a uniform first-run (matching template-vault/draft/compare/sign). Backend-dependent by design: if no backend is present it prints the install recommendation and exits 3, otherwise it converts and reports the backend + output. Ships src/demo-sample.docx; usage text + a bin-level test added. 111 tests pass.
1 parent 15facec commit cf0de2d

4 files changed

Lines changed: 49 additions & 0 deletions

File tree

src/cli.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ function getCapabilities() {
216216
}
217217

218218
function main(argv) {
219+
if (argv[0] === "demo") return runDemo();
220+
219221
const options = parseArgs(argv);
220222

221223
if (options.help) {
@@ -381,6 +383,40 @@ function printSetupHelp() {
381383
process.stderr.write(lines.join("\n") + "\n");
382384
}
383385

386+
// Zero-config first run: convert a bundled sample DOCX to PDF using whatever
387+
// backend is installed. If none is, explain what to install (the conversion
388+
// itself is backend-dependent, so the demo is best-effort by design).
389+
function runDemo() {
390+
const os = require("node:os");
391+
const sampleSrc = path.join(__dirname, "demo-sample.docx");
392+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "docx2pdf-demo-"));
393+
const input = path.join(tmpDir, "sample.docx");
394+
fs.copyFileSync(sampleSrc, input);
395+
396+
process.stderr.write("docx2pdf demo — converting a bundled sample DOCX to PDF.\n");
397+
398+
if (!getAvailableBackends().length) {
399+
process.stderr.write("\nNo PDF backend is installed yet, so the demo can't render the sample.\n");
400+
printSetupHelp();
401+
try { fs.rmSync(tmpDir, { recursive: true, force: true }); } catch { /* best-effort */ }
402+
return EXIT.MISSING_DEP;
403+
}
404+
405+
const output = path.join(tmpDir, "sample.pdf");
406+
try {
407+
const result = convertDocxToPdf({ input, output, backend: "auto", overwrite: true });
408+
const bytes = fs.statSync(result.output).size;
409+
process.stdout.write(`\n✓ Converted the sample DOCX to PDF (${bytes} bytes) via the '${result.backend}' backend.\n`);
410+
process.stdout.write(` ${result.output}\n`);
411+
process.stdout.write("\nNow try your own file:\n docx2pdf your.docx out.pdf\n docx2pdf --doctor # see all detected backends\n");
412+
return 0;
413+
} catch (err) {
414+
process.stderr.write(`\nDemo conversion failed: ${err.message}\n`);
415+
try { fs.rmSync(tmpDir, { recursive: true, force: true }); } catch { /* best-effort */ }
416+
return err.exitCode || EXIT.CONVERT_FAIL;
417+
}
418+
}
419+
384420
if (require.main === module) {
385421
Promise.resolve()
386422
.then(() => main(process.argv.slice(2)))

src/demo-sample.docx

3.46 KB
Binary file not shown.

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ function usageText() {
636636
Usage:
637637
docx2pdf [options] <input.docx> [output.pdf]
638638
docx2pdf [options] --out-dir <dir> <input.docx>...
639+
docx2pdf demo zero-config: convert a bundled sample
639640
640641
Options:
641642
--backend <auto|libreoffice|gotenberg|convertapi|pages|word|textutil-cups>

tests/cli-bin.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,3 +448,15 @@ test("single-file --json success includes outputBytes and durationMs", (t) => {
448448
fs.rmSync(tempDir, { recursive: true, force: true });
449449
}
450450
});
451+
452+
test("demo: zero-config command runs the bundled sample", () => {
453+
const r = runCli(["demo"]);
454+
assert.match(r.stderr, /docx2pdf demo/, `expected demo intro on stderr, got: ${r.stderr}`);
455+
if (r.status === EXIT.MISSING_DEP) {
456+
// No backend installed: the demo explains what to install instead.
457+
assert.match(r.stderr, /No PDF backend is installed/);
458+
return;
459+
}
460+
assert.equal(r.status, 0, `demo failed: ${r.stderr}`);
461+
assert.match(r.stdout, /Converted the sample DOCX to PDF/);
462+
});

0 commit comments

Comments
 (0)